home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / NOPEN.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  20KB  |  909 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. /*#define VALIDATION_SUITE_1*/
  25. /*#define VALIDATION_SUITE_2*/
  26.  
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <fcntl.h>
  30. #include "uni.h"
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <dos.h>
  34. #include <errno.h>
  35. #include "nopen.h"
  36.  
  37. /* If _nopen_cheat != 0, the DOS version of nopen() will not do             *
  38.  * the creat/close/open trick when creating a new file.  Instead, it will   *
  39.  * just use creat.  Although this does not allow our sharing code to        *
  40.  * come into force, it does make things a lot faster when file access       *
  41.  * time is the bottleneck.                                                  */
  42.  
  43. char _stdc _nopen_cheat=0;
  44. extern int __NFiles;
  45.  
  46.  
  47. /* Open a file without sharing attributes - simply a shell for sopen */
  48.  
  49. int _stdc nopen(const char *name, int mode, ...)
  50. {
  51.   int permiss=S_IREAD | S_IWRITE;
  52.  
  53.   if (mode & O_CREAT)
  54.   {
  55.     va_list val;
  56.  
  57.     va_start(val, mode);
  58.     permiss=va_arg(val, int);
  59.     va_end(val);
  60.   }
  61.  
  62.   return nsopen(name, mode, SH_DENYNO, permiss);
  63. }
  64.  
  65. long _stdc ntell(int fd)
  66. {
  67.   return (nlseek(fd, 0, SEEK_CUR));
  68. }
  69.  
  70.  
  71. #if defined(__WATCOMC__)
  72.  
  73. extern __IOMode(int fd);
  74. extern __SetIOMode(int fd, int mode);
  75.  
  76. #define __OrIoMode(fd, flag) __SetIOMode(fd, __IOMode(fd) | (flag))
  77.  
  78. static void near _wcclearflags(int fd)
  79. {
  80.   if (fd==-1 || fd >= __NFiles)
  81.     return;
  82.  
  83.   __SetIOMode(fd, 0);
  84. }
  85.  
  86. static void near _wcsetiomode(int fd, int mode)
  87. {
  88.   if (fd==-1 || fd >= __NFiles)
  89.     return;
  90.  
  91.   __SetIOMode(fd, 0);   /* initialize i/o mode to "no bits set" */
  92.  
  93. /*  if (mode & (O_RDONLY|O_RDWR)) */
  94.   if ((mode & O_WRONLY)==0)   /* WC uses "#define O_RDONLY 0"! */
  95.     __OrIoMode(fd, _READ);
  96.  
  97.   if (mode & (O_WRONLY|O_RDWR))
  98.     __OrIoMode(fd, _WRITE);
  99.  
  100.   if (mode & O_APPEND)
  101.     __OrIoMode(fd, _APPEND);
  102.  
  103.   if (mode & O_BINARY)
  104.     __OrIoMode(fd, _BINARY);
  105.  
  106.   __OrIoMode(fd, _IONBF);
  107.  
  108.   if (isatty(fd))
  109.     __OrIoMode(fd, _ISTTY);
  110. }
  111.  
  112. #endif
  113.  
  114.  
  115. #if defined(__MSDOS__)
  116.  
  117.   #include "fpseg.h"
  118.  
  119.   /* Open a file with sharing attributes */
  120.  
  121.   int _stdc nsopen(const char *name, int mode, int shacc, ...)
  122.   {
  123.     union REGS save, r;
  124.     int permiss=S_IREAD | S_IWRITE;
  125.     int fd;
  126.  
  127.     #ifndef __FLAT__
  128.     struct SREGS sr;
  129.     #endif
  130.  
  131.  
  132.     if (mode & O_CREAT)
  133.     {
  134.       va_list val;
  135.  
  136.       va_start(val, shacc);
  137.       permiss=va_arg(val, int);
  138.       va_end(val);
  139.     }
  140.  
  141.     /* Access mode: read, write, read/write */
  142.  
  143.     r.h.al=(char)((mode & O_RDWR)==O_RDWR      ?  0x02 :
  144.                   (mode & O_WRONLY)==O_WRONLY  ?  0x01 :
  145.                                                   0x00);
  146.  
  147.     r.h.al |= ((shacc==SH_DENYNO) ? 0x40 :
  148.                (shacc==SH_DENYWR) ? 0x20 :
  149.                (shacc==SH_DENYRD) ? 0x30 :
  150.                (shacc==SH_DENYRW) ? 0x10 :
  151.                0) | /* "compatibility" mode */
  152.                ((mode & O_NOINHERIT) ? 0x80u : 0); /* noinherit is default */
  153.  
  154.     /* read-only unless S_IWRITE */
  155.  
  156.   #ifdef __FLAT__
  157.     r.x.ecx = (permiss & S_IWRITE)==S_IWRITE ? 0x00 : 0x01;
  158.     r.x.edx = (unsigned)name;
  159.   #else
  160.     r.x.cx = (permiss & S_IWRITE)==S_IWRITE ? 0x00 : 0x01;
  161.     sr.ds=FP_SEG(((char far *)name));
  162.     r.x.dx=FP_OFF(((char far *)name));
  163.   #endif
  164.  
  165.  
  166.     save=r;
  167.  
  168.     /* If we're not using exclusive access, try to open the file.  If it    *
  169.      * is exclusive access, skip this since we need to call creat() anyway. */
  170.  
  171.     if ((mode & O_EXCL) || (mode & (O_CREAT|O_TRUNC))==(O_CREAT|O_TRUNC))
  172.       r.x.cflag=1;
  173.     else
  174.     {
  175.       /* Function 3d = open file */
  176.  
  177.       r.h.ah=0x3d;
  178.  
  179.   #ifdef __FLAT__
  180.       int386(0x21, &r, &r);
  181.   #else
  182.       int86x(0x21, &r, &r, &sr);
  183.   #endif
  184. /*      printf("int 21 ah=3d rc=%d\n", r.x.cflag);*/
  185.     }
  186.  
  187.  
  188.     /* If it failed, or if using exclusive access, use creat() */
  189.  
  190.     if (r.x.cflag && (mode & O_CREAT))
  191.     {
  192.       r=save;
  193.       r.h.ah=(char)((mode & O_EXCL) ? 0x5b : 0x3c);  /* create new file */
  194.  
  195.   #ifdef __FLAT__
  196.       int386(0x21, &r, &r);
  197.   #else
  198.       int86x(0x21, &r, &r, &sr);
  199.   #endif
  200.  
  201. /*      printf("int 21 ah=%x rc=%d\n", (mode & O_EXCL) ? 0x5b : 0x3c, r.x.cflag);*/
  202.  
  203.       /* If the open succeeded... */
  204.  
  205.       if (r.x.cflag==0 && !_nopen_cheat)
  206.       {
  207.         /* The file is open, but creat() doesn't honour file sharing modes.  *
  208.          * Consequently, we have to close and reopen using the mode we want. */
  209.  
  210.         if (shacc != SH_COMPAT)
  211.         {
  212.   #ifdef __FLAT__
  213.           nclose(r.x.eax);
  214.   #else
  215.           nclose(r.x.ax);
  216.   #endif
  217.  
  218. /*          printf("int 21 ah=close rc=%d\n", r.x.cflag);*/
  219.  
  220.           r=save;
  221.           r.h.ah=0x3d;
  222.  
  223.   #ifdef __FLAT__
  224.           int386(0x21, &r, &r);
  225.   #else
  226.           int86x(0x21, &r, &r, &sr);
  227.   #endif
  228.  
  229. /*          printf("int 21 ah=3d rc=%d\n", r.x.cflag);*/
  230.         }
  231.       }
  232.     }
  233.  
  234.     if (r.x.cflag)
  235.     {
  236.   #ifdef __FLAT__
  237.       errno=r.x.eax;
  238.   #else
  239.       errno=r.x.ax;
  240.   #endif
  241.       return -1;
  242.     }
  243.  
  244.   #ifdef __FLAT__
  245.     fd=r.x.eax;
  246.   #else
  247.     fd=r.x.ax;
  248.   #endif
  249.  
  250.     if (fd != -1 && (mode & O_APPEND))
  251.       nlseek(fd, 0L, SEEK_END);
  252.  
  253.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  254.     _wcsetiomode(fd, mode);
  255.   #endif
  256.  
  257.     return fd;
  258.   }
  259.  
  260.  
  261.   int _stdc nread(int fd, char *buf, unsigned len)
  262.   {
  263.     union REGS r;
  264.  
  265.   #ifdef __FLAT__
  266.     r.h.ah=0x3f;
  267.     r.x.ebx=fd;
  268.     r.x.ecx=len;
  269.     r.x.edx=(unsigned)buf;
  270.     int386(0x21, &r, &r);
  271.  
  272.     if (r.x.cflag)
  273.     {
  274.       errno=r.x.eax;
  275.       return -1;
  276.     }
  277.  
  278.     return r.x.eax;
  279.   #else
  280.     struct SREGS sr;
  281.  
  282.     r.h.ah=0x3f;
  283.     r.x.bx=fd;
  284.     r.x.cx=len;
  285.     sr.ds=FP_SEG(((char far *)buf));
  286.     r.x.dx=FP_OFF(((char far *)buf));
  287.     int86x(0x21, &r, &r, &sr);
  288.  
  289.     if (r.x.cflag)
  290.     {
  291.       errno=r.x.ax;
  292.       return -1;
  293.     }
  294.  
  295.     return r.x.ax;
  296.   #endif
  297.   }
  298.  
  299.   int _stdc nwrite(int fd, char *buf, unsigned len)
  300.   {
  301.     union REGS r;
  302.  
  303.   #ifdef __FLAT__
  304.     r.h.ah=0x40;
  305.     r.x.ebx=fd;
  306.     r.x.ecx=len;
  307.     r.x.edx=(unsigned)buf;
  308.     int386(0x21, &r, &r);
  309.  
  310.     if (r.x.cflag)
  311.     {
  312.       errno=r.x.eax;
  313.       return -1;
  314.     }
  315.  
  316.     return r.x.eax;
  317.   #else
  318.     struct SREGS sr;
  319.     r.h.ah=0x40;
  320.     r.x.bx=fd;
  321.     r.x.cx=len;
  322.     sr.ds=FP_SEG(((char far *)buf));
  323.     r.x.dx=FP_OFF(((char far *)buf));
  324.     int86x(0x21, &r, &r, &sr);
  325.  
  326.     if (r.x.cflag)
  327.     {
  328.       errno=r.x.ax;
  329.       return -1;
  330.     }
  331.  
  332.     return r.x.ax;
  333.   #endif
  334.   }
  335.  
  336.   long _stdc nlseek(int fd, long ofs, int pos)
  337.   {
  338.     union REGS r;
  339.  
  340.     r.h.ah=0x42;
  341.  
  342.     r.h.al=(char)((pos==SEEK_CUR) ? 1 :
  343.                   (pos==SEEK_END) ? 2 :
  344.                                     0);
  345.   #ifdef __FLAT__
  346.     r.x.ebx=fd;
  347.     r.w.cx=(int)(ofs >> 16);
  348.     r.w.dx=(int)(ofs & 0xffffuL);
  349. /*    r.x.edx=ofs;*/
  350.     int386(0x21, &r, &r);
  351.  
  352.     if (r.x.cflag)
  353.     {
  354.       errno=r.x.eax;
  355.       return -1;
  356.     }
  357.  
  358.     return ((long)(r.w.dx << 16) | (long)r.w.ax);
  359.   #else
  360.     r.x.bx=fd;
  361.     r.x.cx=(int)(ofs >> 16);
  362.     r.x.dx=(int)(ofs & 0xffffuL);
  363.     int86(0x21, &r, &r);
  364.  
  365.     if (r.x.cflag)
  366.     {
  367.       errno=r.x.ax;
  368.       return -1;
  369.     }
  370.  
  371.     return ((long)r.x.dx << 16) | (long)r.x.ax;
  372.   #endif
  373.  
  374.   }
  375.  
  376.   int _stdc nclose(int fd)
  377.   {
  378.     union REGS r;
  379.  
  380.     r.h.ah=0x3e;
  381.  
  382.   #ifdef __FLAT__
  383.     r.x.ebx=fd;
  384.     int386(0x21, &r, &r);
  385.   #else
  386.     r.x.bx=fd;
  387.     int86(0x21, &r, &r);
  388.   #endif
  389.  
  390.     /* Now clear out flags for this handle */
  391.  
  392.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  393.   _wcclearflags(fd);
  394.   #endif
  395.  
  396.     return (r.x.cflag ? -1 : 0);
  397.   }
  398.  
  399.   int _stdc ndup(int fd)
  400.   {
  401.     union REGS r;
  402.  
  403.   #ifdef __FLAT__
  404.     r.h.ah=0x45;
  405.     r.x.ebx=fd;
  406.  
  407.     int386(0x21, &r, &r);
  408.  
  409.     if (r.x.cflag)
  410.     {
  411.       errno=r.x.eax;
  412.       return -1;
  413.     }
  414.  
  415.     #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  416.       if (r.x.eax < __NFiles)
  417.         __SetIOMode((int)r.x.eax, __IOMode(fd));
  418.     #endif
  419.  
  420.     return r.x.eax;
  421.   #else
  422.     r.h.ah=0x45;
  423.     r.x.bx=fd;
  424.  
  425.     int86(0x21, &r, &r);
  426.  
  427.     if (r.x.cflag)
  428.     {
  429.       errno=r.x.ax;
  430.       return -1;
  431.     }
  432.  
  433.     #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  434.       if (r.x.ax < __NFiles)
  435.         __SetIOMode((int)r.x.ax, __IOMode(fd));
  436.     #endif
  437.  
  438.     return r.x.ax;
  439.   #endif
  440.   }
  441.  
  442.   int _stdc ndup2(int fd1, int fd2)
  443.   {
  444.     union REGS r;
  445.  
  446.     r.h.ah=0x46;
  447.  
  448.   #ifdef __FLAT__
  449.     r.x.ebx=fd1;
  450.     r.x.ecx=fd2;
  451.  
  452.     int386(0x21, &r, &r);
  453.  
  454.     if (r.x.cflag)
  455.     {
  456.       errno=r.x.eax;
  457.       return -1;
  458.     }
  459.   #else
  460.     r.x.bx=fd1;
  461.     r.x.cx=fd2;
  462.  
  463.     int86(0x21, &r, &r);
  464.  
  465.     if (r.x.cflag)
  466.     {
  467.       errno=r.x.ax;
  468.       return -1;
  469.     }
  470.   #endif
  471.  
  472.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  473.     if (fd2 < __NFiles)
  474.       __SetIOMode(fd2, __IOMode(fd1));
  475.   #endif
  476.  
  477.     return 0;
  478.   }
  479.  
  480.  
  481. #elif defined(OS_2)
  482.  
  483.   #define INCL_DOSFILEMGR
  484.   #include "pos2.h"
  485.  
  486.   int _stdc nsopen(const char *name, int mode, int shacc, ...)
  487.   {
  488.     HFILE hf;
  489.   #ifdef __FLAT__
  490.     ULONG rc, usAction;
  491.   #else
  492.     USHORT rc, usAction;
  493.   #endif
  494.     int permiss=S_IREAD | S_IWRITE;
  495.     char *p;
  496.  
  497.     if (mode & O_CREAT)
  498.     {
  499.       va_list val;
  500.  
  501.       va_start(val, shacc);
  502.       permiss=va_arg(val, int);
  503.       va_end(val);
  504.     }
  505.  
  506.     /* Now use the OS "open file" function */
  507.  
  508.     p=(char *)name;
  509.  
  510.   #ifdef __FLAT__ /* OS/2 2.0 */
  511.     rc=DosOpen(p,
  512.                &hf,
  513.                &usAction,
  514.                0,
  515.                /****/
  516.                (permiss & S_IWRITE) ? FILE_NORMAL : FILE_READONLY,
  517.                /****/
  518.                ((mode & O_CREAT) ? OPEN_ACTION_CREATE_IF_NEW :
  519.                                    OPEN_ACTION_FAIL_IF_NEW) |
  520.                ((mode & O_EXCL) ? OPEN_ACTION_FAIL_IF_EXISTS :
  521.                 (mode & O_TRUNC) ? OPEN_ACTION_REPLACE_IF_EXISTS :
  522.                                    OPEN_ACTION_OPEN_IF_EXISTS),
  523.                /****/
  524.                ((mode & O_WRONLY)==O_WRONLY ? OPEN_ACCESS_WRITEONLY :
  525.                 (mode & O_RDWR)==O_RDWR     ? OPEN_ACCESS_READWRITE :
  526.                                               OPEN_ACCESS_READONLY) |
  527.                (shacc==SH_DENYWR ? OPEN_SHARE_DENYWRITE :
  528.                 shacc==SH_DENYRD ? OPEN_SHARE_DENYREAD :
  529.                 shacc==SH_DENYRW ? OPEN_SHARE_DENYREADWRITE :
  530.                                    OPEN_SHARE_DENYNONE) |
  531.                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_NO_LOCALITY,
  532.                /****/
  533.                NULL);
  534.   #else
  535.     rc=DosOpen(p,
  536.                &hf,
  537.                &usAction,
  538.                0,
  539.                /****/
  540.                (permiss & S_IWRITE) ? FILE_NORMAL : FILE_READONLY,
  541.                /****/
  542.                ((mode & O_EXCL) ? 0 :
  543.                  ((mode & (O_CREAT|O_TRUNC))==(O_CREAT|O_TRUNC)
  544.                  ? 0 : FILE_OPEN)) |
  545.                ((mode & O_CREAT) ? FILE_CREATE : 0)   |
  546.                ((mode & (O_TRUNC|O_EXCL))==O_TRUNC ? FILE_TRUNCATE : 0),
  547.                /****/
  548.                ((mode & O_WRONLY)==O_WRONLY ? OPEN_ACCESS_WRITEONLY :
  549.                 (mode & O_RDWR)==O_RDWR     ? OPEN_ACCESS_READWRITE :
  550.                                               OPEN_ACCESS_READONLY) |
  551.                (shacc==SH_DENYWR ? OPEN_SHARE_DENYWRITE :
  552.                 shacc==SH_DENYRD ? OPEN_SHARE_DENYREAD :
  553.                 shacc==SH_DENYRW ? OPEN_SHARE_DENYREADWRITE :
  554.                                    OPEN_SHARE_DENYNONE) |
  555.                OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_NO_LOCALITY,
  556.                /****/
  557.                0);
  558.   #endif
  559.  
  560.     if (rc)
  561.     {
  562.       errno=rc;
  563.       return -1;
  564.     }
  565.  
  566.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  567.     _wcsetiomode(hf, mode);
  568.   #endif
  569.  
  570.     if (hf != -1 && (mode & O_APPEND))
  571.       nlseek(hf, 0L, SEEK_END);
  572.  
  573.     return hf;
  574.   }
  575.  
  576.   int _stdc nread(int fd, char *buf, unsigned len)
  577.   {
  578.     #ifdef __FLAT__
  579.       ULONG rc, got;
  580.     #else
  581.       USHORT rc, got;
  582.     #endif
  583.  
  584.     rc=DosRead((HFILE)fd, buf, len, &got);
  585.  
  586.     if (rc)
  587.     {
  588.       errno=rc;
  589.       return -1;
  590.     }
  591.  
  592.     return got;
  593.   }
  594.  
  595.   int _stdc nwrite(int fd, char *buf, unsigned len)
  596.   {
  597.     #ifdef __FLAT__
  598.       ULONG rc, got;
  599.     #else
  600.       USHORT rc, got;
  601.     #endif
  602.  
  603.     rc=DosWrite((HFILE)fd, buf, len, &got);
  604.  
  605.     if (rc)
  606.     {
  607.       errno=rc;
  608.       return -1;
  609.     }
  610.  
  611.     return got;
  612.   }
  613.  
  614.   long _stdc nlseek(int fd, long ofs, int pos)
  615.   {
  616.     #ifdef __FLAT__
  617.       ULONG rc;
  618.     #else
  619.       USHORT rc;
  620.     #endif
  621.  
  622.     ULONG newpos;
  623.  
  624.   #ifdef __FLAT__
  625.     rc=DosSetFilePtr((HFILE)fd, ofs,
  626.   #else
  627.     rc=DosChgFilePtr((HFILE)fd, ofs,
  628.   #endif
  629.                      pos==SEEK_END ? FILE_END :
  630.                      pos==SEEK_CUR ? FILE_CURRENT :
  631.                      FILE_BEGIN,
  632.                      &newpos);
  633.  
  634.     if (rc)
  635.     {
  636.       errno=rc;
  637.       return -1;
  638.     }
  639.  
  640.     return newpos;
  641.   }
  642.  
  643.   int _stdc nclose(int fd)
  644.   {
  645.     #ifdef __FLAT__
  646.       ULONG rc;
  647.     #else
  648.       USHORT rc;
  649.     #endif
  650.  
  651.     rc=DosClose(fd);
  652.  
  653.     if (rc)
  654.     {
  655.       errno=rc;
  656.       return -1;
  657.     }
  658.  
  659.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  660.     _wcclearflags(fd);
  661.   #endif
  662.  
  663.     return 0;
  664.   }
  665.  
  666.   int _stdc ndup(int fd)
  667.   {
  668.     HFILE hfNew=(HFILE)-1;
  669.     OS2UINT rc;
  670.  
  671.     if ((rc=DosDupHandle((HFILE)fd, &hfNew)) != 0)
  672.     {
  673.       errno=rc;
  674.       return -1;
  675.     }
  676.  
  677.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  678.     if ((int)hfNew < __NFiles)
  679.       __SetIOMode((int)hfNew, __IOMode(fd));
  680.   #endif
  681.  
  682.     return (int)hfNew;
  683.   }
  684.  
  685.   int _stdc ndup2(int fd1, int fd2)
  686.   {
  687.     HFILE hfNew=(HFILE)fd2;
  688.     OS2UINT rc;
  689.  
  690.     if ((rc=DosDupHandle((HFILE)fd1, &hfNew)) != 0)
  691.     {
  692.       errno=rc;
  693.       return -1;
  694.     }
  695.  
  696.   #if defined(__WATCOMC__) /* handle special declarations for WC's file mode array */
  697.     if (fd2 < __NFiles)
  698.       __SetIOMode(fd2, __IOMode(fd1));
  699.   #endif
  700.  
  701.     return 0;
  702.   }
  703.  
  704. #elif defined(NT) || defined(__POSIX__)
  705.  
  706.   /* We will assume for now that the Windows NT version of the MS RTL       *
  707.    * are okay.  (A bad assumption, but...)  If we run into trouble          *
  708.    * later, our own version of these functions can be written.              */
  709.  
  710.   int _stdc nsopen(const char *name, int mode, int shacc, ...)
  711.   {
  712.     return sopen(name, mode, shacc, S_IREAD | S_IWRITE);
  713.   }
  714.  
  715.   int _stdc nread(int fd, char *buf, unsigned len)
  716.   {
  717.     return read(fd, buf, len);
  718.   }
  719.  
  720.   int _stdc nwrite(int fd, char *buf, unsigned len)
  721.   {
  722.     return write(fd, buf, len);
  723.   }
  724.  
  725.   long _stdc nlseek(int fd, long ofs, int pos)
  726.   {
  727.     return lseek(fd, ofs, pos);
  728.   }
  729.  
  730.   int _stdc nclose(int fd)
  731.   {
  732.     return close(fd);
  733.   }
  734.  
  735.   int _stdc ndup(int fd)
  736.   {
  737.     return dup(fd);
  738.   }
  739.  
  740.   int _stdc ndup2(int fd1, int fd2)
  741.   {
  742.     return dup2(fd1, fd2);
  743.   }
  744.  
  745. #else
  746.   #error Unknown OS
  747. #endif
  748.  
  749.  
  750. #ifdef VALIDATTION_SUITE_1
  751. main()
  752. {
  753.   int fd;
  754.   char buf[2];
  755.  
  756.   if ((fd=nopen("open.obj", O_RDONLY | O_BINARY))==-1)
  757.   {
  758.     printf("err opening open.obj\n");
  759.     return 1;
  760.   }
  761.  
  762.   printf("ntell(fd)=%ld\n", ntell(fd));
  763.  
  764.   printf("nread(fd, buf, 2)=%d\n",
  765.          nread(fd, buf, 2));
  766.  
  767.   printf("ntell(fd)=%ld\n", ntell(fd));
  768.  
  769.   printf("nlseek(fd, 1, SEEK_SET)=%ld\n", nlseek(fd, 1L, SEEK_SET));
  770.  
  771.   printf("ntell(fd)=%ld\n", ntell(fd));
  772.  
  773.   printf("hdr=%c%c\n", buf[0], buf[1]);
  774.  
  775.   printf("nclose(fd)=%d\n", nclose(fd));
  776. }
  777. #endif
  778.  
  779. #ifdef VALIDATION_SUITE_2
  780.  
  781. main()
  782. {
  783.   int fd;
  784.  
  785.   if ((fd=nopen("nopen.obj", O_CREAT | O_TRUNC | O_EXCL | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  786.   {
  787.     printf("  OK: O_EXCL1 (%d)\n", fd);
  788.   }
  789.   else
  790.   {
  791.     printf("FAIL: O_EXCL1\n");
  792.     nclose(fd);
  793.   }
  794.  
  795.   unlink("notexist.fil");
  796.  
  797.   if ((fd=nopen("notexist.fil", O_CREAT | O_TRUNC | O_EXCL | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  798.   {
  799.     printf("FAIL: O_EXCL2 (%d)\n", fd);
  800.   }
  801.   else
  802.   {
  803.     printf("  OK: O_EXCL2\n");
  804.     nclose(fd);
  805.   }
  806.  
  807.   unlink("notexist.fil");
  808.  
  809.   if ((fd=nopen("notexist.fil", O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  810.   {
  811.     printf("FAIL: O_EXCL3 (%d)\n", fd);
  812.   }
  813.   else
  814.   {
  815.     printf("  OK: O_EXCL3\n");
  816.     nclose(fd);
  817.   }
  818.  
  819.   unlink("notexist.fil");
  820.  
  821.  
  822.   if ((fd=nopen("notexist.fil", O_RDONLY | O_BINARY | O_EXCL, S_IREAD | S_IWRITE))==-1)
  823.   {
  824.     printf("  OK: O_RDONLY1\n");
  825.   }
  826.   else
  827.   {
  828.     printf("FAIL: O_RDONLY1\n");
  829.     nclose(fd);
  830.   }
  831.  
  832.  
  833.   if ((fd=nopen("notexist.fil", O_RDONLY | O_BINARY, S_IREAD | S_IWRITE))==-1)
  834.   {
  835.     printf("  OK: O_RDONLY2\n");
  836.   }
  837.   else
  838.   {
  839.     printf("FAIL: O_RDONLY2\n");
  840.     nclose(fd);
  841.   }
  842.  
  843.   if ((fd=nopen("nopen.obj", O_RDONLY | O_BINARY, S_IREAD | S_IWRITE))==-1)
  844.   {
  845.     printf("FAIL: O_RDONLY3 (%d)\n", fd);
  846.   }
  847.   else
  848.   {
  849.     if (nlseek(fd, 0, SEEK_END) > 1)
  850.       printf("  OK: O_RDONLY3\n");
  851.     else printf("FAIL: O_RDONLY3 (truncated)\n");
  852.  
  853.     nclose(fd);
  854.   }
  855.  
  856.   if ((fd=nopen("nopen.obj", O_CREAT | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  857.   {
  858.     printf("FAIL: O_CREAT1 (%d)\n", fd);
  859.   }
  860.   else
  861.   {
  862.     if (nlseek(fd, 0, SEEK_END) > 1)
  863.       printf("  OK: O_CREAT1\n");
  864.     else printf("FAIL: O_CREAT1 (truncated)\n");
  865.  
  866.     nclose(fd);
  867.   }
  868.  
  869.   if ((fd=nopen("nopen.obj", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  870.   {
  871.     printf("FAIL: O_CREAT2 %d %d\n", fd, errno);
  872.   }
  873.   else
  874.   {
  875.     if (nlseek(fd, 0, SEEK_END) != 0)
  876.       printf("FAIL: O_CREAT2 (not truncated)\n");
  877.     else printf("  OK: O_CREAT2\n");
  878.  
  879.     nclose(fd);
  880.   }
  881.  
  882.   unlink("new.fil");
  883.  
  884.   if ((fd=nopen("new.fil", O_CREAT | O_RDWR | O_BINARY, S_IREAD | S_IWRITE))==-1)
  885.   {
  886.     printf("FAIL: O_CREAT3 (%d)\n", fd);
  887.   }
  888.   else
  889.   {
  890.     if (nlseek(fd, 0, SEEK_END)==0)
  891.     {
  892.       long ofs;
  893.  
  894.       if ((ofs=nlseek(fd, 0x123456, SEEK_SET)) != 0x123456)
  895.         printf("FAIL: O_CREAT3 (ofs=%lx)\n", ofs);
  896.       else printf("  OK: O_CREAT3\n");
  897.     }
  898.     else printf("FAIL: O_CREAT3 (not truncated)\n");
  899.  
  900.     nclose(fd);
  901.   }
  902.  
  903.   unlink("new.fil");
  904.   return 0;
  905. }
  906.  
  907. #endif
  908.  
  909.