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.
This commit is contained in:
parent
cfe5e9ef3a
commit
8fdfa7caeb
39
cksum.c
39
cksum.c
|
@ -1,11 +1,11 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <fcntl.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <string.h>
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static void cksum(int, const char *);
|
static void cksum(FILE *, const char *);
|
||||||
static void usage(void);
|
static void usage(void);
|
||||||
|
|
||||||
static const unsigned long crctab[] = { 0x00000000,
|
static const unsigned long crctab[] = { 0x00000000,
|
||||||
|
@ -65,36 +65,43 @@ static const unsigned long crctab[] = { 0x00000000,
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int i, fd;
|
FILE *fp;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
if(argc == 0)
|
if(argc == 0) {
|
||||||
cksum(STDIN_FILENO, NULL);
|
cksum(stdin, NULL);
|
||||||
else for(i = 0; i < argc; i++) {
|
} else {
|
||||||
if((fd = open(argv[i], O_RDONLY)) == -1)
|
for(; argc > 0; argc--, argv++) {
|
||||||
eprintf("open %s:", argv[i]);
|
if (!(fp = fopen(argv[0], "r"))) {
|
||||||
cksum(fd, argv[i]);
|
fprintf(stderr, "fopen %s: %s\n", argv[0],
|
||||||
close(fd);
|
strerror(errno));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cksum(fp, argv[0]);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
cksum(int fd, const char *s)
|
cksum(FILE *fp, const char *s)
|
||||||
{
|
{
|
||||||
unsigned char buf[BUFSIZ];
|
unsigned char buf[BUFSIZ];
|
||||||
unsigned int ck = 0;
|
unsigned int ck = 0;
|
||||||
size_t len;
|
size_t len = 0;
|
||||||
int i, n;
|
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++)
|
for(i = 0; i < n; i++)
|
||||||
ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
|
ck = (ck << 8) ^ crctab[(ck >> 24) ^ buf[i]];
|
||||||
if(n < 0)
|
len += n;
|
||||||
|
}
|
||||||
|
if (ferror(fp))
|
||||||
eprintf("%s: read error:", s ? s : "<stdin>");
|
eprintf("%s: read error:", s ? s : "<stdin>");
|
||||||
|
|
||||||
for(i = len; i > 0; i >>= 8)
|
for(i = len; i > 0; i >>= 8)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user