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