forked from baskerville/sutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexist.c
50 lines (44 loc) · 1.17 KB
/
exist.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
void check_file(char *pth, bool inv) {
struct stat buf;
int ret = stat(pth, &buf);
if ((ret == 0 && !inv) || (ret == -1 && inv))
puts(pth);
}
int main(int argc, char *argv[]) {
char opt;
bool invert_matches = false;
while ((opt = getopt(argc, argv, "hv")) != -1) {
switch (opt) {
case 'h':
printf("exist [-hv] [FILES]\n");
return EXIT_SUCCESS;
break;
case 'v':
invert_matches = true;
break;
}
}
int num = argc - optind;
char **args = argv + optind;
if (num > 0) {
int i;
for (i = 0; i < num; i++)
check_file(args[i], invert_matches);
} else {
char line[BUFSIZ] = {0};
while (fgets(line, sizeof(line), stdin) != NULL) {
int last = strlen(line) - 1;
if (last >= 0 && line[last] == '\n')
line[last] = 0;
check_file(line, invert_matches);
}
}
return EXIT_SUCCESS;
}