DragonFly BSD
DragonFly submit List (threaded) for 2003-10
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

split(1) fixes


From: Simon 'corecode' Schubert <corecode@xxxxxxxxxxxx>
Date: Sun, 19 Oct 2003 21:00:16 +0200

hyas,

latest patch from hmp revealed an existing bug in split(1) by
introducing a new bug :)

here the fix:

Index: split.c
===================================================================
RCS file: /home/dcvs/src/usr.bin/split/split.c,v
retrieving revision 1.3
diff -u -r1.3 split.c
--- split.c	28 Aug 2003 02:22:38 -0000	1.3
+++ split.c	14 Oct 2003 10:48:10 -0000
@@ -242,6 +242,7 @@
 void
 split2(void)
 {
+	int startofline = 1;
 	long lcnt = 0;
 	FILE *infp;
 
@@ -253,27 +254,30 @@
 		err(EX_OSERR, "malloc");
 
 	/* Process input one line at a time */
-	while (fgets(bfr, sizeof(bfr), infp) != NULL) {
+	while (fgets(bfr, MAXBSIZE, infp) != NULL) {
 		const int len = strlen(bfr);
 
-		/* If line is too long to deal with, just write it out */
-		if (bfr[len - 1] != '\n')
-			goto writeit;
-
-		/* Check if we need to start a new file */
-		if (pflag) {
-			regmatch_t pmatch;
-
-			pmatch.rm_so = 0;
-			pmatch.rm_eo = len - 1;
-			if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
+		/* Consider starting a new file only when at beginning of a line */
+		if (startofline) {
+			/* Check if we need to start a new file */
+			if (pflag) {
+				regmatch_t pmatch;
+
+				pmatch.rm_so = 0;
+				pmatch.rm_eo = len - 1;
+				if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
+					newfile();
+			} else if (lcnt++ == numlines) {
 				newfile();
-		} else if (lcnt++ == numlines) {
-			newfile();
-			lcnt = 1;
+				lcnt = 1;
+			}
 		}
+		
+		if (bfr[len - 1] != '\n')
+			startofline = 0;
+		else
+			startofline = 1;
 
-writeit:
 		/* Open output file if needed */
 		if (!file_open)
 			newfile();

cheers
  simon

-- 
/"\   http://corecode.ath.cx/#donate
\ /
 \     ASCII Ribbon Campaign
/ \  Against HTML Mail and News

Attachment: pgp00003.pgp
Description: PGP signature



[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]