~ubuntu-branches/ubuntu/saucy/nspr/saucy-updates

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/md/windows/w16io.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2009-08-10 11:34:26 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090810113426-3uv4diflrkcbdimm
Tags: 4.8-0ubuntu1
* New upstream release: 4.8 (LP: #387812)
* adjust patches to changed upstreanm codebase
  - update debian/patches/99_configure.patch
* update shlibs symbols to include new API elements
  - update debian/libnspr4-0d.symbols

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
 
/* ***** BEGIN LICENSE BLOCK *****
3
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
 
 *
5
 
 * The contents of this file are subject to the Mozilla Public License Version
6
 
 * 1.1 (the "License"); you may not use this file except in compliance with
7
 
 * the License. You may obtain a copy of the License at
8
 
 * http://www.mozilla.org/MPL/
9
 
 *
10
 
 * Software distributed under the License is distributed on an "AS IS" basis,
11
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
 
 * for the specific language governing rights and limitations under the
13
 
 * License.
14
 
 *
15
 
 * The Original Code is the Netscape Portable Runtime (NSPR).
16
 
 *
17
 
 * The Initial Developer of the Original Code is
18
 
 * Netscape Communications Corporation.
19
 
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20
 
 * the Initial Developer. All Rights Reserved.
21
 
 *
22
 
 * Contributor(s):
23
 
 *
24
 
 * Alternatively, the contents of this file may be used under the terms of
25
 
 * either the GNU General Public License Version 2 or later (the "GPL"), or
26
 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
 
 * in which case the provisions of the GPL or the LGPL are applicable instead
28
 
 * of those above. If you wish to allow use of your version of this file only
29
 
 * under the terms of either the GPL or the LGPL, and not to allow others to
30
 
 * use your version of this file under the terms of the MPL, indicate your
31
 
 * decision by deleting the provisions above and replace them with the notice
32
 
 * and other provisions required by the GPL or the LGPL. If you do not delete
33
 
 * the provisions above, a recipient may use your version of this file under
34
 
 * the terms of any one of the MPL, the GPL or the LGPL.
35
 
 *
36
 
 * ***** END LICENSE BLOCK ***** */
37
 
 
38
 
#include "primpl.h"
39
 
#include <sys/types.h>
40
 
#include <sys/stat.h>
41
 
#include <share.h>
42
 
#include <sys/locking.h>
43
 
 
44
 
 
45
 
/*
46
 
** Sleep this many milliseconds on each I/O operation
47
 
** to cause an intentional thread switch. 
48
 
*/
49
 
#define _PR_MD_WIN16_DELAY 1
50
 
 
51
 
 
52
 
/*
53
 
** PR_MD_RegisterW16StdioCallbacks() -- Register Win16 stdio callback functions
54
 
**
55
 
** This public function call is unique to Win16. 
56
 
** ... Sigh ... So much for platform independence.
57
 
** 
58
 
** To get stdio to work from a command line executable, the stdio stream
59
 
** calls must be issued from the .EXE file; calling them from the .DLL
60
 
** sends the output to the bit-bucket. Therefore, the .EXE wanting to
61
 
** do stdio to the console window (must be built as a "quickwin" application)
62
 
** must have the wrapper functions defined in this module statically linked
63
 
** into the .EXE.
64
 
**
65
 
** There appears to be nothing you can do to get stdio to work from a
66
 
** Win16 GUI application. Oh Well!
67
 
**
68
 
*/
69
 
PRStdinRead   _pr_md_read_stdin = 0;
70
 
PRStdoutWrite _pr_md_write_stdout = 0;
71
 
PRStderrWrite _pr_md_write_stderr = 0;
72
 
 
73
 
PRStatus
74
 
PR_MD_RegisterW16StdioCallbacks( PRStdinRead inReadf, PRStdoutWrite outWritef, PRStderrWrite errWritef )
75
 
{
76
 
    _pr_md_write_stdout = outWritef;
77
 
    _pr_md_write_stderr = errWritef;
78
 
    _pr_md_read_stdin   = inReadf;
79
 
    
80
 
    return(PR_SUCCESS);
81
 
} /* end PR_MD_RegisterW16StdioCallbacks() */
82
 
 
83
 
 
84
 
/*
85
 
** _PR_MD_OPEN() -- Open a file
86
 
**
87
 
** Returns: a fileHandle or -1
88
 
**
89
 
**
90
 
*/
91
 
PRInt32
92
 
_PR_MD_OPEN(const char *name, PRIntn osflags, int mode)
93
 
{
94
 
    PRInt32 file;
95
 
    int     access = O_BINARY;
96
 
    int     rights = 0;
97
 
    
98
 
    
99
 
    /*
100
 
    ** Map NSPR open flags to os open flags
101
 
    */
102
 
    if (osflags & PR_RDONLY )
103
 
        access |= O_RDONLY;
104
 
    if (osflags & PR_WRONLY )
105
 
        access |= O_WRONLY;
106
 
    if (osflags & PR_RDWR )
107
 
        access |= O_RDWR;
108
 
    if (osflags & PR_CREATE_FILE )
109
 
    {
110
 
        access |= O_CREAT;
111
 
        rights |= S_IRWXU;
112
 
    }
113
 
    if (osflags & PR_TRUNCATE)
114
 
        access |= O_TRUNC;
115
 
    if (osflags & PR_APPEND)
116
 
        access |= O_APPEND;
117
 
    else
118
 
        access |= O_RDONLY;
119
 
        
120
 
    /*
121
 
    ** Open the file
122
 
    */        
123
 
    file = (PRInt32) sopen( name, access, SH_DENYNO, rights );
124
 
    if ( -1 == (PRInt32)file )
125
 
    {
126
 
        _PR_MD_MAP_OPEN_ERROR( errno );
127
 
    }
128
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
129
 
    return file;
130
 
}
131
 
 
132
 
/*
133
 
** _PR_MD_READ() - Read something
134
 
**
135
 
** Returns: bytes read or -1
136
 
**
137
 
*/
138
 
PRInt32
139
 
_PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 len)
140
 
