Subject: bin/2621: Rdist cleanups
To: None <gnats-bugs@NetBSD.ORG>
From: Christos Zoulas <christos@deshaw.com>
List: netbsd-bugs
Date: 07/11/1996 18:09:20
>Number:         2621
>Category:       bin
>Synopsis:       Rdist cleanups
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 11 18:35:01 1996
>Last-Modified:
>Originator:     Christos Zoulas
>Organization:
	
>Release:        1.2_ALPHA
>Environment:
System: NetBSD ramoth.nyc.deshaw.com 1.2_ALPHA NetBSD 1.2_ALPHA (ZEOS_AIC) #9: Fri Jun 14 13:48:27 EDT 1996 christos@ramoth.nyc.deshaw.com:/usr/src/sys/arch/i386/compile/ZEOS_AIC i386


>Description:
	1. Use snprintf instead of sprintf everywhere.
	2. Use regcomp/regexec instead of re_comp/re_exec.

	The following fixes have not been tested, but they should work.

>How-To-Repeat:
	N/A
>Fix:
Index: Makefile
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/Makefile,v
retrieving revision 1.3
diff -c -r1.3 Makefile
*** Makefile	1994/03/07 05:05:16	1.3
--- Makefile	1996/07/11 22:03:34
***************
*** 9,15 ****
  BINMODE=4555
  CLEANFILES=y.tab.h
  
- LDADD=	-lcompat
- DPADD=	${LIBCOMPAT}
- 
  .include <bsd.prog.mk>
--- 9,12 ----
Index: defs.h
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/defs.h,v
retrieving revision 1.5
diff -c -r1.5 defs.h
*** defs.h	1994/03/07 05:05:20	1.5
--- defs.h	1996/07/11 22:03:34
***************
*** 150,156 ****
  extern struct passwd *pw;	/* pointer to static area used by getpwent */
  extern struct group *gr;	/* pointer to static area used by getgrent */
  extern char host[];		/* host name of master copy */
! extern char buf[];		/* general purpose buffer */
  
  int	 any __P((int, char *));
  char	*colon __P((char *));
--- 150,156 ----
  extern struct passwd *pw;	/* pointer to static area used by getpwent */
  extern struct group *gr;	/* pointer to static area used by getgrent */
  extern char host[];		/* host name of master copy */
! extern char buf[BUFSIZ];	/* general purpose buffer */
  
  int	 any __P((int, char *));
  char	*colon __P((char *));
Index: docmd.c
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/docmd.c,v
retrieving revision 1.6
diff -c -r1.6 docmd.c
*** docmd.c	1994/03/07 05:05:26	1.6
--- docmd.c	1996/07/11 22:03:34
***************
*** 39,44 ****
--- 39,45 ----
  #include "defs.h"
  #include <setjmp.h>
  #include <netdb.h>
+ #include <regex.h>
  
  FILE	*lfp;			/* log file for recording files updated */
  struct	subcmd *subcmds;	/* list of sub-commands for current cmd */
***************
*** 233,239 ****
  		ruser = user;
  	if (!qflag)
  		printf("updating host %s\n", rhost);
