Пример #1
0
FILE* fopen(const char *file, const char *mode)
{
  FILE *f;
  int fd, rw, oflags = 0;
   
  if (file == 0)
    return 0;
  if (mode == 0)
    return 0;

  f = __alloc_file();
  if (f == NULL)
    return NULL;

  rw = (strchr(mode, '+') == NULL) ? 0 : 1;
  if (strchr(mode, 'a'))
    oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
  if (strchr(mode, 'r'))
    oflags = rw ? O_RDWR : O_RDONLY;
  if (strchr(mode, 'w'))
    oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
  if (strchr(mode, 't'))
    oflags |= O_TEXT;
  else if (strchr(mode, 'b'))
    oflags |= O_BINARY;
  else
    oflags |= (_fmode & (O_TEXT|O_BINARY));

  fd = _open(file, oflags, 0);
  if (fd < 0)
    return NULL;

// msvcrt ensures that writes will end up at the end of file in append mode
// we just move the file pointer to the end of file initially

  if (strchr(mode, 'a'))
    lseek(fd, 0, SEEK_END);

  f->_cnt = 0;
  f->_file = fd;
  f->_bufsiz = 0;
  if (rw)
    f->_flag = _IOREAD | _IOWRT;
  else if (strchr(mode, 'r'))
    f->_flag = _IOREAD;
  else
    f->_flag = _IOWRT;

  if (strchr(mode, 't'))
    f->_flag |= _IOTEXT;
  else if (strchr(mode, 'b'))
    f->_flag |= _IOBINARY;
  else if (_fmode & O_BINARY)
    f->_flag |= _IOBINARY;

  f->_base = f->_ptr = NULL;
  return f;
}
Пример #2
0
/*
 * @implemented
 */
FILE* _fdopen(int handle, char* mode)
{
  FILE* file;
  int rw;

  if (handle == 0)
    return stdin;

  if (handle == 1)
    return stdout;

  if (handle == 2)
    return stderr;

  if (handle == 3)
    return stdaux;

  if (handle == 4)
    return stdprn;

  file = __alloc_file();
  if (file == NULL)
    return NULL;
  file->_file = handle;

  rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));

  if (*mode == 'a')
    _lseek(handle, 0, SEEK_END);

  file->_cnt = 0;
  file->_file = handle;
  file->_bufsiz = 0;

// The mode of the stream must be compatible with the mode of the file descriptor.
// this should be checked.

  if (rw)
    file->_flag = _IOREAD | _IOWRT;
  else if (*mode == 'r')
    file->_flag = _IOREAD;
  else
    file->_flag = _IOWRT;

  file->_base = file->_ptr = NULL;

  return file;
}
Пример #3
0
FILE * fopen(const char *file, const char *mode)
{
  FILE *f;
  int fd, rw, oflags = 0;
  char tbchar;

  if (file == 0)
    return 0;
  if (mode == 0)
    return 0;

  f = __alloc_file();
  if (f == NULL)
    return NULL;

  rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));

  switch (*mode)
  {
  case 'a':
    oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
    break;
  case 'r':
    oflags = rw ? O_RDWR : O_RDONLY;
    break;
  case 'w':
    oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
    break;
  default:
    return (NULL);
  }
  if (mode[1] == '+')
    tbchar = mode[2];
  else
    tbchar = mode[1];
  if (tbchar == 't')
    oflags |= O_TEXT;
  else if (tbchar == 'b')
    oflags |= O_BINARY;
  else
    oflags |= (_fmode & (O_TEXT|O_BINARY));

  fd = open(file, oflags, 0666);
  if (fd < 0)
    return NULL;

  if (*mode == 'a')
    lseek(fd, 0, SEEK_END);

  f->_cnt = 0;
  f->_file = fd;
  f->_bufsiz = 0;
  if (rw)
    f->_flag = _IORW;
  else if (*mode == 'r')
    f->_flag = _IOREAD;
  else
    f->_flag = _IOWRT;

  f->_base = f->_ptr = NULL;
  f->std_ops=NULL;
//  __libclog_printf("fopen: return=%x\n",f);
  return f;
}
Пример #4
0
FILE *
_wfopen(const wchar_t *file, const wchar_t *mode)
{
    FILE *f;
    int fd, rw, oflags = 0;
    /*char tbchar;*/

    if (file == 0)
        return 0;
    if (mode == 0)
        return 0;

    rw = (mode[1] == '+') || (mode[1] && (mode[2] == '+'));

    switch (*mode)
    {
    case 'a':
        oflags = FILE_CREATE_OPEN | (rw ? FILE_READ | FILE_WRITE : FILE_WRITE);
        break;
    case 'r':
        oflags = FILE_FORCE_OPEN | (rw ? FILE_READ | FILE_WRITE : FILE_READ);
        break;
    case 'w':
        oflags = FILE_FORCE_CREATE | (rw ? FILE_READ | FILE_WRITE : FILE_READ);
        break;
    default:
        return (NULL);
    }
    
    /* xxx - text/binary distinction ignored for Mobius */
    /*if (mode[1] == '+')
        tbchar = mode[2];
    else
        tbchar = mode[1];

    if (tbchar == 't')
        oflags |= O_TEXT;
    else if (tbchar == 'b')
        oflags |= O_BINARY;
    else
        oflags |= (_fmode & (O_TEXT|O_BINARY));*/

    fd = FsOpen(file, oflags);
    if (fd == NULL)
        return NULL;

    f = __alloc_file();
    if (f == NULL)
        return NULL;

    if (*mode != 'a' ||
		!FsGetFileLength(fileno(f), &f->_offset))
        f->_offset = 0;

    f->_cnt = 0;
    f->_file = fd;
    f->_bufsiz = 0;
    f->_flag = oflags;

    f->_base = f->_ptr = NULL;
    return f;
}