home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / FFIND.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  10KB  |  425 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. /*# name=Portable file-searching hooks
  25.     credit=Thanks go to Peter Fitzsimmons for these routines.
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "compiler.h"
  32.  
  33. #ifndef __IBMC__
  34. #include <dos.h>
  35. #endif
  36.  
  37. #ifdef __TURBOC__
  38. #include <dir.h>
  39. #endif
  40.  
  41. #include "ffind.h"
  42.  
  43. #if defined(OS_2)
  44.   #define INCL_NOPM
  45.   #define INCL_DOS
  46.   #include <os2.h>
  47.  
  48.   #ifdef __FLAT__
  49.     static void near CopyFBUF2FF(FFIND *ff, FILEFINDBUF3 *findbuf)
  50.   #else
  51.     static void near CopyFBUF2FF(FFIND *ff, FILEFINDBUF *findbuf)
  52.   #endif
  53.   {
  54.     ff->usAttr=findbuf->attrFile;
  55.     ff->ulSize=findbuf->cbFile;
  56.  
  57.     ff->scWdate.dos_st.time=*((USHORT *)&findbuf->ftimeLastWrite);
  58.     ff->scWdate.dos_st.date=*((USHORT *)&findbuf->fdateLastWrite);
  59.  
  60.     ff->scCdate.dos_st.time=*((USHORT *)&findbuf->ftimeCreation);
  61.     ff->scCdate.dos_st.date=*((USHORT *)&findbuf->fdateCreation);
  62.  
  63.     ff->scAdate.dos_st.time=*((USHORT *)&findbuf->ftimeLastAccess);
  64.     ff->scAdate.dos_st.date=*((USHORT *)&findbuf->fdateLastAccess);
  65.  
  66.     if (!ff->scCdate.ldate)
  67.       ff->scCdate.ldate=ff->scWdate.ldate;
  68.  
  69.     if (!ff->scAdate.ldate)
  70.       ff->scAdate.ldate=ff->scWdate.ldate;
  71.  
  72.     strncpy(ff->szName, findbuf->achName, sizeof(ff->szName));
  73.   }
  74.  
  75.   /* Find first file in list */
  76.  
  77.   FFIND * _fast FindOpen(char *filespec, unsigned short attribute)
  78.   {
  79.     FFIND *ff;
  80.  
  81.     ff=malloc(sizeof(FFIND));
  82.  
  83.     if (ff)
  84.     {
  85.       #ifdef __FLAT__ /* OS/2 2.0 */
  86.         ULONG usSearchCount=1;
  87.         FILEFINDBUF3 findbuf;
  88.       #else
  89.         USHORT usSearchCount = 1;
  90.         FILEFINDBUF findbuf;
  91.       #endif
  92.  
  93.  
  94.       ff->hdir=HDIR_CREATE;
  95.  
  96.       #ifdef __FLAT__
  97.         if (DosFindFirst(filespec, &ff->hdir, attribute, &findbuf,
  98.                          sizeof findbuf, &usSearchCount, FIL_STANDARD)==0)
  99.       #else
  100.         if (DosFindFirst(filespec, &ff->hdir, attribute, &findbuf,
  101.                          sizeof findbuf, &usSearchCount, 0L)==0)
  102.       #endif
  103.         {
  104.           CopyFBUF2FF(ff, &findbuf);
  105.         }
  106.         else
  107.         {
  108.           free(ff);
  109.           ff=NULL;
  110.         }
  111.     }
  112.  
  113.     return ff;
  114.   }
  115.  
  116.   int _fast FindNext(FFIND *ff)
  117.   {
  118.     int rc=-1;
  119.  
  120.     if (ff)
  121.     {
  122.       #ifdef __FLAT__
  123.         ULONG usSearchCount=1;
  124.         FILEFINDBUF3 findbuf;
  125.       #else
  126.         USHORT usSearchCount=1;
  127.         FILEFINDBUF findbuf;
  128.       #endif
  129.  
  130.       if (ff->hdir &&
  131.           DosFindNext(ff->hdir, &findbuf, sizeof findbuf, &usSearchCount)==0)
  132.       {
  133.         CopyFBUF2FF(ff, &findbuf);
  134.         rc=0;
  135.       }
  136.     }
  137.  
  138.     return rc;
  139.   }
  140.  
  141.   void _fast FindClose(FFIND *ff)
  142.   {
  143.     if (ff)
  144.     {
  145.       if (ff->hdir)
  146.         DosFindClose(ff->hdir);
  147.  
  148.       free(ff);
  149.     }
  150.   }
  151.  
  152.   /* This function was added because it is SIGNIFICANTLY faster under OS/2 to
  153.    * call DosQPathInfo() rather than DosFindFirst(),   if all you are
  154.    * intested in is getting a specific file's date/time/size.
  155.    *
  156.    *PLF Thu  10-17-1991  18:12:37
  157.    */
  158.  
  159.   FFIND * _fast FindInfo(char *filespec)
  160.   {
  161.     FFIND *ff;
  162.     FILESTATUS fs;
  163.     char *f;
  164.  
  165.     ff = malloc(sizeof(*ff));
  166.     if(!ff)
  167.         return NULL;
  168.  
  169.     memset(ff, 0, sizeof(*ff));
  170.  
  171.   #ifdef __FLAT__
  172.     if (DosQueryPathInfo(filespec, FIL_STANDARD, (PBYTE)&fs, sizeof(fs))==0)
  173.   #else
  174.     if (DosQPathInfo(filespec, FIL_STANDARD, (PBYTE)&fs, sizeof(fs), 0L)==0)
  175.   #endif
  176.     {
  177.       ff->usAttr=fs.attrFile;
  178.       ff->ulSize=fs.cbFile;
  179.  
  180.       ff->scWdate.dos_st.time=*((USHORT *)&fs.ftimeLastWrite);
  181.       ff->scWdate.dos_st.date=*((USHORT *)&fs.fdateLastWrite);
  182.  
  183.       ff->scCdate.dos_st.time=*((USHORT *)&fs.ftimeCreation);
  184.       ff->scCdate.dos_st.date=*((USHORT *)&fs.fdateCreation);
  185.  
  186.       ff->scAdate.dos_st.time=*((USHORT *)&fs.ftimeLastAccess);
  187.       ff->scAdate.dos_st.date=*((USHORT *)&fs.fdateLastAccess);
  188.  
  189.  
  190.  
  191.       if ((f=strrchr(filespec, '\\'))==NULL)
  192.         f=filespec;
  193.       else f++;
  194.  
  195.       strncpy(ff->szName, f, sizeof(ff->szName));
  196.     }
  197.     else
  198.     {
  199.       free(ff);
  200.       return NULL;
  201.     }
  202.  
  203.     return ff;
  204.   }
  205.  
  206. #elif defined(NT)
  207.  
  208.   static void near CopyWFD2FF(FFIND *ff, WIN32_FIND_DATA *pwfd)
  209.   {
  210.     ff->usAttr=pwfd->dwFileAttributes;
  211.     ff->ulSize=pwfd->nFileSizeLow;
  212.  
  213.     FileTimeToDosDateTime(&pwfd->ftLastWriteTime,
  214.                           &ff->scWdate.dos_st.date,
  215.                           &ff->scWdate.dos_st.time);
  216.  
  217.     FileTimeToDosDateTime(&pwfd->ftCreationTime,
  218.                           &ff->scCdate.dos_st.date,
  219.                           &ff->scCdate.dos_st.time);
  220.  
  221.     FileTimeToDosDateTime(&pwfd->ftLastAccessTime,
  222.                           &ff->scAdate.dos_st.date,
  223.                           &ff->scAdate.dos_st.time);
  224.  
  225.     strncpy(ff->szName, pwfd->cFileName, sizeof(ff->szName));
  226.   }
  227.  
  228.   FFIND * _fast FindOpen(char *filespec, unsigned short attribute)
  229.   {
  230.     FFIND *ff;
  231.  
  232.     NW(attribute);
  233.  
  234.     ff=malloc(sizeof(FFIND));
  235.  
  236.     if (ff)
  237.     {
  238.       WIN32_FIND_DATA wfd;
  239.  
  240.       if ((ff->hdir=FindFirstFile(filespec, &wfd)) != INVALID_HANDLE_VALUE)
  241.         CopyWFD2FF(ff, &wfd);
  242.       else
  243.       {
  244.         free(ff);
  245.         ff=NULL;
  246.       }
  247.     }
  248.  
  249.     return ff;
  250.   }
  251.  
  252.   int _fast FindNext(FFIND *ff)
  253.   {
  254.     int rc=-1;
  255.  
  256.     if (ff)
  257.     {
  258.       WIN32_FIND_DATA wfd;
  259.  
  260.       if (ff->hdir != INVALID_HANDLE_VALUE && FindNextFile(ff->hdir, &wfd))
  261.       {
  262.         CopyWFD2FF(ff, &wfd);
  263.         rc=0;
  264.       }
  265.     }
  266.  
  267.     return rc;
  268.   }
  269.  
  270.   void _fast FndClose(FFIND *ff)
  271.   {
  272.     if (ff)
  273.     {
  274.       #undef FindClose
  275.  
  276.       if (ff->hdir != INVALID_HANDLE_VALUE)
  277.         FindClose(ff->hdir);
  278.  
  279.       free(ff);
  280.     }
  281.   }
  282.  
  283.   FFIND * _fast FindInfo(char *filespec)
  284.   {
  285.     return FindOpen(filespec, 0);
  286.   }
  287.  
  288. #elif defined(__MSDOS__)
  289.  
  290.   static void near CopyDTA2FF(FFIND *ff)
  291.   {
  292.     ff->usAttr=(word)ff->__dta.bAttr;
  293.  
  294.     ff->scCdate.dos_st.time=ff->__dta.usTime;
  295.     ff->scCdate.dos_st.date=ff->__dta.usDate;
  296.  
  297.     /* Copy dates into other structure too */
  298.  
  299.     ff->scAdate=ff->scWdate=ff->scCdate;
  300.     ff->ulSize=ff->__dta.ulSize;
  301.  
  302.     memset(ff->szName, '\0', sizeof(ff->szName));
  303.     memmove(ff->szName, ff->__dta.achName, sizeof(ff->szName));
  304.     strupr(ff->szName);
  305.   }
  306.  
  307.   FFIND * _fast FindOpen(char *filespec, unsigned short attribute)
  308.   {
  309.     FFIND *ff;
  310.  
  311.     ff=malloc(sizeof(FFIND));
  312.  
  313.     if (ff)
  314.     {
  315.       if (__dfindfirst(filespec, attribute, &ff->__dta)==0)
  316.         CopyDTA2FF(ff);
  317.       else
  318.       {
  319.         free(ff);
  320.         ff=NULL;
  321.       }
  322.     }
  323.  
  324.     return ff;
  325.   }
  326.  
  327.  
  328.   int _fast FindNext(FFIND *ff)
  329.   {
  330.     int rc=-1;
  331.  
  332.     if (ff)
  333.     {
  334.       if ((rc=__dfindnext(&ff->__dta))==0)
  335.         CopyDTA2FF(ff);
  336.     }
  337.  
  338.     return rc;
  339.   }
  340.  
  341.   void _fast FindClose(FFIND *ff)
  342.   {
  343.     if (ff)
  344.       free(ff);
  345.   }
  346.  
  347.   FFIND * _fast FindInfo(char *filespec)
  348.   {
  349.     return FindOpen(filespec, 0);
  350.   }
  351.  
  352.  
  353. #else
  354.   #error Unknown OS
  355. #endif
  356.  
  357.  
  358.  
  359.  
  360. #ifdef TEST_SHELL
  361.  
  362. #define TRUE 1
  363. #define FALSE 0
  364.  
  365. int walk(char *path);
  366.  
  367. void main(int argc, char **argv)
  368. {
  369.     walk("\\");     /* start at root*/
  370. }
  371.  
  372. /* this simple function assumes the path ALWAYS has an ending backslash */
  373. walk(char *path)
  374. {
  375.     FFIND *ff;
  376.     int done = FALSE;
  377.     char full[66];
  378.  
  379.     strcpy(full, path);
  380.     strcat(full, "*");
  381.     if( ff = FindOpen(full, ATTR_SUBDIR) ){
  382.  
  383.         for(done = FALSE; !done; done = FindNext(ff)){
  384.             if( (ff->usAttr & ATTR_SUBDIR) && (ff->szName[0] != '.') ){
  385.                 strcpy(full, path);
  386.                 strcat(full, ff->szName);
  387.                 puts(full);
  388.  
  389.                 {
  390.                   char temp[120];
  391.                   FFIND *f;
  392.  
  393.                   strcpy(temp, full);
  394.                   strcat(temp, "\\*");
  395.  
  396.                   if ((f=FindOpen(temp, 0)) != NULL)
  397.                   {
  398.                     do
  399.                     {
  400.                       printf("\t%s\n", f->szName);
  401.                     }
  402.                     while (FindNext(f)==0);
  403.  
  404.                     FindClose(f);
  405.                   }
  406.  
  407.                 }
  408.  
  409.                 strcat(full, "\\");
  410.                 if( !walk(full) )
  411.                     return(FALSE);
  412.             }
  413.         }
  414.         FindClose(ff);
  415.         return(TRUE);
  416.     }
  417.     else{
  418.         puts("FindOpen() failed");
  419.     }
  420.     return(FALSE);
  421. }
  422.  
  423. #endif
  424.  
  425.