Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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