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