{
141
 
    PRInt32     rv;
142
 
    
143
 
    if ( (PR_GetDescType(fd) == PR_DESC_FILE) &&
144
 
         ( fd->secret->md.osfd == PR_StandardInput ) &&
145
 
         ( _pr_md_write_stdout ))
146
 
    {
147
 
        rv = (*_pr_md_read_stdin)( buf, len);    
148
 
    }
149
 
    else
150
 
    {
151
 
        rv = read( fd->secret->md.osfd, buf, len );
152
 
    }
153
 
    
154
 
    if ( rv == -1)
155
 
    {
156
 
        _PR_MD_MAP_READ_ERROR( errno );
157
 
    }
158
 
    
159
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
160
 
    return rv;
161
 
}
162
 
 
163
 
/*
164
 
** _PR_MD_WRITE() - Write something
165
 
**
166
 
** Returns:  bytes written or -1
167
 
**
168
 
** Note: for file handles 1 and 2 (stdout and stderr)
169
 
** call the Win16 NSPR stdio callback functions, if they are 
170
 
** registered.
171
 
**
172
 
*/
173
 
PRInt32
174
 
_PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len)
175
 
{
176
 
    PRInt32     rv;
177
 
 
178
 
    if ( (PR_GetDescType(fd) == PR_DESC_FILE))
179
 
    {
180
 
        switch ( fd->secret->md.osfd )
181
 
        {
182
 
            case  PR_StandardOutput :
183
 
                if ( _pr_md_write_stdout )
184
 
                    rv = (*_pr_md_write_stdout)( (void *)buf, len);
185
 
                else
186
 
                    rv = len; /* fake success */
187
 
                break;
188
 
                
189
 
            case  PR_StandardError  :
190
 
                if ( _pr_md_write_stderr )
191
 
                    rv = (*_pr_md_write_stderr)( (void *)buf, len);    
192
 
                else
193
 
                    rv = len; /* fake success */
194
 
                break;
195
 
                
196
 
            default:
197
 
                rv = write( fd->secret->md.osfd, buf, len );
198
 
                if ( rv == -1 )
199
 
                {
200
 
                    _PR_MD_MAP_WRITE_ERROR( errno );
201
 
                }
202
 
                break;
203
 
        }
204
 
    }
205
 
    else
206
 
    {
207
 
        rv = write( fd->secret->md.osfd, buf, len );
208
 
        if ( rv == -1 )
209
 
        {
210
 
            _PR_MD_MAP_WRITE_ERROR( errno );
211
 
        }
212
 
    }
213
 
    
214
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
215
 
    return rv;
216
 
} /* --- end _PR_MD_WRITE() --- */
217
 
 
218
 
