if_not_there.c 1.3 KB

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