Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4874 Rev 4921
Line 2... Line 2...
2
 
2
 
3
#include 
3
#include 
4
#include 
4
#include 
5
#include 
5
#include 
6
#include <_syslist.h>
-
 
7
#include 
-
 
8
#include 
-
 
9
#include 
-
 
10
#include 
-
 
Line 11... Line 6...
11
#include 
6
#include <_syslist.h>
12
 
7
 
Line 13... Line 8...
13
/* Some targets provides their own versions of this functions.  Those
8
/* Some targets provides their own versions of this functions.  Those
Line 20... Line 15...
20
#endif
15
#endif
Line 21... Line 16...
21
 
16
 
Line 22... Line 17...
22
#ifndef REENTRANT_SYSCALLS_PROVIDED
17
#ifndef REENTRANT_SYSCALLS_PROVIDED
-
 
18
 
-
 
19
/* We use the errno variable used by the system dependent layer.  */
Line 23... Line 20...
23
 
20
#undef errno
24
/* We use the errno variable used by the system dependent layer.  */
21
extern int errno;
25
 
22
 
Line 47... Line 44...
47
	This is a reentrant version of <>.  It
44
	This is a reentrant version of <>.  It
48
	takes a pointer to the global data block, which holds
45
	takes a pointer to the global data block, which holds
49
	<>.
46
	<>.
50
*/
47
*/
Line 51... Line -...
51
 
-
 
52
 
-
 
53
 
-
 
54
#define NULL_HANDLE  (int)-1
-
 
55
#define DUMMY_HANDLE (int)-2
-
 
56
 
-
 
57
#define _READ   0x0001  /* file opened for reading */
-
 
58
#define _WRITE  0x0002  /* file opened for writing */
-
 
59
#define _UNGET  0x0004  /* ungetc has been done */
-
 
60
#define _BIGBUF 0x0008  /* big buffer allocated */
-
 
61
#define _EOF    0x0010  /* EOF has occurred */
-
 
62
#define _SFERR  0x0020  /* error has occurred on this file */
-
 
63
#define _APPEND 0x0080  /* file opened for append */
-
 
64
#define _BINARY 0x0040  /* file is binary, skip CRLF processing */
-
 
65
#define _TMPFIL 0x0800  /* this is a temporary file */
-
 
66
#define _DIRTY  0x1000  /* buffer has been modified */
-
 
67
#define _ISTTY  0x2000  /* is console device */
-
 
68
#define _DYNAMIC 0x4000 /* FILE is dynamically allocated   */
-
 
69
#define _FILEEXT 0x8000 /* lseek with positive offset has been done */
-
 
70
#define _COMMIT 0x0001  /* extended flag: commit OS buffers on flush */
-
 
71
 
-
 
72
extern int       _fmode;
-
 
73
 
-
 
74
 
-
 
75
static inline void debug_out(const char val)
-
 
76
{
-
 
77
    __asm__ __volatile__(
-
 
78
    "int $0x40 \n\t"
-
 
79
    ::"a"(63), "b"(1),"c"(val));
-
 
80
}
-
 
81
 
-
 
82
int   debugwrite(const char *path, const void *buff,
-
 
83
                 size_t offset, size_t count, size_t *writes)
-
 
84
{
-
 
85
    int ret = count;
-
 
86
    const char *p = buff;
-
 
87
 
-
 
88
    while (count--)
-
 
89
    {
-
 
90
        debug_out(*p++);
-
 
91
    };
-
 
92
    *writes = ret;
-
 
93
    return ret;
-
 
94
};
-
 
95
 
-
 
96
static int __openFileHandle(const char *path, int mode, int *err)
-
 