/*
219
 
** _PR_MD_LSEEK() - Seek to position in a file
220
 
**
221
 
** Note: 'whence' maps directly to PR_...
222
 
**
223
 
** Returns:
224
 
**
225
 
*/
226
 
PRInt32
227
 
_PR_MD_LSEEK(PRFileDesc *fd, PRInt32 offset, int whence)
228
 
{
229
 
    PRInt32     rv;
230
 
    
231
 
    rv = lseek( fd->secret->md.osfd, offset, whence );
232
 
    if ( rv == -1 )
233
 
    {
234
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
235
 
        
236
 
    }
237
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
238
 
    return( rv );
239
 
}
240
 
 
241
 
/*
242
 
** _PR_MD_LSEEK64() -- Seek to position in file, 64bit offset.
243
 
**
244
 
*/
245
 
PRInt64
246
 
_PR_MD_LSEEK64( PRFileDesc *fd, PRInt64 offset, int whence )
247
 
{
248
 
    PRInt64 test;
249
 
    PRInt32 rv, off;
250
 
    LL_SHR(test, offset, 32);
251
 
    if (!LL_IS_ZERO(test))
252
 
    {
253
 
        PR_SetError(PR_FILE_TOO_BIG_ERROR, 0);
254
 
        LL_I2L(test, -1);
255
 
        return test;
256
 
    }
257
 
    LL_L2I(off, offset);
258
 
    rv = _PR_MD_LSEEK(fd, off, whence);
259
 
    LL_I2L(test, rv);
260
 
    return test;
261
 
} /* end _PR_MD_LSEEK64() */
262
 
 
263
 
/*
264
 
** _PR_MD_FSYNC() - Flush file buffers.
265
 
**
266
 
** Returns:
267
 
**
268
 
**
269
 
*/
270
 
PRInt32
271
 
_PR_MD_FSYNC(PRFileDesc *fd)
272
 
{
273
 
    PRInt32     rv;
274
 
    
275
 
    rv = (PRInt32) fsync( fd->secret->md.osfd );
276
 
    if ( rv == -1 )
277
 
    {
278
 
        _PR_MD_MAP_FSYNC_ERROR( errno );
279
 
    }
280
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
281
 
    return(rv);
282
 
}
283
 
 
284
 
/*
285
 
** _PR_MD_CLOSE() - Close an open file handle
286
 
**
287
 
** Returns:
288
 
**
289
 
**
290
 
*/
291
 
PRInt32
292
 
_PR_MD_CLOSE_FILE(PRInt32 osfd)
293
 
{
294
 
    PRInt32     rv;
295
 
    
296
 
    rv = (PRInt32) close( osfd );
297
 
    if ( rv == -1 )
298
 
    {
299
 
        _PR_MD_MAP_CLOSE_ERROR( errno );
300
 
    }
301
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
302
 
    return(rv);
303
 
} /* --- end _MD_CloseFile() --- */
304
 
 
305
 
 
306
 
/* --- DIR IO ------------------------------------------------------------ */
307
 
