Initial import.
authorfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Fri, 18 Nov 2005 01:47:18 +0000 (01:47 +0000)
committerfredrik <fredrik@959494ce-11ee-0310-bf91-de5d638817bd>
Fri, 18 Nov 2005 01:47:18 +0000 (01:47 +0000)
git-svn-id: svn+ssh://svn.dolda2000.com/srv/svn/repos/src/utils@517 959494ce-11ee-0310-bf91-de5d638817bd

findnulls.c [new file with mode: 0644]

diff --git a/findnulls.c b/findnulls.c
new file mode 100644 (file)
index 0000000..8c64653
--- /dev/null
@@ -0,0 +1,94 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+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:
+ */