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