1 /*
2  * libecho.c
3  *
4  * For each argument on the command line, echo it.  Should expand
5  * DOS wildcards correctly.
6  *
7  * Syntax: libecho [-p prefix] list...
8  */
9 #include <stdio.h>
10 #include <io.h>
11 #include <string.h>
12 
13 void echo_files(char *, char *);
14 
15 int
main(int argc,char * argv[])16 main(int argc, char *argv[])
17 {
18   int i;
19   char *prefix;
20 
21   prefix = "";
22 
23   if (argc < 2) {
24     fprintf(stderr, "Usage:  libecho [-p prefix] list...\n");
25     return 1;
26   }
27 
28   for (i = 1 ; i < argc ; i++)
29     if (!stricmp(argv[i], "-p"))
30       prefix = argv[++i];
31     else
32       echo_files(prefix, argv[i]);
33 
34   return 0;
35 }
36 
37 void
echo_files(char * prefix,char * f)38 echo_files(char *prefix, char *f)
39 {
40   long ff;
41   struct _finddata_t fdt;
42   char *slash;
43   char filepath[256];
44 
45   /*
46    * We're unix based quite a bit here.  Look for normal slashes and
47    * make them reverse slashes.
48    */
49   while((slash = strrchr(f, '/')) != NULL)
50     *slash = '\\';
51 
52   strcpy(filepath, f);
53 
54   slash = strrchr(filepath, '\\');
55 
56   if (slash) {
57     slash++;
58     *slash = 0;
59   } else {
60     filepath[0] = '\0';
61   }
62 
63   ff = _findfirst(f, &fdt);
64 
65   if (ff < 0) {
66     printf("%s%s\n", prefix, f);
67     return;
68   }
69 
70   printf("%s%s%s\n", prefix, filepath, fdt.name);
71 
72   for (;;) {
73     if (_findnext(ff, &fdt) < 0)
74       break;
75     printf("%s%s%s\n", prefix, filepath, fdt.name);
76   }
77   _findclose(ff);
78 }
79