home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / dirutl / glob11.arc / GLOB.C next >
Text File  |  1988-05-30  |  3KB  |  94 lines

  1. /* glob  - glob matching filenames for turbo C */
  2. /* path names must use a / instead of msdos \ */
  3. /* compile with tcc glob.c regexp.obj */
  4. /* $header: glob.c ver 2 Matt Cohen size 2315 time 4/22/88 20:18:54$ */
  5.  
  6. /*
  7. *
  8. * Name: glob - match files against regular expression
  9. *
  10. * Synopsis: glob pathname
  11. *
  12. * Description: glob uses regexp(3) to match files against the pathname
  13. *           which may be a regular expression as described in ed(1)
  14. *           and egrep(1). If a path is not given, the current 
  15. *           directory is used. The pathname must use '/' instead
  16. *           of the MSDOS '\' because the \ is interpreted as
  17. *           part of the regular expression.
  18. *
  19. * Example:       The following shows all executable files in the 
  20. *           current dir:    
  21. *            glob "*\.(EXE|BAT|COM)"
  22. *
  23. * Bugs:        Case is insignifigant due to MSDOS . 
  24. *
  25. */    
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #define DIRCH '/'
  31. #define DIRCHSTR "/"
  32. #define SLASH '\\'
  33. #define MODE 0x1f
  34. #include <dir.h>
  35. static char *h="$header: glob.c ver 2 Matt Cohen size 2315 time 4/22/88 20:18:54$";
  36. main(argc,argv)
  37. int argc; char *argv[];
  38.     struct ffblk ff;
  39.     char dirname[BUFSIZ], *getcwd();
  40.    char pat[BUFSIZ];
  41.     char *a,*b,*c,*regcomp(),*regexec(), *strrchr();
  42.     int i,done;
  43.     c=argv[1]; uppercase(c); strcpy(dirname,c);
  44.    if (*c=='*') /* fix up to .* */
  45.    sprintf(pat,"^.%s$",c);
  46.     else
  47.     sprintf(pat,"^%s$",c);
  48.     if ((b=strrchr(dirname,DIRCH))==NULL) /* no slash - current dir */
  49.    {
  50.    strcpy(dirname,getcwd(NULL,100));
  51.    /* make sure we are not in root dir (x:/) */
  52.    if (dirname[strlen(dirname)-1] != SLASH) strcat(dirname,DIRCHSTR);
  53.    }
  54.     /* is a slash.. seperate out the slash part and pattern */
  55.     else  { 
  56.             if (*(b+1)=='*') /* fix * into .* */
  57.             sprintf(pat,"^.%s$",b+1);
  58.          else sprintf(pat,"^%s$",b+1);
  59.          *(b+1)='\0'; /* null it out */
  60.             }    
  61. #ifdef DEBUG
  62.             printf("pat is %s\n",pat);
  63. #endif
  64.     if ((a=regcomp(pat))==0) { printf("could not compile\n");
  65.     exit(0);
  66.     }
  67.    for(i=0;dirname[i];i++) if (dirname[i]==SLASH) dirname[i]=DIRCH;
  68.    strcat(dirname,"*.*");
  69.  #ifdef DEBUG
  70.     printf("dirname is %s\n",dirname);
  71. #endif
  72.  
  73.     done=findfirst(dirname, &ff,MODE);
  74.     
  75.     if (done) { printf("no files\n") ; exit(0);}
  76.     do {
  77.       b=ff.ff_name;
  78.         if (regexec(a,b)) printf("%s\n",b);
  79.         done=findnext(&ff);
  80.         } while (!done);
  81.  
  82. }
  83.  
  84. /* convert a string to upper case */
  85. uppercase(s)
  86. char *s;
  87. {
  88.     int i;
  89.     for (i=0; i < strlen(s); i++)
  90.     if(islower(s[i]))
  91.     s[i]=toupper(s[i]);
  92. }    
  93.