! 	(void) sprintf(buf, "%s -Server%s", _PATH_RDIST, qflag ? " -q" : "");
  	if (port < 0) {
  		struct servent *sp;
  
--- 234,241 ----
  		ruser = user;
  	if (!qflag)
  		printf("updating host %s\n", rhost);
! 	(void) snprintf(buf, sizeof(buf), "%s -Server%s",
! 	    _PATH_RDIST, qflag ? " -q" : "");
  	if (port < 0) {
  		struct servent *sp;
  
***************
*** 530,536 ****
  	/*
  	 * Create a pipe to mailling program.
  	 */
! 	(void)sprintf(buf, "%s -oi -t", _PATH_SENDMAIL);
  	pf = popen(buf, "w");
  	if (pf == NULL) {
  		error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
--- 532,538 ----
  	/*
  	 * Create a pipe to mailling program.
  	 */
! 	(void) snprintf(buf, sizeof(buf), "%s -oi -t", _PATH_SENDMAIL);
  	pf = popen(buf, "w");
  	if (pf == NULL) {
  		error("notify: \"%s\" failed\n", _PATH_SENDMAIL);
***************
*** 593,598 ****
--- 595,602 ----
  {
  	register struct	subcmd *sc;
  	register struct	namelist *nl;
+ 	int err;
+ 	regex_t s;
  
  	if (debug)
  		printf("except(%s)\n", file);
***************
*** 606,613 ****
  					return(1);
  				continue;
  			}
! 			re_comp(nl->n_name);
! 			if (re_exec(file) > 0)
  				return(1);
  		}
  	}
--- 610,621 ----
  					return(1);
  				continue;
  			}
! 			if ((err = regcomp(&s, nl->n_name, 0)) != 0) {
! 				char ebuf[BUFSIZ];
! 				(void) regerror(err, &s, ebuf, sizeof(ebuf));
! 				error("%s: %s\n", nl->n_name, ebuf);
! 			}
! 			if (regexec(&s, file, 0, NULL, 0) == 0)
  				return(1);
  		}
  	}
Index: expand.c
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/expand.c,v
retrieving revision 1.5
diff -c -r1.5 expand.c
*** expand.c	1994/03/07 05:05:28	1.5
--- expand.c	1996/07/11 22:03:35
***************
*** 181,192 ****
  			*tail = savec;
  		if (tp != NULL) {
  			for (; tp != NULL; tp = tp->n_next) {
! 				sprintf(buf, "%s%s%s", s, tp->n_name, tail);
  				expstr(buf);
  			}
  			return;
  		}
! 		sprintf(buf, "%s%s", s, tail);
  		expstr(buf);
  		return;
  	}
--- 181,193 ----
  			*tail = savec;
  		if (tp != NULL) {
  			for (; tp != NULL; tp = tp->n_next) {
! 				snprintf(buf, sizeof(buf), 
! 				    "%s%s%s", s, tp->n_name, tail);
  				expstr(buf);
  			}
  			return;
  		}
! 		snprintf(buf, sizeof(buf), "%s%s", s, tail);
  		expstr(buf);
  		return;
  	}
Index: gram.y
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/gram.y,v
retrieving revision 1.3
diff -c -r1.3 gram.y
*** gram.y	1994/03/07 05:05:30	1.3
--- gram.y	1996/07/11 22:03:35
***************
*** 163,175 ****
  			$$ = $1;
  		}
  		| PATTERN namelist SM = {
! 			struct namelist *nl;
! 			char *cp, *re_comp();
! 
! 			for (nl = $2; nl != NULL; nl = nl->n_next)
! 				if ((cp = re_comp(nl->n_name)) != NULL)
! 					yyerror(cp);
! 			$1->sc_args = expand($2, E_VARS);
  			$$ = $1;
  		}
  		| SPECIAL opt_namelist STRING SM = {
--- 163,170 ----
  			$$ = $1;
  		}
  		| PATTERN namelist SM = {
! 			if ($2 != NULL)
! 				$1->sc_args = expand($2, E_VARS);
  			$$ = $1;
  		}
  		| SPECIAL opt_namelist STRING SM = {
Index: lookup.c
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/lookup.c,v
retrieving revision 1.3
diff -c -r1.3 lookup.c
*** lookup.c	1994/03/07 05:05:33	1.3
--- lookup.c	1996/07/11 22:03:35
***************
*** 142,148 ****
  			continue;
  		if (action != LOOKUP) {
  			if (action != INSERT || s->s_type != CONST) {
! 				(void)sprintf(buf, "%s redefined", name);
  				yyerror(buf);
  			}
  		}
--- 142,149 ----
  			continue;
  		if (action != LOOKUP) {
  			if (action != INSERT || s->s_type != CONST) {
! 				(void)snprintf(buf, sizeof(buf),
! 				    "%s redefined", name);
  				yyerror(buf);
  			}
  		}
***************
*** 150,156 ****
  	}
  
  	if (action == LOOKUP) {
! 		(void)sprintf(buf, "%s undefined", name);
  		yyerror(buf);
  		return(NULL);
  	}
