home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / m / mawk11as.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1991-12-18  |  9KB  |  390 lines

  1.  
  2. /********************************************
  3. files.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log:    files.c,v $
  14.  * Revision 5.1  91/12/05  07:56:00  brennan
  15.  * 1.1 pre-release
  16.  * 
  17. */
  18.  
  19. /* files.c */
  20.  
  21. #include "mawk.h"
  22. #include "files.h"
  23. #include "memory.h"
  24. #include "fin.h"
  25.  
  26.  
  27. #ifdef  V7
  28. #include  <sgtty.h>    /* defines FIOCLEX */
  29. #endif
  30.   
  31.  
  32. #if  HAVE_FCNTL_H  
  33.  
  34. #include <fcntl.h>
  35. #define  CLOSE_ON_EXEC(fd)   (void) fcntl(fd, F_SETFD, 1)
  36.  
  37. #else
  38. #define  CLOSE_ON_EXEC(fd) ioctl(fd, FIOCLEX, (PTR) 0)
  39. #endif
  40.  
  41.  
  42. /* We store dynamically created files on a linked linear
  43.    list with move to the front (big surprise)  */
  44.  
  45. typedef struct file {
  46. struct file *link ;
  47. STRING  *name ;
  48. short type ;
  49. int pid ;  /* we need to wait() when we close an out pipe */
  50.            /* holds temp file index under MSDOS */
  51. PTR   ptr ;  /* FIN*   or  FILE*   */
  52. }  FILE_NODE ;
  53.  
  54. static FILE_NODE *file_list ;
  55.  
  56. void set_stderr()
  57. {
  58.   file_list = ZMALLOC(FILE_NODE) ;
  59.   file_list->link = (FILE_NODE*) 0 ;
  60.   file_list->type = F_TRUNC ;
  61.   file_list->name = new_STRING("/dev/stderr") ;
  62.   file_list->ptr = (PTR) stderr ;
  63. }
  64.  
  65. PTR  file_find( sval, type )
  66.   STRING *sval ;
  67.   int type ;
  68. { register FILE_NODE *p = file_list ;
  69.   FILE_NODE *q = (FILE_NODE *) 0 ;
  70.   char *name = sval->str ;
  71.   char *ostr ;
  72.  
  73.   while (1)
  74.   {
  75.     if ( !p )   /* open a new one */
  76.     {
  77.       p = (FILE_NODE*) zmalloc(sizeof(FILE_NODE)) ;
  78.       switch( p->type = type )
  79.       {
  80.         case  F_TRUNC :
  81. #if MSDOS && NO_BINMODE==0
  82.             ostr = (binmode()&2) ? "wb" : "w" ;
  83. #else
  84.             ostr = "w" ;
  85. #endif
  86.             if ( !(p->ptr = (PTR) fopen(name, ostr)) )
  87.                 goto out_failure ;
  88.             break ;
  89.  
  90.         case  F_APPEND :
  91. #if MSDOS && NO_BINMODE==0
  92.             ostr = (binmode()&2) ? "ab" : "a" ;
  93. #else
  94.             ostr = "a" ;
  95. #endif
  96.             if ( !(p->ptr = (PTR) fopen(name, ostr)) )
  97.                 goto out_failure ;
  98.             break ;
  99.  
  100.         case  F_IN  :
  101.             if ( !(p->ptr = (PTR) FINopen(name, 0)) )
  102.             { zfree(p, sizeof(FILE_NODE)) ; return (PTR) 0 ; }
  103.             break ;
  104.  
  105.         case  PIPE_OUT :
  106.         case  PIPE_IN :
  107.  
  108. #if    HAVE_REAL_PIPES || HAVE_FAKE_PIPES 
  109.  
  110.             if ( !(p->ptr = get_pipe(name, type, &p->pid)) )
  111.                 if ( type == PIPE_OUT ) goto out_failure ;
  112.                 else
  113.                 { zfree(p, sizeof(FILE_NODE) ) ;
  114.                   return (PTR) 0 ;
  115.                 }
  116. #else
  117.          rt_error("pipes not supported") ;
  118. #endif
  119.             break ;
  120.  
  121. #ifdef  DEBUG
  122.         default :
  123.             bozo("bad file type") ;
  124. #endif
  125.       }
  126.       /* successful open */
  127.       p->name = sval ;
  128.       sval->ref_cnt++ ;
  129.       break ; /* while loop */
  130.     }
  131.  
  132.     if ( strcmp(name, p->name->str) == 0 )
  133.     { /* no distinction between F_APPEND and F_TRUNC here */
  134.       if ( p->type != type && 
  135.            (p->type < F_APPEND || type < F_APPEND))  goto type_failure ;
  136.       if ( !q )  /*at front of list */
  137.           return  p->ptr ;
  138.       /* delete from list for move to front */
  139.       q->link = p->link ;
  140.       break ;
  141.     }
  142.     q = p ; p = p->link ;
  143.   }
  144.  
  145.   /* put p at the front of the list */
  146.   p->link = file_list ;
  147.   return  (PTR) (file_list = p)->ptr ;
  148.  
  149. out_failure:
  150.   errmsg(errno, "cannot open \"%s\" for output", name) ;
  151.   mawk_exit(1) ;
  152.  
  153. type_failure :
  154.   rt_error("use of file \"%s\"\n\tis inconsistent with previous use",
  155.            name) ;
  156. }
  157.  
  158.  
  159. /* close a file and delete it's node from the file_list */
  160.  
  161. int  file_close( sval )
  162.   STRING *sval ;
  163. { register FILE_NODE *p = file_list ;
  164.   FILE_NODE *q = (FILE_NODE *) 0 ; /* trails p */
  165.   char *name = sval->str ;
  166.   int retval = 0 ;
  167.  
  168.   while ( p )
  169.         if ( strcmp(name,p->name->str) == 0 ) /* found */
  170.         { 
  171.           switch( p->type )
  172.           {
  173.             case  F_TRUNC :
  174.             case  F_APPEND :    
  175.                 (void) fclose((FILE *) p->ptr) ;
  176.                 break ;
  177.  
  178.             case  PIPE_OUT :
  179.                 (void) fclose((FILE *) p->ptr) ;
  180.  
  181. #if  HAVE_REAL_PIPES
  182.                 retval =  wait_for(p->pid) ;
  183. #endif
  184. #if  HAVE_FAKE_PIPES
  185.                 retval = close_fake_outpipe(p->name->str,p->pid) ;
  186. #endif
  187.                 break ;
  188.  
  189.             case F_IN  :
  190.                 FINclose((FIN *) p->ptr) ;
  191.                 break ;
  192.  
  193.             case PIPE_IN :
  194.                 FINclose((FIN *) p->ptr) ;
  195.  
  196. #if  HAVE_REAL_PIPES
  197.                 retval = wait_for(p->pid) ;
  198. #endif
  199. #if  HAVE_FAKE_PIPES
  200.                 (void) unlink(tmp_file_name(p->pid)) ;
  201. #endif
  202.                 break ;
  203.           }
  204.  
  205.           free_STRING(p->name) ;
  206.           if ( q )  q->link = p->link ;
  207.           else  file_list = p->link ;
  208.  
  209.           zfree(p, sizeof(FILE_NODE)) ;
  210.           return retval ;
  211.         }
  212.         else { q = p ; p = p->link ; }
  213.  
  214.   /* its not on the list */
  215.   return -1 ;
  216. }
  217.  
  218. /* When we exit, we need to close and wait for all output pipes */
  219.  
  220.  
  221. #if   HAVE_REAL_PIPES
  222.  
  223. void close_out_pipes()
  224. { register FILE_NODE *p = file_list ;
  225.  
  226.   while ( p )
  227.   { if ( p->type == PIPE_OUT )
  228.     { (void) fclose((FILE *) p->ptr) ;  (void) wait_for(p->pid) ; }
  229.     p = p->link ;
  230.   }
  231. }
  232.  
  233. #else
  234. #if  HAVE_FAKE_PIPES  /* pipes are faked with temp files */
  235.  
  236. void  close_fake_pipes()
  237. { register FILE_NODE *p = file_list ;
  238.  
  239.   /* close input pipes first to free descriptors for children */
  240.   while ( p )
  241.   {
  242.     if ( p->type == PIPE_IN )
  243.     { FINclose((FIN *) p->ptr) ;
  244.       (void) unlink(tmp_file_name(p->pid)) ; 
  245.     }
  246.     p = p->link ;
  247.   }
  248.   /* doit again */
  249.   p = file_list ;
  250.   while ( p )
  251.   {
  252.     if ( p->type == PIPE_OUT )
  253.     {
  254.       (void) fclose(p->ptr) ;
  255.       (void) close_fake_outpipe(p->name->str,p->pid) ;
  256.     }
  257.     p = p->link ;
  258.   }
  259. }
  260. #endif
  261. #endif
  262.  
  263. /* hardwire to /bin/sh for portability of programs */
  264. char *shell = "/bin/sh" ;
  265.  
  266. #if  HAVE_REAL_PIPES
  267.  
  268. PTR get_pipe( name, type, pid_ptr)
  269.   char *name ;
  270.   int type ;
  271.   int *pid_ptr ;
  272. { int the_pipe[2], local_fd, remote_fd ;
  273.  
  274.   if ( pipe(the_pipe) == -1 )  return (PTR) 0 ;
  275.   local_fd = the_pipe[type == PIPE_OUT] ;
  276.   remote_fd = the_pipe[type == PIPE_IN ] ;
  277.   /* to keep output ordered correctly */
  278.   fflush(stdout) ; fflush(stderr) ;
  279.  
  280.   switch( *pid_ptr = fork() )
  281.   { case -1 :  
  282.       (void) close(local_fd) ;
  283.       (void) close(remote_fd) ;
  284.       return (PTR) 0 ;
  285.  
  286.     case  0 :
  287.         (void) close(local_fd) ;
  288.         (void) close(type == PIPE_IN) ;
  289.         (void) dup( remote_fd ) ;
  290.         (void) close( remote_fd ) ;
  291.         (void) execl(shell, shell, "-c", name, (char *) 0 ) ;
  292.         errmsg(errno, "failed to exec %s -c %s" , shell, name) ;
  293.         fflush(stderr) ;
  294.         _exit(128) ;
  295.  
  296.     default :
  297.         (void) close(remote_fd) ;
  298.         /* we could deadlock if future child inherit the local fd ,
  299.            set close on exec flag */
  300.         CLOSE_ON_EXEC(local_fd) ;
  301.         break ;
  302.   }
  303.  
  304.   return  type == PIPE_IN ? (PTR) FINdopen(local_fd, 0) : 
  305.                             (PTR)  fdopen(local_fd, "w")  ;
  306. }
  307.   
  308.  
  309.  
  310. /*------------ children ------------------*/
  311.  
  312. /* we need to wait for children at the end of output pipes to
  313.    complete so we know any files they have created are complete */
  314.  
  315. /* dead children are kept on this list */
  316.  
  317. static struct child {
  318. int pid ;
  319. int exit_status ;
  320. struct child *link ;
  321. }  *child_list ;
  322.  
  323. static  void  add_to_child_list(pid, exit_status)
  324.   int pid, exit_status ;
  325. { register struct child *p = 
  326.           (struct child *) zmalloc(sizeof(struct child)) ;
  327.  
  328.   p->pid = pid ; p->exit_status = exit_status ;
  329.   p->link = child_list ; child_list = p ;
  330. }
  331.  
  332. static struct child *remove_from_child_list(pid)
  333.   int pid ;
  334. { register struct child *p = child_list ;
  335.   struct child *q = (struct child *) 0 ;
  336.  
  337.   while ( p )
  338.     if ( p->pid == pid )
  339.     {
  340.         if ( q ) q->link = p->link ;
  341.         else child_list = p->link ;
  342.         break ;
  343.     }
  344.     else { q = p ; p = p->link ; }
  345.  
  346.   return p ;  /* null return if not in the list */
  347. }
  348.     
  349.  
  350. /* wait for a specific child to complete and return its 
  351.    exit status 
  352.  
  353.    If pid is zero, wait for any single child
  354. */
  355.  
  356. int wait_for(pid)
  357.   int pid ;
  358. { int exit_status ;
  359.   struct child *p ;
  360.   int id ;
  361.  
  362.   if ( pid == 0 )
  363.   {
  364.     id = wait(&exit_status) ;
  365.     add_to_child_list(id, exit_status) ;
  366.   }
  367.   else
  368.   /* see if an earlier wait() caught our child */
  369.   if ( p = remove_from_child_list(pid) ) 
  370.   { exit_status = p->exit_status ;
  371.     ZFREE(p) ;
  372.   }
  373.   else /* need to really wait */
  374.     while ( (id = wait(&exit_status)) != pid )
  375.         if ( id == -1 ) /* can't happen */  bozo("wait_for") ;
  376.         else
  377.         { /* we got the exit status of another child
  378.              put it on the child list and try again */
  379.           add_to_child_list(id, exit_status ) ;
  380.         }
  381.  
  382.   if ( exit_status & 0xff ) 
  383.        exit_status = 128 + (exit_status & 0xff) ;
  384.   else  exit_status = (exit_status & 0xff00)>>8 ;
  385.  
  386.   return exit_status ;
  387. }
  388.         
  389. #endif  /* HAVE_REAL_PIPES */
  390.