From owner-freebsd-bugs Fri Dec 14 13:35:56 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 EF06337B419 for ; Fri, 14 Dec 2001 13:35:41 -0800 (PST) Received: by sushi.linuxforlesbians.org (Postfix, from userid 1001) id DBA5218C0; Fri, 14 Dec 2001 14:37:27 -0700 (MST) Date: Fri, 14 Dec 2001 14:37:27 -0700 From: Peter Sanchez To: Dag-Erling Smorgrav Cc: freebsd-bugs@freebsd.org Subject: Re: bin/32807: which utility replacement in C Message-ID: <20011214143727.A41700@sushi.linuxforlesbians.org> References: <200112132100.fBDL01r64612@freefall.freebsd.org> <20011214070038.B38914@sushi.linuxforlesbians.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-md5; protocol="application/pgp-signature"; boundary="UlVJffcvxoiEqYs2" Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: ; from des@ofug.org on Fri, Dec 14, 2001 at 04:01:21PM +0100 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 --UlVJffcvxoiEqYs2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Struct rules, but understandable. I like the style(9) format, makes it look much more structured and readable. Here ya go, I hope this is what you had in mind. 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=20 * users PATH environment variable. * * Works for: * FreeBSD 4.x and probably most earlier versions * * AUTHOR: Peter Sanchez */ #ifndef lint static const char rcsid[] =3D "$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 =3D NULL; int found =3D 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 =3D stat(file, &info); if (check =3D=3D -1) return check; /* file doesnt exist */ if (info.st_mode & S_IFREG && access(file, X_OK) =3D=3D 0) return check; else return -1; } void losepath(void) { struct pathinfo *tmp; while (all !=3D NULL) { tmp =3D all->next; free(all); all =3D tmp; } return; } void findpath(void) { struct pathinfo *cur =3D NULL; char *userpath =3D getenv("PATH"); =20 if (userpath =3D=3D NULL || strlen(userpath) =3D=3D 0) errx(EX_OSERR, "No PATH variable."); all =3D (struct pathinfo *)malloc(sizeof(struct pathinfo)); if (all =3D=3D NULL) err(EX_OSERR, (char *)NULL); /* NOTREACHED */ cur =3D all; while ((cur->path =3D strsep(&userpath, ":")) !=3D NULL) { cur->next =3D (struct pathinfo *)malloc(sizeof(struct pathinfo)); if (cur->next =3D=3D NULL) { losepath(); err(EX_OSERR, (char *)NULL); /* NOTREACHED */ } if (strlen(cur->path) =3D=3D 0) warnx("PATH element of 0 length."); cur =3D cur->next; } cur->next =3D NULL; cur =3D all; return; } void findprog(char *prog, int aflag, int sflag) { struct pathinfo *tmp; char *tmpbuf; tmp =3D all; while (all !=3D NULL) { if (all->path =3D=3D NULL) break; if (strchr(prog, '/')) tmpbuf =3D strdup(prog); else { tmpbuf =3D malloc(sizeof(char) * (strlen(all->path) + strlen(prog) + 1)); if (tmpbuf =3D=3D NULL) { losepath(); err(EX_OSERR, (char *)NULL); /* NOTREACHED */ } sprintf(tmpbuf,"%s/%s",all->path,prog); } if (file_exists(tmpbuf) =3D=3D 0) { found =3D 0; if (sflag && aflag) ; else if (sflag && !aflag) { all =3D tmp; free(tmpbuf); return; } else if (aflag && !sflag) (void)printf("%s\n", tmpbuf); else { (void)printf("%s\n", tmpbuf); all =3D tmp; free(tmpbuf); return; } } all =3D all->next; free(tmpbuf); } all =3D tmp; return; } int main(int argc, char *argv[]) { char ch; int aflag, sflag, i; aflag =3D sflag =3D 0; if (argc < 2) exit(EX_USAGE); =20 while ((ch =3D getopt(argc, argv, "ash?")) !=3D -1) { switch (ch) { case 'a': aflag =3D 1; break; case 's': sflag =3D 1; break; case 'h': case '?': default: usage(); /* NOTREACHED */ } } argc -=3D optind; argv +=3D optind; =20 findpath(); for (i =3D 0; i < argc; i++) findprog(argv[i], aflag, sflag); =20 losepath(); return sflag ? found : 0; } On Fri, Dec 14, 2001 at 04:01:21PM +0100, Dag-Erling Smorgrav wrote: > Peter Sanchez writes: > > Thanks for clearing that up. I cleaned up the code, and got rid > > of all the assigned buffers and added access(). Here is the updated sou= rce. >=20 > Please read style(9). >=20 > DES > --=20 > Dag-Erling Smorgrav - des@ofug.org >=20 > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-bugs" in the body of the message --=20 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 --UlVJffcvxoiEqYs2 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (FreeBSD) Comment: For info see http://www.gnupg.org iEYEARECAAYFAjwacRcACgkQQ2cuXVjoDltBrACfU3oFE12i/27qGq5FZv7zEhvO gfIAnjnX2B+rbpbgjcZsSLsKVddIpk4g =2RRH -----END PGP SIGNATURE----- --UlVJffcvxoiEqYs2-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message