1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.
16 */
17
18 /*
19 * DESCRIPTION
20 * Testcase to check the basic functionality of the getcwd(2) system call.
21 * 1) getcwd(2) works fine if buf and size are valid.
22 * 2) getcwd(2) works fine if buf points to NULL and size is set to 0.
23 * 3) getcwd(2) works fine if buf points to NULL and size is greater than strlen(path).
24 */
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "tst_test.h"
31
32 #define TMPDIR "/tmp"
33
34 static char exp_buf[PATH_MAX];
35 static char buffer[PATH_MAX];
36
37 static struct t_case {
38 char *buf;
39 size_t size;
40 } tcases[] = {
41 {buffer, sizeof(buffer)},
42 {NULL, 0},
43 {NULL, PATH_MAX}
44 };
45
verify_getcwd(unsigned int n)46 static void verify_getcwd(unsigned int n)
47 {
48 struct t_case *tc = &tcases[n];
49 char *res = NULL;
50
51 errno = 0;
52 res = getcwd(tc->buf, tc->size);
53 TEST_ERRNO = errno;
54 if (!res) {
55 tst_res(TFAIL | TTERRNO, "getcwd() failed");
56 goto end;
57 }
58
59 if (strcmp(exp_buf, res)) {
60 tst_res(TFAIL, "getcwd() returned unexpected directory: %s, "
61 "expected: %s", res, exp_buf);
62 goto end;
63 }
64
65 tst_res(TPASS, "getcwd() returned expected directory: %s", res);
66
67 end:
68 if (!tc->buf)
69 free(res);
70 }
71
setup(void)72 static void setup(void)
73 {
74 SAFE_CHDIR(TMPDIR);
75
76 if (!realpath(TMPDIR, exp_buf))
77 tst_brk(TBROK | TERRNO, "realpath() failed");
78
79 tst_res(TINFO, "Expected path '%s'", exp_buf);
80 }
81
82 static struct tst_test test = {
83 .tid = "getcwd02",
84 .setup = setup,
85 .tcnt = ARRAY_SIZE(tcases),
86 .test = verify_getcwd
87 };
88