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