whereis: search in path variable
PATCH 1/1 whereis: search in path variable
From: Davidlohr Bueso <[email protected]>
Date: Wed, 20 Jul 2011 00:39:10 -0400
Currently this tool only uses the hardcoded paths for looking up strings for binaries, man pages and source code, adding those directories found in $PATH makes a nice little enhancement.
dave@offbook:~/projects/util-linux/misc-utils$ export PATH=$PATH:/home/dave/whereis-test dave@offbook:~/projects/util-linux/misc-utils$ ./whereis stdlib stdlib: /home/dave/whereis-test/stdlib.h /usr/include/stdlib.h dave@offbook:~/projects/util-linux/misc-utils$ whereis-old stdlib stdlib: /usr/include/stdlib.h
This feature was also discussed previously here (http://www.spinics.net/lists/util-linux-ng/msg03429.html)
--- misc-utils/whereis.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-)
misc-utils/whereis.c +53 -0
@@ -45,6 +45,8 @@#include <stdlib.h> #include <string.h> #include <ctype.h> + +#include "xalloc.h" #include "nls.h" #include "c.h"
@@ -58,6 +60,7 @@ void findv(char **, int, char *);void find (char **,char *);void findin (char *,char *);int itsit (char *,char *); +void fillpath (void );static char *bindirs[] = {"/bin" ,
@@ -144,6 +147,7 @@ int Bcnt;char **Mflag;int Mcnt;char uflag; +char **dirp, **pathdir =NULL ;static void __attribute__ ((__noreturn__))usage (FILE * out) {
@@ -172,6 +176,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)int main (int argc,char **argv) { +int i; +setlocale (LC_ALL ,"" );bindtextdomain (PACKAGE ,LOCALEDIR );textdomain (PACKAGE );
@@ -179,6 +185,9 @@ main(int argc, char **argv) argc--, argv++;if (argc ==0 )usage (stderr); + +fillpath (); + dirp = pathdir; do if (argv[0][0] == '-') {
@@ -232,9 +241,51 @@ main(int argc, char **argv) }else lookup (*argv++);while (--argc >0 ); + +free (pathdir); +return EXIT_SUCCESS ; } +int inpath (const char *str) +{ +int i; + +for (i =0 ; i <sizeof (bindirs)/sizeof (char *) -1 ; i++) +if (!strcmp (bindirs[i], str)) +return 1 ; + +for (i =0 ; i <sizeof (mandirs)/sizeof (char *) -1 ; i++) +if (!strcmp (mandirs[i], str)) +return 1 ; + +for (i =0 ; i <sizeof (srcdirs)/sizeof (char *) -1 ; i++) +if (!strcmp (srcdirs[i], str)) +return 1 ; + +return 0 ; +} + +void fillpath (void ) +{ +char *key=NULL , *tmp=NULL , *tok=NULL , *path =getenv ("PATH" ); +int i =0 ; + +if (!path) +return ; + +for (tmp = path; ;tmp =NULL ) { + tok =strtok_r (tmp,":" , &key); +if (!tok) +break ; +if (inpath (tok))/* make sure we don't repeat the search path */ +continue ; + + pathdir =xrealloc (pathdir, (i +1 ) *sizeof (char *)); + pathdir[i++] = tok; + } +} +void getlist (int *argcp,char ***argvp,char ***flagp,int *cntp) {
@@ -356,6 +407,8 @@ find(char **dirs, char *cp) {while (*dirs)findin (*dirs++, cp); +while (*dirp) +findin (*dirp++, cp); }void
Signature · 1 line
1.7.4.1
Karel Zak
maintainerQuoted 6 lines · Davidlohr Bueso
From: Davidlohr Bueso <[email protected]>
Date: Wed, 20 Jul 2011 00:39:10 -0400Currently this tool only uses the hardcoded paths for looking up strings for binaries, man pages and source code, adding those directories found in $PATH makes a nice little enhancement.
Finally someone who is not lazy to implement it correctly :-)
Quoted 1 line
+char **dirp, **pathdir = NULL;
It's unnecessary to initialize global variables to 0 or NULL.
Review
Quoted 3 lines
main(int argc, char **argv)
{(quoted diff) +1
-0+ int i;
variable not used
misc-utils/whereis.c main(int argc, char **argv) · line 190
Quoted 10 lines
misc-utils/whereis.c +3
-0setlocale (LC_ALL ,"" );bindtextdomain (PACKAGE ,LOCALEDIR );textdomain (PACKAGE );@@ -179,6 +185,9 @@ main(int argc, char **argv) argc--, argv++;if (argc ==0 )usage (stderr); + +fillpath (); + dirp = pathdir;
it would be better to hide 'dirp = pathdir' to fillpath().
misc-utils/whereis.c main(int argc, char **argv) · line 245
Quoted 8 lines
misc-utils/whereis.c +2
-0do if (argv[0][0] == '-') {@@ -232,9 +241,51 @@ main(int argc, char **argv) }else lookup (*argv++);while (--argc >0 ); + +free (pathdir);
add freepath()
misc-utils/whereis.c main(int argc, char **argv) · line 254
Quoted 6 lines
misc-utils/whereis.c +5
-0+int inpath (const char *str) +{ +int i; + +for (i =0 ; i <sizeof (bindirs)/sizeof (char *) -1 ; i++)
^^^^^^^^^^^^^^^^^^^
We have ARRAY_SIZE() in c.h, or you can use the fact that arrays are terminated by zero.
Quoted 14 lines
misc-utils/whereis.c +14
-0+if (!strcmp (bindirs[i], str)) +return 1 ; + +for (i =0 ; i <sizeof (mandirs)/sizeof (char *) -1 ; i++) +if (!strcmp (mandirs[i], str)) +return 1 ; + +for (i =0 ; i <sizeof (srcdirs)/sizeof (char *) -1 ; i++) +if (!strcmp (srcdirs[i], str)) +return 1 ; + +return 0 ; +} +
misc-utils/whereis.c main(int argc, char **argv) · line 278
Quoted 10 lines
misc-utils/whereis.c +10
-0+void fillpath (void ) +{ +char *key=NULL , *tmp=NULL , *tok=NULL , *path =getenv ("PATH" ); +int i =0 ; + +if (!path) +return ; + +for (tmp = path; ;tmp =NULL ) { + tok =strtok_r (tmp,":" , &key);
man getenv, you should not modify the result from getenv().
misc-utils/whereis.c main(int argc, char **argv) · line 286
Quoted 8 lines
misc-utils/whereis.c +8
-0+if (!tok) +break ; +if (inpath (tok))/* make sure we don't repeat the search path */ +continue ; + + pathdir =xrealloc (pathdir, (i +1 ) *sizeof (char *)); + pathdir[i++] = tok; + }
here is bug, see below to gdb backtrace...
misc-utils/whereis.c find(char **dirs, char *cp) · line 410
Quoted 9 lines
misc-utils/whereis.c +2
-0+void getlist (int *argcp,char ***argvp,char ***flagp,int *cntp) {@@ -356,6 +407,8 @@ find(char **dirs, char *cp) {while (*dirs)findin (*dirs++, cp); +while (*dirp)
^^^^^^
misc-utils/whereis.c find(char **dirs, char *cp) · line 411
Quoted 1 line
misc-utils/whereis.c +1
-0+findin (*dirp++, cp);
... this code expects that the array is terminated by zero.
Note that find() is called always for all dir lists.
Maybe it would be better to add lookpathenv() and call it from print_again(). You can also add -p options to control this behavior. See how {s,b,m}flags work.
BTW, the whole whereis code is horrible, for example find() is completely unnecessary if there is also findv() and all lists of the directories are static. It should be possible to use
findv(ary, ARRAY_SIZE(ary), str);
everywhere instead of find(ary, str);
Karel
Starting program: /home/projects/util-linux/util-linux/misc-utils/whereis lsblk
Program received signal SIGSEGV, Segmentation fault. 0x0000003cbae7edfa in __strchr_sse2 () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.14-4.x86_64 (gdb) bt
#0 0x0000003cbae7edfa in __strchr_sse2 () from /lib64/libc.so.6 #1 0x00000000004013c8 in findin (dir=0x1ff41 <Address 0x1ff41 out of bounds>,
cp=0x7fffffffe477 "lsblk") at whereis.c:434
#2 0x0000000000401623 in find (dirs=<optimized out>,
cp=0x7fffffffe477 "lsblk") at whereis.c:421
#3 0x00000000004017a8 in print_again (cp=0x7fffffffe477 "lsblk")
at whereis.c:327#4 0x0000000000401870 in lookup (cp=0x7fffffffe477 "lsblk") at whereis.c:374 #5 0x0000000000400c9b in main (argc=1, argv=0x7fffffffe148) at whereis.c:242
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 9 lines · Karel Zak
Quoted 6 lines · Davidlohr Bueso
From: Davidlohr Bueso <[email protected]>
Date: Wed, 20 Jul 2011 00:39:10 -0400Currently this tool only uses the hardcoded paths for looking up strings for binaries, man pages and source code, adding those directories found in $PATH makes a nice little enhancement.
Finally someone who is not lazy to implement it correctly :-)
...
Quoted 1 line
man getenv, you should not modify the result from
getenv().
ouch, sorry!
Quoted 26 lines
misc-utils/whereis.c main(int argc, char **argv) · line 286
Quoted 9 lines
misc-utils/whereis.c +8
-0+if (!tok) +break ; +if (inpath (tok))/* make sure we don't repeat the search path */ +continue ; + + pathdir =xrealloc (pathdir, (i +1 ) *sizeof (char *)); + pathdir[i++] = tok; + }here is bug, see below to gdb backtrace...
misc-utils/whereis.c find(char **dirs, char *cp) · line 410
Quoted 9 lines
misc-utils/whereis.c +2
-0+void getlist (int *argcp,char ***argvp,char ***flagp,int *cntp) {@@ -356,6 +407,8 @@ find(char **dirs, char *cp) {while (*dirs)findin (*dirs++, cp); +while (*dirp)^^^^^^misc-utils/whereis.c find(char **dirs, char *cp) · line 411
Quoted 1 line
misc-utils/whereis.c +1
-0+findin (*dirp++, cp);... this code expects that the array is terminated by zero.
Quoted 5 lines
Note that
find()is called always for all dir lists.Maybe it would be better to add
lookpathenv()and call it fromprint_again(). You can also add -p options to control this behavior. See how {s,b,m}flags work.
That was my initial design, however since src, bin and man are _types_ of files, it seems that adding a similar behavior for search _paths_ is like mixing apples and pears.
Something I am thinking of, but won't do it quite yet, is to simply rewrite whereis and, instead of hardcoding paths, recursively search from / and use stat + heuristics to differentiate the different files.
Quoted 2 lines
BTW, the whole whereis code is horrible,
Like most BSD '80s code is. I was hoping you weren't going to ask for cleanups :)
Quoted 6 lines
for example
find()is completely
unnecessary if there is alsofindv()and all lists of the directories are static. It should be possible to usefindv(ary, ARRAY_SIZE(ary), str);
Quoted 7 lines
everywhere instead of
find(ary, str);Karel
Starting program:
/home/projects/util-linux/util-linux/misc-utils/whereislsblkProgram received signal SIGSEGV, Segmentation fault.
That'll teach me not to code half asleep. I'm sending you some cleanup patches and then I'll add these fixes to the program.
Quoted 5 lines
0x0000003cbae7edfain__strchr_sse2() from/lib64/libc.so.6Missing separate debuginfos, use: debuginfo-install glibc-2.14-4.x86_64 (gdb) bt
#00x0000003cbae7edfain__strchr_sse2() from/lib64/libc.so.6#10x00000000004013c8in findin (dir=0x1ff41 <Address0x1ff41out of bounds>,
Quoted 7 lines
cp=0x7fffffffe477 "lsblk") at whereis.c:434 #2 0x0000000000401623 in find (dirs=<optimized out>, cp=0x7fffffffe477 "lsblk") at whereis.c:421 #3 0x00000000004017a8 in print_again (cp=0x7fffffffe477 "lsblk") at whereis.c:327#4
0x0000000000401870in lookup (cp=0x7fffffffe477 "lsblk") at whereis.c:374 #50x0000000000400c9bin main (argc=1, argv=0x7fffffffe148) at whereis.c:242
- Davidlohr