home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / turbo_c / glob.arc / GLOB.C next >
C/C++ Source or Header  |  1988-04-22  |  2KB  |  89 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 SLASH '\\'
  32. #define MODE 0x1f
  33. #include <dir.h>
  34. static char *h="$header: glob.c ver 2 Matt Cohen size 2315 time 4/22/88 20:18:54$";
  35. main(argc,argv)
  36. int argc; char *argv[];
  37.     struct ffblk ff;
  38.     char dirname[BUFSIZ], *getcwd();
  39.    char pat[BUFSIZ];
  40.     char *a,*b,*c,*regcomp(),*regexec(), *strrchr();
  41.     int i,done;
  42.     c=argv[1]; uppercase(c); strcpy(dirname,c);
  43.    if (*c=='*') /* fix up to .* */
  44.    sprintf(pat,"^.%s$",c);
  45.     else
  46.     sprintf(pat,"^%s$",c);
  47.     if ((b=strrchr(dirname,DIRCH))==NULL) /* no slash - current dir */
  48.    sprintf(dirname,"%s%c",getcwd(NULL,100),DIRCH);
  49.     /* is a slash.. seperate out the slash part and pattern */
  50.     else  { 
  51.             if (*(b+1)=='*') /* fix * into .* */
  52.             sprintf(pat,"^.%s$",b+1);
  53.          else sprintf(pat,"^%s$",b+1);
  54.          *(b+1)='\0'; /* null it out */
  55.             }    
  56. #ifdef DEBUG
  57.             printf("pat is %s\n",pat);
  58. #endif
  59.     if ((a=regcomp(pat))==0) { printf("could not compile\n");
  60.     exit(0);
  61.     }
  62.    for(i=0;dirname[i];i++) if (dirname[i]==SLASH) dirname[i]=DIRCH;
  63.    strcat(dirname,"*.*");
  64.  #ifdef DEBUG
  65.     printf("dirname is %s\n",dirname);
  66. #endif
  67.  
  68.     done=findfirst(dirname, &ff,MODE);
  69.     
  70.     if (done) { printf("no files\n") ; exit(0);}
  71.     do {
  72.       b=ff.ff_name;
  73.         if (regexec(a,b)) printf("%s\n",b);
  74.         done=findnext(&ff);
  75.         } while (!done);
  76.  
  77. }
  78.  
  79. /* convert a string to upper case */
  80. uppercase(s)
  81. char *s;
  82. {
  83.     int i;
  84.     for (i=0; i < strlen(s); i++)
  85.     if(islower(s[i]))
  86.     s[i]=toupper(s[i]);
  87. }    
  88.