Debian Codegraph @

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