summaryrefslogtreecommitdiffstats
path: root/tests/helpers/test_strerror.c
blob: a574c6ef3fac1d275250f6fe9c0290de8b66ab30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This test program prints errno messages to allow for portable
 * verification of error messages.
 *
 * Copyright (C) 2019 Patrick Steinhardt <ps@pks.im
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define E(x) { #x, x }
static struct {
	const char *str;
	int error;
} errors[] = {
	E(ENOENT),
	E(ENOTTY),
	E(EILSEQ),
	E(EINVAL),
};

int main(int argc, const char *argv[])
{
	size_t i;

	if (argc != 2) {
		fprintf(stderr, "USAGE: %s <errno>\n", argv[0]);
		return -1;
	}

	for (i = 0; i < sizeof(errors)/sizeof(*errors); i++) {
		if (strcmp(errors[i].str, argv[1]) != 0)
			continue;
		puts(strerror(errors[i].error));
		return 0;
	}

	fprintf(stderr, "Invalid errno: %s\n", argv[1]);
	return -1;
}