#define GetFileFromDIR(d)       (d)->d_entry.cFileName
308
 
 
309
 
/*
310
 
** FlipSlashes() - Make forward slashes ('/') into backslashes
311
 
**
312
 
** Returns: void
313
 
**
314
 
**
315
 
*/
316
 
void FlipSlashes(char *cp, int len)
317
 
{
318
 
    while (--len >= 0) {
319
 
    if (cp[0] == '/') {
320
 
        cp[0] = PR_DIRECTORY_SEPARATOR;
321
 
    }
322
 
    cp++;
323
 
    }
324
 
}
325
 
 
326
 
 
327
 
/*
328
 
** _PR_MD_OPEN_DIR() - Open a Directory.
329
 
**
330
 
** Returns:
331
 
**
332
 
**
333
 
*/
334
 
PRStatus
335
 
_PR_MD_OPEN_DIR(_MDDir *d, const char *name)
336
 
{
337
 
    d->dir = opendir( name );
338
 
    
339
 
    if ( d->dir == NULL )
340
 
    {
341
 
        _PR_MD_MAP_OPENDIR_ERROR( errno );
342
 
        return( PR_FAILURE );
343
 
    }
344
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
345
 
    return( PR_SUCCESS );
346
 
}
347
 
 
348
 
 
349
 
/*
350
 
** _PR_MD_READ_DIR() - read next directory entry
351
 
**
352
 
**
353
 
*/
354
 
char *
355
 
_PR_MD_READ_DIR(_MDDir *d, PRIntn flags)
356
 
{
357
 
    struct dirent *de;
358
 
    int err;
359
 
 
360
 
        for (;;) 
361
 
    {
362
 
                de = readdir( d->dir );
363
 
                if ( de == NULL ) {
364
 
                        _PR_MD_MAP_READDIR_ERROR( errno);
365
 
                        return 0;
366
 
                }               
367
 
                if ((flags & PR_SKIP_DOT) &&
368
 
                    (de->d_name[0] == '.') && (de->d_name[1] == 0))
369
 
                        continue;
370
 
                if ((flags & PR_SKIP_DOT_DOT) &&
371
 
                    (de->d_name[0] == '.') && (de->d_name[1] == '.') &&
372
 
                    (de->d_name[2] == 0))
373
 
                        continue;
374
 
                break;
375
 
        }
376
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
377
 
        return de->d_name;
378
 
}
379
 
 
380
 
/*
381
 
** _PR_MD_CLOSE_DIR() - Close a directory.
382
 
**
383
 
**
384
 
*/
385
 
PRInt32
386
 
_PR_MD_CLOSE_DIR(_MDDir *d)
387
 
{
388
 
    PRInt32     rv;
389
 
    
390
 
    if ( d->dir ) 
391
 
    {
392
 
        rv = closedir( d->dir );
393
 
        if (rv != 0) 
394
 
        {
395
 
            _PR_MD_MAP_CLOSEDIR_ERROR( errno );
396
 
        }
397
 
    }
398
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
399
 
    return rv;
400
 
}
401
 
 
402
 
 
403
 
/*
404
 
** _PR_MD_DELETE() - Delete a file.
405
 
**
406
 
** Returns:
407
 
**
408
 
**
409
 
*/
410
 
PRInt32
411
 
_PR_MD_DELETE(const char *name)
412
 
{
413
 
    PRInt32     rv;
414
 
    
415
 
    rv = (PRInt32) remove( name );
416
 
    if ( rv != 0 )
417
 
    {
418
 
        _PR_MD_MAP_DELETE_ERROR( errno );
419
 
    }
420
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
421
 
    return(rv);
422
 
}
423
 
 
424
 
 
425
 
/*
426
 
** _PR_MD_STAT() - Get file attributes by filename
427
 
**
428
 
** Returns:
429
 
**
430
 
**
431
 
*/
432
 
PRInt32
433
 
_PR_MD_STAT(const char *fn, struct stat *info)
434
 
