xref: /freebsd/contrib/netbsd-tests/lib/librumphijack/h_cwd.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /*      $NetBSD: h_cwd.c,v 1.3 2012/04/17 09:23:21 jruoho Exp $	*/
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper  * are met:
10*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper  *
16*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*57718be8SEnji Cooper  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*57718be8SEnji Cooper  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*57718be8SEnji Cooper  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*57718be8SEnji Cooper  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*57718be8SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*57718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*57718be8SEnji Cooper  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*57718be8SEnji Cooper  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*57718be8SEnji Cooper  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*57718be8SEnji Cooper  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*57718be8SEnji Cooper  */
29*57718be8SEnji Cooper 
30*57718be8SEnji Cooper #include <sys/types.h>
31*57718be8SEnji Cooper #include <sys/stat.h>
32*57718be8SEnji Cooper 
33*57718be8SEnji Cooper #include <err.h>
34*57718be8SEnji Cooper #include <errno.h>
35*57718be8SEnji Cooper #include <fcntl.h>
36*57718be8SEnji Cooper #include <stdlib.h>
37*57718be8SEnji Cooper #include <string.h>
38*57718be8SEnji Cooper #include <unistd.h>
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper static const char *prefix;
41*57718be8SEnji Cooper static size_t prefixlen;
42*57718be8SEnji Cooper static char buf[1024];
43*57718be8SEnji Cooper static char pwd[1024];
44*57718be8SEnji Cooper 
45*57718be8SEnji Cooper static const char *
makepath(const char * tail)46*57718be8SEnji Cooper makepath(const char *tail)
47*57718be8SEnji Cooper {
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper 	strcpy(buf, prefix);
50*57718be8SEnji Cooper 	if (prefix[prefixlen-1] != '/')
51*57718be8SEnji Cooper 		strcat(buf, "/");
52*57718be8SEnji Cooper 	strcat(buf, tail);
53*57718be8SEnji Cooper 
54*57718be8SEnji Cooper 	return buf;
55*57718be8SEnji Cooper }
56*57718be8SEnji Cooper 
57*57718be8SEnji Cooper static void
dochdir(const char * path,const char * errmsg)58*57718be8SEnji Cooper dochdir(const char *path, const char *errmsg)
59*57718be8SEnji Cooper {
60*57718be8SEnji Cooper 
61*57718be8SEnji Cooper 	if (chdir(path) == -1)
62*57718be8SEnji Cooper 		err(EXIT_FAILURE, "%s", errmsg);
63*57718be8SEnji Cooper }
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper static void
dofchdir(const char * path,const char * errmsg)66*57718be8SEnji Cooper dofchdir(const char *path, const char *errmsg)
67*57718be8SEnji Cooper {
68*57718be8SEnji Cooper 	int fd;
69*57718be8SEnji Cooper 
70*57718be8SEnji Cooper 	fd = open(path, O_RDONLY);
71*57718be8SEnji Cooper 	if (fd == -1)
72*57718be8SEnji Cooper 		err(EXIT_FAILURE, "open %s", errmsg);
73*57718be8SEnji Cooper 	if (fchdir(fd) == -1)
74*57718be8SEnji Cooper 		err(EXIT_FAILURE, "fchdir %s", errmsg);
75*57718be8SEnji Cooper 	close(fd);
76*57718be8SEnji Cooper }
77*57718be8SEnji Cooper static void (*thechdir)(const char *, const char *);
78*57718be8SEnji Cooper 
79*57718be8SEnji Cooper static void
simple(void)80*57718be8SEnji Cooper simple(void)
81*57718be8SEnji Cooper {
82*57718be8SEnji Cooper 
83*57718be8SEnji Cooper 	thechdir(prefix, "chdir1");
84*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
85*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd1");
86*57718be8SEnji Cooper 	if (strcmp(pwd, prefix) != 0)
87*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp1");
88*57718be8SEnji Cooper 
89*57718be8SEnji Cooper 	if (mkdir("dir", 0777) == -1)
90*57718be8SEnji Cooper 		err(EXIT_FAILURE, "mkdir2");
91*57718be8SEnji Cooper 	thechdir("dir", "chdir2");
92*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
93*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd2");
94*57718be8SEnji Cooper 	if (strcmp(pwd, makepath("dir")) != 0)
95*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp2");
96*57718be8SEnji Cooper 
97*57718be8SEnji Cooper 	if (mkdir("dir", 0777) == -1)
98*57718be8SEnji Cooper 		err(EXIT_FAILURE, "mkdir3");
99*57718be8SEnji Cooper 	thechdir("dir", "chdir3");
100*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
101*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd3");
102*57718be8SEnji Cooper 	if (strcmp(pwd, makepath("dir/dir")) != 0)
103*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp3");
104*57718be8SEnji Cooper 
105*57718be8SEnji Cooper 	thechdir("..", "chdir4");
106*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
107*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd4");
108*57718be8SEnji Cooper 	if (strcmp(pwd, makepath("dir")) != 0)
109*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp4");
110*57718be8SEnji Cooper 
111*57718be8SEnji Cooper 
112*57718be8SEnji Cooper 	thechdir("../../../../../../..", "chdir5");
113*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
114*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd5");
115*57718be8SEnji Cooper 	if (strcmp(pwd, prefix) != 0)
116*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp5");
117*57718be8SEnji Cooper 
118*57718be8SEnji Cooper 	thechdir("/", "chdir6");
119*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
120*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd6");
121*57718be8SEnji Cooper 	if (strcmp(pwd, "/") != 0)
122*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp6");
123*57718be8SEnji Cooper }
124*57718be8SEnji Cooper 
125*57718be8SEnji Cooper static void
symlinktest(void)126*57718be8SEnji Cooper symlinktest(void)
127*57718be8SEnji Cooper {
128*57718be8SEnji Cooper 
129*57718be8SEnji Cooper 	thechdir(prefix, "chdir1");
130*57718be8SEnji Cooper 	if (mkdir("adir", 0777) == -1)
131*57718be8SEnji Cooper 		err(EXIT_FAILURE, "mkdir1");
132*57718be8SEnji Cooper 	if (mkdir("anotherdir", 0777) == -1)
133*57718be8SEnji Cooper 		err(EXIT_FAILURE, "mkdir2");
134*57718be8SEnji Cooper 
135*57718be8SEnji Cooper 	if (symlink("/adir", "anotherdir/lincthesink") == -1)
136*57718be8SEnji Cooper 		err(EXIT_FAILURE, "symlink");
137*57718be8SEnji Cooper 
138*57718be8SEnji Cooper 	thechdir("anotherdir/lincthesink", "chdir2");
139*57718be8SEnji Cooper 	if (getcwd(pwd, sizeof(pwd)) == NULL)
140*57718be8SEnji Cooper 		err(EXIT_FAILURE, "getcwd");
141*57718be8SEnji Cooper 	if (strcmp(pwd, makepath("adir")) != 0)
142*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "strcmp");
143*57718be8SEnji Cooper }
144*57718be8SEnji Cooper 
145*57718be8SEnji Cooper int
main(int argc,char * argv[])146*57718be8SEnji Cooper main(int argc, char *argv[])
147*57718be8SEnji Cooper {
148*57718be8SEnji Cooper 
149*57718be8SEnji Cooper 	if (argc != 4)
150*57718be8SEnji Cooper 		errx(1, "usage");
151*57718be8SEnji Cooper 
152*57718be8SEnji Cooper 	prefix = argv[1];
153*57718be8SEnji Cooper 	prefixlen = strlen(argv[1]);
154*57718be8SEnji Cooper 
155*57718be8SEnji Cooper 	if (strcmp(argv[3], "chdir") == 0)
156*57718be8SEnji Cooper 		thechdir = dochdir;
157*57718be8SEnji Cooper 	else if (strcmp(argv[3], "fchdir") == 0)
158*57718be8SEnji Cooper 		thechdir = dofchdir;
159*57718be8SEnji Cooper 	else
160*57718be8SEnji Cooper 		errx(EXIT_FAILURE, "invalid chdir type");
161*57718be8SEnji Cooper 
162*57718be8SEnji Cooper 	if (strcmp(argv[2], "simple") == 0)
163*57718be8SEnji Cooper 		simple();
164*57718be8SEnji Cooper 	if (strcmp(argv[2], "symlink") == 0)
165*57718be8SEnji Cooper 		symlinktest();
166*57718be8SEnji Cooper 
167*57718be8SEnji Cooper 	return EXIT_SUCCESS;
168*57718be8SEnji Cooper }
169