Debian Codegraph @

gc/tools/if_not_there.c

raw
1 /* Conditionally execute the command argv[2] based if the file argv[1] */
2 /* does not exist. If the command is omitted (and the file does not */
3 /* exist) then just exit with a non-zero code. */
4
5 # include "private/gc_priv.h"
6 # include <stdio.h>
7 # include <stdlib.h>
8 # include <unistd.h>
9 #ifdef __DJGPP__
10 #include <dirent.h>
11 #endif /* __DJGPP__ */
12
13 #ifdef __cplusplus
14 # define EXECV_ARGV_T char**
15 #else
16 # define EXECV_ARGV_T void* /* see the comment in if_mach.c */
17 #endif
18
19 int main(int argc, char **argv)
20 {
21 FILE * f;
22 #ifdef __DJGPP__
23 DIR * d;
24 #endif /* __DJGPP__ */
25 char *fname;
26
27 if (argc < 2 || argc > 3)
28 goto Usage;
29
30 fname = TRUSTED_STRING(argv[1]);
31 f = fopen(fname, "rb");
32 if (f != NULL) {
33 fclose(f);
34 return(0);
35 }
36 f = fopen(fname, "r");
37 if (f != NULL) {
38 fclose(f);
39 return(0);
40 }
41 #ifdef __DJGPP__
42 if ((d = opendir(fname)) != 0) {
43 closedir(d);
44 return(0);
45 }
46 #endif
47 printf("^^^^Starting command^^^^\n");
48 fflush(stdout);
49 if (argc == 2)
50 return(2); /* the file does not exist but no command is given */
51
52 execvp(TRUSTED_STRING(argv[2]), (EXECV_ARGV_T)(argv + 2));
53 exit(1);
54
55 Usage:
56 fprintf(stderr, "Usage: %s file_name [command]\n", argv[0]);
57 return(1);
58 }