e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt/*
3939cc42d8157258c3e65b99da2415e8adb532d3Tinderbox User * Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <config.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <stdlib.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <stdio.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <string.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <sys/stat.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <fcntl.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#include <unistd.h>
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt#define USAGE "usage: gencheck <filename>\n"
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntstatic int
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntcheck(const char *buf, ssize_t count, size_t *start) {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt const char chars[] = "abcdefghijklmnopqrstuvwxyz0123456789";
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt ssize_t i;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
caf7c2fd25a7c4484e7044ecf622d352eabc8e19Mark Andrews for (i = 0;
caf7c2fd25a7c4484e7044ecf622d352eabc8e19Mark Andrews i < count;
caf7c2fd25a7c4484e7044ecf622d352eabc8e19Mark Andrews i++, *start = (*start + 1) % (sizeof(chars) - 1))
caf7c2fd25a7c4484e7044ecf622d352eabc8e19Mark Andrews {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt /* Just ignore the trailing newline */
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (buf[i] == '\n')
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt continue;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (buf[i] != chars[*start])
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt return 0;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt }
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt return 1;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt}
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntint
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Huntmain(int argc, char **argv)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt{
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt int ret;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt int fd;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt ssize_t count;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt char buf[1024];
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt size_t start;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt size_t length;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt ret = EXIT_FAILURE;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt fd = -1;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt length = 0;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (argc != 2) {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt fputs(USAGE, stderr);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt goto out;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt }
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt fd = open(argv[1], O_RDONLY);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (fd == -1)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt goto out;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt start = 0;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt while ((count = read(fd, buf, sizeof(buf))) != 0) {
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (count < 0)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt goto out;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (!check(buf, count, &start))
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt goto out;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt length += count;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt }
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt ret = EXIT_SUCCESS;
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt out:
f9ee67d9cef83dacac985d0f89e34cc0ac65948bMark Andrews printf("%lu\n", (unsigned long)length);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt if (fd != -1)
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt close(fd);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt return (ret);
e32d354f754a5d7847a0862bcd6302827ea225bfEvan Hunt}