From owner-freebsd-bugs Sun Dec 30 7:20: 9 2001 Delivered-To: freebsd-bugs@freebsd.org Received: from sushi.linuxforlesbians.org (cc265612-b.albqrq1.nm.home.com [67.165.160.99]) by hub.freebsd.org (Postfix) with ESMTP id 25F8F37B419 for ; Sun, 30 Dec 2001 07:20:01 -0800 (PST) Received: by sushi.linuxforlesbians.org (Postfix, from userid 1001) id 25A39194F; Sun, 30 Dec 2001 08:21:51 -0700 (MST) Date: Sun, 30 Dec 2001 08:21:50 -0700 From: Peter Sanchez To: Sheldon Hearn Cc: freebsd-bugs@freebsd.org Subject: Re: bin/32807: which utility replacement in C Message-ID: <20011230082150.C17916@sushi.linuxforlesbians.org> References: <89022.1009717138@axl.seasidesoftware.co.za> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <89022.1009717138@axl.seasidesoftware.co.za>; from sheldonh@starjuice.net on Sun, Dec 30, 2001 at 02:58:58PM +0200 Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org > On 15 Dec 2001 00:42:07 +0100, Dag-Erling Smorgrav wrote: > > Since you've been driving the review process, will you don an asbestos > suit and use this one to kill the existing perl script? :-) LOL. Im the one being killed over here ;) > Note that there's a malloc() that isn't tested for failure, but it's > hidden in a strdup(). This is in findprog(). > > Ciao, > Sheldon. Here ya go... Peter /* * which - a which utility for Unix * * SYNOPSIS: For FreeBSD-4.x and later * * DESCRIPTION: * Utility to get the full system path of an executable in the * users PATH environment variable. * * Works for: * FreeBSD 4.x and probably most earlier versions * * AUTHOR: Peter Sanchez */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #define NGROUPS 15 struct pathinfo { struct pathinfo *next; char *path; }; struct pathinfo *all = NULL; int found = 1; void usage(void) { (void)fprintf(stderr, "usage: which [-a] [-s] program ...\n"); exit(EX_USAGE); } int file_exists(char *file) { struct stat info; int check; check = stat(file, &info); if (check == -1) return check; /* file doesnt exist */ return (info.st_mode & S_IFREG && access(file, X_OK) == 0) ? check : -1; } void losepath(void) { struct pathinfo *tmp; while (all != NULL) { tmp = all->next; free(all); all = tmp; } } void findpath(void) { struct pathinfo *cur = NULL; char *userpath = getenv("PATH"); if (userpath == NULL || strlen(userpath) == 0) errx(EX_OSERR, "No PATH variable."); all = (struct pathinfo *)malloc(sizeof(struct pathinfo)); if (all == NULL) err(EX_OSERR, (char *)NULL); /* NOTREACHED */ cur = all; while ((cur->path = strsep(&userpath, ":")) != NULL) { cur->next = (struct pathinfo *)malloc(sizeof(struct pathinfo)); if (cur->next == NULL) { losepath(); err(EX_OSERR, (char *)NULL); /* NOTREACHED */ } if (strlen(cur->path) == 0) warnx("PATH element of 0 length."); cur = cur->next; } cur->next = NULL; cur = all; } void findprog(char *prog, int aflag, int sflag) { struct pathinfo *tmp; char *tmpbuf; tmp = all; while (all != NULL) { if (all->path == NULL) break; if (strchr(prog, '/')) { tmpbuf = strdup(prog); if (tmpbuf == NULL) { losepath(); err(EX_OSERR, (char *)NULL); /* NOTREACHED */ } } else { tmpbuf = malloc(strlen(all->path) + (strlen(prog) + 1)); if (tmpbuf == NULL) { losepath(); err(EX_OSERR, (char *)NULL); /* NOTREACHED */ } sprintf(tmpbuf, "%s/%s", all->path, prog); } if (file_exists(tmpbuf) == 0) { found = 0; if (sflag && aflag) ; else if (sflag && !aflag) { all = tmp; free(tmpbuf); return; } else if (aflag && !sflag) (void)printf("%s\n", tmpbuf); else { (void)printf("%s\n", tmpbuf); all = tmp; free(tmpbuf); return; } } all = all->next; free(tmpbuf); } all = tmp; } int main(int argc, char *argv[]) { char ch; int aflag, sflag, i; aflag = sflag = 0; if (argc < 2) exit(EX_USAGE); while ((ch = getopt(argc, argv, "ash?")) != -1) { switch (ch) { case 'a': aflag = 1; break; case 's': sflag = 1; break; case 'h': case '?': default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; findpath(); for (i = 0; i < argc; i++) findprog(argv[i], aflag, sflag); losepath(); return sflag ? found : 0; } -- Peter Sanchez, aka fut0n | "The ability to read is what - fut0n@linuxforlesbians.org | distinguishes Unix users from - www.linuxforlesbians.org | those of more popular platforms." - FreeBSD or DIE | - John Lasser To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message