File: ds

package info (click to toggle)
icmake 13.05.01-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,132 kB
  • sloc: cpp: 11,595; fortran: 883; makefile: 853; sh: 546; pascal: 342
file content (122 lines) | stat: -rwxr-xr-x 3,797 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/icmake -t.

#define VERSION     "1.06.01"
#define YEAR        "1994--2016"

int debug;
string progname;
string xdev;

void kill(string s)
{
    printf(s, "\n\n");
    exit(1);
}

string backslash_wild(string spec)
{
    string s;
    string ret;

    for (int index = 0; s = element(index, spec); index++)
    {
        if (s == "*" || s == "?")           // wildcard specifiers ?
            ret += "\\";                    // protect the wildcard spec.
        ret += s;
    }
    return ret;                           // return the protected string
}

void preamble(list argv)
{                                         
    progname = get_base(element(0, argv));  // determine progname without .bim
    xdev = "-xdev";                         // no X-device find
    echo(OFF);                              // no display of the exec-ed cmnd
}

void option(string arg)
{
    string optchar;
                                            // process all option characters
    for (int index = 1; optchar = arg[index]; ++index)
    {
        if (optchar == "x")                 // X-dev ok ?
            xdev == "";                     // set appropriate flag
        else if (optchar == "d")            // debug request
            debug++;                        // set flag
        else                                // kill DS if optchar not found
            kill("Unrecognized option: '-" + optchar);
    }
}

list options(int argc, list argv)
{
    list new;

    for (int index = 0; index != argc; ++index)  // all cmd line arguments
    {
        string arg = element(index, argv);         // get next element
        if (element(0, arg) == "-")         // found an option ?
            option(arg);                    // then process it
        else
            new += (list)arg;               // else add to the returnlist
    }
    return new;                           // return argv-list without options
}

void usage()
{
    printf
    (
        "\n"
        "DS (Disk Search). Version " VERSION "\n"
        "Copyright (c) GPL " YEAR ". All Rights Reserved\n"
        "\n"
        "DS by Frank B. Brokken\n"
        "\n"
        "Usage: ", progname, " [options] [dir-spec] file\n"
        "Where:\n"
        "   options:\n"
        "       -d: Display rather than execute the search command\n"
        "       -x: Allow cross-device searches\n"
        "   dir-spec:   specification of the directory where the search is to\n"
        "               be started. By default: / (the root).\n"
        "   file: name of file to search.\n"
        "\n"
        "For the 'file' argument quoted wildcards (e.g., ds '*.local') are ok.\n"
        "\n"
    );
    exit(0);
}

void process(int argc, list argv)
{
    string startdir = argc == 2 ?       // a file given as argument
                        "/"             // start at the root
                    :
                        argv[1];        // otherwise start at specified dir

                                            // protect wildcards in the
                                            // filespecification with \-char
    string filespec = backslash_wild(element(argc - 1, argv));
    
    string cmd = "find " + startdir + " " + xdev + " -name " + filespec + 
                                            " 2>/dev/null"; 

    if (!debug)
        system(P_NOCHECK, cmd);
    else
        printf(cmd, "\n");
}

void main(int argc, list argv)
{
    preamble(argv);                         // preamble: determine progname etc.
    argv = options(argc, argv);             // process options
    argc = listlen(argv);

    if (argc == 1)                          // no arguments ?
        usage();                            // give help
    
    process(argc, argv);                    // else process arguments
}