{
435
 
    PRInt32     rv;
436
 
    
437
 
    rv = _stat(fn, (struct _stat *)info);
438
 
    if ( rv == -1 )
439
 
    {
440
 
        _PR_MD_MAP_STAT_ERROR( errno );
441
 
    }
442
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
443
 
    return( rv );
444
 
}
445
 
 
446
 
/*
447
 
** _PR_MD_GETFILEINFO() - Get file attributes by filename
448
 
**
449
 
** Returns:
450
 
**
451
 
**
452
 
*/
453
 
PRInt32
454
 
_PR_MD_GETFILEINFO(const char *fn, PRFileInfo *info)
455
 
{
456
 
    struct _stat sb;
457
 
    PRInt32 rv;
458
 
 
459
 
    if ( (rv = _stat(fn, &sb)) == 0 ) {
460
 
        if (info) {
461
 
            if (S_IFREG & sb.st_mode)
462
 
                info->type = PR_FILE_FILE ;
463
 
            else if (S_IFDIR & sb.st_mode)
464
 
                info->type = PR_FILE_DIRECTORY;
465
 
            else
466
 
                info->type = PR_FILE_OTHER;
467
 
            info->size = sb.st_size;
468
 
            LL_I2L(info->modifyTime, sb.st_mtime);
469
 
            LL_I2L(info->creationTime, sb.st_ctime);
470
 
        }
471
 
    }
472
 
    else
473
 
    {
474
 
        _PR_MD_MAP_STAT_ERROR( errno );
475
 
    }
476
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
477
 
    return rv;
478
 
}
479
 
 
480
 
PRInt32
481
 
_PR_MD_GETFILEINFO64(const char *fn, PRFileInfo64 *info)
482
 
{
483
 
    PRFileInfo info32;
484
 
    
485
 
    PRInt32 rv = _PR_MD_GETFILEINFO(fn, &info32);
486
 
    if (0 == rv)
487
 
    {
488
 
        info->type = info32.type;
489
 
        info->modifyTime = info32.modifyTime;
490
 
        info->creationTime = info32.creationTime;
491
 
        LL_I2L(info->size, info32.size);
492
 
    }
493
 
    return(rv);
494
 
}
495
 
 
496
 
/*
497
 
** _PR_MD_GETOPENFILEINFO() - Get file attributes from an open file handle
498
 
**
499
 
** Returns:
500
 
**
501
 
**
502
 
*/
503
 
PRInt32
504
 
_PR_MD_GETOPENFILEINFO(const PRFileDesc *fd, PRFileInfo *info)
505
 
{
506
 
    struct stat statBuf;
507
 
    PRInt32 rv = PR_SUCCESS;
508
 
    
509
 
    rv = fstat( fd->secret->md.osfd, &statBuf );
510
 
    if ( rv == 0)
511
 
    {
512
 
        if (statBuf.st_mode & S_IFREG )
513
 
            info->type = PR_FILE_FILE;
514
 
        else if ( statBuf.st_mode & S_IFDIR )
515
 
            info->type = PR_FILE_DIRECTORY;
516
 
        else
517
 
            info->type = PR_FILE_OTHER;
518
 
        info->size = statBuf.st_size;
519
 
        LL_I2L(info->modifyTime, statBuf.st_mtime);
520
 
        LL_I2L(info->creationTime, statBuf.st_ctime);
521
 
        
522
 
    }
523
 
    else
524
 
    {
525
 
        _PR_MD_MAP_FSTAT_ERROR( errno );
526
 
    }
527
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
528
 
    return(rv);
529
 
}
530
 
 
531
 
PRInt32
532
 
_PR_MD_GETOPENFILEINFO64(const PRFileDesc *fd, PRFileInfo64 *info)
533
 
