Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / basename / test.c
blob6a19f8d240e5d6d90dae47ae1fc11a7914cbb824
1 /* $NetBSD: test.c,v 1.2 2003/07/26 19:38:45 salo Exp $ */
3 /*
4 * Regression test for basename(3).
6 * Written by Jason R. Thorpe <thorpej@NetBSD.org>, Oct. 2002.
7 * Public domain.
8 */
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <libgen.h>
16 struct {
17 const char *input;
18 const char *output;
19 } test_table[] = {
21 * The following are taken from the "Sample Input and Output Strings
22 * for basename()" table in IEEE Std 1003.1-2001.
24 { "/usr/lib", "lib" },
25 { "/usr/", "usr" },
26 { "/", "/" },
27 { "///", "/" },
28 { "//usr//lib//", "lib" },
30 * IEEE Std 1003.1-2001:
32 * If path is a null pointer or points to an empty string,
33 * basename() shall return a pointer to the string "." .
35 { "", "." },
36 { NULL, "." },
38 * IEEE Std 1003.1-2001:
40 * If the string is exactly "//", it is implementation-defined
41 * whether "/" or "//" is returned.
43 * The NetBSD implementation returns "/".
45 { "//", "/" },
47 { NULL, NULL }
50 int main(int argc, char *argv[]);
52 int
53 main(int argc, char *argv[])
55 char testbuf[32], *base;
56 int i, rv = 0;
58 for (i = 0; test_table[i].output != NULL; i++) {
59 if (test_table[i].input != NULL) {
60 assert(strlen(test_table[i].input) < sizeof(testbuf));
61 strcpy(testbuf, test_table[i].input);
62 base = basename(testbuf);
63 } else
64 base = basename(NULL);
67 * basename(3) is allowed to modify the input buffer.
68 * However, that is considered hostile by some programs,
69 * and so we elect to consider this an error.
71 * This is not a problem, as basename(3) is also allowed
72 * to return a pointer to a statically-allocated buffer
73 * (it is explicitly not required to be reentrant).
75 if (test_table[i].input != NULL &&
76 strcmp(test_table[i].input, testbuf) != 0) {
77 fprintf(stderr,
78 "Input buffer for \"%s\" was modified\n",
79 test_table[i].input);
80 rv = 1;
83 /* Make sure the result is correct. */
84 if (strcmp(test_table[i].output, base) != 0) {
85 fprintf(stderr,
86 "Input \"%s\", output \"%s\", expected \"%s\"\n",
87 test_table[i].input == NULL ? "(null)"
88 : test_table[i].input,
89 base, test_table[i].output);
90 rv = 1;
94 exit(rv);