1#!/usr/local/bin/icmake -t.
2
3#define VERSION     "1.06.01"
4#define YEAR        "1994--2016"
5
6int debug;
7string progname;
8string xdev;
9
10void kill(string s)
11{
12    printf(s, "\n\n");
13    exit(1);
14}
15
16string backslash_wild(string spec)
17{
18    string s;
19    string ret;
20
21    for (int index = 0; s = element(index, spec); index++)
22    {
23        if (s == "*" || s == "?")           // wildcard specifiers ?
24            ret += "\\";                    // protect the wildcard spec.
25        ret += s;
26    }
27    return ret;                           // return the protected string
28}
29
30void preamble(list argv)
31{
32    progname = get_base(element(0, argv));  // determine progname without .bim
33    xdev = "-xdev";                         // no X-device find
34    echo(OFF);                              // no display of the exec-ed cmnd
35}
36
37void option(string arg)
38{
39    string optchar;
40                                            // process all option characters
41    for (int index = 1; optchar = arg[index]; ++index)
42    {
43        if (optchar == "x")                 // X-dev ok ?
44            xdev == "";                     // set appropriate flag
45        else if (optchar == "d")            // debug request
46            debug++;                        // set flag
47        else                                // kill DS if optchar not found
48            kill("Unrecognized option: '-" + optchar);
49    }
50}
51
52list options(int argc, list argv)
53{
54    list new;
55
56    for (int index = 0; index != argc; ++index)  // all cmd line arguments
57    {
58        string arg = element(index, argv);         // get next element
59        if (element(0, arg) == "-")         // found an option ?
60            option(arg);                    // then process it
61        else
62            new += (list)arg;               // else add to the returnlist
63    }
64    return new;                           // return argv-list without options
65}
66
67void usage()
68{
69    printf
70    (
71        "\n"
72        "DS (Disk Search). Version " VERSION "\n"
73        "Copyright (c) GPL " YEAR ". All Rights Reserved\n"
74        "\n"
75        "DS by Frank B. Brokken\n"
76        "\n"
77        "Usage: ", progname, " [options] [dir-spec] file\n"
78        "Where:\n"
79        "   options:\n"
80        "       -d: Display rather than execute the search command\n"
81        "       -x: Allow cross-device searches\n"
82        "   dir-spec:   specification of the directory where the search is to\n"
83        "               be started. By default: / (the root).\n"
84        "   file: name of file to search.\n"
85        "\n"
86        "For the 'file' argument quoted wildcards (e.g., ds '*.local') are ok.\n"
87        "\n"
88    );
89    exit(0);
90}
91
92void process(int argc, list argv)
93{
94    string startdir = argc == 2 ?       // a file given as argument
95                        "/"             // start at the root
96                    :
97                        argv[1];        // otherwise start at specified dir
98
99                                            // protect wildcards in the
100                                            // filespecification with \-char
101    string filespec = backslash_wild(element(argc - 1, argv));
102
103    string cmd = "find " + startdir + " " + xdev + " -name " + filespec +
104                                            " 2>/dev/null";
105
106    if (!debug)
107        system(P_NOCHECK, cmd);
108    else
109        printf(cmd, "\n");
110}
111
112void main(int argc, list argv)
113{
114    preamble(argv);                         // preamble: determine progname etc.
115    argv = options(argc, argv);             // process options
116    argc = listlen(argv);
117
118    if (argc == 1)                          // no arguments ?
119        usage();                            // give help
120
121    process(argc, argv);                    // else process arguments
122}
123