{
534
 
    PRFileInfo info32;
535
 
    
536
 
    PRInt32 rv = _PR_MD_GETOPENFILEINFO(fd, &info32);
537
 
    if (0 == rv)
538
 
    {
539
 
        info->type = info32.type;
540
 
        info->modifyTime = info32.modifyTime;
541
 
        info->creationTime = info32.creationTime;
542
 
        LL_I2L(info->size, info32.size);
543
 
    }
544
 
    return(rv);
545
 
}
546
 
 
547
 
/*
548
 
** _PR_MD_RENAME() - Rename a file
549
 
**
550
 
** Returns:
551
 
**
552
 
**
553
 
*/
554
 
PRInt32
555
 
_PR_MD_RENAME(const char *from, const char *to)
556
 
{
557
 
    PRInt32 rv;
558
 
    
559
 
    rv = rename( from, to );
560
 
    if ( rv == -1 )
561
 
    {
562
 
        _PR_MD_MAP_RENAME_ERROR( errno );
563
 
    }
564
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
565
 
    return( rv );
566
 
}
567
 
 
568
 
/*
569
 
** _PR_MD_ACCESS() - Return file acesss attribute.
570
 
**
571
 
** Returns:
572
 
**
573
 
**
574
 
*/
575
 
PRInt32
576
 
_PR_MD_ACCESS(const char *name, PRIntn how)
577
 
{
578
 
    PRInt32     rv;
579
 
    int         mode = 0;
580
 
    
581
 
    if ( how & PR_ACCESS_WRITE_OK )
582
 
        mode |= W_OK;
583
 
    if ( how & PR_ACCESS_READ_OK )
584
 
        mode |= R_OK;
585
 
        
586
 
    rv = (PRInt32) access( name, mode );        
587
 
    if ( rv == -1 )
588
 
    {
589
 
        _PR_MD_MAP_ACCESS_ERROR( errno );
590
 
    }
591
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
592
 
    return(rv);
593
 
}
594
 
 
595
 
/*
596
 
** _PR_MD_MKDIR() - Make a directory
597
 
**
598
 
** Returns:
599
 
**
600
 
**
601
 
*/
602
 
PRInt32
603
 
_PR_MD_MKDIR(const char *name, PRIntn mode)
604
 
{
605
 
    PRInt32 rv;
606
 
        
607
 
    rv = mkdir( name );
608
 
    if ( rv == 0 )
609
 
    {
610
 
        PR_Sleep( _PR_MD_WIN16_DELAY );    
611
 
        return PR_SUCCESS;
612
 
    }
613
 
    else
614
 
    {
615
 
        _PR_MD_MAP_MKDIR_ERROR( errno );
616
 
        PR_Sleep( _PR_MD_WIN16_DELAY );    
617
 
        return PR_FAILURE;
618
 
    }
619
 
}
620
 
 
621
 
/*
622
 
** _PR_MD_RMDIR() - Delete a directory
623
 
**
624
 
** Returns:
625
 
**
626
 
**
627
 
*/
628
 
PRInt32
629
 
_PR_MD_RMDIR(const char *name)
630
 
{
631
 
    PRInt32 rv;
632
 
    
633
 
    rv = (PRInt32) rmdir( name );
634
 
    if ( rv == -1 )
635
 
    {
636
 
        _PR_MD_MAP_RMDIR_ERROR( errno );
637
 
    }
638
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
639
 
    return(rv);
640
 
}
641
 
 
642
 
/*
643
 
** _PR_MD_LOCKFILE() - Lock a file.
644
 
**
645
 
** The _locking() call locks relative to the current file pointer.
646
 
** This function is required to lock all of the file, so,
647
 
** 1. Seek to the beginning of the file, preserving the original position.
648
 
** 2. Lock the file, pausing if it is locked by someone else, and
649
 
**    try again.
650
 
** 3. Re-position to the original position in the file.
651
 
**
652
 
** For unlocking, a similar protocol of positioning is required.
653
 
**
654
 
*/
655
 
PRStatus
656
 
_PR_MD_LOCKFILE(PRInt32 f)
657
 
