Home | History | Annotate | Download | only in getcwd
      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)
     21  * system call on a symbolic link.
     22  *
     23  * ALGORITHM
     24  * 1) create a directory, and create a symbolic link to it at the
     25  *    same directory level.
     26  * 2) get the working directory of a directory, and its pathname.
     27  * 3) get the working directory of a symbolic link, and its pathname,
     28  *    and its readlink info.
     29  * 4) compare the working directories and link information.
     30  */
     31 
     32 #define _GNU_SOURCE 1
     33 #include <errno.h>
     34 #include <stdio.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #include <sys/stat.h>
     38 #include <sys/types.h>
     39 #include <stdlib.h>
     40 #include "tst_test.h"
     41 
     42 static char dir[BUFSIZ], dir_link[BUFSIZ];
     43 
     44 static void verify_getcwd(void)
     45 {
     46 	char link[BUFSIZ];
     47 	char *res1 = NULL;
     48 	char *res2 = NULL;
     49 
     50 	SAFE_CHDIR(dir);
     51 
     52 	res1 = getcwd(NULL, 0);
     53 	if (!res1) {
     54 		tst_res(TFAIL | TERRNO, "getcwd() failed to "
     55 			"get working directory of a directory");
     56 		goto end;
     57 	}
     58 
     59 	SAFE_CHDIR("..");
     60 	SAFE_CHDIR(dir_link);
     61 
     62 	res2 = getcwd(NULL, 0);
     63 	if (!res2) {
     64 		tst_res(TFAIL | TERRNO, "getcwd() failed to get "
     65 			"working directory of a symbolic link");
     66 		goto end;
     67 	}
     68 
     69 	if (strcmp(res1, res2)) {
     70 		tst_res(TFAIL,
     71 			"getcwd() got mismatched working directories (%s, %s)",
     72 			res1, res2);
     73 		goto end;
     74 	}
     75 
     76 	SAFE_CHDIR("..");
     77 	SAFE_READLINK(dir_link, link, sizeof(link));
     78 
     79 	if (strcmp(link, SAFE_BASENAME(res1))) {
     80 		tst_res(TFAIL,
     81 			"link information didn't match the working directory");
     82 		goto end;
     83 	}
     84 
     85 	tst_res(TPASS, "getcwd() succeeded on a symbolic link");
     86 
     87 end:
     88 	free(res1);
     89 	free(res2);
     90 }
     91 
     92 static void setup(void)
     93 {
     94 	sprintf(dir, "getcwd1.%d", getpid());
     95 	sprintf(dir_link, "getcwd2.%d", getpid());
     96 	SAFE_MKDIR(dir, 0755);
     97 	SAFE_SYMLINK(dir, dir_link);
     98 }
     99 
    100 static struct tst_test test = {
    101 	.needs_tmpdir = 1,
    102 	.setup = setup,
    103 	.test_all = verify_getcwd
    104 };
    105