-
Notifications
You must be signed in to change notification settings - Fork 0
/
stest.c
144 lines (136 loc) · 3.97 KB
/
stest.c
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
/* See LICENSE file for copyright and license details. */
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#if USE_WINAPI
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <winuser.h>
#include <sys/cygwin.h>
#endif
#define FLAG(x) (flag[(x)-'a'])
static void test(const char *, const char *);
static bool match = false;
static bool flag[26];
static struct stat old, new;
int
main(int argc, char *argv[]) {
struct dirent *d;
char buf[BUFSIZ], *p;
DIR *dir;
int opt;
while((opt = getopt(argc, argv, "abcdefghln:o:pqrsuvwx")) != -1)
switch(opt) {
case 'n': /* newer than file */
case 'o': /* older than file */
if(!(FLAG(opt) = !stat(optarg, (opt == 'n' ? &new : &old))))
perror(optarg);
break;
default: /* miscellaneous operators */
FLAG(opt) = true;
break;
case '?': /* error: unknown flag */
fprintf(stderr, "usage: %s [-abcdefghlpqrsuvwx] [-n file] [-o file] [file...]\n", argv[0]);
exit(2);
}
if(optind == argc)
while(fgets(buf, sizeof buf, stdin)) {
if((p = strchr(buf, '\n')))
*p = '\0';
test(buf, buf);
}
for(; optind < argc; optind++)
#if USE_COREUTILS
if(FLAG('l') && (dir = opendir(argv[optind]))) {
/* test directory contents */
while((d = readdir(dir)))
if(snprintf(buf, sizeof buf, "%s/%s", argv[optind], d->d_name) < sizeof buf)
test(buf, d->d_name);
closedir(dir);
}
else
test(argv[optind], argv[optind]);
#elif USE_CYGWIN
{
if(FLAG('l'))
{
if (strstr(argv[optind],":"))
{
char *p = strtok(argv[optind],":");
while(p)
{
if ((dir = opendir(p)))
{
/* test directory contents */
while((d = readdir(dir)))
{
if(snprintf(buf, sizeof buf, "%s/%s", p, d->d_name) < sizeof buf)
test(buf, d->d_name);
}
}
closedir(dir);
p = strtok(NULL,":");
}
}
}
else
{
if (strstr(argv[optind],":"))
{
char *p = strtok(argv[optind],":");
while(p)
{
test(p, p);
p = strtok(NULL,":");
}
}
/* test(argv[optind], argv[optind]); */
}
}
#endif
return match ? 0 : 1;
}
void
test(const char *path, const char *name) {
struct stat st, ln;
#if USE_CYGWIN
DWORD type;
char path2[MAX_PATH];
cygwin_conv_path(CCP_POSIX_TO_WIN_A, path, path2, MAX_PATH);
#endif
if((!stat(path, &st)
&& (FLAG('a') || name[0] != '.') /* hidden files */
#if USE_COREUTILS
&& (!FLAG('b') || S_ISBLK(st.st_mode)) /* block special */
&& (!FLAG('c') || S_ISCHR(st.st_mode)) /* character special */
#endif
&& (!FLAG('d') || S_ISDIR(st.st_mode)) /* directory */
&& (!FLAG('e') || access(path, F_OK) == 0) /* exists */
&& (!FLAG('f') || S_ISREG(st.st_mode)) /* regular file */
&& (!FLAG('g') || st.st_mode & S_ISGID) /* set-group-id flag */
&& (!FLAG('h') || (!lstat(path, &ln) && S_ISLNK(ln.st_mode))) /* symbolic link */
&& (!FLAG('n') || st.st_mtime > new.st_mtime) /* newer than file */
&& (!FLAG('o') || st.st_mtime < old.st_mtime) /* older than file */
#if USE_COREUTILS
&& (!FLAG('p') || S_ISFIFO(st.st_mode)) /* named pipe */
#endif
&& (!FLAG('r') || access(path, R_OK) == 0) /* readable */
&& (!FLAG('s') || st.st_size > 0) /* not empty */
&& (!FLAG('u') || st.st_mode & S_ISUID) /* set-user-id flag */
&& (!FLAG('w') || access(path, W_OK) == 0) /* writable */
#if USE_COREUTILS
&& (!FLAG('x') || access(path, X_OK) == 0)) != FLAG('v')) { /* executable */
#elif USE_CYGWIN
&& (!FLAG('x') || GetBinaryType(path2, &type)))) { /* executable */
#endif
if(FLAG('q'))
exit(0);
match = true;
puts(name);
}
}