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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* Copyright (c) 2007 Bret S. Lambert <blambert@gsipt.net>
*
* Public domain.
*/
#include <sys/param.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
int
main(void)
{
char path[2 * MAXPATHLEN];
char dname[128];
const char *dir = "junk";
const char *fname = "/file.name.ext";
char *str;
int i;
/* Test normal functioning */
strlcpy(path, "/", sizeof(path));
strlcpy(dname, "/", sizeof(dname));
strlcat(path, dir, sizeof(path));
strlcat(dname, dir, sizeof(dname));
strlcat(path, fname, sizeof(path));
str = dirname(path);
if (strcmp(str, dname) != 0)
errx(1, "0: dirname(%s) = %s != %s", path, str, dname);
/*
* There are four states that require special handling:
*
* 1) path is NULL
* 2) path is the empty string
* 3) path is composed entirely of slashes
* 4) the resulting name is larger than MAXPATHLEN
*
* The first two cases require that a pointer
* to the string "." be returned.
*
* The third case requires that a pointer
* to the string "/" be returned.
*
* The final case requires that NULL be returned
* and errno * be set to ENAMETOOLONG.
*/
/* Case 1 */
str = dirname(NULL);
if (strcmp(str, ".") != 0)
errx(1, "1: dirname(NULL) = %s != .", str);
/* Case 2 */
strlcpy(path, "", sizeof(path));
str = dirname(path);
if (strcmp(str, ".") != 0)
errx(1, "2: dirname(%s) = %s != .", path, str);
/* Case 3 */
for (i = 0; i < MAXPATHLEN - 1; i++)
strlcat(path, "/", sizeof(path)); /* path cleared above */
str = dirname(path);
if (strcmp(str, "/") != 0)
errx(1, "3: dirname(%s) = %s != /", path, str);
/* Case 4 */
strlcpy(path, "/", sizeof(path)); /* reset path */
for (i = 0; i <= MAXPATHLEN; i += strlen(dir))
strlcat(path, dir, sizeof(path));
strlcat(path, fname, sizeof(path));
str = dirname(path);
if (str != NULL)
errx(1, "4: dirname(%s) = %s != NULL", path, str);
if (errno != ENAMETOOLONG)
errx(1, "4: dirname(%s) sets errno to %d", path, errno);
return (0);
}
|