summaryrefslogtreecommitdiff
path: root/pr/src/md/windows/w16io.c
blob: 6089d3992d3801fcfff2ce72b4a5e489bb212e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Netscape Portable Runtime (NSPR).
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "primpl.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <share.h>
#include <sys/locking.h>


/*
** Sleep this many milliseconds on each I/O operation
** to cause an intentional thread switch. 
*/
#define _PR_MD_WIN16_DELAY 1


/*
** PR_MD_RegisterW16StdioCallbacks() -- Register Win16 stdio callback functions
**
** This public function call is unique to Win16. 
** ... Sigh ... So much for platform independence.
** 
** To get stdio to work from a command line executable, the stdio stream
** calls must be issued from the .EXE file; calling them from the .DLL
** sends the output to the bit-bucket. Therefore, the .EXE wanting to
** do stdio to the console window (must be built as a "quickwin" application)
** must have the wrapper functions defined in this module statically linked
** into the .EXE.
**
** There appears to be nothing you can do to get stdio to work from a
** Win16 GUI application. Oh Well!
**
*/
PRStdinRead   _pr_md_read_stdin = 0;
PRStdoutWrite _pr_md_write_stdout = 0;
PRStderrWrite _pr_md_write_stderr = 0;

PRStatus
PR_MD_RegisterW16StdioCallbacks( PRStdinRead inReadf, PRStdoutWrite outWritef, PRStderrWrite errWritef )
{
    _pr_md_write_stdout = outWritef;
    _pr_md_write_stderr = errWritef;
    _pr_md_read_stdin   = inReadf;
    
    return(PR_SUCCESS);
} /* end PR_MD_RegisterW16StdioCallbacks() */


/*
** _PR_MD_OPEN() -- Open a file
**
** Returns: a fileHandle or -1
**
**
*/
PRInt32
_PR_MD_OPEN(const char *name, PRIntn osflags, int mode)
{
    PRInt32 file;
    int     access = O_BINARY;
    int     rights = 0;
    
    
    /*
    ** Map NSPR open flags to os open flags
    */
    if (osflags & PR_RDONLY )
        access |= O_RDONLY;
    if (osflags & PR_WRONLY )
        access |= O_WRONLY;
    if (osflags & PR_RDWR )
        access |= O_RDWR;
    if (osflags & PR_CREATE_FILE )
    {
        access |= O_CREAT;
        rights |= S_IRWXU;
    }
    if (osflags & PR_TRUNCATE)
        access |= O_TRUNC;
    if (osflags & PR_APPEND)
        access |= O_APPEND;
    else
        access |= O_RDONLY;
        
    /*
    ** Open the file
    */        
    file = (PRInt32) sopen( name, access, SH_DENYNO, rights );
    if ( -1 == (PRInt32)file )
    {
        _PR_MD_MAP_OPEN_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return file;
}

/*
** _PR_MD_READ() - Read something
**
** Returns: bytes read or -1
**
*/
PRInt32
_PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 len)
{
    PRInt32     rv;
    
    if ( (PR_GetDescType(fd) == PR_DESC_FILE) &&
         ( fd->secret->md.osfd == PR_StandardInput ) &&
         ( _pr_md_write_stdout ))
    {
        rv = (*_pr_md_read_stdin)( buf, len);    
    }
    else
    {
        rv = read( fd->secret->md.osfd, buf, len );
    }
    
    if ( rv == -1)
    {
        _PR_MD_MAP_READ_ERROR( errno );
    }
    
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
}

