On Friday 02 May 2014 15:42:40 you wrote:
> Tito wrote:
>
> > That's it about PATCH 1.
> > There is still PATCH 2....
> Your patch searches in PATH for dir/file if dir/file is not executable.
> The normal behavior is to not search PATH if the argument contains a '/'.
Fixed. Thanks!
> If you rewrite "which" anyway, you could also change the following points:
> - Remove default PATH, saves some bytes
It mimics the behavior of debianutils which that being a script
sets PATH (from profile?) in its subshell:
debian:~/Desktop/SourceCode/new_bb/busybox$ cp /usr/bin/which .
debian:~/Desktop/SourceCode/new_bb/busybox$ nano which (add echo $PATH)
debian:~/Desktop/SourceCode/new_bb/busybox$ unset PATH
debian:~/Desktop/SourceCode/new_bb/busybox$ echo $PATH
debian:~/Desktop/SourceCode/new_bb/busybox$ ./which pippo
PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
debian:~/Desktop/SourceCode/new_bb/busybox$ echo $PATH
> - Output message if not found (maybe if DESKTOP)
> - Return number of commands not found as per man page (Which returns the
> number of failed arguments, or -1 when no `programname´ was given)
WHICH(1)
WHICH(1)
NAME
which - locate a command
SYNOPSIS
which [-a] filename ...
DESCRIPTION
which returns the pathnames of the files (or links) which would be
executed in the current
environment, had its arguments been given as commands in a strictly
POSIX-conformant shell.
It does this by searching the PATH for executable files matching the
names of the arguments.
It does not follow symbolic links.
OPTIONS
-a print all matching pathnames of each argument
EXIT STATUS
0 if all specified commands are found and executable
1 if one or more specified commands is nonexistent or not executable
2 if an invalid option is specified
Debian 1 May 2009
WHICH(1)
debian:~$ which pippo pluto topolino
debian:~$ echo $?
1
> $ which -v
> GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood.
> $ (unset PATH; /usr/bin/which -a sh bash; echo $?)
> /usr/bin/which: no sh in ((null))
> /usr/bin/which: no bash in ((null))
> 2
>
>
Attached v2 of which patch and a drop in which.c replacement file for
testing purposes.
Ciao,
Tito
This patch removes the #if ENABLE_DESKTOP part from
which relative to the -a option and unifies the code.
Signed-off by Tito Ragusa <[email protected]>
--- debianutils/which.c.orig 2014-05-02 21:17:22.279114884 +0200
+++ debianutils/which.c 2014-05-02 21:20:53.151575421 +0200
@@ -24,75 +24,39 @@
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int which_main(int argc UNUSED_PARAM, char **argv)
{
- IF_DESKTOP(int opt;)
- int status = EXIT_SUCCESS;
+ int found;
char *path;
char *p;
+ char *tmp;
+ int status = 0;
- opt_complementary = "-1"; /* at least one argument */
- IF_DESKTOP(opt =) getopt32(argv, "a");
+ opt_complementary = "-1"; /* at least one argument */
+ getopt32(argv, "a");
argv += optind;
-
- /* This matches what is seen on e.g. ubuntu.
- * "which" there is a shell script. */
- path = getenv("PATH");
- if (!path) {
- path = (char*)bb_PATH_root_path;
- putenv(path);
- path += 5; /* skip "PATH=" */
- }
-
+
do {
-#if ENABLE_DESKTOP
-/* Much bloat just to support -a */
+ found = 0;
+ /* If file contains a slash don't use PATH */
if (strchr(*argv, '/')) {
- if (file_is_executable(*argv)) {
- puts(*argv);
- continue;
+ if(file_is_executable(*argv)) {
+ /* returns non-negative number on success, or EOF (-1) on error */
+ found = puts(*argv);
}
- status = EXIT_FAILURE;
} else {
- char *path2 = xstrdup(path);
- char *tmp = path2;
-
- p = find_executable(*argv, &tmp);
- if (!p)
- status = EXIT_FAILURE;
- else {
- print:
- puts(p);
- free(p);
- if (opt) {
- /* -a: show matches in all PATH components */
- if (tmp) {
- p = find_executable(*argv, &tmp);
- if (p)
- goto print;
- }
- }
+ p = getenv("PATH");
+ if (p == NULL) {
+ p = (char *)bb_default_root_path;
}
- free(path2);
- }
-#else
-/* Just ignoring -a */
- if (strchr(*argv, '/')) {
- if (file_is_executable(*argv)) {
- puts(*argv);
- continue;
- }
- } else {
- char *path2 = xstrdup(path);
- char *tmp = path2;
- p = find_executable(*argv, &tmp);
- free(path2);
- if (p) {
- puts(p);
+ path = tmp = xstrdup(p);
+ while ((p = find_executable(*argv, &tmp)) != NULL) {
+ found = puts(p);
free(p);
- continue;
+ if (!option_mask32) /* -a not set*/
+ break;
}
+ free(path);
}
- status = EXIT_FAILURE;
-#endif
+ status |= (!found);
} while (*(++argv) != NULL);
fflush_stdout_and_exit(status);
/* vi: set sw=4 ts=4: */
/*
* Which implementation for busybox
*
* Copyright (C) 1999-2004 by Erik Andersen <[email protected]>
* Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*
* Based on which from debianutils
*/
//usage:#define which_trivial_usage
//usage: "[COMMAND]..."
//usage:#define which_full_usage "\n\n"
//usage: "Locate a COMMAND"
//usage:
//usage:#define which_example_usage
//usage: "$ which login\n"
//usage: "/bin/login\n"
#include "libbb.h"
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int which_main(int argc UNUSED_PARAM, char **argv)
{
int found;
char *path;
char *p;
char *tmp;
int status = 0;
opt_complementary = "-1"; /* at least one argument */
getopt32(argv, "a");
argv += optind;
do {
found = 0;
/* If file contains a slash don't use PATH */
if (strchr(*argv, '/')) {
if(file_is_executable(*argv)) {
/* returns non-negative number on success, or EOF (-1) on error */
found = puts(*argv);
}
} else {
p = getenv("PATH");
if (p == NULL) {
p = (char *)bb_default_root_path;
}
path = tmp = xstrdup(p);
while ((p = find_executable(*argv, &tmp)) != NULL) {
found = puts(p);
free(p);
if (!option_mask32) /* -a not set*/
break;
}
free(path);
}
status |= (!found);
} while (*(++argv) != NULL);
fflush_stdout_and_exit(status);
}
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox