[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / utils / count / count.c
blob0aacafe2fe78e1fe8a503217a4ab3ec5dbc28d18
1 /*===- count.c - The 'count' testing tool ---------------------------------===*\
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
7 \*===----------------------------------------------------------------------===*/
9 #include <stdlib.h>
10 #include <stdio.h>
12 int main(int argc, char **argv) {
13 unsigned Count, NumLines, NumRead;
14 char Buffer[4096], *End;
16 if (argc != 2) {
17 fprintf(stderr, "usage: %s <expected line count>\n", argv[0]);
18 return 2;
21 Count = strtol(argv[1], &End, 10);
22 if (*End != '\0' && End != argv[1]) {
23 fprintf(stderr, "%s: invalid count argument '%s'\n", argv[0], argv[1]);
24 return 2;
27 NumLines = 0;
28 do {
29 unsigned i;
31 NumRead = fread(Buffer, 1, sizeof(Buffer), stdin);
33 for (i = 0; i != NumRead; ++i)
34 if (Buffer[i] == '\n')
35 ++NumLines;
36 } while (NumRead == sizeof(Buffer));
38 if (!feof(stdin)) {
39 fprintf(stderr, "%s: error reading stdin\n", argv[0]);
40 return 3;
43 if (Count != NumLines) {
44 fprintf(stderr, "Expected %d lines, got %d.\n", Count, NumLines);
45 return 1;
48 return 0;