mblaze/minc.c

85 lines
1.4 KiB
C
Raw Permalink Normal View History

2016-07-21 21:17:49 +00:00
#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;
2017-01-26 19:27:26 +00:00
struct dirent *d;
2016-07-21 21:17:49 +00:00
char src[PATH_MAX];
char dst[PATH_MAX];
2017-07-12 20:31:35 +00:00
squeeze_slash(dir);
2016-07-21 21:17:49 +00:00
snprintf(src, sizeof src, "%s/new", dir);
fd = opendir(src);
if (!fd) {
fprintf(stderr, "minc: can't open maildir '%s': %s\n",
2017-07-12 20:31:35 +00:00
src, strerror(errno));
2016-07-21 21:17:49 +00:00
status = 2;
return;
}
while ((d = readdir(fd))) {
#if defined(DT_REG) && defined(DT_UNKNOWN)
2016-07-21 21:17:49 +00:00
if (d->d_type != DT_REG && d->d_type != DT_UNKNOWN)
continue;
#endif
2016-07-21 21:17:49 +00:00
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:
2016-07-26 19:57:28 +00:00
usage:
fprintf(stderr, "Usage: minc [-q] dirs...\n");
2016-07-21 21:17:49 +00:00
exit(1);
}
2016-07-26 19:57:28 +00:00
if (optind == argc)
goto usage;
2016-07-21 21:17:49 +00:00
status = 0;
for (i = optind; i < argc; i++)
inc(argv[i]);
return status;
}