if_not_there.c raw

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