File: ds

package info (click to toggle)
icmake 7.18.00-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,840 kB
  • sloc: ansic: 7,784; makefile: 3,811; sh: 319; cpp: 83
file content (146 lines) | stat: -rwxr-xr-x 4,018 bytes parent folder | download | duplicates (7)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/icmake -qt /tmp/ds

/*
                                D S
*/

#define VERSION     "1.05"
#define YEAR        "1994--1995"

int
    debug;
string
    progname,
    xdev;

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

string backslash_wild(string spec)
{
    string
        s,
        ret;
    int
        index;

    for (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)
{
    int
        index;
    string
        optchar;
                                            // process all option characters
    for (index = 1; optchar = element(index, arg); 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)
{
    int
        index;
    list
        new;
    string
        arg;

    for (index = 0; index < argc; index++)  // walk all cmd line arguments
    {
        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"
        "ICCE DS (Disk Search). Version " VERSION "\n"
        "Copyright (c) ICCE " 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 (1);
}

void process(int argc, list argv)
{
    string
        cmd,
        filespec,
        startdir;

    if (argc == 2)                          // a file given as argument
        startdir = "/";			    // start at the root
    else
        startdir = element(1, argv);        // otherwise start at specified dir

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

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

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

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