mblaze/mdirs.c

102 lines
1.5 KiB
C
Raw Normal View History

2016-07-22 16:47:39 +00:00
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
2016-07-22 22:47:51 +00:00
#include <limits.h>
2016-07-22 16:47:39 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2018-01-25 12:46:56 +00:00
#include "blaze822.h"
2016-07-22 16:47:39 +00:00
void
pwd()
{
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof cwd))
puts(cwd);
}
void
mdirs(char *fpath)
{
DIR *dir;
2017-01-26 19:27:26 +00:00
struct dirent *d;
2016-07-22 16:47:39 +00:00
struct stat st;
dir = opendir(fpath);
if (!dir)
return;
if (chdir(fpath) < 0) {
closedir(dir);
return;
}
int dotonly = 0;
if (stat("cur", &st) == 0 &&
S_ISDIR(st.st_mode) &&
stat("new", &st) == 0 &&
S_ISDIR(st.st_mode)) {
pwd();
dotonly = 1; // Maildir++
}
while ((d = readdir(dir))) {
#if defined(DT_DIR) && defined(DT_UNKNOWN)
2016-07-22 16:47:39 +00:00
if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN)
continue;
#endif
2016-07-22 16:47:39 +00:00
if (d->d_name[0] == '.' &&
d->d_name[1] == 0)
continue;
if (d->d_name[0] == '.' &&
d->d_name[1] == '.' &&
d->d_name[2] == 0)
continue;
if (dotonly && d->d_name[0] != '.')
continue;
2017-08-31 15:30:17 +00:00
2016-07-22 16:47:39 +00:00
mdirs(d->d_name);
}
2017-08-31 15:30:17 +00:00
2016-07-22 16:47:39 +00:00
if (chdir("..") < 0)
exit(-1);
2017-01-26 19:27:26 +00:00
closedir(dir);
2016-07-22 16:47:39 +00:00
}
int
main(int argc, char *argv[])
{
2016-07-26 19:57:12 +00:00
int c, i;
while ((c = getopt(argc, argv, "")) != -1)
2017-08-31 15:30:17 +00:00
switch (c) {
2016-07-26 19:57:12 +00:00
default:
2017-08-31 15:30:17 +00:00
usage:
2016-07-26 19:57:12 +00:00
fprintf(stderr, "Usage: mdirs dirs...\n");
exit(1);
}
if (argc == optind)
goto usage;
2016-07-22 16:47:39 +00:00
char toplevel[PATH_MAX];
if (!getcwd(toplevel, sizeof toplevel)) {
perror("mdirs: getcwd");
exit(-1);
}
for (i = 0; i < argc; i++) {
2016-07-22 16:47:39 +00:00
mdirs(argv[i]);
if (chdir(toplevel) < 0) {
perror("mdirs: chdir");
exit(-1);
}
}
2016-07-22 16:47:39 +00:00
return 0;
}