[hackers] [sbase] Convert cksum(1) to use FILE * instead of an fd || sin

From: <git_AT_suckless.org>
Date: Tue, 12 Nov 2013 12:18:37 +0100

commit 8fdfa7caeb26a8bfe54bbfe554c336709f01002a
Author: sin <sin_AT_2f30.org>
Date: Tue Nov 12 11:16:13 2013 +0000

    Convert cksum(1) to use FILE * instead of an fd
    
    In sbase we generally do I/O through FILE * instead of file
    descriptors directly.
    
    Do not error out on the first file that can't be opened.

diff --git a/cksum.c b/cksum.c
index d08ca31..725c392 100644
--- a/cksum.c
+++ b/cksum.c
_AT_@ -1,11 +1,11 @@
 /* See LICENSE file for copyright and license details. */
-#include <fcntl.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <unistd.h>
+#include <string.h>
 #include "util.h"
 
-static void cksum(int, const char *);
+static void cksum(FILE *, const char *);
 static void usage(void);
 
 static const unsigned long crctab[] = { 0x00000000,
_AT_@ -65,36 +65,43 @@ static const unsigned long crctab[] = { 0x00000000,
 int
 main(int argc, char *argv[])
 {
- int i, fd;
+ FILE *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);
+ if(argc == 0) {
+ cksum(stdin, NULL);
+ } else {
+ for(; argc > 0; argc--, argv++) {
+ if (!(fp = fopen(argv[0], "r"))) {
+ fprintf(stderr, "fopen %s: %s
", argv[0],
+ strerror(errno));
+ continue;
+ }
+ cksum(fp, argv[0]);
+ fclose(fp);
+ }
         }
         return EXIT_SUCCESS;
 }
 
 void
-cksum(int fd, const char *s)
+cksum(FILE *fp, const char *s)
 {
         unsigned char buf[BUFSIZ];
         unsigned int ck = 0;
- size_t len;
- int i, n;
+ size_t len = 0;
+ size_t i, n;
 
- for(len = 0; (n = read(fd, buf, sizeof buf)) > 0; len += n)
+ while ((n = fread(buf, 1, sizeof(buf), fp)) > 0) {
                 for(i = 0; i < n; i++)
                         ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
- if(n < 0)
+ len += n;
+ }
+ if (ferror(fp))
                 eprintf("%s: read error:", s ? s : "<stdin>");
 
         for(i = len; i > 0; i >>= 8)
Received on Tue Nov 12 2013 - 12:18:37 CET

This archive was generated by hypermail 2.3.0 : Tue Nov 12 2013 - 12:24:14 CET