-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistq.c
150 lines (122 loc) · 2.99 KB
/
listq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include "spool.h"
#include "public.h"
int
Qpicker(const struct dirent *f)
{
return (f->d_name[0] == 'c') && (f->d_name[1] == 'm') && (strlen(f->d_name) == 8);
}
static int
Qcompare(const struct dirent **a, const struct dirent **b)
{
struct stat sa, sb;
int ra, rb;
ra = stat( (*a)->d_name, &sa );
rb = stat( (*b)->d_name, &sb );
if (ra == -1)
return (rb == -1) ? 0 : 1;
else if (rb == -1)
return -1;
return sa.st_ctime - sb.st_ctime;
}
static char *
unit(off_t size)
{
static char fmt[15];
if (size < 10000)
sprintf(fmt, "%ld ", (long)size);
else if (size < 1000000)
sprintf(fmt, "%5.2fK", ((float)size)/1024.0);
else if (size < 1000000000)
sprintf(fmt, "%5.2fM", (float)(size/1024)/1024.0);
else
sprintf(fmt, "%5.2fG", (float)(size/(1024*1024))/1024.0);
return fmt;
}
#define TFMT "%8s %8s %17s %s"
#define FFMT "%35s %s%c"
#define CFMT "%17s %.62s"
void
listq()
{
struct dirent **qf;
int count;
FILE *f;
char df[9];
char qid[10];
char xf[9];
char line[80];
char comment[200];
char date[40];
struct stat st;
int i;
if (chdir(QUEUEDIR) || (count = scandir(".", &qf, Qpicker, Qcompare)) < 0) {
perror(QUEUEDIR);
exit(EX_NOPERM);
}
if (count == 0) {
puts("Mail queue is empty.");
exit(EX_OK);
}
printf(" Mail Queue (%d request%s)\n", count, (count!=1)?"s":"");
printf(" --ID-- --Size-- -----Queued------ ------Sender/Recipient------\n");
for (i=0; count-- > 0; ++i) {
strcpy(df, qf[i]->d_name);
df[0] = 'd';
strcpy(xf, qf[i]->d_name);
xf[0] = 'x';
if (stat(df, &st) != 0) {
printf("%6s (no data file)\n", df+2);
continue;
}
/* need to open the file r/w for flock() to work on Linux */
if ( (f = fopen(qf[i]->d_name, "r+")) == 0 ) {
printf("%6s (no control file)\n", df+2);
continue;
}
comment[0] = 0;
while ( fgets(line, sizeof line, f) != 0 )
if (line[0] == C_STATUS)
strlcpy(comment, line+1, sizeof(comment)-1);
else if (line[0] == C_FROM || line[0] == C_HEADER)
break;
if (line[0] != C_FROM)
sprintf(line, "%c<>\n", C_FROM);
strftime(date, 40, "%H:%M %d-%b-%Y", localtime(&st.st_ctime));
#ifdef NO_FLOCK
sprintf(qid, (access(xf,R_OK) == 0) ? "*%6s*" :" %6s ", df+2);
#else
sprintf(qid, (locker(fileno(f),LOCK_EX|LOCK_NB) == 0) ?
" %6s " :
"*%6s*", df + 2);
locker(fileno(f), LOCK_UN);
#endif
printf(TFMT, qid, unit(st.st_size), date, line+1);
if (comment[0])
printf(CFMT, "", comment);
rewind(f);
while ( fgets(line, sizeof line, f) != 0 && line[0] != C_HEADER)
if (line[0] == C_TO) {
char *p = strchr(line, '|');
if (p) {
*p = 0;
printf(FFMT, "", line+1, '\n');
}
else {
printf(FFMT, "", line+1, 0);
}
}
fclose(f);
}
exit(EX_OK);
}