Subversion Repositories Kolibri OS

Rev

Rev 701 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
701 serge 1
/****************************************************************************
2
*
3
*                            Open Watcom Project
4
*
5
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
6
*
7
*  ========================================================================
8
*
9
*    This file contains Original Code and/or Modifications of Original
10
*    Code as defined in and that are subject to the Sybase Open Watcom
11
*    Public License version 1.0 (the 'License'). You may not use this file
12
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
13
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
14
*    provided with the Original Code and Modifications, and is also
15
*    available at www.sybase.com/developer/opensource.
16
*
17
*    The Original Code and all software distributed under the License are
18
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
19
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
20
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
21
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
22
*    NON-INFRINGEMENT. Please see the License for the specific language
23
*    governing rights and limitations under the License.
24
*
25
*  ========================================================================
26
*
27
* Description:  Win32 implementation of open() and sopen().
28
*
29
****************************************************************************/
30
 
31
 
32
#include "variety.h"
33
#include "widechar.h"
34
#include 
35
#include 
36
#include 
37
#include 
38
#include 
39
#include 
40
#include 
41
#include 
42
#include 
43
#include "liballoc.h"
44
#include "iomode.h"
45
#include "fileacc.h"
46
#include "openmode.h"
47
#include "rtdata.h"
48
#include "seterrno.h"
711 serge 49
#include "kolibri.h"
701 serge 50
 
51
extern unsigned __NFiles;
52
extern char *__appcwd;
53
extern int __appcwdlen;
54
 
55
#if (defined(__WINDOWS__) || defined(__NT__))
56
 
57
typedef struct
58
{
59
  char     *name;
60
  unsigned int offset;
61
}__file_handle;
62
 
63
 
64
char* getfullpath(const char* path)
65
{
66
    int prev_is_slash=0;
67
    int len=0, depth=0, i;
68
    char* buff;
69
    char c;
70
 
71
    if(*path == '/')
72
    {
73
      buff = (char*)lib_malloc(strlen(path)+1);
74
      buff[0] = '\0';
75
      len=0;
76
    }
77
    else
78
    {
79
      len= __appcwdlen;
80
      buff = (char*)lib_malloc(len+strlen(path)+1);
81
      strncpy(buff, __appcwd, __appcwdlen);
82
 
83
      prev_is_slash = 1;
84
      buff[len] = 0;
85
      for(i=0; buff[i]; i++)
86
        if(buff[i] == '/' && i < len-1) depth++;
87
    }
88
 
89
    while(c=*path++)
90
    {
91
      switch (c)
92
      {
93
 
94
        case '.':
95
          if((*path == '.')&&
96
             (*path+1)== '/')
97
          { if(!depth)
98
            {  free(buff);
99
               return 0;
100
            };
101
            buff[len-1] = 0;
102
            len = strrchr(buff, '/') + 1 - buff;
103
            buff[len] = 0;
104
            depth--;
105
            path +=2;
106
            prev_is_slash = 1;
107
            continue;
108
          }
109
          if(*path == '/')
110
          {
111
            path++;
112
            prev_is_slash = 1;
113
            continue;
114
          }
115
          buff[len++] = c;
116
          continue;
117
 
118
        case '/':
119
          prev_is_slash = 1;
120
          buff[len++] = c;
121
          continue;
122
 
123
        default:
124
          prev_is_slash = 0;
125
          buff[len++] = c;
126
          continue;
127
        };
128
    };
129
    buff[len]= '\0';
130
    return buff;
131
};
132
 
133
 
134
size_t FileSize(FILE *fp)
135
{
136
  int hdl;
137
  __file_handle *fh;
138
  FILEINFO info;
139
 
140
  hdl = fileno( fp );
141
 // __handle_check( hdl, -1 );
142
 
143
  fh = (__file_handle*) __getOSHandle(hdl);
144
 
145
 
146
  get_fileinfo(fh->name,&info);
147
 
148
  return info.size;
149
 
150
}
151
 
152
int access(const char *path, int mode)
153
{ size_t retval;
154
  FILEINFO info;
155
  char *p;
156
 
157
  p = getfullpath(path);
158
  retval=get_fileinfo(p,&info);
159
  free(p);
160
 
161
  return retval;
162
 
163
}
164
 
