Debian Codegraph @

src/fnid.c

raw symbols SCIP
1 /* fnid.c -- report which files constitute an ID database
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <fnmatch.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <alloca.h>
27 #include <xalloc.h>
28 #include <pathmax.h>
29 #include <error.h>
30
31 #include "closeout.h"
32 #include "xnls.h"
33 #include "idfile.h"
34 #include "iduglobal.h"
35 #include "progname.h"
36
37 static int show_version = 0;
38 static int show_help = 0;
39 static struct file_link *cw_dlink;
40
41 struct idhead idh;
42
43 /* How shall we separate file names? */
44
45 static enum separator_style separator_style = ss_contextual;
46
47 void usage (void) __attribute__((__noreturn__));
48 void
49 usage (void)
50 {
51 fprintf (stderr, _("Try `%s --help' for more information.\n"),
52 program_name);
53 exit (EXIT_FAILURE);
54 }
55
56 static struct option const long_options[] =
57 {
58 { "file", required_argument, 0, 'f' },
59 { "separator", required_argument, 0, 'S' },
60 { "help", no_argument, &show_help, 1 },
61 { "version", no_argument, &show_version, 1 },
62 {NULL, 0, NULL, 0}
63 };
64
65 static void __attribute__((__noreturn__))
66 help_me (void)
67 {
68 printf (_("\
69 Usage: %s [OPTION]... [PATTERN]...\n\
70 "), program_name);
71
72 printf (_("\
73 Print constituent file names that match PATTERN,\n\
74 using shell-style wildcards.\n\
75 -f, --file=FILE file name of ID database\n\
76 -S, --separator=STYLE STYLE is one of `braces', `space' or `newline'\n\
77 --help display this help and exit\n\
78 --version output version information and exit\n\
79 "));
80 printf (_("\nReport bugs to " PACKAGE_BUGREPORT "\n\n"));
81 exit (EXIT_SUCCESS);
82 }
83
84 int
85 main (int argc, char **argv)
86 {
87 set_program_name (argv[0]);
88 idh.idh_file_name = 0;
89
90 #if ENABLE_NLS
91 /* Set locale according to user's wishes. */
92 setlocale (LC_ALL, "");
93
94 /* Tell program which translations to use and where to find. */
95 bindtextdomain (PACKAGE, LOCALEDIR);
96 textdomain (PACKAGE);
97 #endif
98
99 atexit (close_stdout);
100
101 for (;;)
102 {
103 int optc = getopt_long (argc, argv, "f:S:",
104 long_options, (int *) 0);
105 if (optc < 0)
106 break;
107 switch (optc)
108 {
109 case 0:
110 break;
111
112 case 'f':
113 idh.idh_file_name = optarg;
114 break;
115
116 case 'S':
117 separator_style = parse_separator_style (optarg);
118 break;
119
120 default:
121 usage ();
122 }
123 }
124
125 if (show_version)
126 {
127 printf ("%s - %s\n", program_name, PACKAGE_VERSION);
128 exit (EXIT_SUCCESS);
129 }
130
131 if (show_help)
132 help_me ();
133
134 if (separator_style == ss_contextual)
135 {
136 if (isatty (STDOUT_FILENO))
137 separator_style = DEFAULT_SEPARATOR_STYLE;
138 else
139 separator_style = ss_newline;
140 }
141
142 argc -= optind;
143 argv += optind;
144 if (argc == 0)
145 {
146 static char star[] = "*";
147 static char *starp = star;
148 argc = 1;
149 argv = &starp;
150 }
151
152 /* Look for the ID database up the tree */
153 idh.idh_file_name = locate_id_file_name (idh.idh_file_name);
154 if (idh.idh_file_name == 0)
155 error (EXIT_FAILURE, errno, _("can't locate `ID'"));
156
157 init_idh_obstacks (&idh);
158 init_idh_tables (&idh);
159
160 cw_dlink = get_current_dir_link ();
161 {
162 struct file_link **members = read_id_file (idh.idh_file_name, &idh);
163 struct file_link **members_N = &members[idh.idh_files];
164 struct file_link **flinkv_0 = xmalloc (sizeof(struct file_link *) * (idh.idh_files + 1));
165 struct file_link **flinkv = flinkv_0;
166 char **patv_0 = xmalloc (sizeof(char *) * (argc * 2));
167 char **patv_N;
168 char **patv = patv_0;
169 char *file_name = alloca (PATH_MAX);
170
171 for ( ; argc; argc--, argv++)
172 {
173 char *arg = *argv;
174 *patv++ = arg;
175 if (*arg != '*' && *arg != '/')
176 {
177 char *pat = xmalloc (strlen (arg) + 2);
178 sprintf (pat, "*/%s", arg);
179 *patv++ = pat;
180 }
181 }
182 patv_N = patv;
183
184 for ( ; members < members_N; members++)
185 {
186 maybe_relative_file_name (file_name, *members, cw_dlink);
187 for (patv = patv_0; patv < patv_N; patv++)
188 {
189 if (fnmatch (*patv, file_name, MAYBE_FNM_CASEFOLD) == 0)
190 {
191 *flinkv++ = *members;
192 break;
193 }
194 }
195 }
196 *flinkv = 0;
197 print_filenames (flinkv_0, separator_style);
198 }
199 return 0;
200 }