home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / directry / mv / fstat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  4.9 KB  |  188 lines

  1. /*
  2.  8-Sep-86 16:13:36-PDT,4915;000000000000
  3. Return-Path: <pwu@unix.macc.wisc.edu>
  4. Received: FROM UNIX.MACC.WISC.EDU BY B.ISI.EDU WITH TCP ; 8 Sep 86 16:10:13 PDT
  5. Received: by unix.macc.wisc.edu;
  6.           id AA04962; 4.12/5; Mon, 8 Sep 86 17:31:25 cdt
  7. Date: Mon, 8 Sep 86 17:31:25 cdt
  8. From: Peter Wu <pwu@unix.macc.wisc.edu>
  9. Message-Id: <8609082231.AA04962@unix.macc.wisc.edu>
  10. To: info-ibmpc-request@mosis
  11. Subject: fstat.c
  12. */
  13. /* find first matching file and find next matching file
  14. ** written by Peter Wu @ Faculty Support Center @ Univ. of Wisconsin
  15. ** July 1986
  16. **
  17. ** These functions have an extended capability to search for
  18. ** directories only and ffmf does not return "." and "..", also
  19. ** ffmf will attempt to find root directory and return at least drive number
  20. */
  21. #define LINT_ARGS
  22.  
  23. #include "dta.h"
  24. #include <dos.h>
  25. #include <stdlib.h>
  26.  
  27. char lastc(char *);
  28.  
  29. ffmf(fn,attr,dta)  /* extended version of ffmf1; fn must be normalized */
  30. char *fn;
  31. short attr;
  32. union dtbuf *dta;
  33. {
  34.   int status, len;
  35.   char tmpfn[200];
  36.   union dtbuf tmpdta;
  37.  
  38.   if (lastc(fn) != '\\') {  /* if not looking for root directory */
  39.  
  40.     status = ffmf1(fn,attr,dta);
  41.     if (status) {
  42.       return status;
  43.     }
  44.  
  45.     /* get rid of "." and ".." */
  46.     while (dta->dos.fn[0] == '.') {
  47.       status = fnmf1(dta);
  48.       if (status) {
  49.         return status;
  50.       }
  51.     }
  52.  
  53.     /* now check to see if we should return plain files or not */
  54.     while ( ((dta->dos.e_attr & A_FIL) == 0) &&
  55.             ((dta->dos.attr & A_DIR) == 0) ) {
  56.       /* user didn't ask for plain files and we found one, so skip it */
  57.       status = fnmf1(dta);
  58.       if (status) {
  59.         return status;
  60.       }
  61.     }
  62.  
  63.     fixdta(dta,dta);
  64.     return 0;
  65.   }
  66.  
  67.   /* fn ends with '\', i.e. looking for root directory. In DOS 3 ffmf1 won't
  68.   ** find root directory, and in DOS 2 ffmf1 found root directory but reports
  69.   ** it to have file status! Neither is right. So I have to fix it here.
  70.   ** The reason for wanting to look for a root directory is to find out
  71.   ** what drive it's on.
  72.   */
  73.  
  74.   if (attr & A_DIR) {
  75.     /* look for '\*.*' */
  76.     strcpy(tmpfn, fn);
  77.     strcat(tmpfn, "*.*");
  78.     status = ffmf1(tmpfn, A_DIR | A_FIL, &tmpdta);
  79.     if (status) {  /* can't even find root this way: root must be empty */
  80.       cputs("empty root directory; nothing to move\n\015");
  81.       exit(1);
  82.     }
  83.  
  84.     /* found root; now fill in dta */
  85.     dta->dos.fn[0] = '\0';
  86.     dta->dos.attr = A_DIR;
  87.     fixdta(&tmpdta,dta);
  88.     return 0;
  89.   } else {
  90.     return 1; /* can't find root because attr is not set to A_DIR */
  91.   }
  92. }
  93.  
  94. /* extended version of fnmf1; because of the way ffmf handle root directory,
  95. ** calling ffmf to find root directory and then call fnmf will cause an
  96. ** "allocation table bad" error
  97. */
  98. fnmf(dta)
  99. union dtbuf *dta;
  100. {
  101.   int status;
  102.  
  103.   status = fnmf1(dta);
  104.   if (status) {
  105.     return status;
  106.   }
  107.  
  108.   while (((dta->dos.e_attr & A_FIL) == 0) && ((dta->dos.attr & A_DIR) == 0)) {     /* user didn't ask for plain file and we found one, so skip it */
  109.     status = fnmf1(dta);
  110.     if (status) {
  111.       return status;
  112.     }
  113.   }
  114.  
  115.   fixdta(dta,dta);
  116.   return 0;  /* no error */
  117. }
  118.  
  119. /* copy dos dependent dta fields to the dos independent dta fields */
  120. fixdta(from, to)
  121. union dtbuf *from, *to;
  122. {
  123.   switch (_osmajor) {
  124.  
  125.     case 3:  /* dos 3.xx */
  126.       to->dos.drv_no = from->dos3.drv_no;
  127.       to->dos.slotl = from->dos3.slotl;
  128.       to->dos.sloth = from->dos3.sloth;
  129.       to->dos.clusl = from->dos3.clusl;
  130.       to->dos.clush = from->dos3.clush;
  131.  
  132.       to->dos3.drv_no = from->dos3.drv_no;  /* for compatiblity sake */
  133.       break;
  134.  
  135.     case 2:  /* dos 2.xx */
  136.       to->dos.drv_no = from->dos2.drv_no + 1;
  137.       to->dos.slotl = from->dos2.slotl;
  138.       to->dos.sloth = from->dos2.sloth;
  139.       to->dos.clusl = from->dos2.clusl;
  140.       to->dos.clush = from->dos2.clush;
  141.  
  142.       to->dos2.drv_no = from->dos2.drv_no;  /* for compatiblity sake */
  143.       break;
  144.  
  145.     default:
  146.       cputs("unexpected DOS version\n\015");
  147.       error("fixdta", 0);
  148.   }
  149. }
  150.  
  151. ffmf1(fn,attr,dta)  /* don't call this, call ffmf */
  152. char *fn;
  153. short attr;
  154. union dtbuf *dta;
  155. {
  156.   union REGS inregs,outregs;
  157.  
  158.   dta->dos.e_attr = attr;  /* store the extended attribute */
  159.   bdos(0x1a, (int) dta, 0);  /* set dta */
  160.  
  161.   inregs.h.ah = 0x4e;
  162.   inregs.x.dx = (int) fn;
  163.   inregs.x.cx = attr & 0x3f;  /* mask off the A_FIL bit */
  164.   intdos(&inregs, &outregs);  /* now find first entry */
  165.  
  166.   if (outregs.x.cflag) {
  167.     return outregs.x.ax;  /* return error code */
  168.   }
  169.   return 0;  /* no error */
  170. }
  171.  
  172. fnmf1(dta)  /* find next matching file */
  173. union dtbuf *dta;
  174. {
  175.   union REGS inregs,outregs;
  176.  
  177.   bdos(0x1a, (int) dta, 0);  /* set dta */
  178.  
  179.   inregs.h.ah = 0x4f;
  180.   intdos(&inregs, &outregs);  /* now find next entry */
  181.  
  182.   if (outregs.x.cflag) {
  183.     return (outregs.x.ax);
  184.   } else {
  185.     return (0);
  186.   };
  187. }
  188.