if_not_there.c 876 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Conditionally execute a command based if the file argv[1] doesn't exist */
  2. /* Except for execvp, we stick to ANSI C. */
  3. # include "private/gcconfig.h"
  4. # include <stdio.h>
  5. # include <stdlib.h>
  6. # include <unistd.h>
  7. #ifdef __DJGPP__
  8. #include <dirent.h>
  9. #endif /* __DJGPP__ */
  10. int main(argc, argv, envp)
  11. int argc;
  12. char ** argv;
  13. char ** envp;
  14. {
  15. FILE * f;
  16. #ifdef __DJGPP__
  17. DIR * d;
  18. #endif /* __DJGPP__ */
  19. if (argc < 3) goto Usage;
  20. if ((f = fopen(argv[1], "rb")) != 0
  21. || (f = fopen(argv[1], "r")) != 0) {
  22. fclose(f);
  23. return(0);
  24. }
  25. #ifdef __DJGPP__
  26. if ((d = opendir(argv[1])) != 0) {
  27. closedir(d);
  28. return(0);
  29. }
  30. #endif
  31. printf("^^^^Starting command^^^^\n");
  32. fflush(stdout);
  33. execvp(argv[2], argv+2);
  34. exit(1);
  35. Usage:
  36. fprintf(stderr, "Usage: %s file_name command\n", argv[0]);
  37. return(1);
  38. }