planime: Added base aliases.
[utils.git] / findnulls.c
CommitLineData
306708ee 1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <errno.h>
6
7char buf[65536];
8
9void findnulls(int fd, char *source, int squelch)
10{
11 int i, ret, n, s, off;
12
13 n = 0;
14 s = -1;
15 off = 0;
16 while(1) {
17 ret = read(fd, buf, sizeof(buf));
18 if(ret < 0) {
19 if(source != NULL)
20 perror(source);
21 else
22 perror("read");
23 return;
24 }
25 for(i = 0; i < ret; i++) {
26 if(buf[i] == 0) {
27 if(s == -1)
28 s = off + i;
29 n++;
30 } else {
31 if(n > 0) {
32 if(n > squelch) {
33 if(source == NULL)
34 printf("%i %i\n", s, n);
35 else
36 printf("%s: %i %i\n", source, s, n);
37 }
38 s = -1;
39 n = 0;
40 }
41 }
42 }
43 off += ret;
44 if(ret == 0)
45 break;
46 }
47 if(n > squelch) {
48 if(source == NULL)
49 printf("%i %i\n", s, n);
50 else
51 printf("%s: %i %i\n", source, s, n);
52 }
53}
54
55int main(int argc, char **argv)
56{
57 int i, c;
58 int squelch;
59 int fd;
60
61 squelch = 1024;
62 while((c = getopt(argc, argv, "hs:")) >= 0) {
63 switch(c) {
64 case 's':
65 squelch = atoi(optarg);
66 break;
67 case 'h':
68 case '?':
69 case ':':
70 default:
71 fprintf(stderr, "usage: findnulls [-h] [-s squelch] [FILE...]\n");
72 exit((c == 'h')?0:1);
73 }
74 }
75 if(optind < argc) {
76 for(i = optind; i < argc; i++) {
77 if((fd = open(argv[i], O_RDONLY)) < 0) {
78 perror(argv[i]);
79 continue;
80 }
81 findnulls(fd, argv[i], squelch);
82 close(fd);
83 }
84 } else {
85 findnulls(0, NULL, squelch);
86 }
87 return(0);
88}
89
90/*
91 * Local Variables:
92 * compile-command: "gcc -Wall -g -o findnulls findnulls.c"
93 * End:
94 */