home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / fcntl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  1.2 KB  |  67 lines

  1. /*
  2.  * limited emulation of fcntl
  3.  *
  4.  * ++jrb    bammi@cadence.com
  5.  */
  6.  
  7. #include <fcntl.h>
  8. #include <stdarg.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11.  
  12. #ifdef __STDC__
  13. int fcntl (int f, int cmd, ...)
  14. #else
  15. int fcntl (f, cmd)
  16. int f;
  17. int cmd;
  18. #endif
  19. {
  20.     int handle;
  21.     va_list argp;
  22.  
  23.     va_start(argp, cmd);
  24.  
  25.     /* check for valid descriptor */
  26.     handle = __OPEN_INDEX(f);
  27.     if( (handle >= __NHANDLES) || (handle < 0) )
  28.     {
  29.     errno = EBADF;
  30.     return -1;
  31.     }
  32.     
  33.     if(__open_stat[handle].status == FH_UNKNOWN)
  34.     isatty(f); /* try to set .status */
  35.  
  36.     if((__open_stat[handle].status == FH_UNKNOWN) ||
  37.       ((__open_stat[handle].status == FH_ISAFILE) && (__open_stat[handle].filename == (char *)0)) )
  38.     {
  39.     errno = EBADF;
  40.     return -1;
  41.     }
  42.  
  43.     switch(cmd) {
  44.       case F_DUPFD:
  45.     /* there is no way to emulate this even reasonably under TOS,  so... */
  46.     return dup(f);
  47.  
  48.       case F_GETFD:
  49.     return (int) (__open_stat[handle].eclose);
  50.  
  51.       case F_SETFD:
  52.     __open_stat[handle].eclose = va_arg(argp, int) & 1;
  53.     return 0;
  54.  
  55.       case F_GETFL: /* fudge city */
  56.     return (int)(*((short *)(&(__open_stat[handle]))));
  57.  
  58.       case F_SETFL:
  59.     *((short *)(&(__open_stat[handle]))) = va_arg(argp, int);
  60.     return 0;
  61.  
  62.       default:
  63.     errno = EBADARG;
  64.     return -1;
  65.     }
  66. }
  67.