about summary refs log tree commit diff
path: root/mdirs.c
blob: 5f49906ffb2161be785b8afe0609df2debfd6180 (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
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
#include <sys/stat.h>
#include <sys/types.h>

#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "blaze822.h"
#include "blaze822_priv.h"
#include "xpledge.h"

static char sep = '\n';
int aflag;

void
pwd()
{
	char cwd[PATH_MAX];
	if (getcwd(cwd, sizeof cwd))
		printf("%s%c", cwd, sep);
}

void
mdirs(char *fpath)
{
	DIR *dir;
	struct dirent *d;
	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 (!DIR_DT(d->d_type))
			continue;

		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 (!aflag && dotonly && d->d_name[0] != '.')
			continue;

		mdirs(d->d_name);
	}

	if (chdir("..") < 0)
		exit(-1);

	closedir(dir);
}

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

	if (argc == optind)
		goto usage;

	xpledge("stdio rpath", "");

	char toplevel[PATH_MAX];
	if (!getcwd(toplevel, sizeof toplevel)) {
		perror("mdirs: getcwd");
		exit(-1);
	}

	for (i = 0; i < argc; i++) {
		mdirs(argv[i]);
		if (chdir(toplevel) < 0) {
			perror("mdirs: chdir");
			exit(-1);
		}
	}

	return 0;
}