--- 151,157 ----
  	}
  
  	if (action == LOOKUP) {
! 		(void)snprintf(buf, sizeof(buf), "%s undefined", name);
  		yyerror(buf);
  		return(NULL);
  	}
Index: server.c
===================================================================
RCS file: /a/cvsroot/src/usr.bin/rdist/server.c,v
retrieving revision 1.8
diff -c -r1.8 server.c
*** server.c	1996/05/21 12:11:54	1.8
--- server.c	1996/07/11 22:03:40
***************
*** 91,97 ****
  
  	rem = 0;
  	oumask = umask(0);
! 	(void) sprintf(buf, "V%d\n", VERSION);
  	(void) write(rem, buf, strlen(buf));
  
  	for (;;) {
--- 91,97 ----
  
  	rem = 0;
  	oumask = umask(0);
! 	(void) snprintf(buf, sizeof(buf), "V%d\n", VERSION);
  	(void) write(rem, buf, strlen(buf));
  
  	for (;;) {
***************
*** 276,282 ****
  	/*
  	 * Pass the destination file/directory name to remote.
  	 */
! 	(void) sprintf(buf, "%c%s\n", destdir ? 'T' : 't', dest);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
--- 276,282 ----
  	/*
  	 * Pass the destination file/directory name to remote.
  	 */
! 	(void) snprintf(buf, sizeof(buf), "%c%s\n", destdir ? 'T' : 't', dest);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
***************
*** 332,345 ****
  			log(lfp, "%s: no password entry for uid %d \n",
  				target, stb.st_uid);
  			pw = NULL;
! 			(void)sprintf(user, ":%lu", stb.st_uid);
  		}
  	if (gr == NULL || gr->gr_gid != stb.st_gid)
  		if ((gr = getgrgid(stb.st_gid)) == NULL) {
  			log(lfp, "%s: no name for group %d\n",
  				target, stb.st_gid);
  			gr = NULL;
! 			(void)sprintf(group, ":%lu", stb.st_gid);
  		}
  	if (u == 1) {
  		if (opts & VERIFY) {
--- 332,345 ----
  			log(lfp, "%s: no password entry for uid %d \n",
  				target, stb.st_uid);
  			pw = NULL;
! 			(void)snprintf(user, sizeof(user), ":%lu", stb.st_uid);
  		}
  	if (gr == NULL || gr->gr_gid != stb.st_gid)
  		if ((gr = getgrgid(stb.st_gid)) == NULL) {
  			log(lfp, "%s: no name for group %d\n",
  				target, stb.st_gid);
  			gr = NULL;
! 			(void)snprintf(group, sizeof(group), ":%lu", stb.st_gid);
  		}
  	if (u == 1) {
  		if (opts & VERIFY) {
***************
*** 356,363 ****
  			error("%s: %s\n", target, strerror(errno));
  			return;
  		}
! 		(void) sprintf(buf, "D%o %04o 0 0 %s %s %s\n", opts,
! 			stb.st_mode & 07777, protoname(), protogroup(), rname);
  		if (debug)
  			printf("buf = %s", buf);
  		(void) write(rem, buf, strlen(buf));
--- 356,364 ----
  			error("%s: %s\n", target, strerror(errno));
  			return;
  		}
! 		(void) snprintf(buf, sizeof(buf), "D%o %04o 0 0 %s %s %s\n",
! 		    opts, stb.st_mode & 07777, protoname(), protogroup(),
! 		    rname);
  		if (debug)
  			printf("buf = %s", buf);
  		(void) write(rem, buf, strlen(buf));
***************
*** 404,414 ****
  			if ((lp = savelink(&stb)) != NULL) {
  				/* install link */
  				if (*lp->target == 0)
! 				(void) sprintf(buf, "k%o %s %s\n", opts,
! 					lp->pathname, rname);
  				else
! 				(void) sprintf(buf, "k%o %s/%s %s\n", opts,
! 					lp->target, lp->pathname, rname);
  				if (debug)
  					printf("buf = %s", buf);
  				(void) write(rem, buf, strlen(buf));
--- 405,416 ----
  			if ((lp = savelink(&stb)) != NULL) {
  				/* install link */
  				if (*lp->target == 0)
! 				(void) snprintf(buf, sizeof(buf), "k%o %s %s\n",
! 				    opts, lp->pathname, rname);
  				else
! 				(void) snprintf(buf, sizeof(buf),
! 				    "k%o %s/%s %s\n", opts, lp->target,
! 				    lp->pathname, rname);
  				if (debug)
  					printf("buf = %s", buf);
  				(void) write(rem, buf, strlen(buf));
***************
*** 416,422 ****
  				return;
  			}
  		}
! 		(void) sprintf(buf, "K%o %o %qd %ld %s %s %s\n", opts,
  			stb.st_mode & 07777, stb.st_size, stb.st_mtime,
  			protoname(), protogroup(), rname);
  		if (debug)
--- 418,425 ----
  				return;
  			}
  		}
