summaryrefslogtreecommitdiff
path: root/pr/src/md/os2/os2io.c
blob: 926d6e10ed9fc7403eb2cee9c4f6bd2553cb4c5d (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 
 * 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 Netscape are 
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */


/*
 * This Original Code has been modified by IBM Corporation.
 * Modifications made by IBM described herein are
 * Copyright (c) International Business Machines
 * Corporation, 2000
 *
 * Modifications to Mozilla code or documentation
 * identified per MPL Section 3.3
 *
 * Date             Modified by     Description of modification
 * 03/23/2000       IBM Corp.       Changed write() to DosWrite(). EMX i/o
 *                                  calls cannot be intermixed with DosXXX
 *                                  calls since EMX remaps file/socket
 *                                  handles.
 * 04/27/2000       IBM Corp.       Changed open file to be more like NT and
 *                                  better handle PR_TRUNCATE | PR_CREATE_FILE
 *                                  and also fixed _PR_MD_SET_FD_INHERITABLE
 */

/* OS2 IO module
 *
 * Assumes synchronous I/O.
 *
 */

#include "primpl.h"
#include "prio.h"
#include <ctype.h>
#ifdef XP_OS2_VACPP
#include <direct.h>
#else
#include <limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <io.h>
#endif

struct _MDLock               _pr_ioq_lock;

PRStatus
_PR_MD_WAIT(PRThread *thread, PRIntervalTime ticks)
{
    PRInt32 rv;
    ULONG count;

    PRUint32 msecs = (ticks == PR_INTERVAL_NO_TIMEOUT) ?
        SEM_INDEFINITE_WAIT : PR_IntervalToMilliseconds(ticks);
    rv = DosWaitEventSem(thread->md.blocked_sema.sem, msecs);
    DosResetEventSem(thread->md.blocked_sema.sem, &count); 
    switch(rv) 
    {
        case NO_ERROR:
            return PR_SUCCESS;
            break;
        case ERROR_TIMEOUT:
            _PR_THREAD_LOCK(thread);
            if (thread->state == _PR_IO_WAIT) {
			  ;
            } else {
                if (thread->wait.cvar != NULL) {
                    thread->wait.cvar = NULL;
                    _PR_THREAD_UNLOCK(thread);
                } else {
                    /* The CVAR was notified just as the timeout
                     * occurred.  This led to us being notified twice.
                     * call SemRequest() to clear the semaphore.
                     */
                    _PR_THREAD_UNLOCK(thread);
                    rv = DosWaitEventSem(thread->md.blocked_sema.sem, 0);
                    DosResetEventSem(thread->md.blocked_sema.sem, &count); 
                    PR_ASSERT(rv == NO_ERROR);
                }
            }
            return PR_SUCCESS;
            break;
        default:
            break;
    }
    return PR_FAILURE;
}
PRStatus
_PR_MD_WAKEUP_WAITER(PRThread *thread)
{
    if ( _PR_IS_NATIVE_THREAD(thread) ) 
    {
        if (DosPostEventSem(thread->md.blocked_sema.sem) != NO_ERROR)
            return PR_FAILURE;
        else
			return PR_SUCCESS;
	}
}


/* --- FILE IO ----------------------------------------------------------- */
/*
 *  _PR_MD_OPEN() -- Open a file
 *
 *  returns: a fileHandle
 *
 *  The NSPR open flags (osflags) are translated into flags for OS/2
 *
 *  Mode seems to be passed in as a unix style file permissions argument
 *  as in 0666, in the case of opening the logFile. 
 *
 */
PRInt32
_PR_MD_OPEN(const char *name, PRIntn osflags, int mode)
{
    HFILE file;
    PRInt32 access = OPEN_SHARE_DENYNONE;
    PRInt32 flags = 0L;
    PRInt32 rc;
    PRUword actionTaken;

    ULONG CurMaxFH = 0;
    LONG ReqCount = 1;
    ULONG fattr;

    if (osflags & PR_SYNC) access |= OPEN_FLAGS_WRITE_THROUGH;

    if (osflags & PR_RDONLY)
        access |= OPEN_ACCESS_READONLY;
    else if (osflags & PR_WRONLY)
        access |= OPEN_ACCESS_WRITEONLY;
    else if(osflags & PR_RDWR)
        access |= OPEN_ACCESS_READWRITE;

    if ( osflags & PR_CREATE_FILE && osflags & PR_EXCL )
    {
        flags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_FAIL_IF_EXISTS;
    }
    else if (osflags & PR_CREATE_FILE)
    {
        if (osflags & PR_TRUNCATE)
            flags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
        else
            flags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
    } 
    else
    {
        if (osflags & PR_TRUNCATE)
            flags = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
        else
            flags = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
    }

    if (isxdigit(mode) == 0) /* file attribs are hex, UNIX modes octal */
      fattr = ((ULONG)mode == FILE_HIDDEN) ? FILE_HIDDEN : FILE_NORMAL;
    else fattr = FILE_NORMAL;

    /* OS/2 sets the Max file handles per process to 20 by default */
    DosSetRelMaxFH(&ReqCount, &CurMaxFH);

    rc = DosOpen((char*)name,
                 &file,            /* file handle if successful */
                 &actionTaken,     /* reason for failure        */
                 0,                /* initial size of new file  */
                 fattr,            /* file system attributes    */
                 flags,            /* Open flags                */
                 access,           /* Open mode and rights      */
                 0);               /* OS/2 Extended Attributes  */
    if (rc != NO_ERROR) {
		_PR_MD_MAP_OPEN_ERROR(rc);
      return -1; 
	}

    return (PRInt32)file;
}

PRInt32
_PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 len)
{
    ULONG bytes;
    int rv;

    rv = DosRead((HFILE)fd->secret->md.osfd,
                 (PVOID)buf,
                 len,
                 &bytes);
    
    if (rv != NO_ERROR) 
    {
        /* ERROR_HANDLE_EOF can only be returned by async io */
        PR_ASSERT(rv != ERROR_HANDLE_EOF);
        if (rv == ERROR_BROKEN_PIPE)
            return 0;
		else {
			_PR_MD_MAP_READ_ERROR(rv);
        return -1;
    }
    }
    return (PRInt32)bytes;
}

