1 /* $NetBSD: gencheck.c,v 1.3 2020/05/24 19:46:18 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14 #include <fcntl.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <sys/stat.h> 19 #include <unistd.h> 20 21 #include <isc/print.h> 22 23 #define USAGE "usage: gencheck <filename>\n" 24 25 static int 26 check(const char *buf, ssize_t count, size_t *start) { 27 const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789"; 28 ssize_t i; 29 30 for (i = 0; i < count; i++, *start = (*start + 1) % (sizeof(chars) - 1)) 31 { 32 /* Just ignore the trailing newline */ 33 if (buf[i] == '\n') { 34 continue; 35 } 36 if (buf[i] != chars[*start]) { 37 return (0); 38 } 39 } 40 41 return (1); 42 } 43 44 int 45 main(int argc, char **argv) { 46 int ret; 47 int fd; 48 ssize_t count; 49 char buf[1024]; 50 size_t start; 51 size_t length; 52 53 ret = EXIT_FAILURE; 54 fd = -1; 55 length = 0; 56 57 if (argc != 2) { 58 fputs(USAGE, stderr); 59 goto out; 60 } 61 62 fd = open(argv[1], O_RDONLY); 63 if (fd == -1) { 64 goto out; 65 } 66 67 start = 0; 68 while ((count = read(fd, buf, sizeof(buf))) != 0) { 69 if (count < 0) { 70 goto out; 71 } 72 73 if (!check(buf, count, &start)) { 74 goto out; 75 } 76 77 length += count; 78 } 79 80 ret = EXIT_SUCCESS; 81 82 out: 83 printf("%lu\n", (unsigned long)length); 84 85 if (fd != -1) { 86 close(fd); 87 } 88 89 return (ret); 90 } 91