! 		(void) snprintf(buf, sizeof(buf),
! 			"K%o %o %qd %ld %s %s %s\n", opts,
  			stb.st_mode & 07777, stb.st_size, stb.st_mtime,
  			protoname(), protogroup(), rname);
  		if (debug)
***************
*** 452,462 ****
  		if ((lp = savelink(&stb)) != NULL) {
  			/* install link */
  			if (*lp->target == 0)
! 			(void) sprintf(buf, "k%o %s %s\n", opts,
  				lp->pathname, rname);
  			else
! 			(void) sprintf(buf, "k%o %s/%s %s\n", opts,
! 				lp->target, lp->pathname, rname);
  			if (debug)
  				printf("buf = %s", buf);
  			(void) write(rem, buf, strlen(buf));
--- 455,465 ----
  		if ((lp = savelink(&stb)) != NULL) {
  			/* install link */
  			if (*lp->target == 0)
! 			(void) snprintf(buf, sizeof(buf), "k%o %s %s\n", opts,
  				lp->pathname, rname);
  			else
! 			(void) snprintf(buf, sizeof(buf), "k%o %s/%s %s\n",
! 			    opts, lp->target, lp->pathname, rname);
  			if (debug)
  				printf("buf = %s", buf);
  			(void) write(rem, buf, strlen(buf));
***************
*** 469,475 ****
  		error("%s: %s\n", target, strerror(errno));
  		return;
  	}
! 	(void) sprintf(buf, "R%o %o %qd %ld %s %s %s\n", opts,
  		stb.st_mode & 07777, stb.st_size, stb.st_mtime,
  		protoname(), protogroup(), rname);
  	if (debug)
--- 472,478 ----
  		error("%s: %s\n", target, strerror(errno));
  		return;
  	}
! 	(void) snprintf(buf, sizeof(buf), "R%o %o %qd %ld %s %s %s\n", opts,
  		stb.st_mode & 07777, stb.st_size, stb.st_mtime,
  		protoname(), protogroup(), rname);
  	if (debug)
***************
*** 507,513 ****
  		log(lfp, "special \"%s\"\n", sc->sc_name);
  		if (opts & VERIFY)
  			continue;
! 		(void) sprintf(buf, "SFILE=%s;%s\n", target, sc->sc_name);
  		if (debug)
  			printf("buf = %s", buf);
  		(void) write(rem, buf, strlen(buf));
--- 510,517 ----
  		log(lfp, "special \"%s\"\n", sc->sc_name);
  		if (opts & VERIFY)
  			continue;
