aboutsummaryrefslogtreecommitdiff
path: root/which.c
diff options
context:
space:
mode:
authorbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-09-18 09:37:40 +0000
committerbug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-09-18 09:37:40 +0000
commit43cdbaf7283fe1774c163141520370706b02b36a (patch)
treed1efb5c62c9b12c8d787f4486007ae9bc502a056 /which.c
parent66581111b6a333a636c21452c346859caac202fb (diff)
downloadbusybox-w32-43cdbaf7283fe1774c163141520370706b02b36a.tar.gz
busybox-w32-43cdbaf7283fe1774c163141520370706b02b36a.tar.bz2
busybox-w32-43cdbaf7283fe1774c163141520370706b02b36a.zip
Simplify and remove dependence on PATH_MAX
buf[PATH_MAX] now defined using strlen to the exact size to be used. removed output that was displayed if which didnt find the command, this makes itmatch the behaviour of GNU's which. These modifications result in a slight size decrease. git-svn-id: svn://busybox.net/trunk/busybox@1063 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to '')
-rw-r--r--which.c43
1 files changed, 12 insertions, 31 deletions
diff --git a/which.c b/which.c
index 07c0e0d85..dc162dca5 100644
--- a/which.c
+++ b/which.c
@@ -23,14 +23,12 @@
23 23
24#include "internal.h" 24#include "internal.h"
25#include <stdio.h> 25#include <stdio.h>
26#include <sys/param.h>
27 26
28extern int which_main(int argc, char **argv) 27extern int which_main(int argc, char **argv)
29{ 28{
30 char *path_list, *test, *tmp, *path_parsed; 29 char *path_list, *path_n;
31 char buf[PATH_MAX];
32 struct stat filestat; 30 struct stat filestat;
33 int count = 0; 31 int i, count=0;
34 32
35 if (argc <= 1 || **(argv + 1) == '-') 33 if (argc <= 1 || **(argv + 1) == '-')
36 usage(which_usage); 34 usage(which_usage);
@@ -40,45 +38,28 @@ extern int which_main(int argc, char **argv)
40 if (!path_list) 38 if (!path_list)
41 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; 39 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
42 40
43 path_parsed = xmalloc (strlen(path_list) + 1);
44 strcpy (path_parsed, path_list);
45
46 /* Replace colons with zeros in path_parsed and count them */ 41 /* Replace colons with zeros in path_parsed and count them */
47 count = 1; 42 for(i=strlen(path_list); i > 0; i--)
48 test = path_parsed; 43 if (path_list[i]==':') {
49 while (1) { 44 path_list[i]=0;
50 tmp = strchr(test, ':'); 45 count++;
51 if (tmp == NULL) 46 }
52 break;
53 *tmp = 0;
54 test = tmp + 1;
55 count++;
56 }
57
58 47
59 while(argc-- > 0) { 48 while(argc-- > 0) {
60 int i; 49 path_n = path_list;
61 int found = FALSE;
62 test = path_parsed;
63 argv++; 50 argv++;
64 for (i = 0; i < count; i++) { 51 for (i = 0; i < count; i++) {
65 strcpy (buf, test); 52 char buf[strlen(path_n)+1+strlen(*argv)];
53 strcpy (buf, path_n);
66 strcat (buf, "/"); 54 strcat (buf, "/");
67 strcat (buf, *argv); 55 strcat (buf, *argv);
68 if (stat (buf, &filestat) == 0 56 if (stat (buf, &filestat) == 0
69 && filestat.st_mode & S_IXUSR) 57 && filestat.st_mode & S_IXUSR)
70 { 58 {
71 found = TRUE; 59 printf ("%s\n", buf);
72 break; 60 break;
73 } 61 }
74 test += (strlen(test) + 1); 62 path_n += (strlen(path_n) + 1);
75 }
76 if (found == TRUE)
77 printf ("%s\n", buf);
78 else
79 {
80 printf ("which: no %s in (%s)\n", *argv, path_list);
81 exit (FALSE);
82 } 63 }
83 } 64 }
84 return(TRUE); 65 return(TRUE);