{
658
 
    PRInt32 rv = PR_SUCCESS;    /* What we return to our caller */
659
 
    long    seekOrigin;         /* original position in file */
660
 
    PRInt32 rc;                 /* what the system call returns to us */
661
 
 
662
 
    /*
663
 
    ** Seek to beginning of file, saving original position.
664
 
    */    
665
 
    seekOrigin = lseek( f, 0l, SEEK_SET );
666
 
    if ( rc == -1 )
667
 
    {
668
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
669
 
        return( PR_FAILURE );
670
 
    }
671
 
    
672
 
    /*
673
 
    ** Attempt to lock the file.
674
 
    ** If someone else has it, Sleep-a-while and try again.
675
 
    */
676
 
    for( rc = -1; rc != 0; )
677
 
    {
678
 
        rc = _locking( f, _LK_NBLCK , 0x7fffffff );
679
 
        if ( rc == -1 )
680
 
        {
681
 
            if ( errno == EACCES )
682
 
            {
683
 
                PR_Sleep( 100 );
684
 
                continue;
685
 
            }
686
 
            else
687
 
            {
688
 
                _PR_MD_MAP_LOCKF_ERROR( errno );
689
 
                rv = PR_FAILURE;
690
 
                break;
691
 
            }
692
 
        }
693
 
    } /* end for() */
694
 
    
695
 
    /*
696
 
    ** Now that the file is locked, re-position to
697
 
    ** the original file position.
698
 
    **
699
 
    */
700
 
    rc = lseek( f, seekOrigin, SEEK_SET );
701
 
    if ( rc == -1 )
702
 
    {
703
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
704
 
        rv = PR_FAILURE;
705
 
    }
706
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
707
 
    return PR_SUCCESS;
708
 
} /* end _PR_MD_LOCKFILE() */
709
 
 
710
 
/*
711
 
** _PR_MD_TLOCKFILE() - Test and Lock file.
712
 
**
713
 
** The _locking() call locks relative to the current file pointer.
714
 
** This function is required to lock all of the file, so,
715
 
** 1. Seek to the beginning of the file, preserving the original position.
716
 
** 2. Attempt to Lock the file.
717
 
**    If the file is locked by someone else, try NO MORE.
718
 
** 3. Re-position to the original position in the file.
719
 
**
720
 
** See the discussion of _PR_MD_LOCKFILE
721
 
**
722
 
**
723
 
*/
724
 
PRStatus
725
 
_PR_MD_TLOCKFILE(PRInt32 f)
726
 
{
727
 
    PRInt32 rv = PR_SUCCESS;    /* What we return */
728
 
    long    seekOrigin;         /* original position in file */
729
 
    PRInt32 rc;                 /* return value from system call */
730
 
 
731
 
    /*
732
 
    ** Seek to beginning of file, saving original position.
733
 
    */    
734
 
    seekOrigin = lseek( f, 0l, SEEK_SET );
735
 
    if ( rc == -1 )
736
 
    {
737
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
738
 
        return( PR_FAILURE );
739
 
    }
740
 
    
741
 
    /*
742
 
    ** Attempt to lock the file. One ping; one ping only, Vasily.
743
 
    ** If someone else has it, Reposition and return failure.
744
 
    */
745
 
    rc = _locking( f, _LK_NBLCK , 0x7fffffff );
746
 
    if ( rc == -1 )
747
 
    {
748
 
        if ( errno != EACCES )
749
 
            _PR_MD_MAP_LOCKF_ERROR( errno );
750
 
        rv = PR_FAILURE;
751
 
    }
752
 
    
753
 
    /*
754
 
    ** Now that the file is locked, maybe, re-position to
755
 
    ** the original file position.
756
 
    */
757
 
    rc = lseek( f, seekOrigin, SEEK_SET );
758
 
    if ( rc == -1 )
759
 
    {
760
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
761
 
        rv = PR_FAILURE;
762
 
    }
763
 
    
764
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
765
 
    return rv;
766
 
} /* end _PR_MD_TLOCKFILE() */
767
 
 
768
 
 
769
 
