• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  * Testcase to check the basic functionality of the getcwd(2)
9  * system call on a symbolic link.
10  *
11  * ALGORITHM
12  * 1) create a directory, and create a symbolic link to it at the
13  *    same directory level.
14  * 2) get the working directory of a directory, and its pathname.
15  * 3) get the working directory of a symbolic link, and its pathname,
16  *    and its readlink info.
17  * 4) compare the working directories and link information.
18  */
19 
20 #define _GNU_SOURCE 1
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include "tst_test.h"
29 
30 static char dir[BUFSIZ], dir_link[BUFSIZ];
31 
verify_getcwd(void)32 static void verify_getcwd(void)
33 {
34 	char link[BUFSIZ];
35 	char *res1 = NULL;
36 	char *res2 = NULL;
37 
38 	SAFE_CHDIR(dir);
39 
40 	res1 = getcwd(NULL, 0);
41 	if (!res1) {
42 		tst_res(TFAIL | TERRNO, "getcwd() failed to "
43 			"get working directory of a directory");
44 		goto end;
45 	}
46 
47 	SAFE_CHDIR("..");
48 	SAFE_CHDIR(dir_link);
49 
50 	res2 = getcwd(NULL, 0);
51 	if (!res2) {
52 		tst_res(TFAIL | TERRNO, "getcwd() failed to get "
53 			"working directory of a symbolic link");
54 		goto end;
55 	}
56 
57 	if (strcmp(res1, res2)) {
58 		tst_res(TFAIL,
59 			"getcwd() got mismatched working directories (%s, %s)",
60 			res1, res2);
61 		goto end;
62 	}
63 
64 	SAFE_CHDIR("..");
65 	SAFE_READLINK(dir_link, link, sizeof(link));
66 
67 	if (strcmp(link, SAFE_BASENAME(res1))) {
68 		tst_res(TFAIL,
69 			"link information didn't match the working directory");
70 		goto end;
71 	}
72 
73 	tst_res(TPASS, "getcwd() succeeded on a symbolic link");
74 
75 end:
76 	free(res1);
77 	free(res2);
78 }
79 
setup(void)80 static void setup(void)
81 {
82 	sprintf(dir, "getcwd1.%d", getpid());
83 	sprintf(dir_link, "getcwd2.%d", getpid());
84 	SAFE_MKDIR(dir, 0755);
85 	SAFE_SYMLINK(dir, dir_link);
86 }
87 
88 static struct tst_test test = {
89 	.needs_tmpdir = 1,
90 	.setup = setup,
91 	.test_all = verify_getcwd
92 };
93