aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--findutils/which.c43
-rw-r--r--which.c43
2 files changed, 24 insertions, 62 deletions
diff --git a/findutils/which.c b/findutils/which.c
index 07c0e0d85..dc162dca5 100644
--- a/findutils/which.c
+++ b/findutils/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);
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);