PRInt32
_PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len)
{
    PRInt32 bytes;
    int rv; 

    rv = DosWrite((HFILE)fd->secret->md.osfd,
                  (PVOID)buf,
                  len,
                  (PULONG)&bytes);

    if (rv != NO_ERROR) 
    {
        _PR_MD_MAP_WRITE_ERROR(rv);
        return -1;
    }

    return bytes;
} /* --- end _PR_MD_WRITE() --- */

PRInt32
_PR_MD_LSEEK(PRFileDesc *fd, PRInt32 offset, PRSeekWhence whence)
{
    PRInt32 rv;
    PRUword newLocation;

    rv = DosSetFilePtr((HFILE)fd->secret->md.osfd, offset, whence, &newLocation);

	if (rv != NO_ERROR) {
		_PR_MD_MAP_LSEEK_ERROR(rv);
		return -1;
	} else
		return newLocation;
}

PRInt64
_PR_MD_LSEEK64(PRFileDesc *fd, PRInt64 offset, PRSeekWhence whence)
{
#ifdef NO_LONG_LONG
    PRInt64 result;
    PRInt32 rv, low = offset.lo, hi = offset.hi;
    PRUword newLocation;

    rv = DosSetFilePtr((HFILE)fd->secret->md.osfd, low, whence, &newLocation);
    rv = DosSetFilePtr((HFILE)fd->secret->md.osfd, hi, FILE_CURRENT, &newLocation);

  	if (rv != NO_ERROR) {
		_PR_MD_MAP_LSEEK_ERROR(rv);
		hi = newLocation = -1;
   }

    result.lo = newLocation;
    result.hi = hi;
	return result;

#else
    PRInt32 where, rc, lo = (PRInt32)offset, hi = (PRInt32)(offset >> 32);
    PRUint64 rv;
    PRUint32 newLocation, uhi;

    switch (whence)
      {
      case PR_SEEK_SET:
        where = FILE_BEGIN;
        break;
      case PR_SEEK_CUR:
        where = FILE_CURRENT;
        break;
      case PR_SEEK_END:
        where = FILE_END;
        break;
      default:
        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
        return -1;
}

    rc = DosSetFilePtr((HFILE)fd->secret->md.osfd, lo, where, (PULONG)&newLocation);
     
    if (rc != NO_ERROR) {
      _PR_MD_MAP_LSEEK_ERROR(rc);
      return -1;
    }
    
    uhi = (PRUint32)hi;
    PR_ASSERT((PRInt32)uhi >= 0);
    rv = uhi;
    PR_ASSERT((PRInt64)rv >= 0);
    rv = (rv << 32);
    PR_ASSERT((PRInt64)rv >= 0);
    rv += newLocation;
    PR_ASSERT((PRInt64)rv >= 0);
    return (PRInt64)rv;
#endif
}

