blob: d112f3c22ce52c60f99c2e09ac5ea67937b94bf8 [file] [log] [blame]
Juan Cespedesac3db291998-04-25 14:31:58 +02001#if HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Juan Cespedes5e01f651998-03-08 22:31:44 +01005#include <string.h>
6#include <stdlib.h>
7#include <unistd.h>
Juan Cespedes1b9cfd61999-08-30 19:34:50 +02008#include <fcntl.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +01009#include <errno.h>
10#include <limits.h>
11
Juan Cespedesac3db291998-04-25 14:31:58 +020012#if HAVE_GETOPT_H
13#include <getopt.h>
14#endif
15
Juan Cespedes5e01f651998-03-08 22:31:44 +010016#include "ltrace.h"
17#include "options.h"
18#include "defs.h"
19
Steve Fink58c73a72006-07-17 23:18:35 +020020#ifndef SYSCONFDIR
21#define SYSCONFDIR "/etc"
22#endif
23
Olaf Heringe948e582006-10-12 23:53:44 +020024#define SYSTEM_CONFIG_FILE SYSCONFDIR "/ltrace.conf"
Steve Fink58c73a72006-07-17 23:18:35 +020025#define USER_CONFIG_FILE "~/.ltrace.conf"
26
Juan Cespedes1cd999a2001-07-03 00:46:04 +020027#define MAX_LIBRARY 30
28char *library[MAX_LIBRARY];
29int library_num = 0;
Juan Cespedesac3db291998-04-25 14:31:58 +020030static char *progname; /* Program name (`ltrace') */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031FILE *output;
Juan Cespedesaee09312007-08-31 18:49:48 +020032int opt_a = DEFAULT_ACOLUMN; /* alignment column for results */
33int opt_A = DEFAULT_ARRAYLEN; /* maximum # array elements to print */
34int opt_c = 0; /* Report a summary on program exit */
Juan Cespedes5e01f651998-03-08 22:31:44 +010035int opt_d = 0; /* debug */
36int opt_i = 0; /* instruction pointer */
Juan Cespedesaee09312007-08-31 18:49:48 +020037int opt_s = DEFAULT_STRLEN; /* maximum # of bytes printed in strings */
Juan Cespedes5e01f651998-03-08 22:31:44 +010038int opt_S = 0; /* display syscalls */
39int opt_L = 1; /* display library calls */
40int opt_f = 0; /* trace child processes as they are created */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010041char *opt_u = NULL; /* username to run command as */
Juan Cespedesf666d191998-09-20 23:04:34 +020042int opt_r = 0; /* print relative timestamp */
Juan Cespedes5e0acdb1998-04-04 08:34:07 +020043int opt_t = 0; /* print absolute timestamp */
Juan Cespedesd914a202004-11-10 00:15:33 +010044#ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +020045int opt_C = 0; /* Demangle low-level symbol names */
Juan Cespedesac3db291998-04-25 14:31:58 +020046#endif
Juan Cespedesaee09312007-08-31 18:49:48 +020047int opt_n = 0; /* indent output according to program flow */
Juan Cespedesd65efa32003-02-03 00:22:30 +010048int opt_T = 0; /* show the time spent inside each call */
Juan Cespedesaee09312007-08-31 18:49:48 +020049int opt_o = 0; /* output to a specific file */
Juan Cespedes5e01f651998-03-08 22:31:44 +010050
51/* List of pids given to option -p: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010052struct opt_p_t *opt_p = NULL; /* attach to process with a given pid */
Juan Cespedes5e01f651998-03-08 22:31:44 +010053
Juan Cespedesac3db291998-04-25 14:31:58 +020054/* List of function names given to option -e: */
Ian Wienand2d45b1a2006-02-20 22:48:07 +010055struct opt_e_t *opt_e = NULL;
56int opt_e_enable = 1;
Juan Cespedesac3db291998-04-25 14:31:58 +020057
Ian Wienand9a2ad352006-02-20 22:44:45 +010058/* List of global function names given to -x: */
Paul Gilliam24e643a2006-03-13 18:43:13 +010059struct opt_x_t *opt_x = NULL;
Ian Wienand9a2ad352006-02-20 22:44:45 +010060
Steve Fink58c73a72006-07-17 23:18:35 +020061/* List of filenames give to option -F: */
62struct opt_F_t *opt_F = NULL; /* alternate configuration file(s) */
63
Paul Gilliambe320772006-04-24 22:06:23 +020064#ifdef PLT_REINITALISATION_BP
Ian Wienand9a2ad352006-02-20 22:44:45 +010065/* Set a break on the routine named here in order to re-initialize breakpoints
66 after all the PLTs have been initialzed */
Paul Gilliambe320772006-04-24 22:06:23 +020067char *PLTs_initialized_by_here = PLT_REINITALISATION_BP;
68#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010069
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070static void usage(void)
71{
Juan Cespedesac3db291998-04-25 14:31:58 +020072#if !(HAVE_GETOPT || HAVE_GETOPT_LONG)
73 fprintf(stdout, "Usage: %s [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010074 "Trace library calls of a given program.\n\n", progname);
Juan Cespedesac3db291998-04-25 14:31:58 +020075#else
76 fprintf(stdout, "Usage: %s [option ...] [command [arg ...]]\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 "Trace library calls of a given program.\n\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020078# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 " -a, --align=COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020080# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010081 " -a COLUMN align return values in a secific column.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020082# endif
Juan Cespedesaee09312007-08-31 18:49:48 +020083 " -A ARRAYLEN maximum number of array elements to print.\n"
Ian Wienand2d45b1a2006-02-20 22:48:07 +010084 " -c count time and calls, and report a summary on exit.\n"
Juan Cespedesd914a202004-11-10 00:15:33 +010085# ifdef USE_DEMANGLE
Juan Cespedesac3db291998-04-25 14:31:58 +020086# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010087 " -C, --demangle decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020088# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010089 " -C decode low-level symbol names into user-level names.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020090# endif
91# endif
92# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +010093 " -d, --debug print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020094# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010095 " -d print debugging info.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +020096# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 " -e expr modify which events to trace.\n"
98 " -f follow forks.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +010099# if HAVE_GETOPT_LONG
Juan Cespedesaee09312007-08-31 18:49:48 +0200100 " -F, --config=FILE load alternate configuration file\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200101# else
Juan Cespedesaee09312007-08-31 18:49:48 +0200102 " -F FILE load alternate configuration file\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200103# endif
Juan Cespedesaee09312007-08-31 18:49:48 +0200104 " (can be repeated).\n"
Steve Fink58c73a72006-07-17 23:18:35 +0200105# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 " -h, --help display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100107# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100108 " -h display this help and exit.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100109# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100110 " -i print instruction pointer at time of library call.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100111# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 " -l, --library=FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100113# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100114 " -l FILE print library calls from this library only.\n"
Juan Cespedesd65efa32003-02-03 00:22:30 +0100115# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100116 " -L do NOT display library calls.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200117# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100118 " -n, --indent=NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200119# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100120 " -n NR indent output by NR spaces for each call level nesting.\n"
Juan Cespedes5b3ffdf2001-07-02 00:52:45 +0200121# endif
122# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 " -o, --output=FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200124# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100125 " -o FILE write the trace output to that file.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200126# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100127 " -p PID attach to the process with the process ID pid.\n"
128 " -r print relative timestamps.\n"
129 " -s STRLEN specify the maximum string size to print.\n"
130 " -S display system calls.\n"
131 " -t, -tt, -ttt print absolute timestamps.\n"
132 " -T show the time spent inside each call.\n"
133 " -u USERNAME run command with the userid, groupid of username.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200134# if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100135 " -V, --version output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200136# else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 " -V output version information and exit.\n"
Juan Cespedesac3db291998-04-25 14:31:58 +0200138# endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100139 " -x NAME treat the global NAME like a library subroutine.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200140#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100141 " -X NAME same as -x; and PLT's will be initialized by here.\n"
Paul Gilliambe320772006-04-24 22:06:23 +0200142#endif
143 "\nReport bugs to ltrace-devel@lists.alioth.debian.org\n",
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100144 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200145#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100146}
147
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100148static char *search_for_command(char *filename)
149{
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100150 static char pathname[PATH_MAX];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100151 char *path;
152 int m, n;
153
154 if (strchr(filename, '/')) {
155 return filename;
156 }
157 for (path = getenv("PATH"); path && *path; path += m) {
158 if (strchr(path, ':')) {
159 n = strchr(path, ':') - path;
160 m = n + 1;
161 } else {
162 m = n = strlen(path);
163 }
Juan Cespedesf1bfe202002-03-27 00:22:23 +0100164 if (n + strlen(filename) + 1 >= PATH_MAX) {
165 fprintf(stderr, "Error: filename too long\n");
166 exit(1);
167 }
168 strncpy(pathname, path, n);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100169 if (n && pathname[n - 1] != '/') {
170 pathname[n++] = '/';
171 }
172 strcpy(pathname + n, filename);
173 if (!access(pathname, X_OK)) {
174 return pathname;
175 }
176 }
177 return filename;
178}
179
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100180char **process_options(int argc, char **argv)
181{
Juan Cespedesac3db291998-04-25 14:31:58 +0200182 progname = argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100183 output = stderr;
184
Juan Cespedesac3db291998-04-25 14:31:58 +0200185#if HAVE_GETOPT || HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100186 while (1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200187 int c;
188#if HAVE_GETOPT_LONG
189 int option_index = 0;
190 static struct option long_options[] = {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100191 {"align", 1, 0, 'a'},
Juan Cespedesaee09312007-08-31 18:49:48 +0200192 {"config", 1, 0, 'F'},
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 {"debug", 0, 0, 'd'},
Juan Cespedesd914a202004-11-10 00:15:33 +0100194# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100195 {"demangle", 0, 0, 'C'},
Juan Cespedes8e3e0821998-09-24 13:49:55 +0200196#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100197 {"indent", 1, 0, 'n'},
198 {"help", 0, 0, 'h'},
199 {"library", 1, 0, 'l'},
200 {"output", 1, 0, 'o'},
201 {"version", 0, 0, 'V'},
202 {0, 0, 0, 0}
Juan Cespedesac3db291998-04-25 14:31:58 +0200203 };
Juan Cespedesd65efa32003-02-03 00:22:30 +0100204 c = getopt_long(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100205# ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100206 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200207# endif
Steve Fink1150bc42006-08-07 06:04:43 +0200208 "a:A:e:F:l:n:o:p:s:u:x:X:", long_options,
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 &option_index);
Juan Cespedesac3db291998-04-25 14:31:58 +0200210#else
Juan Cespedesd65efa32003-02-03 00:22:30 +0100211 c = getopt(argc, argv, "+cdfhiLrStTV"
Juan Cespedesd914a202004-11-10 00:15:33 +0100212# ifdef USE_DEMANGLE
Juan Cespedesaee09312007-08-31 18:49:48 +0200213 "C"
Juan Cespedesac3db291998-04-25 14:31:58 +0200214# endif
Juan Cespedesaee09312007-08-31 18:49:48 +0200215 "a:A:e:F:l:n:o:p:s:u:x:X:");
Juan Cespedesac3db291998-04-25 14:31:58 +0200216#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100217 if (c == -1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200218 break;
Juan Cespedes5e01f651998-03-08 22:31:44 +0100219 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100220 switch (c) {
221 case 'a':
222 opt_a = atoi(optarg);
223 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200224 case 'A':
Steve Fink1150bc42006-08-07 06:04:43 +0200225 opt_A = atoi(optarg);
226 break;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100227 case 'c':
228 opt_c++;
229 break;
Juan Cespedesd914a202004-11-10 00:15:33 +0100230#ifdef USE_DEMANGLE
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100231 case 'C':
232 opt_C++;
233 break;
Juan Cespedesac3db291998-04-25 14:31:58 +0200234#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100235 case 'd':
236 opt_d++;
237 break;
238 case 'e':
239 {
240 char *str_e = strdup(optarg);
241 if (!str_e) {
242 perror("ltrace: strdup");
243 exit(1);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100244 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100245 if (str_e[0] == '!') {
246 opt_e_enable = 0;
247 str_e++;
248 }
249 while (*str_e) {
250 struct opt_e_t *tmp;
251 char *str2 = strchr(str_e, ',');
252 if (str2) {
253 *str2 = '\0';
254 }
255 tmp = malloc(sizeof(struct opt_e_t));
Juan Cespedesd65efa32003-02-03 00:22:30 +0100256 if (!tmp) {
257 perror("ltrace: malloc");
258 exit(1);
259 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260 tmp->name = str_e;
261 tmp->next = opt_e;
262 opt_e = tmp;
263 if (str2) {
264 str_e = str2 + 1;
265 } else {
266 break;
267 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100268 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100269 break;
270 }
271 case 'f':
272 opt_f = 1;
273 break;
Juan Cespedesaee09312007-08-31 18:49:48 +0200274 case 'F':
275 {
276 struct opt_F_t *tmp = malloc(sizeof(struct opt_F_t));
277 if (!tmp) {
278 perror("ltrace: malloc");
279 exit(1);
280 }
281 tmp->filename = strdup(optarg);
282 tmp->next = opt_F;
283 opt_F = tmp;
284 break;
285 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100286 case 'h':
287 usage();
288 exit(0);
289 case 'i':
290 opt_i++;
291 break;
292 case 'l':
293 if (library_num == MAX_LIBRARY) {
294 fprintf(stderr,
295 "Too many libraries. Maximum is %i.\n",
296 MAX_LIBRARY);
297 exit(1);
298 }
299 library[library_num++] = optarg;
300 break;
301 case 'L':
302 opt_L = 0;
303 break;
304 case 'n':
305 opt_n = atoi(optarg);
306 break;
307 case 'o':
Juan Cespedesaee09312007-08-31 18:49:48 +0200308 opt_o++;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 output = fopen(optarg, "w");
310 if (!output) {
311 fprintf(stderr,
312 "Can't open %s for output: %s\n",
313 optarg, strerror(errno));
314 exit(1);
315 }
316 setvbuf(output, (char *)NULL, _IOLBF, 0);
317 fcntl(fileno(output), F_SETFD, FD_CLOEXEC);
318 break;
319 case 'p':
320 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200321 struct opt_p_t *tmp = malloc(sizeof(struct opt_p_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100322 if (!tmp) {
323 perror("ltrace: malloc");
324 exit(1);
325 }
326 tmp->pid = atoi(optarg);
327 tmp->next = opt_p;
328 opt_p = tmp;
329 break;
330 }
331 case 'r':
332 opt_r++;
333 break;
334 case 's':
335 opt_s = atoi(optarg);
336 break;
337 case 'S':
338 opt_S = 1;
339 break;
340 case 't':
341 opt_t++;
342 break;
343 case 'T':
344 opt_T++;
345 break;
346 case 'u':
347 opt_u = optarg;
348 break;
349 case 'V':
350 printf("ltrace version " PACKAGE_VERSION ".\n"
Juan Cespedesaee09312007-08-31 18:49:48 +0200351 "Copyright (C) 1997-2007 Juan Cespedes <cespedes@debian.org>.\n"
352 "This is free software; see the GNU General Public Licence\n"
353 "version 2 or later for copying conditions. There is NO warranty.\n");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100354 exit(0);
355 case 'X':
Paul Gilliambe320772006-04-24 22:06:23 +0200356#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100357 PLTs_initialized_by_here = optarg;
Paul Gilliambe320772006-04-24 22:06:23 +0200358#else
Juan Cespedesaee09312007-08-31 18:49:48 +0200359 fprintf(stderr, "WARNING: \"-X\" not used for this "
Paul Gilliambe320772006-04-24 22:06:23 +0200360 "architecture: assuming you meant \"-x\"\n");
361#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100362 /* Fall Thru */
Juan Cespedesac3db291998-04-25 14:31:58 +0200363
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100364 case 'x':
365 {
Paul Gilliam24e643a2006-03-13 18:43:13 +0100366 struct opt_x_t *p = opt_x;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100367
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100368 /* First, check for duplicate. */
369 while (p && strcmp(p->name, optarg)) {
370 p = p->next;
371 }
372 if (p) {
Ian Wienand9a2ad352006-02-20 22:44:45 +0100373 break;
374 }
375
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100376 /* If not duplicate, add to list. */
Paul Gilliam24e643a2006-03-13 18:43:13 +0100377 p = malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100378 if (!p) {
379 perror("ltrace: malloc");
380 exit(1);
381 }
382 p->name = optarg;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100383 p->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100384 p->next = opt_x;
385 opt_x = p;
386 break;
387 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100388
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100389 default:
Juan Cespedesac3db291998-04-25 14:31:58 +0200390#if HAVE_GETOPT_LONG
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100391 fprintf(stderr,
392 "Try `%s --help' for more information\n",
393 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200394#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100395 fprintf(stderr, "Try `%s -h' for more information\n",
396 progname);
Juan Cespedesac3db291998-04-25 14:31:58 +0200397#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100398 exit(1);
Juan Cespedes5e01f651998-03-08 22:31:44 +0100399 }
400 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100401 argc -= optind;
402 argv += optind;
Juan Cespedesac3db291998-04-25 14:31:58 +0200403#endif
Juan Cespedes5e01f651998-03-08 22:31:44 +0100404
Juan Cespedesaee09312007-08-31 18:49:48 +0200405 if (!opt_F) {
406 opt_F = malloc(sizeof(struct opt_F_t));
407 opt_F->next = malloc(sizeof(struct opt_F_t));
408 opt_F->next->next = NULL;
409 opt_F->filename = USER_CONFIG_FILE;
410 opt_F->next->filename = SYSTEM_CONFIG_FILE;
411 }
Steve Fink58c73a72006-07-17 23:18:35 +0200412 /* Reverse the config file list since it was built by
413 * prepending, and it would make more sense to process the
414 * files in the order they were given. Probably it would make
415 * more sense to keep a tail pointer instead? */
416 {
Juan Cespedesaee09312007-08-31 18:49:48 +0200417 struct opt_F_t *egg = NULL;
418 struct opt_F_t *chicken;
419 while (opt_F) {
420 chicken = opt_F->next;
421 opt_F->next = egg;
422 egg = opt_F;
423 opt_F = chicken;
424 }
425 opt_F = egg;
Steve Fink58c73a72006-07-17 23:18:35 +0200426 }
427
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100428 if (!opt_p && argc < 1) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200429 fprintf(stderr, "%s: too few arguments\n", progname);
Juan Cespedes5c3fe062004-06-14 18:08:37 +0200430 usage();
Juan Cespedes5e01f651998-03-08 22:31:44 +0100431 exit(1);
432 }
Juan Cespedesf666d191998-09-20 23:04:34 +0200433 if (opt_r && opt_t) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100434 fprintf(stderr, "%s: Incompatible options -r and -t\n",
435 progname);
Juan Cespedesf666d191998-09-20 23:04:34 +0200436 exit(1);
437 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100438 if (argc > 0) {
Juan Cespedesac3db291998-04-25 14:31:58 +0200439 command = search_for_command(argv[0]);
Juan Cespedes1fe93d51998-03-13 00:29:21 +0100440 }
Juan Cespedesac3db291998-04-25 14:31:58 +0200441 return &argv[0];
Juan Cespedes5e01f651998-03-08 22:31:44 +0100442}