home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 163_01 / baseio2.c < prev    next >
Text File  |  1988-01-31  |  6KB  |  211 lines

  1. /*
  2. ** include baseio2.c -- base I/O routines part 2
  3. */
  4.  
  5.  
  6. /*
  7. ** reposition within a file
  8. */
  9.  
  10. _seek(fildes, offset, whence) int fildes, offset, whence; {
  11.   int temp, count;
  12.   char *fcb, adder[4], *from, *to;
  13.   if((whence < 0) | (whence > 5)) {
  14.     errno = EINVAL;
  15.     return -1;
  16.     }
  17.   fcb = _filedes[fildes];
  18.   if((fcb[FCB_FLAG]&255) != 255) {
  19.     errno = ESPIPE;
  20.     return -1;
  21.     }
  22.   if(_iflush(fcb)) return -1;
  23.   if((whence == 0) | (whence == 3)) { /* if seek from front */
  24.     to = &fcb[FCB_RRECNO];
  25.     count = 4;
  26.     while(count--) *to++ = 0;
  27.     }
  28.   if((whence == 2) | (whence == 5)) { /* if seek from end */
  29.     from = &fcb[FCB_FSIZE];
  30.     to = &fcb[FCB_RRECNO];
  31.     count = 4;
  32.     while(count--) *to++ = *from++;
  33.     }
  34.   if(whence < 3) { /* if offset in bytes */
  35.     adder[0] = offset;
  36.     adder[1] = offset >> 8;
  37.     adder[2] = offset >> 15;
  38.     adder[3] = offset >> 15;
  39.     }
  40.   else { /* if offset in 512 byte blocks */
  41.     adder[0] = 0;
  42.     adder[1] = (offset << 1) & 254;
  43.     adder[2] = offset >> 7;
  44.     adder[3] = offset >> 15;
  45.     }
  46.   temp = 0;
  47.   count = 4;
  48.   from = &adder[0];
  49.   to = &fcb[FCB_RRECNO];
  50.   while(count--) { /* return current position */
  51.     temp = (*from++ & 255) + (*to & 255) + (temp >> 8);
  52.     *to++ = temp;
  53.     }
  54.   if(whence < 3) {
  55.     temp = (fcb[FCB_RRECNO+1] << 8) + fcb[FCB_RRECNO];
  56.     }
  57.   else {
  58.     temp = (fcb[FCB_RRECNO+3] << 15) + (fcb[FCB_RRECNO+2] << 7) +
  59.         ((fcb[FCB_RRECNO+1] >> 1) & 127);
  60.     }
  61.   return temp;
  62.   }
  63.  
  64.  
  65. /*
  66. ** set file parameters
  67. */
  68.  
  69. _ioctl(fildes, request, argp) int fildes, request; char *argp; {
  70.   char *fcb, *from, *to;
  71.   int count, *ibuf;
  72.   fcb = _filedes[fildes];
  73.   if(request == TIOCGETP) {
  74.     from = &fcb[FCB_SGTTYB];
  75.     to = argp;
  76.     }
  77.   else if(request == TIOCSETP) {
  78.     from = argp;
  79.     to = &fcb[FCB_SGTTYB];
  80.     ibuf = &fcb[FCB_IBUF];
  81.     if(*ibuf && !(from[SG_FLGS] & 32)) { /* if changing to non-CR-LF conv */
  82.       if(_iflush(fcb)) return -1;
  83.       free(*ibuf);
  84.       *ibuf = 0;
  85.       }
  86.     }
  87.   else {
  88.     errno = EINVAL; /* invalid request code */
  89.     return -1;
  90.     }
  91.   count = SG_SIZE;
  92.   while(count--) *to++ = *from++;
  93.   return 0;
  94.   }
  95.  
  96.  
  97. /*
  98. ** determine if the specified fildes is the console
  99. */
  100.  
  101. _isatty(fildes) int fildes; {
  102.   char *fcb;
  103.   fcb = _filedes[fildes];
  104.   if((fcb[FCB_FLAGS]&255) == 255) return 0;
  105.   if(fcb[FCB_FLAGS]&'\300') return 1;
  106.   return 0;
  107.   }
  108.  
  109.  
  110. /*
  111. ** allocate a DOS FCB and initialize it
  112. */
  113.  
  114. static _getfcb(path) char *path; {
  115.   int i, j, drive;
  116.   char *fcb, *ptr1, *ptr2;
  117.   i = 0;
  118.   while(i < NUMFILES) {
  119.     if(_filedes[i] == 0) { /* if handle is available */
  120.       if((fcb = malloc(FCBSIZE)) == 0) break; /* quit if we can't get space */
  121.       ptr1 = _filedes[i] = fcb; /* record fcb address */
  122.       j = FCBSIZE;
  123.       while(j--) *ptr1++ = 0; /* clear the FCB */
  124.       _parsedos(path, &fcb[FCB_DRIVE]); /* parse the file name */
  125.       /* (relative record number is already 0) */
  126.       /* determine if file is reserved file name */
  127.       if     (_equal(&fcb[FCB_FILE],"kbd     ")) fcb[FCB_FLAG] = 128 + 2;
  128.       else if(_equal(&fcb[FCB_FILE],"scrn    ")) fcb[FCB_FLAG] =  64 + 1;
  129.       else if(_equal(&fcb[FCB_FILE],"con     ")) fcb[FCB_FLAG] = 192 + 3;
  130.       else if(_equal(&fcb[FCB_FILE],"prn     ")) fcb[FCB_FLAG] =  32 + 1;
  131.       else if(_equal(&fcb[FCB_FILE],"async   ")) fcb[FCB_FLAG] =  16 + 3;
  132.       else{
  133.         /* DOS expects upper case file names */
  134.         j = 11;
  135.         ptr1 = &fcb[FCB_FILE];
  136.         while(j--) {
  137.           if((*ptr1 >= 'a') & (*ptr1 <= 'z')) *ptr1 += ('A' - 'a');
  138.           ++ptr1;
  139.           }
  140.         fcb[FCB_FLAG] =  255;
  141.         }
  142.       return i;
  143.       }
  144.     ++i;
  145.     }
  146.   return -1;
  147.   }
  148.  
  149.  
  150. /*
  151. ** deallocate a DOS FCB
  152. */
  153.  
  154. static _freefcb(filedes) int filedes; {
  155.   free(_filedes[filedes]); /* free the FCB */
  156.   _filedes[filedes] = 0; /* free the handle */
  157.   }
  158.  
  159.  
  160. /*
  161. ** parse a DOS file name (but don't upcase it yet)
  162. */
  163.  
  164. static _parsedos(path, target) char *path, *target; {
  165.   /* target includes drive number as first byte */
  166.   /* returns pointer to first byte beyond valid name */
  167.   char *ptr1, *ptr2;
  168.   int drive, j;
  169.   ptr1 = path;
  170.   while(*ptr1 == ' ') ++ptr1; /* scan off leading blanks */
  171.   /* get drive number */
  172.   drive = 0;
  173.   if(ptr1[1] == ':') {
  174.     if((*ptr1 >= 'a') & (*ptr1 <= 'z')) drive = *ptr1 - 'a' + 1;
  175.     if((*ptr1 >= 'A') & (*ptr1 <= 'Z')) drive = *ptr1 - 'A' + 1;
  176.     if(drive) ptr1 += 2;
  177.     }
  178.   *target = drive;
  179.   /* blank the name */
  180.   j = 11;
  181.   ptr2 = target + 1;
  182.   while(j-- > 0) *ptr2++ = ' ';
  183.   /* get the file name */
  184.   j = 11;
  185.   ptr2 = target + 1;
  186.   while(_doschar(*ptr1) & (j-- > 0)) *ptr2++ = *ptr1++;
  187.   /* get the extension */
  188.   if(*ptr1 == '.') {
  189.     ++ptr1; /* skip '.' */
  190.     j = 3;
  191.     ptr2 = target + 9;
  192.     while(j-- > 0) *ptr2++ = ' ';
  193.     j = 3;
  194.     ptr2 = target + 9;
  195.     while(_doschar(*ptr1) & (j-- > 0)) *ptr2++ = *ptr1++;
  196.     }
  197.   return ptr1;
  198.   }
  199.  
  200.  
  201. /*
  202. ** compare file name to reserved name
  203. */
  204.  
  205. static _equal(name1,name2) char *name1, *name2; {
  206.   int count;
  207.   count = 8;
  208.   while(count--) {if(*name1++!=*name2++) return 0;}
  209.   return 1;
  210.   }
  211.