9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

strings.c (1261B)


      1 #include	<u.h>
      2 #include 	<libc.h>
      3 #include	<bio.h>
      4 
      5 Biobuf	*fin;
      6 Biobuf	fout;
      7 
      8 #define	MINSPAN		6		/* Min characters in string */
      9 
     10 #define BUFSIZE		70
     11 
     12 void stringit(char *);
     13 #undef isprint
     14 #define isprint risprint
     15 int isprint(Rune);
     16 
     17 void
     18 main(int argc, char **argv)
     19 {
     20 	int i;
     21 
     22 	Binit(&fout, 1, OWRITE);
     23 	if(argc < 2) {
     24 		stringit("/dev/stdin");
     25 		exits(0);
     26 	}
     27 
     28 	for(i = 1; i < argc; i++) {
     29 		if(argc > 2)
     30 			print("%s:\n", argv[i]);
     31 
     32 		stringit(argv[i]);
     33 	}
     34 
     35 	exits(0);
     36 }
     37 
     38 void
     39 stringit(char *str)
     40 {
     41 	long posn, start;
     42 	int cnt = 0;
     43 	long c;
     44 
     45 	Rune buf[BUFSIZE];
     46 
     47 	if ((fin = Bopen(str, OREAD)) == 0) {
     48 		perror("open");
     49 		return;
     50 	}
     51 
     52 	start = 0;
     53 	posn = Boffset(fin);
     54 	while((c = Bgetrune(fin)) >= 0) {
     55 		if(isprint(c)) {
     56 			if(start == 0)
     57 				start = posn;
     58 			buf[cnt++] = c;
     59 			if(cnt == BUFSIZE-1) {
     60 				buf[cnt] = 0;
     61 				Bprint(&fout, "%8ld: %S ...\n", start, buf);
     62 				start = 0;
     63 				cnt = 0;
     64 			}
     65 		} else {
     66 			 if(cnt >= MINSPAN) {
     67 				buf[cnt] = 0;
     68 				Bprint(&fout, "%8ld: %S\n", start, buf);
     69 			}
     70 			start = 0;
     71 			cnt = 0;
     72 		}	
     73 		posn = Boffset(fin);
     74 	}
     75 
     76 	if(cnt >= MINSPAN){
     77 		buf[cnt] = 0;
     78 		Bprint(&fout, "%8ld: %S\n", start, buf);
     79 	}
     80 	Bterm(fin);
     81 }
     82 
     83 int
     84 isprint(Rune r)
     85 {
     86 	if ((r >= ' ' && r <0x7f) || r > 0xA0)
     87 		return 1;
     88 	else
     89 		return 0;
     90 }