home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gnulib / libsrc98.zoo / spawnvp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-05  |  1.5 KB  |  70 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. #include "lib.h"
  15.  
  16. __EXTERN char *    findfile __PROTO((char *, char *, char **));
  17.  
  18. extern int _x_Bit_set_in_stat; /* in stat.c */
  19.  
  20. static char *extensions[] = { "ttp", "prg", "tos", NULL };
  21.  
  22. int
  23. spawnvp(mode, name, argv)
  24.     int mode;
  25.     const char      *name;
  26.     char * const    *argv;
  27. {
  28.     char *execname;
  29.  
  30.     execname = findfile(name, getenv("PATH"), extensions);
  31.     if (!execname) {
  32.         errno = ENOENT;
  33.         return -1;        /* file not found */
  34.     }
  35.  
  36.     /* check to make sure that the file is executable. alas, this is not
  37.        foolproof, since .g and .sh files are marked as executable even though
  38.        they're not, really.
  39.      */
  40.     if(mode < 0)
  41.     {
  42.         int savex = _x_Bit_set_in_stat;
  43.  
  44.         mode = (-mode);
  45.         _x_Bit_set_in_stat = 1;
  46.         if (access(execname, X_OK)) {
  47.             _x_Bit_set_in_stat = savex;
  48.         errno = ENOENT;
  49.         return -1;
  50.         }
  51.         _x_Bit_set_in_stat = savex;
  52.     }
  53.  
  54.     return spawnve(mode, execname, argv, (char **)NULL);
  55. }
  56.  
  57. #ifdef __STDC__
  58. int spawnlp(int mode, const char *name, ...)
  59. #else
  60. int spawnlp(mode, name)
  61.     int            mode;
  62.     const char    *name;
  63. #endif
  64. {
  65.     va_list args;
  66.  
  67.     va_start(args, name);
  68.     return spawnvp(mode, name, (char **)args);
  69. }
  70.