]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / src / tools / bwild.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Test program for testing wild card expressions
21  * 
22  *  Kern Sibbald, MMVI
23  *
24  */
25
26 #include "bacula.h"
27 #include "lib/fnmatch.h"
28
29 static void usage()
30 {
31    fprintf(stderr,
32 "\n"
33 "Usage: bwild [-d debug_level] -f <data-file>\n"
34 "       -f          specify file of data to be matched\n"
35 "       -i          use case insenitive match\n"
36 "       -l          suppress line numbers\n"
37 "       -n          print lines that do not match\n"
38 "       -?          print this message.\n"
39 "\n\n");
40
41    exit(1);
42 }
43
44 int main(int argc, char *const *argv)
45 {
46    char *fname = NULL;
47    int rc, ch;
48    char data[1000];
49    char pat[500];
50    FILE *fd;
51    bool match_only = true;
52    int lineno;
53    bool no_linenos = false;
54    int ic = 0;
55    
56
57    setlocale(LC_ALL, "");
58    bindtextdomain("bacula", LOCALEDIR);
59    textdomain("bacula");
60
61    while ((ch = getopt(argc, argv, "d:f:in?")) != -1) {
62       switch (ch) {
63       case 'd':                       /* set debug level */
64          debug_level = atoi(optarg);
65          if (debug_level <= 0) {
66             debug_level = 1;
67          }
68          break;
69
70       case 'f':                       /* data */
71          fname = optarg;
72          break;
73
74       case 'i':                       /* ignore case */
75          ic = FNM_CASEFOLD;
76          break;
77
78       case 'l':
79          no_linenos = true;
80          break;
81
82       case 'n':
83          match_only = false;
84          break;
85
86       case '?':
87       default:
88          usage();
89
90       }
91    }
92    argc -= optind;
93    argv += optind;
94
95    if (!fname) {
96       printf("A data file must be specified.\n");
97       usage();
98    }
99
100    OSDependentInit();
101
102    for ( ;; ) {
103       printf("Enter a wild-card: ");
104       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
105          break;
106       }
107       strip_trailing_newline(pat);
108       if (pat[0] == 0) {
109          exit(0);
110       }
111       fd = fopen(fname, "r");
112       if (!fd) {
113          printf(_("Could not open data file: %s\n"), fname);
114          exit(1);
115       }
116       lineno = 0;
117       while (fgets(data, sizeof(data)-1, fd)) {
118          strip_trailing_newline(data);
119          lineno++;
120          rc = fnmatch(pat, data, ic);
121          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
122             if (no_linenos) {
123                printf("%s\n", data);
124             } else {
125                printf("%5d: %s\n", lineno, data);
126             }
127          }
128       }
129       fclose(fd);
130    }
131    exit(0);
132 }