shithub: riscv

Download patch

ref: 17d0dea87c80203aaf0199cb33dea0afc4a7f956
parent: 52464166216095a5747f7541eff179ecf8d94e1f
author: cinap_lenrek <[email protected]>
date: Fri May 30 00:05:18 EDT 2014

we look for strings.c, it is broken, this strings.c will make us go.

--- a/sys/man/1/strings
+++ b/sys/man/1/strings
@@ -24,12 +24,10 @@
 .SM ASCII
 characters from blank through tilde (hexadecimal 20 through 7E), inclusive,
 and
-all other characters from value 00A0 to FFFF.
+all other characters above A0.
 Strings reports
 the decimal offset within the file at which the string starts and the text
-of the string. If the string is longer than 70 runes the line is
-terminated by three dots and the printing is resumed on the next
-line with the offset of the continuation line.
+of the string.
 .SH SOURCE
 .B /sys/src/cmd/strings.c
 .SH SEE ALSO
--- a/sys/src/cmd/strings.c
+++ b/sys/src/cmd/strings.c
@@ -2,16 +2,14 @@
 #include 	<libc.h>
 #include	<bio.h>
 
-Biobuf	*fin;
+Biobuf	fin;
 Biobuf	fout;
 
-#define	MINSPAN		6		/* Min characters in string (default) */
-#define BUFSIZE		70
-
-void stringit(char *);
+void stringit(int);
 int isprint(Rune);
 
-static int minspan = MINSPAN;
+int minspan = 6;	/* Min characters in string (default) */
+Rune *span;
 
 static void
 usage(void)
@@ -23,27 +21,40 @@
 void
 main(int argc, char **argv)
 {
-	int i;
+	int i, fd;
 
 	ARGBEGIN{
 	case 'm':
 		minspan = atoi(EARGF(usage()));
+		if(minspan <= 0)
+			usage();
 		break;
 	default:
 		usage();
 		break;
 	}ARGEND
+
+	span = malloc(sizeof(Rune)*(minspan+1));
+	if(span == nil)
+		sysfatal("out of memory");
+
 	Binit(&fout, 1, OWRITE);
 	if(argc < 1) {
-		stringit("/fd/0");
+		stringit(0);
 		exits(0);
 	}
 
 	for(i = 0; i < argc; i++) {
-		if(argc > 2)
-			print("%s:\n", argv[i]);
-
-		stringit(argv[i]);
+		if(argc > 1){
+			Bprint(&fout, "%s:\n", argv[i]);
+			Bflush(&fout);
+		}
+		if((fd = open(argv[i], OREAD)) < 0){
+			perror("open");
+			continue;
+		}
+		stringit(fd);
+		close(fd);
 	}
 
 	exits(0);
@@ -50,48 +61,34 @@
 }
 
 void
-stringit(char *str)
+stringit(int fd)
 {
-	long posn, start;
-	int cnt = 0;
+	Rune *sp;
 	long c;
 
-	Rune buf[BUFSIZE];
-
-	if ((fin = Bopen(str, OREAD)) == 0) {
-		perror("open");
-		return;
-	}
-
-	start = 0;
-	posn = Boffset(fin);
-	while((c = Bgetrune(fin)) >= 0) {
+	Binit(&fin, fd, OREAD);
+	sp = span;
+	while((c = Bgetrune(&fin)) >= 0) {
 		if(isprint(c)) {
-			if(start == 0)
-				start = posn;
-			buf[cnt++] = c;
-			if(cnt == BUFSIZE-1) {
-				buf[cnt] = 0;
-				Bprint(&fout, "%8ld: %S ...\n", start, buf);
-				start = 0;
-				cnt = 0;
+			if(sp == nil){
+				Bputrune(&fout, c);
+				continue;
 			}
+			*sp++ = c;
+			if((sp-span) < minspan)
+				continue;
+			*sp = 0;
+			Bprint(&fout, "%8lld: %S", Boffset(&fin)-minspan, span);
+			sp = nil;
 		} else {
-			 if(cnt >= minspan) {
-				buf[cnt] = 0;
-				Bprint(&fout, "%8ld: %S\n", start, buf);
-			}
-			start = 0;
-			cnt = 0;
-		}	
-		posn = Boffset(fin);
+			if(sp == nil)
+				Bputrune(&fout, '\n');
+			sp = span;
+		}
 	}
-
-	if(cnt >= minspan){
-		buf[cnt] = 0;
-		Bprint(&fout, "%8ld: %S\n", start, buf);
-	}
-	Bterm(fin);
+	if(sp == nil)
+		Bputrune(&fout, '\n');
+	Bterm(&fin);
 }
 
 int