165
static HANDLE __openFileHandle(const CHAR_TYPE *name, int mode)
166
{
167
  FILEINFO info;
168
  __file_handle *handle;
169
  char *path;
170
  int err;
171
 
172
  path = getfullpath(name);
173
 
174
  if(err=get_fileinfo(path,&info))
175
  {
176
//    printf("failed getfileinfo %s\n\r", path);
177
 
178
    if(mode & O_CREAT)
179
      err=create_file(path);
180
 
181
    if(err)
182
    {
183
      lib_free(path);
184
      return (HANDLE)-1;
185
    };
186
  };
187
 
188
  if ( !(handle=(__file_handle*)lib_malloc(sizeof( __file_handle) )))
189
  { lib_free(path);
190
    return (HANDLE)-1;
191
  };
192
 
193
  handle->name = path;
194
  handle->offset = 0;
195
 
196
  return (HANDLE)handle;
197
};
198
 
199
 
200
static int __F_NAME(_sopen,__wsopen)( const CHAR_TYPE *name, int mode, int share, va_list args )
201
{
202
    HANDLE              handle;
203
    int                 hid, rwmode;
204
    unsigned            iomode_flags;
205
 
206
    // First try to get the required slot.
207
    // No point in creating a file only to not use it.  JBS 99/10/26
208
    hid = __allocPOSIXHandle( DUMMY_HANDLE );
209
    if( hid == -1 )
210
     {
211
        return( -1 );
212
    }
213
 
214
    rwmode = mode;
215
 
216
 
217
    /*** Open the file ***/
218
 
219
    handle = __openFileHandle( name, mode);
220
 
221
    if( handle==(HANDLE)-1 )
222
    {
223
      __freePOSIXHandle( hid );
224
      return( -1 ); //error
225
    }
226
 
227
// Now use the slot we got.
228
    __setOSHandle( hid, handle );   // JBS 99/11/01
229
 
230
    iomode_flags = 0;
231
 
232
 
233
    if( rwmode == O_RDWR )       iomode_flags |= _READ | _WRITE;
234
    else if( rwmode == O_RDONLY) iomode_flags |= _READ;
235
    else if( rwmode == O_WRONLY) iomode_flags |= _WRITE;
236
    if( mode & O_APPEND )        iomode_flags |= _APPEND;
237
    if( mode & (O_BINARY|O_TEXT) ) {
238
        if( mode & O_BINARY )    iomode_flags |= _BINARY;
239
    } else {
240
        if( _RWD_fmode == O_BINARY ) iomode_flags |= _BINARY;
241
    }
242
    __SetIOMode( hid, iomode_flags );
243
    return( hid );
244
}
245
 
