annotate toys/other/which.c @ 656:6df4ccc0acbe

Regularize command headers, update links to standards documents.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 18:08:51 -0500
parents 2986aa63a021
children 786841fdb1e0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
2 *
151
bb6048c923ec Add or correct file descriptions
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 90
diff changeset
3 * which.c - Find executable files in $PATH.
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2006 Rob landley <rob@landley.net>
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
6
295
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 234
diff changeset
7 USE_WHICH(NEWTOY(which, "<1a", TOYFLAG_USR|TOYFLAG_BIN))
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
8
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
9 config WHICH
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
10 bool "which"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
11 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
12 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 usage: which [-a] filename ...
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
14
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
15 Search $PATH for executable files matching filename(s).
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
16
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
17 -a Show all matches
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
18 */
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #include "toys.h"
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
21 // Find an exectuable file either at a path with a slash in it (absolute or
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
22 // relative to current directory), or in $PATH. Returns absolute path to file,
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
23 // or NULL if not found.
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
24
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
25 static int which_in_path(char *filename)
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
26 {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
27 struct string_list *list;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
28
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
29 // If they gave us a path, don't worry about $PATH or -a
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
30
535
d51be130fda2 More stabs at getting #includes right, and moving off of deprecated functions.
Rob Landley <rob@landley.net>
parents: 295
diff changeset
31 if (strchr(filename, '/')) {
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
32 // Confirm it has the executable bit set, and it's not a directory.
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
33 if (!access(filename, X_OK)) {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
34 struct stat st;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
35
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
36 if (!stat(filename, &st) && S_ISREG(st.st_mode)) {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
37 puts(filename);
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
38 return 0;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
39 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
40 return 1;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
42 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
43
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
44 // Search $PATH for matches.
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
45 list = find_in_path(getenv("PATH"), filename);
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
46 if (!list) return 1;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
47
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
48 // Print out matches
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
49 while (list) {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
50 if (!access(list->str, X_OK)) {
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
51 puts(list->str);
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
52 // If we should stop at one match, do so
295
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 234
diff changeset
53 if (!toys.optflags) {
624
1e8b9acdafeb Genericize llist code a bit: rename llist_free() to llist_traverse(), and no longer accept NULL as a synonym for free.
Rob Landley <rob@landley.net>
parents: 535
diff changeset
54 llist_traverse(list, free);
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
55 break;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
56 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
57 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
58 free(llist_pop(&list));
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
59 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
60
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
61 return 0;
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
62 }
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
63
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 151
diff changeset
64 void which_main(void)
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
65 {
295
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 234
diff changeset
66 int i;
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 234
diff changeset
67 for (i=0; toys.optargs[i]; i++)
5a0faa267866 Fix which (the meaning of -a was reversed, and it was finding the _last_ hit).
Rob Landley <rob@landley.net>
parents: 234
diff changeset
68 toys.exitval |= which_in_path(toys.optargs[i]);
20
3981c96f9285 Implement which. Add hello world to menuconfig. Wrap the various applet main
Rob Landley <rob@landley.net>
parents:
diff changeset
69 }