! 		(void) snprintf(buf, sizeof(buf), "SFILE=%s;%s\n", target,
! 		    sc->sc_name);
  		if (debug)
  			printf("buf = %s", buf);
  		(void) write(rem, buf, strlen(buf));
***************
*** 566,572 ****
  	/*
  	 * Check to see if the file exists on the remote machine.
  	 */
! 	(void) sprintf(buf, "Q%s\n", rname);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
--- 570,576 ----
  	/*
  	 * Check to see if the file exists on the remote machine.
  	 */
! 	(void) snprintf(buf, sizeof(buf), "Q%s\n", rname);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
***************
*** 657,663 ****
  	struct stat stb;
  
  	if (catname)
! 		(void) sprintf(tp, "/%s", name);
  
  	if (lstat(target, &stb) < 0) {
  		if (errno == ENOENT)
--- 661,668 ----
  	struct stat stb;
  
  	if (catname)
! 		(void) snprintf(tp, sizeof(target) - (tp - target), "/%s",
! 		    name);
  
  	if (lstat(target, &stb) < 0) {
  		if (errno == ENOENT)
***************
*** 670,676 ****
  
  	switch (stb.st_mode & S_IFMT) {
  	case S_IFREG:
! 		(void) sprintf(buf, "Y%qd %ld\n", stb.st_size,
  		    stb.st_mtime);
  		(void) write(rem, buf, strlen(buf));
  		break;
--- 675,681 ----
  
  	switch (stb.st_mode & S_IFMT) {
  	case S_IFREG:
! 		(void) snprintf(buf, sizeof(buf), "Y%qd %ld\n", stb.st_size,
  		    stb.st_mtime);
  		(void) write(rem, buf, strlen(buf));
  		break;
***************
*** 770,776 ****
  					return;
  				}
  				buf[0] = '\0';
! 				(void) sprintf(buf + 1,
  					"%s: Warning: remote mode %o != local mode %o\n",
  					target, stb.st_mode & 07777, mode);
  				(void) write(rem, buf, strlen(buf + 1) + 1);
--- 775,781 ----
  					return;
  				}
  				buf[0] = '\0';
! 				(void) snprintf(buf + 1, sizeof(buf) - 1, 
  					"%s: Warning: remote mode %o != local mode %o\n",
  					target, stb.st_mode & 07777, mode);
  				(void) write(rem, buf, strlen(buf + 1) + 1);
***************
*** 790,804 ****
  	}
  
  	if (catname)
! 		(void) sprintf(tp, "/%s", cp);
  	cp = rindex(target, '/');
  	if (cp == NULL)
  		strcpy(new, tempname);
  	else if (cp == target)
! 		(void) sprintf(new, "/%s", tempname);
  	else {
  		*cp = '\0';
! 		(void) sprintf(new, "%s/%s", target, tempname);
  		*cp = '/';
  	}
  
--- 795,809 ----
  	}
  
  	if (catname)
