[hackers] [sbase] cksum: ARGBEGIN, no buffering || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Mon, 14 May 2012 22:30:42 +0200 (CEST)

changeset: 118:118a80a266bf
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Mon May 14 21:30:02 2012 +0100
files: cksum.c
description:
cksum: ARGBEGIN, no buffering


diff -r e34b04007eb3 -r 118a80a266bf cksum.c
--- a/cksum.c Mon May 14 21:28:41 2012 +0100
+++ b/cksum.c Mon May 14 21:30:02 2012 +0100
_AT_@ -1,10 +1,13 @@
 /* See LICENSE file for copyright and license details. */
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include "util.h"
 
-static void cksum(FILE *, const char *);
+#define USAGE() usage("[files...]")
+
+static void cksum(int, const char *);
 
 static const unsigned long crctab[] = { 0x00000000,
 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
_AT_@ -63,35 +66,42 @@
 int
 main(int argc, char *argv[])
 {
- FILE *fp;
+ int i, fd;
 
- if(getopt(argc, argv, "") != -1)
- exit(EXIT_FAILURE);
- if(optind == argc)
- cksum(stdin, NULL);
- else for(; optind < argc; optind++) {
- if(!(fp = fopen(argv[optind], "r")))
- eprintf("fopen %s:", argv[optind]);
- cksum(fp, argv[optind]);
- fclose(fp);
+ ARGBEGIN {
+ default:
+ USAGE();
+ } ARGEND;
+
+ if(argc == 0)
+ cksum(STDIN_FILENO, NULL);
+ else for(i = 0; i < argc; i++) {
+ if((fd = open(argv[i], O_RDONLY)) == -1)
+ eprintf("open %s:", argv[i]);
+ cksum(fd, argv[i]);
+ close(fd);
         }
         return EXIT_SUCCESS;
 }
 
 void
-cksum(FILE *fp, const char *s)
+cksum(int fd, const char *s)
 {
- int c;
- unsigned long i, n, ck = 0;
+ unsigned char buf[BUFSIZ];
+ unsigned int ck = 0;
+ size_t len;
+ int i, n;
 
- for(n = 0; (c = getc(fp)) != EOF; n++)
- ck = (ck << 8) ^ crctab[(ck >> 24) ^ c];
- for(i = n; i > 0; i >>= 8)
- ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0377)];
- if(ferror(fp))
+ for(len = 0; (n = read(fd, buf, sizeof buf)) > 0; len += n)
+ for(i = 0; i < n; i++)
+ ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
+ if(n < 0)
                 eprintf("%s: read error:", s ? s : "<stdin>");
 
- printf("%lu %lu", ~ck, n);
+ for(i = len; i > 0; i >>= 8)
+ ck = (ck << 8) ^ crctab[(ck >> 24) ^ (i & 0xFF)];
+
+ printf("%u %lu", ~ck, len);
         if(s != NULL)
                 printf(" %s", s);
         putchar('\n');
Received on Mon May 14 2012 - 22:30:42 CEST

This archive was generated by hypermail 2.3.0 : Mon May 14 2012 - 22:36:08 CEST