home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / spawnvp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  1.4 KB  |  69 lines

  1. /* spawnvp, spawnlp: try to execute a program on the default system
  2.    execution path. WARNING: the current directory is always searched
  3.    first.
  4.  
  5.    Written by Eric R. Smith and placed in the public domain.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <process.h>
  10. #include <stdarg.h>
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14.  
  15. __EXTERN char *    findfile __PROTO((char *, char *, char **));
  16.  
  17. extern int _x_Bit_set_in_stat; /* in stat.c */
  18.  
  19. static char *extensions[] = { "ttp", "prg", "tos", NULL };
  20.  
  21. int
  22. spawnvp(mode, name, argv)
  23.     int mode;
  24.     char *name;
  25.     char **argv;
  26. {
  27.     char *execname;
  28.  
  29.     execname = findfile(name, getenv("PATH"), extensions);
  30.     if (!execname) {
  31.         errno = ENOENT;
  32.         return -1;        /* file not found */
  33.     }
  34.  
  35.     /* check to make sure that the file is executable. alas, this is not
  36.        foolproof, since .g and .sh files are marked as executable even though
  37.        they're not, really.
  38.      */
  39.     if(mode < 0)
  40.     {
  41.         int savex = _x_Bit_set_in_stat;
  42.  
  43.         mode = (-mode);
  44.         _x_Bit_set_in_stat = 1;
  45.         if (access(execname, X_OK)) {
  46.             _x_Bit_set_in_stat = savex;
  47.         errno = ENOENT;
  48.         return -1;
  49.         }
  50.         _x_Bit_set_in_stat = savex;
  51.     }
  52.  
  53.     return spawnve(mode, execname, argv, (char **)NULL);
  54. }
  55.  
  56. #ifdef __STDC__
  57. int spawnlp(int mode, char *name, ...)
  58. #else
  59. int spawnlp(mode, name)
  60.     int    mode;
  61.     char    *name;
  62. #endif
  63. {
  64.     va_list args;
  65.  
  66.     va_start(args, name);
  67.     return spawnvp(mode, name, (char **)args);
  68. }
  69.