Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <sys/types.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <io.h>
  8. #include <libc/file.h>
  9. #include <libc/local.h>
  10.  
  11. FILE *
  12. fdopen(int fildes, const char *mode)
  13. {
  14.   FILE *f;
  15.   int rw, oflags = 0;
  16.   char tbchar;
  17.  
  18.   f = __alloc_file();
  19.   if (f == NULL)
  20.     return NULL;
  21.  
  22.   rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));
  23.  
  24.   switch (*mode)
  25.   {
  26.   case 'a':
  27.     oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
  28.     break;
  29.   case 'r':
  30.     oflags = rw ? O_RDWR : O_RDONLY;
  31.     break;
  32.   case 'w':
  33.     oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
  34.     break;
  35.   default:
  36.     return (NULL);
  37.   }
  38.   if (mode[1] == '+')
  39.     tbchar = mode[2];
  40.   else
  41.     tbchar = mode[1];
  42.   if (tbchar == 't')
  43.     oflags |= O_TEXT;
  44.   else if (tbchar == 'b')
  45.     oflags |= O_BINARY;
  46.   else
  47.     oflags |= (_fmode & (O_TEXT|O_BINARY));
  48.  
  49.   if (*mode == 'a')
  50.     lseek(fildes, 0, SEEK_END);
  51.  
  52.   f->_cnt = 0;
  53.   f->_file = fildes;
  54.   f->_bufsiz = 0;
  55.   if (rw)
  56.     f->_flag = _IORW;
  57.   else if (*mode == 'r')
  58.     f->_flag = _IOREAD;
  59.   else
  60.     f->_flag = _IOWRT;
  61.  
  62.   f->_base = f->_ptr = NULL;
  63.  
  64.   setmode(fildes, oflags & (O_TEXT|O_BINARY));
  65.  
  66.   return f;
  67. }
  68.