PRInt32
_PR_MD_FSYNC(PRFileDesc *fd)
{
    PRInt32 rc = DosResetBuffer((HFILE)fd->secret->md.osfd);

    if (rc != NO_ERROR) {
   	if (rc != ERROR_ACCESS_DENIED) {	
   			_PR_MD_MAP_FSYNC_ERROR(rc);
   	    return -1;
   	}
    }
    return 0;
}

PRInt32
_MD_CloseFile(PRInt32 osfd)
{
    PRInt32 rv;
    
    rv = DosClose((HFILE)osfd);
 	if (rv != NO_ERROR)
		_PR_MD_MAP_CLOSE_ERROR(rv);
    return rv;
}


/* --- DIR IO ------------------------------------------------------------ */
#define GetFileFromDIR(d)       (d)->d_entry.achName
#define GetFileAttr(d)          (d)->d_entry.attrFile

void FlipSlashes(char *cp, int len)
{
    while (--len >= 0) {
    if (cp[0] == '/') {
        cp[0] = PR_DIRECTORY_SEPARATOR;
    }
    cp++;
    }
}

/*
**
** Local implementations of standard Unix RTL functions which are not provided
** by the VAC RTL.
**
*/

PRInt32
_PR_MD_CLOSE_DIR(_MDDir *d)
{
   PRInt32 rc;

    if ( d ) {
      rc = DosFindClose(d->d_hdl);
      if(rc == NO_ERROR){
        d->magic = (PRUint32)-1;
        return PR_SUCCESS;
		} else {
			_PR_MD_MAP_CLOSEDIR_ERROR(rc);
        	return PR_FAILURE;
		}
    }
    PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
    return PR_FAILURE;
}


PRStatus
_PR_MD_OPEN_DIR(_MDDir *d, const char *name)
{
    char filename[ CCHMAXPATH ];
    PRUword numEntries, rc;

    numEntries = 1;

    PR_snprintf(filename, CCHMAXPATH, "%s%s%s",
                name, PR_DIRECTORY_SEPARATOR_STR, "*.*");
    FlipSlashes( filename, strlen(filename) );

    d->d_hdl = HDIR_CREATE;

    rc = DosFindFirst( filename,
                       &d->d_hdl,
                       FILE_DIRECTORY | FILE_HIDDEN,
                       &(d->d_entry),
                       sizeof(d->d_entry),
                       &numEntries,
                       FIL_STANDARD);
    if ( rc != NO_ERROR ) {
		_PR_MD_MAP_OPENDIR_ERROR(rc);
        return PR_FAILURE;
    }
    d->firstEntry = PR_TRUE;
    d->magic = _MD_MAGIC_DIR;
    return PR_SUCCESS;
}

char *
_PR_MD_READ_DIR(_MDDir *d, PRIntn flags)
{
    PRUword numFiles = 1;
    BOOL rv;
    char *fileName;
    USHORT fileAttr;

    if ( d ) {
       while (1) {
           if (d->firstEntry) {
               d->firstEntry = PR_FALSE;
               rv = NO_ERROR;
           } else {
               rv = DosFindNext(d->d_hdl,
                                &(d->d_entry),
                                sizeof(d->d_entry),
                                &numFiles);
           }
           if (rv != NO_ERROR) {
               break;
           }
           fileName = GetFileFromDIR(d);
           fileAttr = GetFileAttr(d);
           if ( (flags & PR_SKIP_DOT) &&
                (fileName[0] == '.') && (fileName[1] == '\0'))
                continue;
           if ( (flags & PR_SKIP_DOT_DOT) &&
                (fileName[0] == '.') && (fileName[1] == '.') &&
                (fileName[2] == '\0'))
                continue;
			/*
			 * XXX
			 * Is this the correct definition of a hidden file on OS/2?
			 */
           if ((flags & PR_SKIP_NONE) && (fileAttr & FILE_HIDDEN))
                return fileName;
           else if ((flags & PR_SKIP_HIDDEN) && (fileAttr & FILE_HIDDEN))
                continue;
           return fileName;
        }
        PR_ASSERT(NO_ERROR != rv);
			_PR_MD_MAP_READDIR_ERROR(rv);
        return NULL;
		}
    PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
    return NULL;
}

