patches for patch

Chris Torek chris at umcp-cs.UUCP
Sun Nov 25 17:24:58 AEST 1984


"patch" looks like a useful program.  However, try running "lint" on it
. . . !  Here's a patch to make it pass lint (with one warning about a
"possible pointer alignment problem" from a typecast on malloc; big
deal).  I don't claim it's any less buggy; I didn't make any real
substantive changes.

RCS file: RCS/patch.c,v
retrieving revision 1.1
diff -c1 -r1.1 patch.c
*** /tmp/,RCSt1022548	Sun Nov 25 02:19:48 1984
--- patch.c	Sun Nov 25 02:19:42 1984
***************
*** 59,60
  
  char buf[MAXLINELEN];			/* general purpose buffer */

--- 59,61 -----
  
+ char serrbuf[BUFSIZ];			/* buffer for stderr */
  char buf[MAXLINELEN];			/* general purpose buffer */
***************
*** 102,103
  char *strcpy();
  int my_exit();

--- 103,106 -----
  char *strcpy();
+ char *strcat();
+ char *sprintf();		/* usually */
  int my_exit();
***************
*** 105,106
  char *fetchname();
  

--- 108,112 -----
  char *fetchname();
+ long atol();
+ long lseek();
+ char *mktemp();
  
***************
*** 113,115
  LINENUM patch_first();
! int patch_lines();
  char patch_char();

--- 119,121 -----
  LINENUM patch_first();
! LINENUM patch_lines();
  char patch_char();
***************
*** 133,135
  
!     setbuf(stderr,malloc(BUFSIZ));
      for (i = 0; i<MAXFILEC; i++)

--- 139,141 -----
  
!     setbuf(stderr,serrbuf);
      for (i = 0; i<MAXFILEC; i++)
***************
*** 200,202
  	move_file(TMPOUTNAME,outname);
! 	fclose(rejfp);
  	rejfp = Nullfp;

--- 206,208 -----
  	move_file(TMPOUTNAME,outname);
! 	(void)fclose(rejfp);
  	rejfp = Nullfp;
***************
*** 204,207
  	    if (!*rejname) {
! 		strcpy(rejname, outname);
! 		strcat(rejname, ".rej");
  	    }

--- 210,213 -----
  	    if (!*rejname) {
! 		(void)strcpy(rejname, outname);
! 		(void)strcat(rejname, ".rej");
  	    }
***************
*** 251,253
      register char *s;
-     register int i;
  

--- 257,258 -----
      register char *s;
  
***************
*** 291,293
  	    case 'r':
! 		strcpy(rejname,Argv[1]);
  		Argc--,Argv++;

--- 296,298 -----
  	    case 'r':
! 		(void)strcpy(rejname,Argv[1]);
  		Argc--,Argv++;