/*
** _PR_MD_WRITE() - Write something
**
** Returns:  bytes written or -1
**
** Note: for file handles 1 and 2 (stdout and stderr)
** call the Win16 NSPR stdio callback functions, if they are 
** registered.
**
*/
PRInt32
_PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len)
{
    PRInt32     rv;

    if ( (PR_GetDescType(fd) == PR_DESC_FILE))
    {
        switch ( fd->secret->md.osfd )
        {
            case  PR_StandardOutput :
                if ( _pr_md_write_stdout )
                    rv = (*_pr_md_write_stdout)( (void *)buf, len);
                else
                    rv = len; /* fake success */
                break;
                
            case  PR_StandardError  :
                if ( _pr_md_write_stderr )
                    rv = (*_pr_md_write_stderr)( (void *)buf, len);    
                else
                    rv = len; /* fake success */
                break;
                
            default:
                rv = write( fd->secret->md.osfd, buf, len );
                if ( rv == -1 )
                {
                    _PR_MD_MAP_WRITE_ERROR( errno );
                }
                break;
        }
    }
    else
    {
        rv = write( fd->secret->md.osfd, buf, len );
        if ( rv == -1 )
        {
            _PR_MD_MAP_WRITE_ERROR( errno );
        }
    }
    
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
} /* --- end _PR_MD_WRITE() --- */