PRInt32
_PR_MD_DELETE(const char *name)
{
    PRInt32 rc = DosDelete((char*)name);
    if(rc == NO_ERROR) {
        return 0;
    } else {
		_PR_MD_MAP_DELETE_ERROR(rc);
        return -1;
    }
}

PRInt32
_PR_MD_STAT(const char *fn, struct stat *info)
{
    PRInt32 rv;
    char filename[CCHMAXPATH];

    PR_snprintf(filename, CCHMAXPATH, "%s", fn);
    FlipSlashes(filename, strlen(filename));

    rv = _stat((char*)filename, info);
    if (-1 == rv) {
        /*
         * Check for MSVC runtime library _stat() bug.
         * (It's really a bug in FindFirstFile().)
         * If a pathname ends in a backslash or slash,
         * e.g., c:\temp\ or c:/temp/, _stat() will fail.
         * Note: a pathname ending in a slash (e.g., c:/temp/)
         * can be handled by _stat() on NT but not on Win95.
         *
         * We remove the backslash or slash at the end and
         * try again.  
         *
         * Not sure if this happens on OS/2 or not,
         * but it doesn't hurt to be careful.
         */

        int len = strlen(fn);
        if (len > 0 && len <= _MAX_PATH
                && (fn[len - 1] == '\\' || fn[len - 1] == '/')) {
            char newfn[_MAX_PATH + 1];

            strcpy(newfn, fn);
            newfn[len - 1] = '\0';
            rv = _stat(newfn, info);
        }
    }

    if (-1 == rv) {
        _PR_MD_MAP_STAT_ERROR(errno);
    }
    return rv;
}

PRInt32
_PR_MD_GETFILEINFO(const char *fn, PRFileInfo *info)
{
    struct stat sb;
    PRInt32 rv;
    PRInt64 s, s2us;
 
    if ( (rv = _PR_MD_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(s2us, PR_USEC_PER_SEC);
            LL_I2L(s, sb.st_mtime);
            LL_MUL(s, s, s2us);
            info->modifyTime = s;
            LL_I2L(s, sb.st_ctime);
            LL_MUL(s, s, s2us);
            info->creationTime = s;
        }
    }
    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;
        LL_UI2L(info->size,info32.size);
        info->modifyTime = info32.modifyTime;
        info->creationTime = info32.creationTime;
    }
    return rv;
}

PRInt32
_PR_MD_GETOPENFILEINFO(const PRFileDesc *fd, PRFileInfo *info)
{
   /* For once, the VAC compiler/library did a nice thing.
    * The file handle used by the C runtime is the same one
    * returned by the OS when you call DosOpen().  This means
    * that you can take an OS HFILE and use it with C file
    * functions.  The only caveat is that you have to call
    * _setmode() first to initialize some junk.  This is
    * immensely useful because I did not have a clue how to
    * implement this function otherwise.  The windows folks
    * took the source from the Microsoft C library source, but
    * IBM wasn't kind enough to ship the source with VAC.
    * On second thought, the needed function could probably
    * be gotten from the OS/2 GNU library source, but the
    * point is now moot.
    */
   struct stat hinfo;
    PRInt64 s, s2us;

    _setmode(fd->secret->md.osfd, O_BINARY);
    if(fstat((int)fd->secret->md.osfd, &hinfo) != NO_ERROR) {
		_PR_MD_MAP_FSTAT_ERROR(errno);
        return -1;
	}

    if (hinfo.st_mode & S_IFDIR)
        info->type = PR_FILE_DIRECTORY;
    else
        info->type = PR_FILE_FILE;

    info->size = hinfo.st_size;
    LL_I2L(s2us, PR_USEC_PER_SEC);
    LL_I2L(s, hinfo.st_mtime);
    LL_MUL(s, s, s2us);
    info->modifyTime = s;
    LL_I2L(s, hinfo.st_ctime);
    LL_MUL(s, s, s2us);
    info->creationTime = s;

    return 0;
}

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;
       LL_UI2L(info->size,info32.size);

       info->modifyTime = info32.modifyTime;
       info->creationTime = info32.creationTime;
   }
   return rv;
}


PRInt32
_PR_MD_RENAME(const char *from, const char *to)
{
   PRInt32 rc;
    /* Does this work with dot-relative pathnames? */
    if ( (rc = DosMove((char *)from, (char *)to)) == NO_ERROR) {
        return 0;
    } else {
		_PR_MD_MAP_RENAME_ERROR(rc);
        return -1;
    }
}

