home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnudiff.zip / waitpid.c < prev    next >
C/C++ Source or Header  |  1994-09-12  |  2KB  |  69 lines

  1. /* Emulate waitpid on systems that just have wait.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "system.h"
  21.  
  22. #define WAITPID_CHILDREN 8
  23. static pid_t waited_pid[WAITPID_CHILDREN];
  24. static int waited_status[WAITPID_CHILDREN];
  25.  
  26. pid_t
  27. waitpid (pid, stat_loc, options)
  28.      pid_t pid;
  29.      int *stat_loc;
  30.      int options;
  31. {
  32.   int i;
  33.   pid_t p;
  34.  
  35.   if (!options  &&  (0 < pid || pid == -1))
  36.     {
  37.       /* If we have already waited for this child, return it immediately.  */
  38.       for (i = 0;  i < WAITPID_CHILDREN;  i++)
  39.     {
  40.       p = waited_pid[i];
  41.       if (p  &&  (p == pid  ||  pid == -1))
  42.         {
  43.           waited_pid[i] = 0;
  44.           goto success;
  45.         }
  46.     }
  47.  
  48.       /* The child has not returned yet; wait for it, accumulating status.  */
  49.       for (i = 0;  i < WAITPID_CHILDREN;  i++)
  50.     if (! waited_pid[i])
  51.       {
  52.         p = wait (&waited_status[i]);
  53.         if (p < 0)
  54.           return p;
  55.         if (p == pid  ||  pid == -1)
  56.           goto success;
  57.         waited_pid[i] = p;
  58.       }
  59.     }
  60.  
  61.   /* We cannot emulate this wait call, e.g. because of too many children.  */
  62.   abort ();
  63.  
  64. success:
  65.   if (stat_loc)
  66.     *stat_loc = waited_status[i];
  67.   return p;
  68. }
  69.