***************
*** 354,356
  {
!     register int i;
      register int pat_last = patch_last();

--- 359,361 -----
  {
!     register LINENUM i;
      register int pat_last = patch_last();
***************
*** 378,382
  {
!     register int old = 1;
!     register int lastline = patch_lines();
!     register int new = lastline+1;
  

--- 383,387 -----
  {
!     register LINENUM old = 1;
!     register LINENUM lastline = patch_lines();
!     register LINENUM new = lastline+1;
  
***************
*** 437,439
      bool this_line_is_command = FALSE;
!     register char *s, *t;
      long beginning_of_this_line;

--- 442,444 -----
      bool this_line_is_command = FALSE;
!     register char *t;
      long beginning_of_this_line;
***************
*** 440,442
  
!     unlink(TMPOUTNAME);
      copy_file(filearg[0],TMPOUTNAME);

--- 445,447 -----
  
!     (void)unlink(TMPOUTNAME);
      copy_file(filearg[0],TMPOUTNAME);
***************
*** 442,444
      copy_file(filearg[0],TMPOUTNAME);
!     sprintf(buf,"/bin/ed %s",TMPOUTNAME);
      pipefp = popen(buf,"w");

--- 447,449 -----
      copy_file(filearg[0],TMPOUTNAME);
!     (void)sprintf(buf,"/bin/ed %s",TMPOUTNAME);
      pipefp = popen(buf,"w");
***************
*** 470,473
      fprintf(pipefp,"q\n");
!     fflush(pipefp);
!     pclose(pipefp);
      ignore_signals();

--- 475,478 -----
      fprintf(pipefp,"q\n");
!     (void)fflush(pipefp);
!     (void)pclose(pipefp);
      ignore_signals();
***************
*** 480,482
  {
!     mktemp(name);
      ofp = fopen(name,"w");

--- 485,487 -----
  {
!     (void)mktemp(name);
      ofp = fopen(name,"w");
***************
*** 489,491
  {
!     mktemp(name);
      rejfp = fopen(name,"w");

--- 494,496 -----
  {
!     (void)mktemp(name);
      rejfp = fopen(name,"w");
***************
*** 496,498
  move_file(from,to)
! char *to;
  {

--- 501,503 -----
  move_file(from,to)
! char *from, *to;
  {
***************
*** 514,517
  	while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	    write(1,buf,i);
! 	close(fromfd);
  	return;

--- 519,523 -----
  	while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	    if (write(1,buf,i) != 1)
! 		fatal("patch: write failed\n");
! 	(void)close(fromfd);
  	return;
***************
*** 519,522
  
!     strcpy(bakname,to);
!     strcat(bakname,origext?origext:ORIGEXT);
      if (stat(to,&filestat) >= 0) {	/* output file exists */

--- 525,528 -----
  
!     (void)strcpy(bakname,to);
!     (void)strcat(bakname,origext?origext:ORIGEXT);
      if (stat(to,&filestat) >= 0) {	/* output file exists */
***************
*** 537,539
  	    else
! 		strcpy(simplename, simplename+1);
  	}

--- 543,545 -----
  	    else
! 		(void)strcpy(simplename, simplename+1);
  	}
***************
*** 568,572
  	while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	    write(tofd,buf,i);
! 	close(fromfd);
! 	close(tofd);
      }

--- 574,579 -----
  	while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	    if (write(tofd,buf,i) != i)
! 		fatal("patch: write failed\n");
! 	(void)close(fromfd);
! 	(void)close(tofd);
      }
***************
*** 572,574
      }
!     unlink(from);
  }

--- 579,581 -----
      }
!     (void)unlink(from);
  }
***************
*** 576,577
  copy_file(from,to)
  {

--- 583,585 -----
  copy_file(from,to)
+ char *from, *to;
  {
***************
*** 588,592
      while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	write(tofd,buf,i);
!     close(fromfd);
!     close(tofd);
  }

--- 596,601 -----
      while ((i=read(fromfd,buf,sizeof buf)) > 0)
! 	if (write(tofd,buf,i) != i)
! 	    fatal("patch: write (%s) failed\n", to);
!     (void)close(fromfd);
!     (void)close(tofd);
  }
***************
*** 606,608
      copy_till(input_lines);		/* dump remainder of file */
!     fclose(ofp);
      ofp = Nullfp;

--- 615,617 -----
      copy_till(input_lines);		/* dump remainder of file */
!     (void)fclose(ofp);
      ofp = Nullfp;
***************
*** 625,627
  {
!     register int pline;
      register LINENUM iline;

--- 634,636 -----
  {
!     register LINENUM pline;
      register LINENUM iline;
***************
*** 627,629
      register LINENUM iline;
!     register int pat_lines = patch_lines();
  

--- 636,638 -----
      register LINENUM iline;
!     register LINENUM pat_lines = patch_lines();
  
***************
*** 655,657
  	i_size = 0;
! 	free(i_ptr);
  	free(i_womp);

--- 664,666 -----
  	i_size = 0;
! 	free((char *)i_ptr);
  	free(i_womp);
***************
*** 662,664
  	using_plan_a = TRUE;		/* maybe the next one is smaller */
! 	close(tifd);
  	tifd = -1;

--- 671,673 -----
  	using_plan_a = TRUE;		/* maybe the next one is smaller */
! 	(void)close(tifd);
  	tifd = -1;
***************
*** 692,694
      if (stat(filename,&filestat) < 0) {
! 	sprintf(buf,"RCS/%s%s",filename,RCSSUFFIX);
  	if (stat(buf,&filestat) >= 0 || stat(buf+4,&filestat) >= 0) {

--- 701,703 -----
      if (stat(filename,&filestat) < 0) {
! 	(void)sprintf(buf,"RCS/%s%s",filename,RCSSUFFIX);
  	if (stat(buf,&filestat) >= 0 || stat(buf+4,&filestat) >= 0) {
***************
*** 694,696
  	if (stat(buf,&filestat) >= 0 || stat(buf+4,&filestat) >= 0) {
! 	    sprintf(buf,CHECKOUT,filename);
  	    if (verbose)

--- 703,705 -----
  	if (stat(buf,&filestat) >= 0 || stat(buf+4,&filestat) >= 0) {
! 	    (void)sprintf(buf,CHECKOUT,filename);
  	    if (verbose)
***************
*** 702,704
  	else {
! 	    sprintf(buf,"%s%s",SCCSPREFIX,filename);
  	    if (stat(buf,&filestat) >= 0) {

--- 711,713 -----
  	else {
! 	    (void)sprintf(buf,"%s%s",SCCSPREFIX,filename);
  	    if (stat(buf,&filestat) >= 0) {
***************
*** 704,706
  	    if (stat(buf,&filestat) >= 0) {
! 		sprintf(buf,GET,filename);
  		if (verbose)

--- 713,715 -----
  	    if (stat(buf,&filestat) >= 0) {
! 		(void)sprintf(buf,GET,filename);
  		if (verbose)
***************
*** 716,718
      i_size = filestat.st_size;
!     i_womp = malloc(i_size+2);
      if (i_womp == Nullch)

--- 725,727 -----
      i_size = filestat.st_size;
!     i_womp = malloc((unsigned)(i_size+2));
      if (i_womp == Nullch)
***************
*** 721,723
  	fatal("Can't open file %s\n",filename);
!     if (read(ifd,i_womp,i_size) < i_size)
  	fatal("Can't read file %s\n",filename);

--- 730,732 -----
  	fatal("Can't open file %s\n",filename);
!     if (read(ifd,i_womp,(int)i_size) < i_size)/* but what if i_size>32767? */
  	fatal("Can't read file %s\n",filename);
***************
*** 723,725
  	fatal("Can't read file %s\n",filename);
!     close(ifd);
      if (i_womp[i_size-1] != '\n')

--- 732,734 -----
  	fatal("Can't read file %s\n",filename);
!     (void)close(ifd);
      if (i_womp[i_size-1] != '\n')
***************
*** 735,737
      }
!     i_ptr = (char **)malloc((iline + 1) * sizeof(char *));
      if (i_ptr == Null(char **)) {	/* shucks, it was a near thing */

--- 744,746 -----
      }
!     i_ptr = (char **)malloc((unsigned)((iline + 1) * sizeof(char *)));
      if (i_ptr == Null(char **)) {	/* shucks, it was a near thing */
***************
*** 737,739
      if (i_ptr == Null(char **)) {	/* shucks, it was a near thing */
! 	free(i_womp);
  	return FALSE;

--- 746,748 -----
      if (i_ptr == Null(char **)) {	/* shucks, it was a near thing */
! 	free((char *)i_womp);
  	return FALSE;
***************
*** 780,782
  	fatal("Can't open file %s\n",filename);
!     mktemp(TMPINNAME);
      if ((tifd = creat(TMPINNAME,0666)) < 0)

--- 789,791 -----
  	fatal("Can't open file %s\n",filename);
!     (void)mktemp(TMPINNAME);
      if ((tifd = creat(TMPINNAME,0666)) < 0)
***************
*** 800,802
      }
!     fseek(ifp,0L,0);			/* rewind file */
      lines_per_buf = BUFFERSIZE / maxlen;

--- 809,811 -----
      }
!     (void)fseek(ifp,0L,0);		/* rewind file */
      lines_per_buf = BUFFERSIZE / maxlen;
***************
*** 803,806
      tireclen = maxlen;
!     tibuf[0] = malloc(BUFFERSIZE + 1);
!     tibuf[1] = malloc(BUFFERSIZE + 1);
      if (tibuf[1] == Nullch)

--- 812,815 -----
      tireclen = maxlen;
!     tibuf[0] = malloc((unsigned)(BUFFERSIZE + 1));
!     tibuf[1] = malloc((unsigned)(BUFFERSIZE + 1));
      if (tibuf[1] == Nullch)
***************
*** 809,811
  	if (! (i % lines_per_buf))	/* new block */
! 	    write(tifd,tibuf[0],BUFFERSIZE);
  	if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)

--- 818,820 -----
  	if (! (i % lines_per_buf))	/* new block */
! 	    (void)write(tifd,tibuf[0],BUFFERSIZE);
  	if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
***************
*** 814,816
  	    if (i % lines_per_buf)
! 		write(tifd,tibuf[0],BUFFERSIZE);
  	    break;

--- 823,825 -----
  	    if (i % lines_per_buf)
! 		(void)write(tifd,tibuf[0],BUFFERSIZE);
  	    break;
***************
*** 818,821
      }
!     fclose(ifp);
!     close(tifd);
      if ((tifd = open(TMPINNAME,0)) < 0) {

--- 827,830 -----
      }
!     (void)fclose(ifp);
!     (void)close(tifd);
      if ((tifd = open(TMPINNAME,0)) < 0) {
***************
*** 845,847
  	    tiline[whichbuf] = baseline;
! 	    lseek(tifd,(long)baseline / lines_per_buf * BUFFERSIZE,0);
  	    if (read(tifd,tibuf[whichbuf],BUFFERSIZE) < 0)

--- 854,856 -----
  	    tiline[whichbuf] = baseline;
! 	    (void)lseek(tifd,(long)baseline / lines_per_buf * BUFFERSIZE,0);
  	    if (read(tifd,tibuf[whichbuf],BUFFERSIZE) < 0)
***************
*** 882,884
      if (filename == Nullch || !*filename || strEQ(filename,"-")) {
! 	mktemp(TMPPATNAME);
  	pfp = fopen(TMPPATNAME,"w");

--- 891,893 -----
      if (filename == Nullch || !*filename || strEQ(filename,"-")) {
! 	(void)mktemp(TMPPATNAME);
  	pfp = fopen(TMPPATNAME,"w");
***************
*** 888,890
  	    fputs(buf,pfp);
! 	fclose(pfp);
  	filename = TMPPATNAME;

--- 897,899 -----
  	    fputs(buf,pfp);
! 	(void)fclose(pfp);
  	filename = TMPPATNAME;
***************
*** 894,896
  	fatal("patch file %s not found\n",filename);
!     fstat(pfp->_file,&filestat);
      p_filesize = filestat.st_size;

--- 903,905 -----
  	fatal("patch file %s not found\n",filename);
!     (void)fstat(pfp->_file,&filestat);
      p_filesize = filestat.st_size;
***************
*** 953,955
      register char *s, *t;
-     char *ret;
      char *oldname = Nullch;

--- 962,963 -----
      register char *s, *t;
      char *oldname = Nullch;
***************
*** 958,960
  
!     fseek(pfp,p_base,0);
      for (;;) {

--- 966,968 -----
  
!     (void)fseek(pfp,p_base,0);
      for (;;) {
***************
*** 1050,1053
      register char *t;
!     char *test;
!     char tmpbuf[64];
  

--- 1058,1060 -----
      register char *t;
!     char tmpbuf[200];
  
***************
*** 1060,1062
      name = savestr(name);
!     sprintf(tmpbuf,"RCS/%s",name);
      free(s);

--- 1067,1069 -----
      name = savestr(name);
!     (void)sprintf(tmpbuf,"RCS/%s",name);
      free(s);
***************
*** 1063,1065
      if (stat(name,&filestat) < 0) {
! 	strcat(tmpbuf,RCSSUFFIX);
  	if (stat(tmpbuf,&filestat) < 0 && stat(tmpbuf+4,&filestat) < 0) {

--- 1070,1072 -----
      if (stat(name,&filestat) < 0) {
! 	(void)strcat(tmpbuf,RCSSUFFIX);
  	if (stat(tmpbuf,&filestat) < 0 && stat(tmpbuf+4,&filestat) < 0) {
***************
*** 1065,1067
  	if (stat(tmpbuf,&filestat) < 0 && stat(tmpbuf+4,&filestat) < 0) {
! 	    sprintf(tmpbuf,"%s%s",SCCSPREFIX,name);
  	    if (stat(tmpbuf,&filestat) < 0) {

--- 1072,1074 -----
  	if (stat(tmpbuf,&filestat) < 0 && stat(tmpbuf+4,&filestat) < 0) {
! 	    (void)sprintf(tmpbuf,"%s%s",SCCSPREFIX,name);
  	    if (stat(tmpbuf,&filestat) < 0) {
***************
*** 1088,1090
      if (verbose && p_base < file_pos) {
! 	fseek(pfp,p_base,0);
  	say("The text leading up to this was:\n--------------------------\n");

--- 1095,1097 -----
      if (verbose && p_base < file_pos) {
! 	(void)fseek(pfp,p_base,0);
  	say("The text leading up to this was:\n--------------------------\n");
***************
*** 1097,1099
      else
! 	fseek(pfp,file_pos,0);
  }

--- 1104,1106 -----
      else
! 	(void)fseek(pfp,file_pos,0);
  }
***************
*** 1126,1128
  		if (p_max - p_last < 4)
! 		    strcpy(buf,"  \n");	/* assume blank lines got chopped */
  		else

--- 1133,1135 -----
  		if (p_max - p_last < 4)
! 		    (void)strcpy(buf,"  \n");	/* assume blank lines got chopped */
  		else
***************
*** 1232,1234
  	}
! 	sprintf(buf,"*** %d,%d\n", p_first, p_first + p_lines - 1);
  	p_line[0] = savestr(buf);

--- 1239,1241 -----
  	}
! 	(void)sprintf(buf,"*** %d,%d\n", p_first, p_first + p_lines - 1);
  	p_line[0] = savestr(buf);
***************
*** 1256,1258
  	}
! 	sprintf(buf,"--- %d,%d\n",min,max);
  	p_line[i] = savestr(buf);

--- 1263,1265 -----
  	}
! 	(void)sprintf(buf,"--- %d,%d\n",min,max);
  	p_line[i] = savestr(buf);
***************
*** 1307,1309
  	if (buf != s)
! 	    strcpy(buf,s);
      }

--- 1314,1316 -----
  	if (buf != s)
! 	    (void)strcpy(buf,s);
      }
***************
*** 1318,1319
  
  patch_lines()

--- 1325,1327 -----
  
+ LINENUM
  patch_lines()
***************
*** 1359,1362
  char *
! savestr(string)
! char *string;
  {

--- 1367,1370 -----
  char *
! savestr(s)
! register char *s;
  {
***************
*** 1362,1364
  {
!     return strcpy(malloc(strlen(string)+1),string);
  }

--- 1370,1382 -----
  {
!     register char  *rv,
!                    *t;
! 
!     t = s;
!     while (*t++);
!     rv = malloc ((unsigned) (t - s));
!     if (rv == NULL)
! 	fatal ("patch: out of memory (savestr)\n");
!     t = rv;
!     while (*t++ = *s++);
!     return rv;
  }
***************
*** 1368,1373
  {
!     unlink(TMPINNAME);
!     unlink(TMPOUTNAME);
!     unlink(TMPREJNAME);
!     unlink(TMPPATNAME);
      exit(status);

--- 1386,1391 -----
  {
!     (void)unlink(TMPINNAME);
!     (void)unlink(TMPOUTNAME);
!     (void)unlink(TMPREJNAME);
!     (void)unlink(TMPPATNAME);
      exit(status);
***************
*** 1375,1376
  
  say(pat,arg1,arg2,arg3)

--- 1393,1405 -----
  
+ #ifdef lint
+ 
+ /*VARARGS ARGSUSED*/
+ say(pat) char *pat; { ; }
+ /*VARARGS ARGSUSED*/
+ fatal(pat) char *pat; { ; }
+ /*VARARGS ARGSUSED*/
+ ask(pat) char *pat; { ; }
+ 
+ #else lint
+ 
  say(pat,arg1,arg2,arg3)
***************
*** 1380,1382
      fprintf(stderr,pat,arg1,arg2,arg3);
!     fflush(stderr);
  }

--- 1409,1411 -----
      fprintf(stderr,pat,arg1,arg2,arg3);
!     (void)fflush(stderr);
  }
***************
*** 1396,1397
      int ttyfd = open("/dev/tty",2);
  

--- 1425,1427 -----
      int ttyfd = open("/dev/tty",2);
+     int r;
  
***************
*** 1398,1401
      say(pat,arg1,arg2,arg3);
!     read((ttyfd < 0 ? 2 : ttyfd), buf, sizeof buf);
!     close(ttyfd);
  }

--- 1428,1437 -----
      say(pat,arg1,arg2,arg3);
!     if (ttyfd >= 0) {
! 	r = read(ttyfd, buf, sizeof buf);
! 	(void)close(ttyfd);
!     }
!     else
! 	r = read(2, buf, sizeof buf);
!     if (r <= 0)
! 	buf[0] = 0;
  }
***************
*** 1401,1402
  }
  

--- 1437,1439 -----
  }
+ #endif lint
  
***************
*** 1423,1426
  {
!     signal(SIGHUP,my_exit);
!     signal(SIGINT,my_exit);
  }

--- 1460,1465 -----
  {
!     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
! 	(void) signal(SIGHUP, my_exit);
!     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
! 	(void) signal(SIGINT, my_exit);
  }
***************
*** 1429,1432
  {
!     signal(SIGHUP, SIG_IGN);
!     signal(SIGINT, SIG_IGN);
  }

--- 1468,1471 -----
  {
!     (void) signal(SIGHUP, SIG_IGN);
!     (void) signal(SIGINT, SIG_IGN);
  }
-- 
(This line accidently left nonblank.)

In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.sources.bugs mailing list