PRInt32
_PR_MD_ACCESS(const char *name, PRAccessHow how)
{
  PRInt32 rv;
    switch (how) {
      case PR_ACCESS_WRITE_OK:
        rv = access(name, 02);
		break;
      case PR_ACCESS_READ_OK:
        rv = access(name, 04);
		break;
      case PR_ACCESS_EXISTS:
        return access(name, 00);
	  	break;
      default:
		PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
		return -1;
    }
	if (rv < 0)
		_PR_MD_MAP_ACCESS_ERROR(errno);
    return rv;
}

PRInt32
_PR_MD_MKDIR(const char *name, PRIntn mode)
{
   PRInt32 rc;
    /* XXXMB - how to translate the "mode"??? */
    if ((rc = DosCreateDir((char *)name, NULL))== NO_ERROR) {
        return 0;
    } else {
		_PR_MD_MAP_MKDIR_ERROR(rc);
        return -1;
    }
}

PRInt32
_PR_MD_RMDIR(const char *name)
{
   PRInt32 rc;
    if ( (rc = DosDeleteDir((char *)name)) == NO_ERROR) {
        return 0;
    } else {
		_PR_MD_MAP_RMDIR_ERROR(rc);
        return -1;
    }
}

PRStatus
_PR_MD_LOCKFILE(PRInt32 f)
{
	PRInt32   rv;
   FILELOCK lock, unlock;

   lock.lOffset = 0;
   lock.lRange = 0xffffffff;
   unlock.lOffset = 0;
   unlock.lRange = 0;

	/*
     * loop trying to DosSetFileLocks(),
     * pause for a few miliseconds when can't get the lock
     * and try again
     */
    for( rv = FALSE; rv == FALSE; /* do nothing */ )
    {
    
	    rv = DosSetFileLocks( (HFILE) f,
			                    &unlock, &lock,
			                    0, 0); 
		if ( rv != NO_ERROR )
        {
            DosSleep( 50 );  /* Sleep() a few milisecs and try again. */
        }            
    } /* end for() */
    return PR_SUCCESS;
} /* end _PR_MD_LOCKFILE() */

PRStatus
_PR_MD_TLOCKFILE(PRInt32 f)
{
    return _PR_MD_LOCKFILE(f);
} /* end _PR_MD_TLOCKFILE() */


PRStatus
_PR_MD_UNLOCKFILE(PRInt32 f)
{
	PRInt32   rv;
   FILELOCK lock, unlock;

   lock.lOffset = 0;
   lock.lRange = 0;
   unlock.lOffset = 0;
   unlock.lRange = 0xffffffff;
    
   rv = DosSetFileLocks( (HFILE) f,
                          &unlock, &lock,
                          0, 0); 
            
    if ( rv != NO_ERROR )
    {
    	return PR_SUCCESS;
    }
    else
    {
		return PR_FAILURE;
    }
} /* end _PR_MD_UNLOCKFILE() */

PRStatus
_PR_MD_SET_FD_INHERITABLE(PRFileDesc *fd, PRBool inheritable)
{
    int rv = 0;
    ULONG flags;

    rv = DosQueryFHState((HFILE)fd->secret->md.osfd, &flags);
    if (rv != 0) {
        PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
        return PR_FAILURE;
    }

    if (inheritable)
      flags &= ~OPEN_FLAGS_NOINHERIT;
    else
      flags |= OPEN_FLAGS_NOINHERIT;

    rv = DosSetFHState((HFILE)fd->secret->md.osfd, flags);
    if (rv != 0) {
        PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
        return PR_FAILURE;
    }
    return PR_SUCCESS;
}

void
_PR_MD_INIT_FD_INHERITABLE(PRFileDesc *fd, PRBool imported)
{
    /* XXX this function needs to be implemented */
    fd->secret->inheritable = _PR_TRI_UNKNOWN;
}

void
_PR_MD_QUERY_FD_INHERITABLE(PRFileDesc *fd)
{
    /* XXX this function needs to be reviewed */
    ULONG flags;

    PR_ASSERT(_PR_TRI_UNKNOWN == fd->secret->inheritable);
    if (DosQueryFHState((HFILE)fd->secret->md.osfd, &flags) == 0) {
        if (flags & OPEN_FLAGS_NOINHERIT) {
            fd->secret->inheritable = _PR_TRI_FALSE;
        } else {
            fd->secret->inheritable = _PR_TRI_TRUE;
        }
    }
}