1/*===- count.c - The 'count' testing tool ---------------------------------===*\
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7\*===----------------------------------------------------------------------===*/
8
9#include "llvm/Support/AutoConvert.h"
10#include <stdio.h>
11#include <stdlib.h>
12
13int main(int argc, char **argv) {
14#ifdef __MVS__
15 if (enableAutoConversion(fileno(stdin)) == -1)
16 fprintf(stderr, "Setting conversion on stdin failed\n");
17
18 if (enableAutoConversion(fileno(stderr)) == -1)
19 fprintf(stdout, "Setting conversion on stderr failed\n");
20#endif
21 size_t Count, NumLines, NumRead;
22 char Buffer[4096], *End;
23
24 if (argc != 2) {
25 fprintf(stderr, format: "usage: %s <expected line count>\n", argv[0]);
26 return 2;
27 }
28
29 Count = strtoul(nptr: argv[1], endptr: &End, base: 10);
30 if (*End != '\0' && End != argv[1]) {
31 fprintf(stderr, format: "%s: invalid count argument '%s'\n", argv[0], argv[1]);
32 return 2;
33 }
34
35 NumLines = 0;
36 do {
37 size_t i;
38
39 NumRead = fread(ptr: Buffer, size: 1, n: sizeof(Buffer), stdin);
40
41 for (i = 0; i != NumRead; ++i)
42 if (Buffer[i] == '\n')
43 ++NumLines;
44 } while (NumRead == sizeof(Buffer));
45
46 if (!feof(stdin)) {
47 fprintf(stderr, format: "%s: error reading stdin\n", argv[0]);
48 return 3;
49 }
50
51 if (Count != NumLines) {
52 fprintf(stderr, format: "Expected %zu lines, got %zu.\n", Count, NumLines);
53 return 1;
54 }
55
56 return 0;
57}
58