aboutsummaryrefslogtreecommitdiff
path: root/mlist.c
blob: 18d00d15d0a1c61b43ac8280ac3c78bdbc32aa10 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#define _GNU_SOURCE

#include <sys/stat.h>

#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "blaze822.h"

#define lc(c) ((c) | 0x20)
#define uc(c) ((c) & 0xdf)

/*

PRSTDF
prstdf

-N new
-n not new

-C cur
-c not cur

-m age

-r recursive?

*/

static int8_t flags[255];
static int flagsum;
static int flagset;
static int Nflag;
static int Cflag;
static int iflag;

static long icount;
static long iunseen;
static long iflagged;
static long imatched;

static long tdirs;
static long tunseen;
static long tflagged;
static long tcount;

void
list(char *prefix, char *file)
{
	if (flagset) {
		int sum = 0;
		char *f = strstr(file, ":2,");
		if (!f)
			return;
		icount++;
		tcount++;
		f += 3;
		while (*f) {
			if (flags[(unsigned int)*f] == -1)
				return;
			if (flags[(unsigned int)*f] == 1)
				sum++;
			f++;
		}
		if (sum != flagsum)
			return;
	}

	if (iflag) {
		char *f = strstr(file, ":2,");
		if (!f)
			return;
		imatched++;
		if (!flagset)
			icount++, tcount++;
		if (!strchr(f, 'S'))
			iunseen++, tunseen++;
		if (strchr(f, 'F'))
			iflagged++, tflagged++;
		return;
	}

	if (prefix) {
		fputs(prefix, stdout);
		putc('/', stdout);
	}
	puts(file);
}

#ifdef __linux__
// faster implementation of readdir using a bigger getdents buffer

#include <sys/syscall.h>

struct linux_dirent64 {
	ino64_t d_ino;           /* 64-bit inode number */
	off64_t d_off;           /* 64-bit offset to next structure */
	unsigned short d_reclen; /* Size of this dirent */
	unsigned char d_type;    /* File type */
	char d_name[];           /* Filename (null-terminated) */
};
#define BUF_SIZE 1024000
char buf[BUF_SIZE];

void
listdir(char *dir)
{
	int fd;
	ssize_t nread;
	ssize_t bpos;
	struct linux_dirent64 *d;

	fd = open(dir, O_RDONLY | O_DIRECTORY);
	if (fd == -1) {
		perror("open");
		return;
	}

	while (1) {
		nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
		if (nread == -1) {
			perror("getdents64");
			break;
		}

		if (nread == 0)
			break;

		for (bpos = 0; bpos < nread; bpos += d->d_reclen) {
			d = (struct linux_dirent64 *)(buf + bpos);
			if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
				continue;
			if (d->d_name[0] == '.')
				continue;
			list(dir, d->d_name);
		}
	}

	close(fd);
}
#else
void
listdir(char *dir)
{
	DIR *fd;
	struct dirent *d;

	fd = opendir(dir);
	if (!fd)
		return;
	while ((d = readdir(fd))) {
		if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
			continue;
		if (d->d_name[0] == '.')
			continue;
		list(dir, d->d_name);
	}
	closedir(fd);
}
#endif

void
listarg(char *arg)
{
	squeeze_slash(arg);

	struct stat st;
	if (stat(arg, &st) < 0)
		return;
	if (S_ISDIR(st.st_mode)) {
		char subdir[PATH_MAX];
		struct stat st2;
		int maildir = 0;

		long gcount = icount;
		long gunseen = iunseen;
		long gflagged = iflagged;
		long gmatched = imatched;

		icount = 0;
		iunseen = 0;
		iflagged = 0;

		snprintf(subdir, sizeof subdir, "%s/cur", arg);
		if (stat(subdir, &st2) == 0) {
			maildir = 1;
			if (Cflag >= 0 && Nflag <= 0)
				listdir(subdir);
		}

		snprintf(subdir, sizeof subdir, "%s/new", arg);
		if (stat(subdir, &st2) == 0) {
			maildir = 1;
			if (Nflag >= 0 && Cflag <= 0)
				listdir(subdir);
		}

		if (!maildir)
			listdir(arg);

		if (iflag && (imatched || (maildir && !flagset))) {
			tdirs++;
			printf("%6ld unseen  %3ld flagged  %6ld msg  %s\n",
			    iunseen, iflagged, icount, arg);
		}

		icount = gcount;
		iunseen = gunseen;
		iflagged = gflagged;
		imatched = gmatched;
	} else if (S_ISREG(st.st_mode)) {
		list(0, arg);
	}
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "PRSTDFprstdfX:x:NnCci")) != -1)
		switch (c) {
		case 'P': case 'R': case 'S': case 'T': case 'D': case 'F':
			flags[(unsigned int)c] = 1;
			break;
		case 'p': case 'r': case 's': case 't': case 'd': case 'f':
			flags[(unsigned int)uc(c)] = -1;
			break;
		case 'X':
			while (*optarg)
				flags[(unsigned int)*optarg++] = 1;
			break;
		case 'x':
			while (*optarg)
				flags[(unsigned int)*optarg++] = -1;
			break;
		case 'N': Nflag = 1; break;
		case 'n': Nflag = -1; break;
		case 'C': Cflag = 1; break;
		case 'c': Cflag = -1; break;
		case 'i': iflag = 1; break;
		default:
usage:
			fprintf(stderr,
			    "Usage: mlist [-DFPRST] [-X str]\n"
			    "             [-dfprst] [-x str]\n"
			    "             [-N | -n | -C | -c]\n"
			    "             [-i] [dirs...]\n"
			);
			exit(1);
		}

	int i;

	for (i = 0, flagsum = 0, flagset = 0; (size_t)i < sizeof flags; i++) {
		if (flags[i] != 0)
			flagset++;
		if (flags[i] == 1)
			flagsum++;
	}

	if (optind == argc) {
		if (isatty(0))
			goto usage;
		blaze822_loop(0, 0, listarg);
	} else {
		for (i = optind; i < argc; i++)
			listarg(argv[i]);
	}

	if (iflag && tdirs > 1)
		printf("%6ld unseen  %3ld flagged  %6ld msg\n",
		    tunseen, tflagged, tcount);

	return 0;
}