/*
** _PR_MD_LSEEK() - Seek to position in a file
**
** Note: 'whence' maps directly to PR_...
**
** Returns:
**
*/
PRInt32
_PR_MD_LSEEK(PRFileDesc *fd, PRInt32 offset, int whence)
{
    PRInt32     rv;
    
    rv = lseek( fd->secret->md.osfd, offset, whence );
    if ( rv == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return( rv );
}

/*
** _PR_MD_LSEEK64() -- Seek to position in file, 64bit offset.
**
*/
PRInt64
_PR_MD_LSEEK64( PRFileDesc *fd, PRInt64 offset, int whence )
{
    PRInt64 test;
    PRInt32 rv, off;
    LL_SHR(test, offset, 32);
    if (!LL_IS_ZERO(test))
    {
        PR_SetError(PR_FILE_TOO_BIG_ERROR, 0);
        LL_I2L(test, -1);
        return test;
    }
    LL_L2I(off, offset);
    rv = _PR_MD_LSEEK(fd, off, whence);
    LL_I2L(test, rv);
    return test;
} /* end _PR_MD_LSEEK64() */

/*
** _PR_MD_FSYNC() - Flush file buffers.
**
** Returns:
**
**
*/
PRInt32
_PR_MD_FSYNC(PRFileDesc *fd)
{
    PRInt32     rv;
    
    rv = (PRInt32) fsync( fd->secret->md.osfd );
    if ( rv == -1 )
    {
        _PR_MD_MAP_FSYNC_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
}

/*
** _PR_MD_CLOSE() - Close an open file handle
**
** Returns:
**
**
*/
PRInt32
_PR_MD_CLOSE_FILE(PRInt32 osfd)
{
    PRInt32     rv;
    
    rv = (PRInt32) close( osfd );
    if ( rv == -1 )
    {
        _PR_MD_MAP_CLOSE_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
} /* --- end _MD_CloseFile() --- */


/* --- DIR IO ------------------------------------------------------------ */
#define GetFileFromDIR(d)       (d)->d_entry.cFileName

/*
** FlipSlashes() - Make forward slashes ('/') into backslashes
**
** Returns: void
**
**
*/
void FlipSlashes(char *cp, int len)
{
    while (--len >= 0) {
    if (cp[0] == '/') {
        cp[0] = PR_DIRECTORY_SEPARATOR;
    }
    cp++;
    }
}


/*
** _PR_MD_OPEN_DIR() - Open a Directory.
**
** Returns:
**
**
*/
PRStatus
_PR_MD_OPEN_DIR(_MDDir *d, const char *name)
{
    d->dir = opendir( name );
    
    if ( d->dir == NULL )
    {
        _PR_MD_MAP_OPENDIR_ERROR( errno );
        return( PR_FAILURE );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return( PR_SUCCESS );
}


/*
** _PR_MD_READ_DIR() - read next directory entry
**
**
*/
char *
_PR_MD_READ_DIR(_MDDir *d, PRIntn flags)
{
    struct dirent *de;
    int err;

	for (;;) 
    {
		de = readdir( d->dir );
		if ( de == NULL ) {
			_PR_MD_MAP_READDIR_ERROR( errno);
			return 0;
		}		
		if ((flags & PR_SKIP_DOT) &&
		    (de->d_name[0] == '.') && (de->d_name[1] == 0))
			continue;
		if ((flags & PR_SKIP_DOT_DOT) &&
		    (de->d_name[0] == '.') && (de->d_name[1] == '.') &&
		    (de->d_name[2] == 0))
			continue;
		break;
	}
    PR_Sleep( _PR_MD_WIN16_DELAY );    
	return de->d_name;
}

/*
** _PR_MD_CLOSE_DIR() - Close a directory.
**
**
*/
PRInt32
_PR_MD_CLOSE_DIR(_MDDir *d)
{
    PRInt32     rv;
    
    if ( d->dir ) 
    {
        rv = closedir( d->dir );
        if (rv != 0) 
        {
            _PR_MD_MAP_CLOSEDIR_ERROR( errno );
        }
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
}


/*
** _PR_MD_DELETE() - Delete a file.
**
** Returns:
**
**
*/
PRInt32
_PR_MD_DELETE(const char *name)
{
    PRInt32     rv;
    
    rv = (PRInt32) remove( name );
    if ( rv != 0 )
    {
        _PR_MD_MAP_DELETE_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
}


/*
** _PR_MD_STAT() - Get file attributes by filename
**
** Returns:
**
**
*/
PRInt32
_PR_MD_STAT(const char *fn, struct stat *info)
{
    PRInt32     rv;
    
    rv = _stat(fn, (struct _stat *)info);
    if ( rv == -1 )
    {
        _PR_MD_MAP_STAT_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return( rv );
}

/*
** _PR_MD_GETFILEINFO() - Get file attributes by filename
**
** Returns:
**
**
*/
PRInt32
_PR_MD_GETFILEINFO(const char *fn, PRFileInfo *info)
{
    struct _stat sb;
    PRInt32 rv;
 
    if ( (rv = _stat(fn, &sb)) == 0 ) {
        if (info) {
            if (S_IFREG & sb.st_mode)
                info->type = PR_FILE_FILE ;
            else if (S_IFDIR & sb.st_mode)
                info->type = PR_FILE_DIRECTORY;
            else
                info->type = PR_FILE_OTHER;
            info->size = sb.st_size;
            LL_I2L(info->modifyTime, sb.st_mtime);
            LL_I2L(info->creationTime, sb.st_ctime);
        }
    }
    else
    {
        _PR_MD_MAP_STAT_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
}

PRInt32
_PR_MD_GETFILEINFO64(const char *fn, PRFileInfo64 *info)
{
    PRFileInfo info32;
    
    PRInt32 rv = _PR_MD_GETFILEINFO(fn, &info32);
    if (0 == rv)
    {
        info->type = info32.type;
        info->modifyTime = info32.modifyTime;
        info->creationTime = info32.creationTime;
        LL_I2L(info->size, info32.size);
    }
    return(rv);
}

/*
** _PR_MD_GETOPENFILEINFO() - Get file attributes from an open file handle
**
** Returns:
**
**
*/
PRInt32
_PR_MD_GETOPENFILEINFO(const PRFileDesc *fd, PRFileInfo *info)
{
    struct stat statBuf;
    PRInt32 rv = PR_SUCCESS;
    
    rv = fstat( fd->secret->md.osfd, &statBuf );
    if ( rv == 0)
    {
        if (statBuf.st_mode & S_IFREG )
            info->type = PR_FILE_FILE;
        else if ( statBuf.st_mode & S_IFDIR )
            info->type = PR_FILE_DIRECTORY;
        else
            info->type = PR_FILE_OTHER;
        info->size = statBuf.st_size;
        LL_I2L(info->modifyTime, statBuf.st_mtime);
        LL_I2L(info->creationTime, statBuf.st_ctime);
        
    }
    else
    {
        _PR_MD_MAP_FSTAT_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
}

PRInt32
_PR_MD_GETOPENFILEINFO64(const PRFileDesc *fd, PRFileInfo64 *info)
{
    PRFileInfo info32;
    
    PRInt32 rv = _PR_MD_GETOPENFILEINFO(fd, &info32);
    if (0 == rv)
    {
        info->type = info32.type;
        info->modifyTime = info32.modifyTime;
        info->creationTime = info32.creationTime;
        LL_I2L(info->size, info32.size);
    }
    return(rv);
}

/*
** _PR_MD_RENAME() - Rename a file
**
** Returns:
**
**
*/
PRInt32
_PR_MD_RENAME(const char *from, const char *to)
{
    PRInt32 rv;
    
    rv = rename( from, to );
    if ( rv == -1 )
    {
        _PR_MD_MAP_RENAME_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return( rv );
}

/*
** _PR_MD_ACCESS() - Return file acesss attribute.
**
** Returns:
**
**
*/
PRInt32
_PR_MD_ACCESS(const char *name, PRIntn how)
{
    PRInt32     rv;
    int         mode = 0;
    
    if ( how & PR_ACCESS_WRITE_OK )
        mode |= W_OK;
    if ( how & PR_ACCESS_READ_OK )
        mode |= R_OK;
        
    rv = (PRInt32) access( name, mode );        
    if ( rv == -1 )
    {
        _PR_MD_MAP_ACCESS_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
}

/*
** _PR_MD_MKDIR() - Make a directory
**
** Returns:
**
**
*/
PRInt32
_PR_MD_MKDIR(const char *name, PRIntn mode)
{
    PRInt32 rv;
        
    rv = mkdir( name );
    if ( rv == 0 )
    {
        PR_Sleep( _PR_MD_WIN16_DELAY );    
        return PR_SUCCESS;
    }
    else
    {
        _PR_MD_MAP_MKDIR_ERROR( errno );
        PR_Sleep( _PR_MD_WIN16_DELAY );    
        return PR_FAILURE;
    }
}

/*
** _PR_MD_RMDIR() - Delete a directory
**
** Returns:
**
**
*/
PRInt32
_PR_MD_RMDIR(const char *name)
{
    PRInt32 rv;
    
    rv = (PRInt32) rmdir( name );
    if ( rv == -1 )
    {
        _PR_MD_MAP_RMDIR_ERROR( errno );
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return(rv);
}

/*
** _PR_MD_LOCKFILE() - Lock a file.
**
** The _locking() call locks relative to the current file pointer.
** This function is required to lock all of the file, so,
** 1. Seek to the beginning of the file, preserving the original position.
** 2. Lock the file, pausing if it is locked by someone else, and
**    try again.
** 3. Re-position to the original position in the file.
**
** For unlocking, a similar protocol of positioning is required.
**
*/
PRStatus
_PR_MD_LOCKFILE(PRInt32 f)
{
    PRInt32 rv = PR_SUCCESS;    /* What we return to our caller */
    long    seekOrigin;         /* original position in file */
    PRInt32 rc;                 /* what the system call returns to us */

    /*
    ** Seek to beginning of file, saving original position.
    */    
    seekOrigin = lseek( f, 0l, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        return( PR_FAILURE );
    }
    
    /*
    ** Attempt to lock the file.
    ** If someone else has it, Sleep-a-while and try again.
    */
    for( rc = -1; rc != 0; )
    {
        rc = _locking( f, _LK_NBLCK , 0x7fffffff );
        if ( rc == -1 )
        {
            if ( errno == EACCES )
            {
                PR_Sleep( 100 );
                continue;
            }
            else
            {
                _PR_MD_MAP_LOCKF_ERROR( errno );
                rv = PR_FAILURE;
                break;
            }
        }
    } /* end for() */
    
    /*
    ** Now that the file is locked, re-position to
    ** the original file position.
    **
    */
    rc = lseek( f, seekOrigin, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        rv = PR_FAILURE;
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return PR_SUCCESS;
} /* end _PR_MD_LOCKFILE() */

/*
** _PR_MD_TLOCKFILE() - Test and Lock file.
**
** The _locking() call locks relative to the current file pointer.
** This function is required to lock all of the file, so,
** 1. Seek to the beginning of the file, preserving the original position.
** 2. Attempt to Lock the file.
**    If the file is locked by someone else, try NO MORE.
** 3. Re-position to the original position in the file.
**
** See the discussion of _PR_MD_LOCKFILE
**
**
*/
PRStatus
_PR_MD_TLOCKFILE(PRInt32 f)
{
    PRInt32 rv = PR_SUCCESS;    /* What we return */
    long    seekOrigin;         /* original position in file */
    PRInt32 rc;                 /* return value from system call */

    /*
    ** Seek to beginning of file, saving original position.
    */    
    seekOrigin = lseek( f, 0l, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        return( PR_FAILURE );
    }
    
    /*
    ** Attempt to lock the file. One ping; one ping only, Vasily.
    ** If someone else has it, Reposition and return failure.
    */
    rc = _locking( f, _LK_NBLCK , 0x7fffffff );
    if ( rc == -1 )
    {
        if ( errno != EACCES )
            _PR_MD_MAP_LOCKF_ERROR( errno );
        rv = PR_FAILURE;
    }
    
    /*
    ** Now that the file is locked, maybe, re-position to
    ** the original file position.
    */
    rc = lseek( f, seekOrigin, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        rv = PR_FAILURE;
    }
    
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
} /* end _PR_MD_TLOCKFILE() */


/*
** _PR_MD_UNLOCKFILE() - Unlock a file.
**
** See the discussion of _PR_MD_LOCKFILE
**
*/
PRStatus
_PR_MD_UNLOCKFILE(PRInt32 f)
{
    PRInt32 rv = PR_SUCCESS;    /* What we return */
    long    seekOrigin;         /* original position in file */
    PRInt32 rc;                 /* return value from system call */

    /*
    ** Seek to beginning of file, saving original position.
    */    
    seekOrigin = lseek( f, 0l, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        return( PR_FAILURE );
    }
    
    /*
    ** Unlock the file.
    */
    rc = _locking( f, _LK_UNLCK , 0x7fffffff );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LOCKF_ERROR( errno );
        rv = PR_FAILURE;
    }
    
    /*
    ** Now that the file is unlocked, re-position to
    ** the original file position.
    */
    rc = lseek( f, seekOrigin, SEEK_SET );
    if ( rc == -1 )
    {
        _PR_MD_MAP_LSEEK_ERROR( errno );
        rv = PR_FAILURE;
    }
    PR_Sleep( _PR_MD_WIN16_DELAY );    
    return rv;
} /* end _PR_MD_UNLOCKFILE() */

/*
** PR_Stat() -- Return status on a file
**
** This is a hack! ... See BugSplat: 98516 
** Basically, this hack takes a name and stat buffer as input.
** The input stat buffer is presumed to be a Microsoft stat buffer.
** The functions does a Watcom stat() then maps the result to
** the MS stat buffer. ...
**
*/
PR_IMPLEMENT(PRInt32) PR_Stat(const char *name, struct stat *buf)
{
    PRInt32     rv;
    _MDMSStat   *mssb = (_MDMSStat*) buf; /* this is Microsoft's stat buffer */
    struct stat statBuf;   /* this is Watcom's stat buffer */

    /* First, get Watcom's idea of stat
    ** then reformat it into a Microsoft idea of stat
    */
    rv = (PRInt32) _stat( name, &statBuf);
    if (rv == 0l )
    {
        mssb->st_dev = statBuf.st_dev;
        mssb->st_ino = statBuf.st_ino; /* not used, really */
        mssb->st_mode = statBuf.st_mode;
        mssb->st_nlink = 1; /* always 1, says MS */
        mssb->st_uid = statBuf.st_uid;
        mssb->st_gid = statBuf.st_gid;
        mssb->st_rdev = statBuf.st_rdev; /* please Gh0d! Let these be the same */
        mssb->st_size = statBuf.st_size;
        mssb->st_atime = statBuf.st_atime;
        mssb->st_mtime = statBuf.st_mtime;
        mssb->st_ctime = statBuf.st_ctime;
    }
    return rv;
} /* end PR_Stat() */



/* $$ end W16io.c */