! 		(void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
  	cp = rindex(target, '/');
  	if (cp == NULL)
  		strcpy(new, tempname);
  	else if (cp == target)
! 		(void) snprintf(new, sizeof(new), "/%s", tempname);
  	else {
  		*cp = '\0';
! 		(void) snprintf(new, sizeof(new), "%s/%s", target, tempname);
  		*cp = '/';
  	}
  
***************
*** 898,904 ****
  		(void) fclose(f2);
  		if (opts & VERIFY) {
  differ:			buf[0] = '\0';
! 			(void) sprintf(buf + 1, "need to update: %s\n",target);
  			(void) write(rem, buf, strlen(buf + 1) + 1);
  			goto badnew2;
  		}
--- 903,910 ----
  		(void) fclose(f2);
  		if (opts & VERIFY) {
  differ:			buf[0] = '\0';
! 			(void) snprintf(buf + 1, sizeof(buf) - 1, 
! 			    "need to update: %s\n",target);
  			(void) write(rem, buf, strlen(buf + 1) + 1);
  			goto badnew2;
  		}
***************
*** 931,937 ****
  
  	if (opts & COMPARE) {
  		buf[0] = '\0';
! 		(void) sprintf(buf + 1, "updated %s\n", target);
  		(void) write(rem, buf, strlen(buf + 1) + 1);
  	} else
  		ack();
--- 937,944 ----
  
  	if (opts & COMPARE) {
  		buf[0] = '\0';
! 		(void) snprintf(buf + 1, sizeof(buf) - 1,
! 		    "updated %s\n", target);
  		(void) write(rem, buf, strlen(buf + 1) + 1);
  	} else
  		ack();
***************
*** 967,973 ****
  	*cp++ = '\0';
  
  	if (catname) {
! 		(void) sprintf(tp, "/%s", cp);
  	}
  	if (lstat(target, &stb) == 0) {
  		int mode = stb.st_mode & S_IFMT;
--- 974,980 ----
  	*cp++ = '\0';
  
  	if (catname) {
! 		(void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
  	}
  	if (lstat(target, &stb) == 0) {
  		int mode = stb.st_mode & S_IFMT;
***************
*** 1103,1109 ****
  	/*
  	 * Tell the remote to clean the files from the last directory sent.
  	 */
! 	(void) sprintf(buf, "C%o\n", opts & VERIFY);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
--- 1110,1116 ----
  	/*
  	 * Tell the remote to clean the files from the last directory sent.
  	 */
! 	(void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
  	if (debug)
  		printf("buf = %s", buf);
  	(void) write(rem, buf, strlen(buf));
***************
*** 1124,1130 ****
  			 * Y\n -- file doesn't exist - REMOVE.
  			 */
  			*--cp = '\0';
! 			(void) sprintf(tp, "/%s", s);
  			if (debug)
  				printf("check %s\n", target);
  			if (except(target))
--- 1131,1138 ----
  			 * Y\n -- file doesn't exist - REMOVE.
  			 */
  			*--cp = '\0';
! 			(void) snprintf(tp, sizeof(target) - (tp - target), 
! 			    "/%s", s);
  			if (debug)
  				printf("check %s\n", target);
  			if (except(target))
***************
*** 1215,1221 ****
  			error("%s:%s: %s\n", host, target, strerror(errno));
  			continue;
  		}
! 		(void) sprintf(buf, "Q%s\n", dp->d_name);
  		(void) write(rem, buf, strlen(buf));
  		cp = buf;
  		do {
--- 1223,1229 ----
  			error("%s:%s: %s\n", host, target, strerror(errno));
  			continue;
  		}
! 		(void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
  		(void) write(rem, buf, strlen(buf));
  		cp = buf;
  		do {
***************
*** 1229,1235 ****
  		if (opts & VERIFY) {
  			cp = buf;
  			*cp++ = '\0';
! 			(void) sprintf(cp, "need to remove: %s\n", target);
  			(void) write(rem, buf, strlen(cp) + 1);
  		} else
  			removeit(&stb);
--- 1237,1244 ----
  		if (opts & VERIFY) {
  			cp = buf;
  			*cp++ = '\0';
! 			(void) snprintf(cp, sizeof(buf) - 1,
! 			    "need to remove: %s\n", target);
  			(void) write(rem, buf, strlen(cp) + 1);
  		} else
  			removeit(&stb);
***************
*** 1307,1313 ****
  removed:
  	cp = buf;
  	*cp++ = '\0';
! 	(void) sprintf(cp, "removed %s\n", target);
  	(void) write(rem, buf, strlen(cp) + 1);
  }
  
--- 1316,1322 ----
  removed:
  	cp = buf;
  	*cp++ = '\0';
! 	(void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
  	(void) write(rem, buf, strlen(cp) + 1);
  }
  
>Audit-Trail:
>Unformatted: