home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / close.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  1.3 KB  |  69 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.  
  13. int (*pipeclose_p) __PROTO((int fd)) = 0;
  14.  
  15. #ifdef DEBUG
  16. #include <stdio.h>
  17. #endif
  18.  
  19. int close(fd)
  20. int    fd;
  21. {
  22.     int     oldfd;
  23.     extern    int    errno;
  24.     int        rval;
  25.     struct _device    *dev;
  26.     
  27.     if ((dev = _dev_fd(fd)) && dev->close)
  28.     return (*dev->close)(fd);
  29.     oldfd=fd;
  30.     fd = __OPEN_INDEX(fd);
  31.     
  32.     if ((fd >= 0) && (fd < __NHANDLES))
  33.     {
  34.     if (__open_stat[fd].pipe)
  35.     {
  36.         if (oldfd < 3)
  37.         {
  38.         __open_stat[fd].pipe = 0;
  39.         rval = close(oldfd);
  40.         }
  41.         else 
  42.         rval = (*pipeclose_p)(oldfd);
  43.         return(rval);
  44.     }
  45.     else 
  46.         if((__open_stat[fd].status == FH_ISAFILE) &&
  47.            (__open_stat[fd].filename))
  48.             free(__open_stat[fd].filename);
  49.     __open_stat[fd].status = FH_UNKNOWN;
  50.     __open_stat[fd].append = 0;
  51.     __open_stat[fd].nodelay = 0;
  52.     __open_stat[fd].pipe = 0;
  53.     __open_stat[fd].eclose = 0;
  54.     __open_stat[fd].filename = (char *) 0;
  55.     }
  56.     
  57.     if ((rval = Fclose(oldfd)) < 0) {
  58.     errno = -rval;
  59.     rval = -1;
  60.     }
  61.     
  62. #ifdef DEBUG
  63.     fprintf(stderr, "close(%d)->%d\n", (fd + __SMALLEST_VALID_HANDLE),
  64.         rval);
  65. #endif
  66.     return rval;
  67. }
  68.  
  69.