home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / dirutl / fl.arc / FL.C next >
Text File  |  1989-06-29  |  8KB  |  289 lines

  1. /* FL.C  -- FILE LOCATOR by Michael P. Kelly
  2.  
  3.    This program was written in Turbo C v2.0 and will search your current
  4.    disk drive (hard or floppy) for the file(s) that you request.
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10. #include <dir.h>
  11. #include <string.h>
  12. #include <conio.h>
  13. #include <alloc.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <bios.h>
  17. #include <flash.h>
  18.  
  19. #define        DIRS        50        /* max number of subdirectories */
  20. #define        SIZE        13    /* filename.ext length        */
  21. #define        PATHLEN        67    /* max length of path name    */
  22.  
  23. struct ffblk info;                         /* holds file find information  */
  24. char original[PATHLEN];                 /* users original directory    */
  25. char filespec[SIZE];                       /* filespec user is looking for */
  26. int matches = 0;            /* matches of filespec found    */
  27. char *months[] = { "JAN","FEB","MAR","APR","MAY","JUN",
  28.            "JUL","AUG","SEP","OCT","NOV","DEC" };
  29. char *location;
  30. char drive[MAXDRIVE];
  31. char dir[MAXDIR];
  32. char file[MAXFILE];
  33. char ext[MAXEXT];
  34. char test[PATHLEN];
  35.  
  36. void traverse(char *subname);
  37. int getsubdirs(char (*mike)[SIZE]);
  38. void look(char *lostfile);
  39. char *str_2_lower(char *str);
  40. void file_time(unsigned t);
  41. void file_date(unsigned t);
  42. char *justify(char *name);
  43. void userwait(void);
  44. int read_key(void);
  45. int wbuffer;
  46.  
  47. main(int argc, char *argv[])
  48. {
  49.    /* make <filespec> global and get the original directory      */
  50.    strcpy(filespec,argv[1]);
  51.    getcwd(original,PATHLEN-1);
  52.  
  53.    /* if there are no arguments, display the instructions and quit.    */
  54.    if(argc < 2) {
  55.       location = searchpath("FL.DAT");
  56.       fnsplit(location,drive,dir,file,ext);
  57.       if(strcmp("\\",dir) != 0)
  58.      dir[strlen(dir)-1] = '\0';
  59.       chdir(dir);
  60.       if(win_lib("FL.DAT")) {
  61.      printf("\nNo help available since FL.DAT cannot be found.\n");
  62.      printf("\nHINT:  Place FL.EXE and FL.DAT in any of your PATH directories!\n");
  63.      return(0);
  64.       }
  65.       wbuffer = open_win("title",WE_CENTER_OUT,3);
  66.       read_key();
  67.       close_win(wbuffer,WE_SLIDE_DOWN,1);
  68.       wbuffer = open_win("instruct",WE_SLIDE_UP,1);
  69.       read_key();
  70.       close_win(wbuffer,WE_CURTAIN,1);
  71.       wbuffer = open_win("final",WE_CENTER_OUT,1);
  72.       chdir(original);
  73.       return(0);
  74.    }
  75.  
  76.    clrscr();
  77.    printf("\nFL - File Locator by 2LT Michael P. Kelly, (c) 1988-1989.\n");
  78.  
  79.    /* start at users request or the root directory        */
  80.    if(argc == 3)
  81.       traverse(argv[2]);
  82.    else
  83.       traverse("\\");
  84.  
  85.    /* return user to the same directory that he started in.    */
  86.    chdir(original);
  87.    printf("\n\n%d files found meeting the %s criteria.\n",matches,filespec);
  88.  
  89.    return(0);
  90. }
  91.  
  92. void traverse(char *subname)
  93. {
  94.    char (*dir)[SIZE];        /* pointer to an array of SIZE chars    */
  95.    register int total;      /* number of directories found       */
  96.    register int index;      /* counter                       */
  97.    char (*tmp)[SIZE];
  98.  
  99.    /* allocate enough memory and initialize the pointer to it.    */
  100.    dir = malloc(DIRS * SIZE + 1);
  101.    if(dir == NULL) {
  102.       printf("\nERROR:  Failed memory allocation.\n");
  103.       chdir(original);
  104.       exit(1);
  105.    }
  106.    tmp = dir;
  107.  
  108.    /* check to be sure that the directory change worked.    */
  109.    if(chdir(subname) != 0) {
  110.       printf("\n    ERROR:  Directory %s does not exist.\n",subname);
  111.       chdir(original);
  112.       exit(1);
  113.    }
  114.  
  115.    /* get the number & names of subdirectories and check out each one    */
  116.    total = getsubdirs(dir);
  117.    for(index = 0;index < total;dir++,index++)
  118.       traverse((char *)dir);
  119.  
  120.    /* check for <filespec>, free memory, go to previous subdirectory    */
  121.    look(filespec);
  122.    free(tmp);
  123.    chdir("..");
  124. }
  125.  
  126. /*
  127.    Accepts a pointer to an array of SIZE characters, fills the arrays with
  128.    the names of any subdirectories it finds and returns the number found
  129.    to the calling routine.
  130. */
  131. getsubdirs(char (*ptr)[SIZE])
  132. {
  133.    register int count = 0;
  134.  
  135.    findfirst("*.*", &info, FA_DIREC);    /* start looking               */
  136.    if(info.ff_name[0] == '.') {        /* do we have the . subdir?       */
  137.       findnext(&info);                /* yes, get the .. subdir too     */
  138.       if(findnext(&info) != 0)        /* is there another dir?      */
  139.      return(0);                    /* no, so return 0 subdirs here */
  140.    }
  141.  
  142.    if(info.ff_attrib == FA_DIREC) {             /* is this a subdir?    */
  143.       strcpy(*ptr++,info.ff_name);             /* yes, place in array  */
  144.       count++;                                 /* increment counter    */
  145.    }
  146.  
  147.    while(findnext(&info) == 0) {            /* go thru the rest     */
  148.       if(info.ff_attrib == FA_DIREC) {          /* is this a subdir?    */
  149.      strcpy(*ptr++,info.ff_name);       /* yes, place in array  */
  150.      count++;                              /* increment counter    */
  151.       }
  152.    }
  153.  
  154.    return(count);                  /* return number of directories */
  155. }
  156.  
  157. /*
  158.    Check for any file(s) that meet the <filespec> criteria in this
  159.    directory.  If found, print directory, file names and increment
  160.    matches counter.
  161. */
  162. void look(char *lostfile)
  163. {
  164.    char here[PATHLEN];                      /* used to hold pathname  */
  165.  
  166.    if(findfirst(lostfile,&info,0x00) == 0) {
  167.       getcwd(here,PATHLEN-1);
  168.       printf("\n\n%s",here);
  169.       printf("\n\t%s",justify(str_2_lower(info.ff_name)));
  170.       printf("\t%7ld bytes ",info.ff_fsize);
  171.       file_time(info.ff_ftime);
  172.       file_date(info.ff_fdate);
  173.       matches++;
  174.       while(findnext(&info) == 0) {
  175.      printf("\n\t%s",justify(str_2_lower(info.ff_name)));
  176.      printf("\t%7ld bytes ",info.ff_fsize);
  177.      file_time(info.ff_ftime);
  178.      file_date(info.ff_fdate);
  179.      matches++;
  180.       }
  181.    }
  182. }
  183.  
  184. /*
  185.    When passed a pointer to a character string, it will convert the string
  186.    to all uppercase.  It also returns a pointer to that string as well.
  187. */
  188. char *str_2_lower(char *str)
  189. {
  190.    char *tmp;
  191.  
  192.    tmp = str;
  193.    while(*str != '\0')
  194.       *str++ = tolower(*str);
  195.    return(tmp);
  196. }
  197.  
  198. /*
  199.    The unsigned number from the ffblk structure is coded as follows:
  200.  
  201.     HHHHHMMM MMMSSSSS   -- as unsigned int
  202.       c[1]     c[0]     -- as two unsigned characters
  203. */
  204. void file_time(unsigned t)
  205. {
  206.    union twobytes {
  207.       unsigned char c[2];
  208.       unsigned u;
  209.    } f;
  210.    unsigned hours;
  211.    unsigned mins;
  212.    char zone[3] = "am";
  213.  
  214.    f.u = t;
  215.    hours = f.c[1] >> 3;
  216.    mins = ((f.c[1] & 7) << 3) + (f.c[0] >> 5);
  217.    if(hours > 11) {
  218.       strcpy(zone,"pm");
  219.       if(hours > 12)
  220.      hours -= 12;
  221.    }
  222.    if(hours == 0)
  223.       hours = 12;
  224.    printf(" %2u:%02u %s ",hours,mins,zone);
  225. }
  226.  
  227. /*
  228.    The unsigned number from the ffblk structure is coded as follows:
  229.  
  230.     YYYYYYYM MMMDDDDD    -- as unsigned int
  231.       c[1]     c[0]      -- as two unsigned characters
  232. */
  233. void file_date(unsigned t)
  234. {
  235.    union twobytes {
  236.       unsigned char c[2];
  237.       unsigned u;
  238.    } f;
  239.    unsigned day;
  240.    unsigned month;
  241.    unsigned year;
  242.  
  243.    f.u = t;
  244.    day = (f.c[0] & 31);
  245.    month = ((f.c[1] & 1) * 8) + (f.c[0] >> 5);
  246.    year = 1980 + (f.c[1] >> 1);
  247.    printf(" %s %2u %4u ",months[month-1],day,year);
  248. }
  249.  
  250. /*
  251.    When passed a pointer to an array of SIZE characters, it will left-
  252.    justify the filename and extension by padding with blank spaces.  It
  253.    returns a pointer to the changed array.
  254. */
  255. char *justify(char *name)
  256. {
  257.    char *tmp;
  258.    char work[SIZE];
  259.    int index;
  260.    int count;
  261.  
  262.    tmp = work;
  263.    for(index = 0;index < 13;index++)
  264.       *(tmp + index) = ' ';
  265.    *(tmp + 12) = '\0';
  266.    for(index = 0;*(name + index) != '.';index++)
  267.       *(tmp + index) = *(name + index);
  268.    *(tmp + 8) = '.';
  269.    index++;
  270.    for(count = 9;*(name + index) != '\0';count++,index++)
  271.       *(tmp + count) = *(name + index);
  272.    strcpy(name,tmp);
  273.  
  274.    userwait();            /* best place to check for a pause!!!    */
  275.    return(name);
  276. }
  277.  
  278. /*
  279.    This function checks for any keystroke.  If found it pauses and waits
  280.    for another keystroke before returning to the caller.
  281. */
  282. void userwait(void)
  283. {
  284.    if(bioskey(1) != 0) {
  285.       bioskey(0);
  286.       while(bioskey(1) == 0);
  287.       bioskey(0);
  288.    }
  289. }