summary refs log tree commit diff
path: root/minc.c
blob: 4e5529250920ae016fbfae5affd65caff74c1ae9 (plain) (blame)
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
#include <sys/types.h>

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

#include "blaze822.h"

static int qflag;
static int status;

void
inc(char *dir)
{
	DIR *fd;
	struct dirent *d;
	char src[PATH_MAX];
	char dst[PATH_MAX];

	squeeze_slash(dir);

	snprintf(src, sizeof src, "%s/new", dir);
	fd = opendir(src);
	if (!fd) {
		fprintf(stderr, "minc: can't open maildir '%s': %s\n",
		    src, strerror(errno));
		status = 2;
		return;
	}

	while ((d = readdir(fd))) {
#if defined(DT_REG) && defined(DT_UNKNOWN)
		if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
			continue;
#endif
		if (d->d_name[0] == '.')
			continue;

		snprintf(src, sizeof src, "%s/new/%s",
		    dir, d->d_name);
		snprintf(dst, sizeof dst, "%s/cur/%s%s",
		    dir, d->d_name, strstr(d->d_name, ":2,") ? "" : ":2,");
		if (rename(src, dst) < 0) {
			fprintf(stderr, "minc: can't rename '%s' to '%s': %s\n",
			    src, dst, strerror(errno));
			status = 3;
			continue;
		}

		if (!qflag)
			printf("%s\n", dst);
	}

	closedir(fd);
}

int
main(int argc, char *argv[])
{
	int c, i;
	while ((c = getopt(argc, argv, "q")) != -1)
		switch (c) {
		case 'q': qflag = 1; break;
		default:
usage:
			fprintf(stderr, "Usage: minc [-q] dirs...\n");
			exit(1);
		}

	if (optind == argc)
		goto usage;

	status = 0;
	for (i = optind; i < argc; i++)
		inc(argv[i]);

	return status;
}