/*
770
 
** _PR_MD_UNLOCKFILE() - Unlock a file.
771
 
**
772
 
** See the discussion of _PR_MD_LOCKFILE
773
 
**
774
 
*/
775
 
PRStatus
776
 
_PR_MD_UNLOCKFILE(PRInt32 f)
777
 
{
778
 
    PRInt32 rv = PR_SUCCESS;    /* What we return */
779
 
    long    seekOrigin;         /* original position in file */
780
 
    PRInt32 rc;                 /* return value from system call */
781
 
 
782
 
    /*
783
 
    ** Seek to beginning of file, saving original position.
784
 
    */    
785
 
    seekOrigin = lseek( f, 0l, SEEK_SET );
786
 
    if ( rc == -1 )
787
 
    {
788
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
789
 
        return( PR_FAILURE );
790
 
    }
791
 
    
792
 
    /*
793
 
    ** Unlock the file.
794
 
    */
795
 
    rc = _locking( f, _LK_UNLCK , 0x7fffffff );
796
 
    if ( rc == -1 )
797
 
    {
798
 
        _PR_MD_MAP_LOCKF_ERROR( errno );
799
 
        rv = PR_FAILURE;
800
 
    }
801
 
    
802
 
    /*
803
 
    ** Now that the file is unlocked, re-position to
804
 
    ** the original file position.
805
 
    */
806
 
    rc = lseek( f, seekOrigin, SEEK_SET );
807
 
    if ( rc == -1 )
808
 
    {
809
 
        _PR_MD_MAP_LSEEK_ERROR( errno );
810
 
        rv = PR_FAILURE;
811
 
    }
812
 
    PR_Sleep( _PR_MD_WIN16_DELAY );    
813
 
    return rv;
814
 
} /* end _PR_MD_UNLOCKFILE() */
815
 
 
816
 
/*
817
 
** PR_Stat() -- Return status on a file
818
 
**
819
 
** This is a hack! ... See BugSplat: 98516 
820
 
** Basically, this hack takes a name and stat buffer as input.
821
 
** The input stat buffer is presumed to be a Microsoft stat buffer.
822
 
** The functions does a Watcom stat() then maps the result to
823
 
** the MS stat buffer. ...
824
 
**
825
 
*/
826
 
PR_IMPLEMENT(PRInt32) PR_Stat(const char *name, struct stat *buf)
827
 
{
828
 
    PRInt32     rv;
829
 
    _MDMSStat   *mssb = (_MDMSStat*) buf; /* this is Microsoft's stat buffer */
830
 
    struct stat statBuf;   /* this is Watcom's stat buffer */
831
 
 
832
 
    /* First, get Watcom's idea of stat
833
 
    ** then reformat it into a Microsoft idea of stat
834
 
    */
835
 
    rv = (PRInt32) _stat( name, &statBuf);
836
 
    if (rv == 0l )
837
 
    {
838
 
        mssb->st_dev = statBuf.st_dev;
839
 
        mssb->st_ino = statBuf.st_ino; /* not used, really */
840
 
        mssb->st_mode = statBuf.st_mode;
841
 
        mssb->st_nlink = 1; /* always 1, says MS */
842
 
        mssb->st_uid = statBuf.st_uid;
843
 
        mssb->st_gid = statBuf.st_gid;
844
 
        mssb->st_rdev = statBuf.st_rdev; /* please Gh0d! Let these be the same */
845
 
        mssb->st_size = statBuf.st_size;
846
 
        mssb->st_atime = statBuf.st_atime;
847
 
        mssb->st_mtime = statBuf.st_mtime;
848
 
        mssb->st_ctime = statBuf.st_ctime;
849
 
    }
850
 
    return rv;
851
 
} /* end PR_Stat() */
852
 
 
853
 
 
854
 
 
855
 
/* $$ end W16io.c */