home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / asmutl / make-ds.lbr / OSDATE.CQ / OSDATE.C
Encoding:
C/C++ Source or Header  |  1986-09-05  |  4.5 KB  |  165 lines

  1. /* File OSDATE.C BDS-C Ver 1.1  04May85 */
  2. #include <bdscio.h>
  3. #include "make.h"
  4.  
  5. #define SEARCH_FIRST    17
  6. /*
  7.  * osdate - return the DATSTAMP create or modify date for file.
  8.  * returns 0 for okay, -1 if no date.
  9.  *
  10.  */
  11. int osdate(fs, date)
  12. char *fs;        /* Input filespec. */
  13. char date[5];        /* The returned date. */
  14. {
  15.     int fsun;    /* User number on filespec. */
  16.     char fsdisk;    /* Disk on filespec. (ascii) */
  17.     char fe[20];    /* Filename.ext of filespec (+ some safe space). */
  18.     char fcb[36];
  19.     int curun;    /* Current user number. */
  20.     char curdisk;    /* Current disk. */
  21.     char e_o;    /* Value returned by search call. */
  22.     int e_r_n;    /* Entry record number returned from DATSTAMP. */
  23.     int error;    /* Return value from getfdate(). */
  24.  
  25.     curun=bdos(32, 0xFF);        /* Get current user number. */
  26.     curdisk=bdos(25)+'A';        /* Get current disk. */
  27.  
  28.     crackudf(fs, &fsun, &fsdisk, fe);
  29.     if (fsun == -1) fsun=curun;
  30.     if (fsdisk == '\0') fsdisk=curdisk;
  31.  
  32.     bdos(26, BASE+0x80);    /* Go set DMA addr to BASE + 0x80. */
  33.     setfcb(fcb, fe);
  34.  
  35.     bdos(14, fsdisk-'A');    /* Go select desired disk. */
  36.     bdos(32, fsun);        /* Select the desire user number. */
  37.     e_o=bdosde(SEARCH_FIRST, fcb, &e_r_n);
  38.     if(e_o != 255) error=getfdate(e_r_n, e_o, date, fsdisk-'A');
  39.     else error=-1;
  40.  
  41.     bdos(14, curdisk-'A');    /* Restore current disk. */
  42.     bdos(32, curun);    /* Restore current user number. */
  43.     return error;        /* Return the correct value. */
  44. }
  45.  
  46. /* crackudf - crack a filespec into user#, disk, filename.ext.
  47.  * I accept two formats [u/][d:]file.ext or
  48.  *                      [d[u]:]file.ext. Actually I do this a little dirty so
  49.  * that u/du:file.ext will work and the "u/" part is essentially ignored.
  50.  */
  51. crackudf(fs,un,disk,fe)
  52. char *fs;        /* File spec string. */
  53. int *un;        /* Returned user #, -1 for none or out of range. */
  54. char *disk;        /* Disk letter, \0 for none. */
  55. char *fe;        /* Returned file.ext string. */
  56. {
  57.     *un=-1;        /* Default none. */
  58.     *disk='\0';    /* Default none. */
  59.  
  60. /* Check for "n/" or "nn/". */
  61.     if (isdigit(*fs)) {            /* Started with a number. */
  62.         if (isdigit(*(fs+1))) {        /* A 2nd number. */
  63.             if (*(fs+2) == '/') {    /* "nn/" form. */
  64.                 *un=(*fs++ - '0')*10;
  65.                 *un += (*fs++ - '0');
  66.                 fs++;        /* Move past '/'. */
  67.             }
  68.         }
  69.         else if (*(fs+1) == '/') {    /* "n/" form. */
  70.             *un=*fs++ - '0';
  71.             fs++;            /* Move past '/'. */
  72.         }
  73.     }
  74.  
  75. /* Check for "d[nn]:" form. */
  76.     if (isalpha(*fs)) {
  77.         if (*(fs+1) == ':') {        /* "d:" form. */
  78.             *disk=toupper(*fs++);
  79.             fs++;            /* Move past ':'. */
  80.         }
  81.         else if (isdigit(*(fs+1))) {
  82.             if (*(fs+2) == ':') {    /* "dn:" form. */
  83.                 *disk=toupper(*fs++);
  84.                 *un=*fs++ - '0';
  85.                 fs++;        /* Move past ':'. */
  86.             }
  87.             else if (isdigit(*(fs+2)) && *(fs+3) == ':') {
  88.                         /* "dnn:" form. */
  89.                 *disk=toupper(*fs++);
  90.                 *un=(*fs++ - '0')*10;
  91.                 *un += (*fs++ -'0');
  92.                 fs++;        /* Move past ':'. */
  93.             }
  94.         }
  95.     }
  96.     if(*un > 31) *un=-1;
  97.  
  98. /* Now just copy the rest of the string. */
  99.     while (*fs) *fe++=*fs++;
  100.     *fe='\0';        /* Terminate fe[]. */
  101. }
  102.  
  103. getfdate(e_r_n, e_o, fdate, disk)
  104. int e_r_n;
  105. char e_o;
  106. char fdate[5];
  107. int    disk;
  108. {
  109. /* v 1.1 read first disk's entire time&date file to buffer */
  110.     int i, fd, un, offset;
  111.     unsigned sects;
  112.     char bad, *ptr, *tmp;
  113.     char    localbuff[128];
  114.  
  115.     bad = FALSE;
  116.  
  117.     if(e_o < 0 || e_o > 3)    return -1;
  118.  
  119.     /* Set fdate to 0 in case of error. */
  120.     for (i=0; i<5; i++)    fdate[i] = 0;
  121.  
  122.     /* on first call, read entire time&date file to buffer */
  123.     if (!havedates || datesdisk != disk) {
  124.         un=bdos(32, 0xff); bdos(32, 0);    /* Point to user 0 */
  125.     
  126.         if ((fd=open("!!!TIME&.DAT", 0)) == -1) {
  127.              bdos(32, un); return -1;}
  128.         if (datesdisk == -1) {
  129.             sects = cfsize(fd);
  130.             if ( (datebuff = alloc(128*sects) ) == 0){
  131.                 bdos(32, un);    allerr(); }
  132.             bad =  (read(fd, datebuff, sects) != sects);
  133.             havedates = TRUE;
  134.             datesdisk = disk;    /* save disk of this file*/
  135.             /* allocate, then free a large chunk so 
  136.                individual calls won't cause a sbrk() */
  137.             if ( tmp = alloc(128*8) )
  138.                 free(tmp);
  139.             }
  140.         else    {
  141.             if (seek(fd, e_r_n/2, 0) == -1 ||
  142.                 read(fd, localbuff, 1) != 1)
  143.                     bad = TRUE;
  144.             }
  145.         close(fd); bdos(32, un);
  146.         if (bad)
  147.             return -1;
  148.         }
  149.  
  150.     /* if initial disk, point at file's date slot t&d file buffer */
  151.     /* otherwise, into just-read sector */
  152.  
  153.     offset =  16*(4*(e_r_n & 1) + e_o);
  154.     ptr =  (datesdisk == disk) ?    datebuff  + 128*(e_r_n/2) + offset :
  155.                     localbuff + offset;
  156.  
  157.     /* If a modify date use that as the file date. */
  158.     if (ptr[10]) ptr += 10;
  159.  
  160.     for (i=0; i<5; i++) fdate[i] = *ptr++;
  161.     return 0;
  162. }
  163. 
  164.     
  165.         if ((fd=o