246
#elif
247
static int __F_NAME(_sopen,__wsopen)( const CHAR_TYPE *name, int mode, int share, va_list args )
248
{
249
    DWORD               create_disp, exists_disp;
250
    DWORD               perm, fileattr;
251
    DWORD               desired_access, share_mode;
252
    SECURITY_ATTRIBUTES security;
253
    HANDLE              handle;
254
    int                 hid, rwmode;
255
    unsigned            iomode_flags;
256
 
257
    // First try to get the required slot.
258
    // No point in creating a file only to not use it.  JBS 99/10/26
259
    hid = __allocPOSIXHandle( DUMMY_HANDLE );
260
    if( hid == -1 ) {
261
        return( -1 );
262
    }
263
 
264
    rwmode = mode & OPENMODE_ACCESS_MASK;
265
    __GetNTAccessAttr( rwmode, &desired_access, &perm );
266
    __GetNTShareAttr( share|rwmode, &share_mode );
267
    fileattr = FILE_ATTRIBUTE_NORMAL;
268
 
269
    security.nLength = sizeof( SECURITY_ATTRIBUTES );
270
    security.lpSecurityDescriptor = NULL;
271
    security.bInheritHandle = mode&O_NOINHERIT ? FALSE : TRUE;
272
 
273
#ifdef DEFAULT_WINDOWING
274
#ifdef __WIDECHAR__
275
    if( _WindowsNewWindow != 0 && !_wcsicmp( name, L"con" ) )
276
#else
277
    if( _WindowsNewWindow != 0 && !stricmp( name, "con" ) )
278
#endif
279
    {
280
        handle = (HANDLE) __NTGetFakeHandle();
281
 
282
        // Now use the slot we got.
283
        __setOSHandle( hid, handle );   // JBS 99/11/01
284
        _WindowsNewWindow( NULL, hid, -1 );
285
 
286
        iomode_flags = _ISTTY;
287
    } else {
288
#endif
289
        if( mode & O_CREAT ) {
290
            perm = va_arg( args, int );
291
                va_end( args );
292
                perm &= ~_RWD_umaskval;             /* 05-jan-95 */
293
            if( ( perm & S_IREAD ) && !( perm & S_IWRITE ) ) {
294
                fileattr = FILE_ATTRIBUTE_READONLY;
295
            }
296
            if( mode & O_EXCL ) {
297
                create_disp = CREATE_NEW;
298
                exists_disp = CREATE_NEW;
299
            } else if( mode & O_TRUNC ) {
300
                create_disp = CREATE_ALWAYS;
301
                exists_disp = CREATE_NEW;
302
            } else {
303
                create_disp = OPEN_ALWAYS;
304
                exists_disp = OPEN_EXISTING;
305
            }
306
        } else if( mode & O_TRUNC ) {
307
            exists_disp = TRUNCATE_EXISTING;
308
        } else {
309
            exists_disp = OPEN_EXISTING;
310
        }
311
 
312
        /*** Open the file ***/
313
        #ifdef __WIDECHAR__
314
            handle = __lib_CreateFileW( name, desired_access, share_mode,
315
                                        &security, exists_disp, fileattr,
316
                                        NULL );
317
        #else
318
            handle = CreateFileA( name, desired_access, share_mode,
319
                                  &security, exists_disp, fileattr, NULL );
320
        #endif
321
        if( handle==(HANDLE)-1 ) {
322
            if( mode&O_CREAT ) {
323
                #ifdef __WIDECHAR__
324
                    handle = __lib_CreateFileW( name, desired_access,
325
                                                share_mode, NULL, create_disp,
326
                                                fileattr, NULL );
327
                #else
328
                    handle = CreateFileA( name, desired_access,
329
                                          share_mode, NULL, create_disp,
330
                                          fileattr, NULL );
331
                #endif
332
            }
333
            if( handle == (HANDLE)-1 ) {
334
                __freePOSIXHandle( hid );
335
                return( __set_errno_nt() );
336
            }
337
        }
338
 
339
        // Now use the slot we got.
340
        __setOSHandle( hid, handle );   // JBS 99/11/01
341
 
342
        iomode_flags = 0;
343
 
344
        if( isatty(hid) ) {
345
            iomode_flags = _ISTTY;
346
        }
347
#ifdef DEFAULT_WINDOWING
348
    }
349
#endif
350
 
351
    if( rwmode == O_RDWR )       iomode_flags |= _READ | _WRITE;
352
    else if( rwmode == O_RDONLY) iomode_flags |= _READ;
353
    else if( rwmode == O_WRONLY) iomode_flags |= _WRITE;
354
    if( mode & O_APPEND )        iomode_flags |= _APPEND;
355
    if( mode & (O_BINARY|O_TEXT) ) {
356
        if( mode & O_BINARY )    iomode_flags |= _BINARY;
357
    } else {
358
        if( _RWD_fmode == O_BINARY ) iomode_flags |= _BINARY;
359
    }
360
    __SetIOMode( hid, iomode_flags );
361
    return( hid );
362
}
363
#endif
364
 
365
_WCRTLINK int __F_NAME(open,_wopen)( const CHAR_TYPE *name, int mode, ... )
366
{
367
    int         permission;
368
    va_list     args;
369
 
370
    va_start( args, mode );
371
    permission = va_arg( args, int );
372
    va_end( args );
373
    return( __F_NAME(sopen,_wsopen)( name, mode, SH_COMPAT, permission ) );
374
}
375
 
376
 
377
_WCRTLINK int __F_NAME(sopen,_wsopen)( const CHAR_TYPE *name, int mode, int shflag, ... )
378
{
379
    va_list             args;
380
 
381
    va_start( args, shflag );
382
    return( __F_NAME(_sopen,__wsopen)( name, mode, shflag, args ) );
383
}