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