Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / dirname / test.c
blob254a7f595bfad20e7a0c94684fc63dba159d23fd
1 /* $NetBSD: test.c,v 1.4 2003/07/26 19:38:46 salo Exp $ */
3 /*
4 * Regression test for dirname(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 <string.h>
13 #include <libgen.h>
14 #include <stdlib.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 dirname()" table in IEEE Std 1003.1-2001.
24 { "/usr/lib", "/usr" },
25 { "/usr/", "/" },
26 { "usr", "." },
27 { "/", "/" },
28 { ".", "." },
29 { "..", "." },
31 * IEEE Std 1003.1-2001:
33 * If path is a null pointer or points to an empty string,
34 * dirname() shall return a pointer to the string "." .
36 { "", "." },
37 { NULL, "." },
39 * IEEE Std 1003.1-2001:
41 * Since the meaning of the leading "//" is implementation-defined,
42 * dirname("//foo") may return either "//" or "/" (but nothing else).
44 * The NetBSD implementation returns "/".
46 { "//foo", "/" },
48 * Make sure the trailing slashes after the directory name component
49 * get trimmed. The Std does not talk about this, but this is what
50 * Solaris 8's dirname(3) does.
52 { "/usr///lib", "/usr" },
54 { NULL, NULL }
57 int main(int argc, char *argv[]);
59 int
60 main(int argc, char *argv[])
62 char testbuf[32], *base;
63 int i, rv = 0;
65 for (i = 0; test_table[i].output != NULL; i++) {
66 if (test_table[i].input != NULL) {
67 assert(strlen(test_table[i].input) < sizeof(testbuf));
68 strcpy(testbuf, test_table[i].input);
69 base = dirname(testbuf);
70 } else
71 base = dirname(NULL);
74 * dirname(3) is allowed to modify the input buffer.
75 * However, that is considered hostile by some programs,
76 * and so we elect to consider this an error.
78 * This is not a problem, as dirname(3) is also allowed
79 * to return a pointer to a statically-allocated buffer
80 * (it is explicitly not required to be reentrant).
82 if (test_table[i].input != NULL &&
83 strcmp(test_table[i].input, testbuf) != 0) {
84 fprintf(stderr,
85 "Input buffer for \"%s\" was modified\n",
86 test_table[i].input);
87 rv = 1;
90 /* Make sure the result is correct. */
91 if (strcmp(test_table[i].output, base) != 0) {
92 fprintf(stderr,
93 "Input \"%s\", output \"%s\", expected \"%s\"\n",
94 test_table[i].input == NULL ? "(null)"
95 : test_table[i].input,
96 base, test_table[i].output);
97 rv = 1;
101 exit(rv);