summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/where.c
blob: d6ffdc44aeea44a11109370eee211c4459b38eaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
**  WHERE.C:  will search all DIRs on the given drive for specified file.
*/

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <string.h>

#if defined(__ZTC__)
 #include <direct.h>
 #define GetDrive(d) dos_getdrive(&d)
 #define SetDrive(d) {unsigned x;dos_setdrive(d,&x);}
 #define FAR _far
#elif defined(__TURBOC__)
 #include <dir.h>
 #define GetDrive(d) ((d) = getdisk() + 1)
 #define SetDrive(d) (setdisk(d - 1))
 #define FAR far
 #define _dos_findfirst(f,a,b) findfirst(f,b,a)
 #define _dos_findnext(b) findnext(b)
 #define find_t ffblk
 #define _A_SUBDIR FA_DIREC
 #define attrib ff_attrib
 #define name ff_name
#else /* assume MSC */
 #include <direct.h>
 #define GetDrive(d) _dos_getdrive(&d)
 #define SetDrive(d) {unsigned x;_dos_setdrive(d,&x);}
 #define FAR _far
#endif

int count=0;

main(int argc, char *argv[])
{
      char *curdir,
            sought[80],
           *temp;
      int   curdrive, newdrive, p;
      void  searchdir(char *dir, char *ptrn);

      /*  Find out where we are */

      curdir=getcwd(NULL,80);
      GetDrive(curdrive);

      /*  Find out what we're looking for  */

      if(argc>1)
            strcpy(sought,argv[1]);
      else
      {
            printf("\n\nPattern to search for: ");
            gets(sought);
      }

      /*  Get designator for another drive if specified  */

      if(sought[1]==':')
      {
            newdrive=(toupper(sought[0]))-64;       /* convert  */
            SetDrive(newdrive);
            p = (sought[2]=='\\') ? 3:2;
            strcpy(sought, &(sought[p]));
      }

      /*  Add wildcard prefix/suffix if necessary  */

      if(sought[0]=='.')
      {
            temp=strcat("*",sought);        /*  set prefix  */
            strcpy(sought,temp);
      }
      if(!strchr(sought,'.'))
            strcpy(sought,"*.*");           /*  set suffix  */

      /*  Perform search for pattern starting in root  */

      searchdir("\\",sought);
      printf("\nNumber of matches: %d",count);

      /*  Restore Original Drive and Directory  */

      SetDrive(curdrive);
      chdir(curdir);
      return EXIT_SUCCESS;
}

/*------------------------------------------------------------------------- */

void searchdir(char *path, char *ptrn)
#define ANYFILE 0xFF                         /*  recursive routine  */
{
      struct find_t *f;
      char          *wholepath;
      unsigned      rtn;

      chdir(path);                    /*  change to new path  */
      wholepath=getcwd(NULL,80);      /*  get full path name  */
      f=malloc(sizeof(*f));

      /*  Search for filename matches in this directory  */

      rtn= _dos_findfirst(ptrn,ANYFILE,f);
      while(rtn==0)
      {
            if( f->attrib != _A_SUBDIR )
                  printf("%s\\%s\n",wholepath,f->name);
            else  printf("%s\\%s <DIR>\n",wholepath, f->name);
            ++count;

            rtn = _dos_findnext(f);         /* find next match  */
      }  /* end while loop  */

      /*  Now search any subdirectories under this directory  */

      rtn= _dos_findfirst("*.*", _A_SUBDIR,f);
      while(rtn==0)
      {
            if( (f->attrib == _A_SUBDIR) && (f->name[0] != '.'))
            {
                  searchdir(f->name,ptrn);   /* recursive call */
                  chdir(wholepath);
            }
            rtn = _dos_findnext(f);  /* search next dir  */
      }

      free(wholepath);
      free(f);
}