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
|
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char** argv) {
char* pathenv = getenv("PATH");
if (!pathenv) {
fprintf(stderr, "Failed to find PATH enviroment variable\n");
return 1;
}
if (argc == 2) {
struct stat file;
if (stat(argv[1], &file) == -1) {
return 0;
}
char* dir = NULL;
for (dir = strtok(pathenv, ":"); (dir = strtok(NULL, ":"));) {
struct stat buf;
if (stat(dir, &buf) == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno), argv[1]);
continue;
}
if (S_ISDIR(buf.st_mode) && buf.st_mtime > file.st_mtime) {
return 0;
}
}
return 1;
}
char path[PATH_MAX];
char* dir = NULL;
for (dir = strtok(pathenv, ":"); dir ;dir = strtok(NULL, ":")) {
DIR* dirfd = opendir(dir);
if (!dirfd) {
fprintf(stderr, "%s: %s\n", strerror(errno), dir);
continue;
}
struct dirent* d = NULL;
while ((d = readdir(dirfd))) {
int r = snprintf(path, sizeof(path), "%s/%s", dir, d->d_name);
if (r <= 0 || r >= PATH_MAX) continue;
struct stat buf;
if (stat(path, &buf) == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno), path);
continue;
}
if (S_ISREG(buf.st_mode) && !access(path, X_OK)) {
puts(d->d_name);
}
}
closedir(dirfd);
}
return 0;
}
|