home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / OS2.C < prev    next >
C/C++ Source or Header  |  1991-07-14  |  6KB  |  202 lines

  1. /* os2.c */
  2.  
  3. /* Highly system-dependent routines go here */
  4.  
  5. #include "options.h"
  6. #include "zoo.h"
  7. #include "zooio.h"        /* to satisfy declarations in zoofns.h */
  8. #include "zoofns.h"
  9. #include "errors.i"
  10. #include <stdio.h>        /* to get fileno() */
  11. #include <stdlib.h>
  12.  
  13. #define INCL_NOPM
  14. #define INCL_DOS
  15. #define INCL_DOSERRORS
  16. #include <os2.h>
  17.  
  18. #include "assert.h"     /* macro definition:  assert() macro            */
  19.  
  20. /* settime() */
  21.  
  22. /* Accepts a date/time in DOS format and sets the file time. Returns 1
  23. if OK, 0 if error */
  24.  
  25. int settime (file,date,time)
  26. ZOOFILE file;
  27. unsigned date, time;
  28. {
  29.     FILESTATUS info;
  30.     HFILE handle = fileno(file);
  31.  
  32.     fflush(file);
  33.     DosQFileInfo(handle, 1, &info, sizeof(info));
  34.     *((unsigned *)&info.fdateLastWrite) = date;
  35.     *((unsigned *)&info.ftimeLastWrite) = time;
  36.     return DosSetFileInfo(handle, 1, (PBYTE) &info, sizeof(info)) == 0;
  37. } /* settime */
  38.  
  39. /* gets date and time of file */
  40.  
  41. gettime (file,date,time)
  42. ZOOFILE file;
  43. unsigned *date, *time;
  44. {
  45.     FILESTATUS info;
  46.     HFILE handle = fileno(file);
  47.     int ret;
  48.  
  49.     ret = DosQFileInfo(handle, 1, &info, sizeof(info));
  50.     *date = *((unsigned *) &info.fdateLastWrite);
  51.     *time = *((unsigned *) &info.ftimeLastWrite);
  52.     return ret == 0;
  53. } /* settime */
  54.  
  55.  
  56. /* space() */
  57.  
  58. /* Returns free space in bytes on disk n (0 = default, 1 = A, etc.).  Returns
  59.     0 if drive number is invalid.  Before getting disk space, the function
  60.     requests DOS to flush its internal buffers */
  61.  
  62. unsigned long space (drive, alloc_size)
  63. int drive;
  64. int *alloc_size;
  65. {
  66.     unsigned long free_space;
  67.     FSALLOCATE info;
  68.     int ret;
  69.  
  70.     DosBufReset(-1);
  71.     ret = DosQFSInfo(drive, 1, (BYTE FAR *)&info, sizeof(info));
  72.     *alloc_size = info.cbSector * (int) info.cSectorUnit;
  73.     return ret ? 0L : (unsigned long) (info.cUnitAvail * *alloc_size);
  74. }
  75.  
  76. int IsFileNameValid(char *name)
  77. {
  78.   HFILE hf;
  79.   USHORT usAction;
  80.  
  81.   switch( DosOpen(name, &hf, &usAction, 0L, 0, FILE_OPEN,
  82.                   OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0L) )
  83.   {
  84.   case ERROR_INVALID_NAME:
  85.   case ERROR_FILENAME_EXCED_RANGE:
  86.     return FALSE;
  87.   case NO_ERROR:
  88.     DosClose(hf);
  89.   default:
  90.     return TRUE;
  91.   }
  92. }
  93.  
  94.  
  95. int IsFileSystemDumb(char *dir)
  96. {                         
  97.   char drive[5], path[256], name[256];
  98.   
  99.   _splitpath(dir, drive, path, NULL, NULL);
  100.   _makepath(name, drive, path, ".DUMB.TEST.NAME", NULL);
  101.   
  102.   return !IsFileNameValid(name);
  103. }
  104.  
  105.  
  106. #define  FMAX  3        /* Number of different filename patterns */
  107.  
  108. void fcbpath (FILEFINDBUF *, char *, char *);
  109.  
  110. char *nextfile (what, filespec, fileset)
  111. int what;                        /* whether to initialize or match      */
  112. register char *filespec;         /* filespec to match if initializing   */
  113. register int fileset;            /* which set of files                  */
  114. {
  115.    static FILEFINDBUF file_info [FMAX+1];     /* our own private dta        */
  116.    static HDIR handles [FMAX+1];
  117.    static int first_time [FMAX+1];
  118.    static char pathholder [FMAX+1][PATHSIZE]; /* holds a pathname to return */
  119.    static char saved_fspec [FMAX+1][PATHSIZE];/* our own copy of filespec   */
  120.    static int lower;
  121.    USHORT count;
  122.    int ret;
  123.  
  124.    assert(fileset >= 0 && fileset <= FMAX);
  125.    if (what == 0) {
  126.       assert(filespec != NULL);
  127.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  128.       first_time[fileset] = 1;
  129.       return (NULL);
  130.    }
  131.  
  132.    assert(what == 1);
  133.    assert(filespec == NULL);
  134.    assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  135.  
  136.    if (first_time[fileset]) {             /* first time -- initialize etc. */
  137.       /* find first matching file */
  138.       count = 1;
  139.       lower = IsFileSystemDumb(saved_fspec[fileset]);
  140.       handles[fileset] = 0xffff;      /* Get new handle */
  141.       ret = DosFindFirst(saved_fspec[fileset], &handles[fileset], 0,
  142.                  &file_info[fileset], sizeof(FILEFINDBUF), &count, 0L);
  143.    } else {
  144.       /* find next matching file */
  145.       count = 1;
  146.       ret = DosFindNext(handles[fileset], &file_info[fileset], sizeof(FILEFINDBUF),
  147.                 &count);
  148.    }
  149.  
  150.    if (ret != 0 || count != 1) {            /* if error status                  */
  151.       if (first_time[fileset]) {       /*   if file never matched then     */
  152.          first_time[fileset] = 0;
  153.          return (saved_fspec[fileset]);/*      return original filespec    */
  154.       } else {                         /*   else                           */
  155.          first_time[fileset] = 0;      /*                                  */
  156.          return (NULL);                /*      return (NULL) for no more   */
  157.       }
  158.    } else {                                        /* a file matched */
  159.       first_time[fileset] = 0;
  160.       /* add path info  */
  161.       if ( lower )
  162.         strlwr(file_info[fileset].achName);
  163.       fcbpath (&file_info[fileset], saved_fspec[fileset], pathholder[fileset]);
  164.       return (pathholder[fileset]);                /* matching path  */
  165.    }
  166. } /* nextfile */
  167.  
  168. /*
  169. fcbpath() accepts a pointer to the FILEFINDBUF, a
  170. character pointer to a pathname that may contain wildcards, and a character
  171. pointer to a buffer.  Copies into buffer the path prefix from the pathname
  172. and the filename prefix from the DTA so that it forms a complete path
  173. */
  174.  
  175. void fcbpath (info, old_path, new_path)
  176. FILEFINDBUF *info;
  177. char *old_path;
  178. register char *new_path;
  179. {
  180.    register int i;
  181.    int length, start_pos;
  182.  
  183.    strcpy(new_path, old_path);               /* copy the whole thing first */
  184.    length = strlen(new_path);
  185.    i = length - 1;                           /* i points to end of path */
  186.    while (i >= 0 && new_path[i] != '/' && new_path[i] != '\\' && new_path[i] != ':')
  187.       i--;
  188.    /* either we found a "/", "\", or ":", or we reached the beginning of
  189.       the name.  In any case, i points to the last character of the
  190.       path part. */
  191.    start_pos = i + 1;
  192.    for (i = 0; info->achName[i]; i++)
  193.       new_path[start_pos+i] = info->achName[i];
  194.    new_path[start_pos+i] = '\0';
  195. }
  196.  
  197.  
  198. int outchar(int c)
  199. {
  200.   putchar(c);
  201. }
  202.