Subject: Re: pkg/29152: pkgfind patch for linux and openbsd
To: None <pkg-manager@netbsd.org, gnats-admin@netbsd.org,>
From: Peter Postma <peter@pointless.nl>
List: pkgsrc-bugs
Date: 01/30/2005 17:54:01
The following reply was made to PR pkg/29152; it has been noted by GNATS.

From: Peter Postma <peter@pointless.nl>
To: Jan Schaumann <jschauma@netmeister.org>
Cc: "Julio M. Merino Vidal" <jmmv84@gmail.com>, gnats-bugs@netbsd.org
Subject: Re: pkg/29152: pkgfind patch for linux and openbsd
Date: Sun, 30 Jan 2005 18:53:08 +0100

 --OgqxwSJOaUobr8KG
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 On Sun, Jan 30, 2005 at 11:48:56AM -0500, Jan Schaumann wrote:
 > Peter Postma <peter@pointless.nl> wrote:
 >  
 > > [Added jschauma to cc]
 > > 
 > > jschauma@ recently added set/getprogname() to make it more portable.
 > > But if these functions aren't available outside NetBSD then I don't see
 > > how it would be more portable.
 > 
 > Through use of libnbcompat, which we provide for all supported OS.
 > 
 > > 
 > > I see a few ways of fixing this:
 > > 1) get rid of set/getprogname and hardcode it in the usage, as jmmv suggested.
 > > 2) revert the set/getprogname changes and use extern char *__progname again.
 > >    (but will this work everywhere? e.g. IRIX?)
 > > 3) add libnbcompat to systems that don't have set/getprogname.
 > > 
 > > Personally, I would prefer 2) and then 1).
 > 
 > I don't see a problem with (3):  the systems that need it, would likely
 > have libnbcompat installed anyway.
 > 
 > However, the program is small enough so that eliminating the need of
 > these might be more practical.  Plus, it's your program, so it's your
 > call. :-)
 > 
 
 Yes, it's so small that I'd rather fix problem functions then adding
 a dependency... Not that I dislike libnbcompat, but it just too easy
 to fix the need for it.
 
 > > I've attached a diff that attempts to make it more portable, I would
 > > appreciate if you could test it on a non-NetBSD system.
 > 
 > IRIX needs limits.h for PATH_MAX, and it doesn't seem to know
 > __progname.
 > 
 
 Ok, the attached diff should be ok now I think.
 Could you please check it one more time before I commit it?
 
 Thanks,
 -- 
 Peter Postma
 
 --OgqxwSJOaUobr8KG
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename=diff
 
 Index: Makefile
 ===================================================================
 RCS file: /cvsroot/pkgsrc/pkgtools/pkgfind/Makefile,v
 retrieving revision 1.8
 diff -u -r1.8 Makefile
 --- Makefile	27 Jan 2005 20:42:23 -0000	1.8
 +++ Makefile	30 Jan 2005 17:50:08 -0000
 @@ -1,6 +1,6 @@
  # $NetBSD: Makefile,v 1.8 2005/01/27 20:42:23 jschauma Exp $
  
 -DISTNAME=	pkgfind-20050127
 +DISTNAME=	pkgfind-20050130
  CATEGORIES=	pkgtools
  MASTER_SITES=	# empty
  DISTFILES=	# empty
 @@ -32,12 +32,4 @@
  SUBST_SED.path=		-e "s,/usr/pkgsrc,${PKGSRCDIR},g"
  SUBST_MESSAGE.path=	"Adjusting pkgsrc directory."
  
 -.include "../../mk/bsd.prefs.mk"
 -
 -.if ${OPSYS} == "IRIX"
 -CFLAGS+=	-DNEED_LIBNBCOMPAT
 -LDFLAGS+=	-lnbcompat
 -.  include "../../pkgtools/libnbcompat/buildlink3.mk" # need err(1), warn(1)
 -.endif
 -
  .include "../../mk/bsd.pkg.mk"
 Index: files/pkgfind.c
 ===================================================================
 RCS file: /cvsroot/pkgsrc/pkgtools/pkgfind/files/pkgfind.c,v
 retrieving revision 1.5
 diff -u -r1.5 pkgfind.c
 --- files/pkgfind.c	27 Jan 2005 20:42:23 -0000	1.5
 +++ files/pkgfind.c	30 Jan 2005 17:50:08 -0000
 @@ -38,14 +38,9 @@
  #include <sys/param.h>
  #include <sys/stat.h>
  
 -#ifdef NEED_LIBNBCOMPAT
 -#include <nbcompat.h>
 -#else
 -#include <err.h>
 -#endif
 -
  #include <ctype.h>
  #include <dirent.h>
 +#include <errno.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
 @@ -64,7 +59,7 @@
  static int		checkskip(const struct dirent *);
  static int		partialmatch(const char *, const char *);
  static int		exactmatch(const char *, const char *);
 -static void		usage(void);
 +static void		usage(const char *);
  
  static int		(*search)(const char *, const char *);
  
 @@ -73,11 +68,10 @@
  int
  main(int argc, char *argv[])
  {
 +	const char *progname = argv[0];
  	const char *path;
  	int ch;
  
 -	setprogname("pkgfind");
 -
  	/* default searches have partial matches */
  	search = partialmatch;
  
 @@ -98,7 +92,7 @@
  			search = exactmatch;
  			break;
  		default:
 -			usage();
 +			usage(progname);
  			/* NOTREACHED */
  		}
  	}
 @@ -106,7 +100,7 @@
  	argv += optind;
  
  	if (argc < 1)
 -		usage();
 +		usage(progname);
  
  	if ((path = getenv("PKGSRCDIR")) == NULL)
  		path = PKGSRCDIR;
 @@ -126,25 +120,27 @@
  	char *text, *comment = NULL;
  	struct stat sb;
  
 -	if ((ncat = scandir(path, &cat, checkskip, alphasort)) < 0)
 -		err(EXIT_FAILURE, "%s", path);
 +	if ((ncat = scandir(path, &cat, checkskip, alphasort)) < 0) {
 +		fprintf(stderr, "%s: %s\n", path, strerror(errno));
 +		exit(EXIT_FAILURE);
 +	}
  
  	for (i = 0; i < ncat; i++) {
  		if (snprintf(tmp, sizeof(tmp), "%s/%s", path, cat[i]->d_name)
  		    >= sizeof(tmp)) {
 -			warnx("filename too long");
 +			fprintf(stderr, "filename too long\n");
  			continue;
  		}
  		if (stat(tmp, &sb) < 0 || !S_ISDIR(sb.st_mode))
  			continue;
  		if ((nlist = scandir(tmp, &list, checkskip, alphasort)) < 0) {
 -			warn("%s", tmp);
 +			fprintf(stderr, "%s: %s\n", tmp, strerror(errno));
  			continue;
  		}
  		for (j = 0; j < nlist; j++) {
  			if (snprintf(tmp, sizeof(tmp), "%s/%s/%s", path,
  			    cat[i]->d_name, list[j]->d_name) >= sizeof(tmp)) {
 -				warnx("filename too long");
 +				fprintf(stderr, "filename too long\n");
  				continue;
  			}
  			if (stat(tmp, &sb) < 0 || !S_ISDIR(sb.st_mode))
 @@ -177,13 +173,17 @@
  	len = strlen(path) + strlen(cat) + strlen(pkg) + strlen("Makefile") + 3 + 1;
  
  	if (!qflag) {
 -		if ((mk = malloc(len)) == NULL)
 -			err(EXIT_FAILURE, "malloc");
 +		if ((mk = malloc(len)) == NULL) {
 +			fprintf(stderr, "malloc: %s\n", strerror(errno));
 +			exit(EXIT_FAILURE);
 +		}
  		(void)snprintf(mk, len, "%s/%s/%s/Makefile", path, cat, pkg);
  
  		if (getcomment(mk, &comment) == 0) {
 -			if ((mk = realloc(mk, len + 7)) == NULL)
 -				err(EXIT_FAILURE, "malloc");
 +			if ((mk = realloc(mk, len + 7)) == NULL) {
 +				fprintf(stderr, "malloc: %s\n", strerror(errno));
 +				exit(EXIT_FAILURE);
 +			}
  			(void)snprintf(mk, len+7, "%s/%s/%s/Makefile.common",
  			    path, cat, pkg);
  			(void)getcomment(mk, &comment);
 @@ -266,8 +266,8 @@
  }
  
  static void
 -usage(void)
 +usage(const char *progname)
  {
 -	(void)fprintf(stderr, "Usage: %s [-Ccqx] keyword [...]\n", getprogname());
 +	(void)fprintf(stderr, "Usage: %s [-Ccqx] keyword [...]\n", progname);
  	exit(EXIT_FAILURE);
  }
 
 --OgqxwSJOaUobr8KG--