home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / FILE_DOS.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  17KB  |  514 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                This module was written by Vince Perriello                */
  14. /*                   OS/2 code contributed by Bill Andrus                   */
  15. /*                                                                          */
  16. /*            DOS and OS/2 File I/O routines used by BinkleyTerm            */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. /* Include this file before any other includes or defines! */
  47.  
  48. #include "includes.h"
  49.  
  50. #ifdef NEED_MKTEMP
  51. char *mktemp (char *template)
  52. {
  53.    static char save[8] = "Z";
  54.    char *p;
  55.    int i;
  56.  
  57.    p = save;
  58.  
  59.    if (*p == 'Z')
  60.       sprintf (p, "%06d", TaskNumber);
  61.    while (*p)
  62.       {
  63.       if (isdigit (*p))
  64.          {
  65.          *p = 'a';
  66.          break;
  67.          }
  68.       if (*p++ < 'z')
  69.          {
  70.          break;
  71.          }
  72.       }
  73.    if ((*p == '\0') || ((i = strlen (template)) < 6))
  74.       return (NULL);
  75.    strcpy (&template[i-6], save);
  76.    return (template);
  77. }
  78. #endif
  79.  
  80. #ifdef NEED_DOSREAD
  81. int _dos_read (int fd, void far *buf, unsigned nbytes, unsigned int *bytes_read)
  82. {
  83.     union REGS regs;
  84.     struct SREGS sregs;
  85.  
  86.     regs.h.ah = 0x3f;    /* read file */
  87.     regs.x.bx = fd;
  88.     regs.x.cx = nbytes;
  89.     regs.x.dx = FP_OFF (buf);
  90.     sregs.ds = FP_SEG (buf);
  91.     *bytes_read = intdosx (®s, ®s, &sregs);
  92.     return regs.x.cflag ? -1 : 0;
  93. }
  94.  
  95. int _dos_write (int fd, void far *buf, unsigned nbytes, unsigned int *bytes_read)
  96. {
  97.     union REGS regs;
  98.     struct SREGS sregs;
  99.  
  100.     regs.h.ah = 0x40;    /* write file */
  101.     regs.x.bx = fd;
  102.     regs.x.cx = nbytes;
  103.     regs.x.dx = FP_OFF (buf);
  104.     sregs.ds = FP_SEG (buf);
  105.     *bytes_read = intdosx (®s, ®s, &sregs);
  106.     return regs.x.cflag ? -1 : 0;
  107. }
  108. #endif
  109.  
  110. #ifdef __TURBOC__ 
  111. /* 
  112.  * utime function for Turbo / Borland C.
  113.  *
  114.  * We should make this more generic in case some other DOS
  115.  * compiler comes up lacking, but for now the only one we
  116.  * have that needs it happens to be Borland.
  117.  *
  118.  */
  119.  
  120. int cdecl utime (char *name, struct utimbuf *times)
  121. {
  122.     int handle;
  123.     struct date d;
  124.     struct time t;
  125.     struct ftime ft;
  126.  
  127.     unixtodos (times->modtime, &d, &t);
  128.     ft.ft_tsec = t.ti_sec / 2;
  129.     ft.ft_min = t.ti_min;
  130.     ft.ft_hour = t.ti_hour;
  131.     ft.ft_day = d.da_day;
  132.     ft.ft_month = d.da_mon;
  133.     ft.ft_year = d.da_year - 1980;
  134.     if ((handle = open(name, O_RDONLY)) == -1)
  135.         return -1;
  136.  
  137.     setftime (handle, &ft);
  138.     close (handle);
  139.     return 0;
  140. }
  141. #endif
  142.  
  143.  
  144. int dexists (char *filename)
  145. {
  146.    struct stat stbuf;
  147.  
  148.    return (stat (filename, &stbuf) != -1) ? 1 : 0;
  149. }
  150.  
  151. int dfind (struct FILEINFO *dta, char *name, int times)
  152. {
  153. #ifndef OS_2
  154.    union REGS r;
  155.    struct SREGS s;
  156.    char far *dtaptr = (char *)dta;
  157.    char far *nameptr = (char *)name;
  158.  
  159.    s.ds = FP_SEG (dtaptr);
  160.    r.x.dx = FP_OFF (dtaptr);
  161.    r.h.ah = 0x1a;
  162.    (void) intdosx (&r, &r, &s);
  163.    r.x.bx = 0;
  164.    r.x.cx = ~0x08;
  165.    s.ds = FP_SEG (nameptr);
  166.    r.x.dx = FP_OFF (nameptr);
  167.    r.x.si = 0;
  168.    r.x.di = 0;
  169.    if (times == 0)
  170.       {
  171.       r.h.ah = 0x4e;
  172.       (void) intdosx (&r, &r, &s);
  173.       dta->nill = '\0';
  174.       /* If not found or a character device (e.g. "COM1") */
  175.       if ((r.x.cflag != 0) || ((dta->attr & 0x40) != 0))
  176.          {
  177.          dta->name[0] = '\0';
  178.          return (1);
  179.          }
  180.       return (0);
  181.       }
  182.    else
  183.       {
  184.       r.h.ah = 0x4f;
  185.       (void) intdosx (&r, &r, &s);
  186.       dta->nill = '\0';
  187.       /* If not found or a character device (e.g. "COM1") */
  188.       if ((r.x.cflag != 0) || ((dta->attr & 0x40) != 0))
  189.          {
  190.          dta->name[0] = '\0';
  191.          return (1);
  192.          }
  193.       return (0);
  194.       }
  195. #else
  196.    if (times == 0)
  197.       {
  198.       return (dir_findfirst (name, 0x37, dta));
  199.       }
  200.    else
  201.       {
  202.       return (dir_findnext (dta));
  203.       }
  204. #endif
  205. }
  206.  
  207.  
  208. int got_error (char *string1, char *string2)
  209. {
  210.    if (errno == 0x18)
  211.       errno = 0;
  212.    if (errno != 0)
  213.       {
  214.       status_line ("%s %d, %s %s %s", MSG_TXT(M_ERROR), errno, MSG_TXT(M_CANT), string1, string2);
  215.       errno = 0;
  216.       return (1);
  217.       }
  218.    return (0);
  219. }
  220.  
  221.  
  222. /* Z F R E E -- Return total number of free bytes on drive specified */
  223. #ifdef OS_2
  224. long zfree (char *path)
  225. {
  226.    int drive;
  227.    FSALLOCATE dt;
  228.  
  229.    if (*path != '\0' && path[1] == ':')
  230.       drive = tolower (*path) - 'a' + 1;
  231.    else
  232.       drive = 0;
  233.    DosQFSInfo (drive, 1, (char far *) &dt, sizeof (FSALLOCATE));
  234.    return ( dt.cSectorUnit * dt.cUnitAvail * dt.cbSector);
  235. }
  236.  
  237. #else /* OS_2 */
  238.  
  239. long zfree (char *drive)
  240. {
  241.    union REGS r;
  242.  
  243.    unsigned char driveno;
  244.    long stat;
  245.  
  246.    if (drive[0] != '\0' && drive[1] == ':')
  247.       {
  248.       driveno = (unsigned char) (islower (*drive) ? toupper (*drive) : *drive);
  249.       driveno = (unsigned char) (driveno - 'A' + 1);
  250.       }
  251.    else driveno = 0;                             /* Default drive    */
  252.  
  253.    r.x.ax = 0x3600;                              /* get free space   */
  254.    r.h.dl = driveno;                             /* on this drive    */
  255.    (void) int86 (0x21, &r, &r);                  /* go do it         */
  256.  
  257.    if (r.x.ax == 0xffff)                         /* error return??   */
  258.       return (0);
  259.  
  260.    stat = (long) r.x.bx                          /* bx = clusters avail  */
  261.       * (long) r.x.ax                            /* ax = sectors/clust   */
  262.       * (long) r.x.cx;                           /* cx = bytes/sector    */
  263.  
  264.    return (stat);
  265.  
  266. }
  267.  
  268. #endif /* OS_2 */
  269.  
  270.  
  271. static int share_flags[] = {
  272.                              SH_COMPAT,
  273.                              SH_DENYNO,
  274.                              SH_DENYRD,
  275.                              SH_DENYRW,
  276.                              SH_DENYWR
  277.                            };
  278.  
  279.  
  280. int share_open (char *filename, int oflag, int shflag)
  281. {
  282.     if ((no_sharing == 0) && (_osmajor >= 3))
  283.         return (sopen (filename, oflag, share_flags[shflag]));
  284.     else
  285.         return (open (filename, oflag));
  286.  
  287. }
  288.  
  289. FILE *share_fopen (char *filename, char *mode, int shflag)
  290. {
  291. #ifdef MUST_FDOPEN
  292.     int fd;
  293.     int open_mode;
  294.     char c, *p;
  295.     int sflag = S_IREAD | S_IWRITE;
  296.     FILE *stream;
  297. /*
  298.  *  Microsoft made this easy for us. They gave us a stream-open
  299.  *  function that supports file sharing. Borland was not so kind.
  300.  *  So -- what we do here is open the file using the only sharing
  301.  *  API they provide -- that gives us a file handle -- then we
  302.  *  use fdopen to get a file stream out of it. Sheesh!
  303.  *
  304.  *  Of course, Microsoft 5.1 provides about the same level of
  305.  *  support as Borland -- almost. They explicitly warn you not
  306.  *  to do this neat thing I did for Borland. So I get no sharing
  307.  *  support for fopen() under 5.1. Double sheesh!
  308.  */
  309.     if ((no_sharing == 0) && (_osmajor >= 3))
  310.         {
  311.  
  312.         /* Figure out the translation from fopen-mode to
  313.            open-mode... */
  314.  
  315.         p = mode;
  316.         c = *p++;
  317.         if (c == 'w')
  318.             open_mode = O_CREAT | O_RDWR;
  319.         else if (c == 'r')
  320.             open_mode = O_RDONLY;
  321.         else if (c == 'a')
  322.             open_mode = O_CREAT | O_RDWR | O_APPEND;
  323.         else return (FILE *) NULL;
  324.         while (*p)
  325.             {
  326.             c = *p++;
  327.             if (c == 't')
  328.                 open_mode |= O_TEXT;
  329.             if (c == 'b')
  330.                 open_mode |= O_BINARY;
  331.             if (c == '+')
  332.                 {
  333.                 if (open_mode & O_RDONLY)
  334.                     {
  335.                     open_mode = (open_mode & ~O_RDONLY) | O_RDWR;
  336.                     }
  337.                 else if (!(open_mode & O_APPEND))
  338.                     open_mode |= O_TRUNC;
  339.                 }
  340.             }
  341.  
  342.         /* Open the file handle */
  343.  
  344.         fd = open (filename, open_mode | share_flags[shflag], sflag);
  345.  
  346.         if (fd == -1)
  347.             return (FILE *) NULL;
  348.  
  349.         /* Got the handle, make the stream */
  350.  
  351.         if ((stream = fdopen (fd, mode)) == (FILE *) NULL)
  352.             (void) close (fd);
  353.         return (stream);
  354.         }
  355.     else
  356. #else
  357. #ifndef CANT_FSOPEN
  358.     if ((no_sharing == 0) && (_osmajor >= 3))
  359.         return (_fsopen (filename, mode, share_flags[shflag]));
  360.     else
  361. #else
  362.     happy_compiler = shflag;    /* Makes the compiler happy */
  363. #endif
  364. #endif
  365.         return (fopen (filename, mode));
  366. }
  367.  
  368. #ifdef OS_2
  369.  
  370. /*--------------------------------------------------------------------------*/
  371. /* Static variable definitions                                              */
  372. /*--------------------------------------------------------------------------*/
  373.  
  374. struct FileTimeBuf {
  375.    unsigned c_date;       /* date of file creation */
  376.    unsigned c_time;       /* time of file creation */
  377.    unsigned a_date;       /* date of last access   */
  378.    unsigned a_time;       /* time of last access   */
  379.    unsigned w_date;       /* date of last write    */
  380.    unsigned w_time;       /* time of last write    */
  381.    };
  382.  
  383. static struct FileTimeBuf TimeBuf;
  384.  
  385. struct FileInfo {
  386.    USHORT hDir;
  387.    char rsvd[19];
  388.    char attrib;
  389.    unsigned wr_date;
  390.    unsigned wr_time;
  391.    long size;
  392.    char name[13];
  393.    char nill;
  394.    };
  395.  
  396. struct FileFindBuf {
  397.         unsigned create_date;           /* date of file creation */
  398.         unsigned create_time;           /* time of file creation */
  399.         unsigned access_date;           /* date of last access */
  400.         unsigned access_time;           /* time of last access */
  401.         unsigned wr_date;               /* date of last write */
  402.         unsigned wr_time;               /* time of last write */
  403.         unsigned long size;             /* file size (end of data) */
  404.         unsigned long falloc_size;      /* file allocated size */
  405.         unsigned attrib;                /* attributes of the file */
  406.         unsigned char string_len;       /* returned length of ascii name str. */
  407.                                         /* length does not include null byte */
  408.         char name[255];                 /* name string */
  409.         };
  410.  
  411. static struct FileFindBuf InfoBuf;
  412.  
  413. /*--------------------------------------------------------------------------*/
  414. /* Locally defined globals                                                  */
  415. /*--------------------------------------------------------------------------*/
  416.  
  417. struct FileInfo *dtap;
  418. HDIR             hDirA;
  419. USHORT           cSearch;
  420. USHORT           usAttrib;
  421.  
  422. /*--------------------------------------------------------------------------*/
  423. /* Local constants                                                          */
  424. /*--------------------------------------------------------------------------*/
  425.  
  426. #define FILENAMELEN 13
  427.  
  428. int dir_findfirst(char far * filename, int attribute, struct FILEINFO * dta)
  429. {
  430.  
  431.     cSearch  = 1;
  432.     dtap     = (struct FileInfo *)dta;
  433.     hDirA    =  dtap->hDir;
  434.     usAttrib = attribute;
  435.  
  436.     if ((hDirA != 0xffff) && (hDirA != 0x0000))
  437.         (void) DosFindClose( hDirA);
  438.  
  439.     hDirA = 0xffff;
  440.  
  441.     if ((DosFindFirst( filename
  442.                  , &hDirA
  443.                  , usAttrib
  444.                  , (PFILEFINDBUF) &InfoBuf
  445.                  , (USHORT) (sizeof(InfoBuf) * cSearch)
  446.                  , &cSearch
  447.                  , (ULONG) NULL) != 0) || (cSearch != 1))
  448.     {
  449.         (void) DosFindClose( hDirA);
  450.         dtap->hDir = 0xffff;
  451.         errno = ENOENT;
  452.         return (1);
  453.     } else {
  454.         dtap->wr_date   = InfoBuf.wr_date;
  455.         dtap->wr_time   = InfoBuf.wr_time;
  456.         dtap->attrib    = (char) InfoBuf.attrib;
  457.         dtap->size      = InfoBuf.size / ((_osmajor == 10) ? 2 : 1);
  458.         strcpy( dtap->name, InfoBuf.name);
  459.         dtap->hDir      = hDirA;
  460.         errno = 0;
  461.         return (0);
  462.     }
  463. }
  464.  
  465.  
  466. int dir_findnext(struct FILEINFO * dta)
  467. {
  468.  
  469.     cSearch        = 1;
  470.     dtap           = (struct FileInfo *)dta;
  471.     hDirA          = dtap->hDir;
  472.  
  473.     if ((DosFindNext( hDirA
  474.                 , (PFILEFINDBUF) &InfoBuf
  475.                 , (USHORT) (sizeof(InfoBuf) * cSearch)
  476.                 , &cSearch) != 0) || (cSearch != 1))
  477.     {
  478.         (void) DosFindClose( hDirA);
  479.         dtap->hDir = 0xffff;
  480.         errno      = ENOENT;
  481.         return (1);
  482.     } else {
  483.         dtap->wr_date   = InfoBuf.wr_date;
  484.         dtap->wr_time   = InfoBuf.wr_time;
  485.         dtap->attrib    = (char) InfoBuf.attrib;
  486.         dtap->size      = InfoBuf.size / ((_osmajor == 10) ? 2 : 1);
  487.         strcpy( dtap->name, InfoBuf.name);
  488.         dtap->hDir      = hDirA;
  489.         errno = 0;
  490.         return (0);
  491.     }
  492. }
  493.  
  494. void set_fileinfo(int fh, unsigned date, unsigned time)
  495. {
  496.  
  497.     if ((time/2048) < (unsigned)(timezone/3600L)) {
  498.         TimeBuf.a_date = TimeBuf.w_date = TimeBuf.c_date = date-1;
  499.         TimeBuf.c_time = time+((unsigned)(86400-((unsigned)(timezone/3600L)*2048)));
  500.         TimeBuf.a_time = TimeBuf.w_time = TimeBuf.c_time;
  501.     } else {
  502.         TimeBuf.a_date = TimeBuf.w_date = TimeBuf.c_date = date;
  503.         TimeBuf.c_time = time-((unsigned)(timezone/3600L)*2048);
  504.         TimeBuf.a_time = TimeBuf.w_time = TimeBuf.c_time;
  505.     }
  506.     (void) DosSetFileInfo((HFILE) fh,
  507.                         (USHORT) 1,
  508.                         (PBYTE) &TimeBuf,
  509.                         (USHORT) 12);
  510. }
  511.  
  512. #endif /* ifdef OS_2 */
  513.  
  514.