* [PATCH v2] whereis: search in path
@ 2011-08-12 17:55 Davidlohr Bueso
2011-08-30 9:47 ` Karel Zak
0 siblings, 1 reply; 2+ messages in thread
From: Davidlohr Bueso @ 2011-08-12 17:55 UTC (permalink / raw)
To: Karel Zak, util-linux
Sorry for the delay, lots of things on my plate these days. Below is the patch with the mentioned fixes.
From: Davidlohr Bueso <dave@gnu.org>
Date: Fri, 12 Aug 2011 13:49:59 -0400
Currently this tool only uses the hardcoded paths for looking up strings for binaries, man pages and source code.
Adding directories found in $PATH makes a nice little enhancement to support a wider range of lookups.
This feature was also discussed previously here (http://www.spinics.net/lists/util-linux-ng/msg03429.html)
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
---
misc-utils/whereis.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 68 insertions(+), 1 deletions(-)
diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c
index 08b62e6..6393a70 100644
--- a/misc-utils/whereis.c
+++ b/misc-utils/whereis.c
@@ -37,6 +37,10 @@
* - added Native Language Support
*/
+/* 2011-08-12 Davidlohr Bueso <dave@gnu.org>
+ * - added $PATH lookup
+ */
+
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -45,6 +49,8 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+
+#include "xalloc.h"
#include "nls.h"
#include "c.h"
@@ -124,7 +130,7 @@ static char *srcdirs[] = {
};
static char sflag = 1, bflag = 1, mflag = 1, uflag;
-static char **Sflag, **Bflag, **Mflag;
+static char **Sflag, **Bflag, **Mflag, **dirp, **pathdir;
static int Scnt, Bcnt, Mcnt, count, print;
static void __attribute__ ((__noreturn__)) usage(FILE * out)
@@ -231,11 +237,68 @@ findin(char *dir, char *cp)
}
+static int inpath(const char *str)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(bindirs) - 1 ; i++)
+ if (!strcmp(bindirs[i], str))
+ return 1;
+
+ for (i = 0; i < ARRAY_SIZE(mandirs) - 1; i++)
+ if (!strcmp(mandirs[i], str))
+ return 1;
+
+ for (i = 0; i < ARRAY_SIZE(srcdirs) - 1; i++)
+ if (!strcmp(srcdirs[i], str))
+ return 1;
+
+ return 0;
+}
+
+static void fillpath(void)
+{
+ char *key=NULL, *tmp=NULL, *tok=NULL, *pathcp, *path = getenv("PATH");
+ int i = 0;
+
+
+ if (!path)
+ return;
+ pathcp = xstrdup(path);
+
+ for (tmp = pathcp; ;tmp = NULL, tok) {
+ tok = strtok_r(tmp, ":", &key);
+ if (!tok)
+ break;
+
+ /* make sure we don't repeat the search path */
+ if (inpath(tok))
+ continue;
+
+ pathdir = xrealloc(pathdir, (i + 1) * sizeof(char *));
+ pathdir[i++] = xstrdup(tok);
+ }
+
+ pathdir = xrealloc(pathdir, (i + 1) * sizeof(char *));
+ pathdir[i] = NULL;
+
+ dirp = pathdir;
+ free(pathcp);
+}
+
+static void freepath(void)
+{
+ free(pathdir);
+}
+
static void
findv(char **dirv, int dirc, char *cp)
{
+
while (dirc > 0)
findin(*dirv++, cp), dirc--;
+ while (*dirp)
+ findin(*dirp++, cp);
}
static void
@@ -359,6 +422,8 @@ main(int argc, char **argv)
if (argc == 0)
usage(stderr);
+ fillpath();
+
do
if (argv[0][0] == '-') {
register char *cp = argv[0] + 1;
@@ -411,5 +476,7 @@ main(int argc, char **argv)
} else
lookup(*argv++);
while (--argc > 0);
+
+ freepath();
return EXIT_SUCCESS;
}
--
1.7.4.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] whereis: search in path
2011-08-12 17:55 [PATCH v2] whereis: search in path Davidlohr Bueso
@ 2011-08-30 9:47 ` Karel Zak
0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2011-08-30 9:47 UTC (permalink / raw)
To: Davidlohr Bueso; +Cc: util-linux
On Fri, Aug 12, 2011 at 01:55:42PM -0400, Davidlohr Bueso wrote:
> misc-utils/whereis.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 files changed, 68 insertions(+), 1 deletions(-)
Applied, thanks. ...but:
$ whereis -m git-rm-commit
git-rm-commit: /home/kzak/bin/git-rm-commit
the -m means "man pages only", but the result is executable file.
I have fixed this problem, the $PATH is used for binaries only and
ignored if -B is specified. See below.
Karel
>From 4ff826f68a7d9bd1239644ab53be9b81fc8b7a37 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 30 Aug 2011 11:44:15 +0200
Subject: [PATCH] whereis: search in path for binaries only
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/whereis.c | 15 ++++++++-------
1 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c
index 90f7b90..03ea95e 100644
--- a/misc-utils/whereis.c
+++ b/misc-utils/whereis.c
@@ -299,8 +299,6 @@ findv(char **dirv, int dirc, char *cp)
while (dirc > 0)
findin(*dirv++, cp), dirc--;
- while (*dirp)
- findin(*dirp++, cp);
}
static void
@@ -315,9 +313,11 @@ looksrc(char *cp)
static void
lookbin(char *cp)
{
- if (Bflag == 0)
+ if (Bflag == 0) {
findv(bindirs, ARRAY_SIZE(bindirs)-1, cp);
- else
+ while (*dirp)
+ findin(*dirp++, cp); /* look $PATH */
+ } else
findv(Bflag, Bcnt, cp);
}
@@ -424,8 +424,6 @@ main(int argc, char **argv)
if (argc == 0)
usage(stderr);
- fillpath();
-
do
if (argv[0][0] == '-') {
register char *cp = argv[0] + 1;
@@ -475,8 +473,11 @@ main(int argc, char **argv)
usage(stderr);
}
argv++;
- } else
+ } else {
+ if (Bcnt == 0 && pathdir == NULL)
+ fillpath();
lookup(*argv++);
+ }
while (--argc > 0);
freepath();
--
1.7.6
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-08-30 9:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-12 17:55 [PATCH v2] whereis: search in path Davidlohr Bueso
2011-08-30 9:47 ` Karel Zak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox