home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0638.ZIP / CCE_0638 / GNULIB / LIBSRC93.ZOO / close.c < prev    next >
C/C++ Source or Header  |  1993-03-01  |  1KB  |  70 lines

  1. /*
  2.  *        Cross Development System for Atari ST 
  3.  *     Copyright (c) 1988, Memorial University of Newfoundland
  4.  *
  5.  */
  6. #include     <compiler.h>
  7. #include    <osbind.h>
  8. #include    <fcntl.h>
  9. #include    <device.h>
  10. #include    <stdlib.h>
  11. #include     <unistd.h>
  12. #include    "lib.h"
  13.  
  14. int (*pipeclose_p) __PROTO((int fd)) = 0;
  15.  
  16. #ifdef DEBUG
  17. #include <stdio.h>
  18. #endif
  19.  
  20. int close(fd)
  21. int    fd;
  22. {
  23.     int     oldfd;
  24.     extern    int    errno;
  25.     int        rval;
  26.     struct _device    *dev;
  27.     
  28.     if ((dev = _dev_fd(fd)) && dev->close)
  29.     return (*dev->close)(fd);
  30.     oldfd=fd;
  31.     fd = __OPEN_INDEX(fd);
  32.     
  33.     if ((fd >= 0) && (fd < __NHANDLES))
  34.     {
  35.     if (__open_stat[fd].pipe)
  36.     {
  37.         if (oldfd < 3)
  38.         {
  39.         __open_stat[fd].pipe = 0;
  40.         rval = close(oldfd);
  41.         }
  42.         else 
  43.         rval = (*pipeclose_p)(oldfd);
  44.         return(rval);
  45.     }
  46.     else 
  47.         if((__open_stat[fd].status == FH_ISAFILE) &&
  48.            (__open_stat[fd].filename))
  49.             free(__open_stat[fd].filename);
  50.     __open_stat[fd].status = FH_UNKNOWN;
  51.     __open_stat[fd].append = 0;
  52.     __open_stat[fd].nodelay = 0;
  53.     __open_stat[fd].pipe = 0;
  54.     __open_stat[fd].eclose = 0;
  55.     __open_stat[fd].filename = (char *) 0;
  56.     }
  57.     
  58.     if ((rval = Fclose(oldfd)) < 0) {
  59.     errno = -rval;
  60.     rval = -1;
  61.     }
  62.     
  63. #ifdef DEBUG
  64.     fprintf(stderr, "close(%d)->%d\n", (fd + __SMALLEST_VALID_HANDLE),
  65.         rval);
  66. #endif
  67.     return rval;
  68. }
  69.  
  70.