home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bricon20.zip / bricons / gen.c < prev    next >
C/C++ Source or Header  |  1992-07-23  |  3KB  |  134 lines

  1. static char* sccs_gen_c = "%W%%G%";
  2.  
  3. #include "gen.h"
  4. #include <stdio.h>
  5.  
  6. static    int    IsExecutable PROTO((char *,char *)), 
  7.         Executable PROTO((char *));
  8. extern    int    GetCommand PROTO((char*, char*, char*, char*, int));
  9. extern    void    GetPath PROTO((char*));
  10.  
  11. /*
  12.     IsExecutable function searches along the users path trying to
  13.     locate the file program.  If found a check is made to see if it
  14.     is executable.  If it is TRUE is returned, otherwise FALSE is
  15.     returned.
  16. */
  17. static int
  18. IsExecutable(path,program)
  19. char    *path;
  20. char    *program;
  21. {
  22.     int i, j;
  23.     char    pathname[MAXPATHLEN];
  24.  
  25.     i = 0;
  26.     if(*program == '/')
  27.     {
  28.         if(Executable(program))
  29.             return(TRUE);
  30.         else
  31.             return(FALSE);
  32.     }
  33.     while (path[i] != '\0')
  34.     {
  35.         if (path[i] == ' ')
  36.             ++i;
  37.         for (j = 0; path[i] != ' ' && path[i] != '\0'; ++i, ++j)
  38.             pathname[j] = path[i];
  39.         pathname[j++] = '/';
  40.         strcpy(&pathname[j], program);
  41.         if(Executable(pathname)) 
  42.             return(TRUE);
  43.     }
  44.     return(FALSE);
  45. }
  46.  
  47.  
  48. /*
  49.     GetCommand reads a line of text from the file pointed at by file
  50.     pointer fp.  It then checks if the first word is a valid command.
  51. */
  52. extern int
  53. GetCommand(path, command, source_file, exe_name, line_no)
  54. char    *path;
  55. char    *command;
  56. char    *source_file;
  57. char    *exe_name;
  58. int    line_no;
  59. {
  60.     int    i = 0;
  61.     int    is_binary = FALSE;
  62.     char    prog_name[MAXPATHLEN];
  63.     char    *c_ptr;
  64.  
  65.     prog_name[0] = '\0';
  66.     if(*command != NULL)
  67.     {
  68.         c_ptr = command;
  69.         /* get first word from line of text */
  70.         while((*c_ptr != ' ') && (*c_ptr != '\0') && (*c_ptr != ';') &&
  71.                 (*c_ptr != '\n'))
  72.         {
  73.             if(*c_ptr == '`')
  74.                 c_ptr++;
  75.             prog_name[i++] = *c_ptr++;
  76.         }
  77.         prog_name[i] = '\0';
  78.         if(IsExecutable(path,prog_name))
  79.             is_binary = TRUE;
  80.         else
  81.         {
  82.             (void) fprintf(stderr,"%s: Errr... in ", exe_name);
  83.             (void) fprintf(stderr,"\"%s\" file\nline: %d cannot execute [%s] (not on path?).\n", source_file, line_no, prog_name);
  84.         }
  85.     }
  86.     return(is_binary);
  87. }
  88.  
  89.  
  90.  
  91. /*
  92.     The function GetPath fills in the given char array with the value of 
  93.     the environmental variable PATH, with spaces instead of colons, and a 
  94.     '.' in the appropriate place.
  95. */
  96. extern void
  97. GetPath(p)
  98. char *p;
  99. {
  100.         char *path;
  101.  
  102.         path = getenv("PATH");
  103.         if (*path == ':')
  104.                 *p++ = '.';
  105.         while (*path != '\0') {
  106.                 if (*path == ':')
  107.                         *p++ = ' ';
  108.                 else
  109.                         *p++ = *path;
  110.                 ++path;
  111.         }
  112.         if (*--path == ':')
  113.                 *p++ = '.';
  114.         *p = '\0';
  115. }
  116.  
  117.  
  118. /*
  119.     The Executable  function returns True if file exists and is a directly 
  120.     executable Regular file. Otherwise False is returned.
  121.  */
  122. static int
  123. Executable(file)
  124. char *file;
  125. {
  126.         struct stat st_buf;
  127.  
  128.         if ( stat(file, &st_buf) != 0 )
  129.            return (FALSE); 
  130.         else 
  131.            return (S_IFREG & st_buf.st_mode) && (access(file,1)==0) && 
  132.         ( !(S_IFDIR & st_buf.st_mode) ) ; 
  133. }
  134.