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