From: fredrik Date: Fri, 18 Nov 2005 01:47:18 +0000 (+0000) Subject: Initial import. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=306708ee9f3e2632783812e098c46e96ac2f76b7;p=utils.git Initial import. git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/utils@517 959494ce-11ee-0310-bf91-de5d638817bd --- diff --git a/findnulls.c b/findnulls.c new file mode 100644 index 0000000..8c64653 --- /dev/null +++ b/findnulls.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +char buf[65536]; + +void findnulls(int fd, char *source, int squelch) +{ + int i, ret, n, s, off; + + n = 0; + s = -1; + off = 0; + while(1) { + ret = read(fd, buf, sizeof(buf)); + if(ret < 0) { + if(source != NULL) + perror(source); + else + perror("read"); + return; + } + for(i = 0; i < ret; i++) { + if(buf[i] == 0) { + if(s == -1) + s = off + i; + n++; + } else { + if(n > 0) { + if(n > squelch) { + if(source == NULL) + printf("%i %i\n", s, n); + else + printf("%s: %i %i\n", source, s, n); + } + s = -1; + n = 0; + } + } + } + off += ret; + if(ret == 0) + break; + } + if(n > squelch) { + if(source == NULL) + printf("%i %i\n", s, n); + else + printf("%s: %i %i\n", source, s, n); + } +} + +int main(int argc, char **argv) +{ + int i, c; + int squelch; + int fd; + + squelch = 1024; + while((c = getopt(argc, argv, "hs:")) >= 0) { + switch(c) { + case 's': + squelch = atoi(optarg); + break; + case 'h': + case '?': + case ':': + default: + fprintf(stderr, "usage: findnulls [-h] [-s squelch] [FILE...]\n"); + exit((c == 'h')?0:1); + } + } + if(optind < argc) { + for(i = optind; i < argc; i++) { + if((fd = open(argv[i], O_RDONLY)) < 0) { + perror(argv[i]); + continue; + } + findnulls(fd, argv[i], squelch); + close(fd); + } + } else { + findnulls(0, NULL, squelch); + } + return(0); +} + +/* + * Local Variables: + * compile-command: "gcc -Wall -g -o findnulls findnulls.c" + * End: + */