• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
31 #include <sys/stat.h>
32 
33 #include "tst_test.h"
34 
35 static char exp_buf[PATH_MAX];
36 static char buffer[PATH_MAX];
37 
38 static struct t_case {
39 	char *buf;
40 	size_t size;
41 } tcases[] = {
42 	{buffer, sizeof(buffer)},
43 	{NULL, 0},
44 	{NULL, PATH_MAX}
45 };
46 
dir_exists(const char * dirpath)47 static int dir_exists(const char *dirpath)
48 {
49 	struct stat sb;
50 
51 	if (!stat(dirpath, &sb) && S_ISDIR(sb.st_mode))
52 		return 1;
53 
54 	return 0;
55 }
56 
get_tmpdir_path(void)57 static const char *get_tmpdir_path(void)
58 {
59 	char *tmpdir = "/tmp";
60 
61 	if (dir_exists(tmpdir))
62 		goto done;
63 
64 	/* fallback to $TMPDIR */
65 	tmpdir = getenv("TMPDIR");
66 	if (!tmpdir)
67 		tst_brk(TBROK | TERRNO, "Failed to get $TMPDIR");
68 
69 	if (tmpdir[0] != '/')
70 		tst_brk(TBROK, "$TMPDIR must be an absolute path");
71 
72 	if (!dir_exists(tmpdir))
73 		tst_brk(TBROK | TERRNO, "TMPDIR '%s' doesn't exist", tmpdir);
74 
75 done:
76 	return tmpdir;
77 }
78 
verify_getcwd(unsigned int n)79 static void verify_getcwd(unsigned int n)
80 {
81 	struct t_case *tc = &tcases[n];
82 	char *res = NULL;
83 
84 	errno = 0;
85 	res = getcwd(tc->buf, tc->size);
86 	TST_ERR = errno;
87 	if (!res) {
88 		tst_res(TFAIL | TTERRNO, "getcwd() failed");
89 		goto end;
90 	}
91 
92 	if (strcmp(exp_buf, res)) {
93 		tst_res(TFAIL, "getcwd() returned unexpected directory: %s, "
94 			"expected: %s", res, exp_buf);
95 		goto end;
96 	}
97 
98 	tst_res(TPASS, "getcwd() returned expected directory: %s", res);
99 
100 end:
101 	if (!tc->buf)
102 		free(res);
103 }
104 
setup(void)105 static void setup(void)
106 {
107 	const char *tmpdir = get_tmpdir_path();
108 
109 	SAFE_CHDIR(tmpdir);
110 
111 	if (!realpath(tmpdir, exp_buf))
112 		tst_brk(TBROK | TERRNO, "realpath() failed");
113 
114 	tst_res(TINFO, "Expected path '%s'", exp_buf);
115 }
116 
117 static struct tst_test test = {
118 	.setup = setup,
119 	.tcnt = ARRAY_SIZE(tcases),
120 	.test = verify_getcwd
121 };
122