97
{
-
 
98
    fileinfo_t     info;
-
 
99
    __file_handle *handle;
-
 
100
 
-
 
101
//    path = getfullpath(name);
-
 
102
 
-
 
103
    *err = get_fileinfo(path, &info);
-
 
104
 
-
 
105
    if( mode & O_EXCL && mode & O_CREAT )
-
 
106
    {
-
 
107
        if( ! *err)
-
 
108
        {
-
 
109
            *err = EEXIST;
-
 
110
            return -1;
-
 
111
        };
-
 
112
    }
-
 
113
 
-
 
114
    if( *err)
-
 
115
    {
-
 
116
        if(mode & O_CREAT)
-
 
117
            *err=create_file(path);
-
 
118
 
-
 
119
        if( *err)
-
 
120
        {
-
 
121
            return -1;
-
 
122
        };
-
 
123
    };
-
 
124
    if( mode & O_TRUNC )
-
 
125
        set_file_size(path, 0);
-
 
126
 
-
 
127
    if ( !(handle=(__file_handle*)malloc(sizeof( __file_handle) )))
-
 
128
    {
-
 
129
        *err = ENOMEM;
-
 
130
        return -1;
-
 
131
    };
-
 
132
 
-
 
133
    handle->name   = strdup(path);
-
 
134
    handle->offset = 0;
-
 
135
    handle->write  = write_file;
-
 
136
 
-
 
137
    *err = 0;
-
 
138
 
-
 
139
    return (int)handle;
-
 
140
};
-
 
141
 
-
 
142
 
-
 
143
 
48
 
144
int
49
int
145
_DEFUN (_open_r, (ptr, file, flags, dmode),
50
_DEFUN (_open_r, (ptr, file, flags, mode),
146
     struct _reent *ptr _AND
51
     struct _reent *ptr _AND
147
     _CONST char *file _AND
52
     _CONST char *file _AND
148
     int flags _AND
53
     int flags _AND
149
     int dmode)
-
 
150
{
-
 
151
    int         hid;
-
 
152
    int         handle;
-
 
153
    int         err = 0;
-
 
154
    unsigned    iomode_flags;
-
 
155
    int         rwmode;
-
 
156
 
-
 
157
/*
-
 
158
    if (flags & ~(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_APPEND | O_TRUNC))
-
 
159
    {
-
 
160
        ptr->_errno = ENOSYS;
-
 
161
        return -1;
-
 
162
    }
-
 
163
*/
-
 
164
 
-
 
165
    // First try to get the required slot.
-
 
166
    // No point in creating a file only to not use it.  JBS 99/10/26
-
 
167
    hid = __allocPOSIXHandle( DUMMY_HANDLE );
-
 
168
    if( hid == -1 )
-
 
169
    {
-
 
170
        ptr->_errno = EMFILE;
-
 
171
        return( -1 );
-
 
172
    }
-
 
173
 
-
 
174
    handle = __openFileHandle( file, flags, &err);
-
 
175
 
-
 
176
    if( handle == -1 )
-
 
177
    {
-
 
178
        __freePOSIXHandle( hid );
-
 
179
        ptr->_errno = err;
-
 
180
        return( -1 );
-
 
181
    }
-
 
182
 
-
 
183
    __setOSHandle( hid, handle );   // JBS 99/11/01
-
 
184
 
-
 
185
    rwmode = flags & ( O_RDONLY | O_WRONLY | O_RDWR | O_NOINHERIT );
-
 
186
 
-
 
187
    iomode_flags = 0;
-
 
188
 
-
 
189
    if( rwmode == O_RDWR )       iomode_flags |= _READ | _WRITE;
-
 
190
    else if( rwmode == O_RDONLY) iomode_flags |= _READ;
-
 
191
    else if( rwmode == O_WRONLY) iomode_flags |= _WRITE;
-
 
192
    if( flags & O_APPEND )        iomode_flags |= _APPEND;
-
 
193
    if( flags & (O_BINARY|O_TEXT) ) {
-
 
194
        if( flags & O_BINARY )    iomode_flags |= _BINARY;
-
 
195
    } else {
-
 
196
        if( _fmode == O_BINARY ) iomode_flags |= _BINARY;
-
 
197
    }
-
 
198
    __SetIOMode( hid, iomode_flags );
-
 
199
 
-
 
200
    ptr->_errno = 0;
-
 
201
 
-
 
202
    return (hid);
-
 
203
}
-
 
204
 
-
 
205
int
-
 
206
_DEFUN (open, (file, flags, ...),
-
 
207
        const char *file _AND
-
 
208
        int flags _DOTS)
54
     int mode)
209
{
-
 
210
  va_list ap;
55
{
Line 211... Line 56...
211
  int ret;
56
  int ret;
212
 
57
 
213
  va_start (ap, flags);
58
  errno = 0;
214
  ret = _open_r (_REENT, file, flags, va_arg (ap, int));
59
  if ((ret = _open (file, flags, mode)) == -1 && errno != 0)
215
  va_end (ap);
60
    ptr->_errno = errno;
Line 216... Line -...
216
  return ret;
-
 
217
}
61
  return ret;