1272343Sngie/* $NetBSD: h_cwd.c,v 1.3 2012/04/17 09:23:21 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28272343Sngie */ 29272343Sngie 30272343Sngie#include <sys/types.h> 31272343Sngie#include <sys/stat.h> 32272343Sngie 33272343Sngie#include <err.h> 34272343Sngie#include <errno.h> 35272343Sngie#include <fcntl.h> 36272343Sngie#include <stdlib.h> 37272343Sngie#include <string.h> 38272343Sngie#include <unistd.h> 39272343Sngie 40272343Sngiestatic const char *prefix; 41272343Sngiestatic size_t prefixlen; 42272343Sngiestatic char buf[1024]; 43272343Sngiestatic char pwd[1024]; 44272343Sngie 45272343Sngiestatic const char * 46272343Sngiemakepath(const char *tail) 47272343Sngie{ 48272343Sngie 49272343Sngie strcpy(buf, prefix); 50272343Sngie if (prefix[prefixlen-1] != '/') 51272343Sngie strcat(buf, "/"); 52272343Sngie strcat(buf, tail); 53272343Sngie 54272343Sngie return buf; 55272343Sngie} 56272343Sngie 57272343Sngiestatic void 58272343Sngiedochdir(const char *path, const char *errmsg) 59272343Sngie{ 60272343Sngie 61272343Sngie if (chdir(path) == -1) 62272343Sngie err(EXIT_FAILURE, "%s", errmsg); 63272343Sngie} 64272343Sngie 65272343Sngiestatic void 66272343Sngiedofchdir(const char *path, const char *errmsg) 67272343Sngie{ 68272343Sngie int fd; 69272343Sngie 70272343Sngie fd = open(path, O_RDONLY); 71272343Sngie if (fd == -1) 72272343Sngie err(EXIT_FAILURE, "open %s", errmsg); 73272343Sngie if (fchdir(fd) == -1) 74272343Sngie err(EXIT_FAILURE, "fchdir %s", errmsg); 75272343Sngie close(fd); 76272343Sngie} 77272343Sngiestatic void (*thechdir)(const char *, const char *); 78272343Sngie 79272343Sngiestatic void 80272343Sngiesimple(void) 81272343Sngie{ 82272343Sngie 83272343Sngie thechdir(prefix, "chdir1"); 84272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 85272343Sngie err(EXIT_FAILURE, "getcwd1"); 86272343Sngie if (strcmp(pwd, prefix) != 0) 87272343Sngie errx(EXIT_FAILURE, "strcmp1"); 88272343Sngie 89272343Sngie if (mkdir("dir", 0777) == -1) 90272343Sngie err(EXIT_FAILURE, "mkdir2"); 91272343Sngie thechdir("dir", "chdir2"); 92272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 93272343Sngie err(EXIT_FAILURE, "getcwd2"); 94272343Sngie if (strcmp(pwd, makepath("dir")) != 0) 95272343Sngie errx(EXIT_FAILURE, "strcmp2"); 96272343Sngie 97272343Sngie if (mkdir("dir", 0777) == -1) 98272343Sngie err(EXIT_FAILURE, "mkdir3"); 99272343Sngie thechdir("dir", "chdir3"); 100272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 101272343Sngie err(EXIT_FAILURE, "getcwd3"); 102272343Sngie if (strcmp(pwd, makepath("dir/dir")) != 0) 103272343Sngie errx(EXIT_FAILURE, "strcmp3"); 104272343Sngie 105272343Sngie thechdir("..", "chdir4"); 106272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 107272343Sngie err(EXIT_FAILURE, "getcwd4"); 108272343Sngie if (strcmp(pwd, makepath("dir")) != 0) 109272343Sngie errx(EXIT_FAILURE, "strcmp4"); 110272343Sngie 111272343Sngie 112272343Sngie thechdir("../../../../../../..", "chdir5"); 113272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 114272343Sngie err(EXIT_FAILURE, "getcwd5"); 115272343Sngie if (strcmp(pwd, prefix) != 0) 116272343Sngie errx(EXIT_FAILURE, "strcmp5"); 117272343Sngie 118272343Sngie thechdir("/", "chdir6"); 119272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 120272343Sngie err(EXIT_FAILURE, "getcwd6"); 121272343Sngie if (strcmp(pwd, "/") != 0) 122272343Sngie errx(EXIT_FAILURE, "strcmp6"); 123272343Sngie} 124272343Sngie 125272343Sngiestatic void 126272343Sngiesymlinktest(void) 127272343Sngie{ 128272343Sngie 129272343Sngie thechdir(prefix, "chdir1"); 130272343Sngie if (mkdir("adir", 0777) == -1) 131272343Sngie err(EXIT_FAILURE, "mkdir1"); 132272343Sngie if (mkdir("anotherdir", 0777) == -1) 133272343Sngie err(EXIT_FAILURE, "mkdir2"); 134272343Sngie 135272343Sngie if (symlink("/adir", "anotherdir/lincthesink") == -1) 136272343Sngie err(EXIT_FAILURE, "symlink"); 137272343Sngie 138272343Sngie thechdir("anotherdir/lincthesink", "chdir2"); 139272343Sngie if (getcwd(pwd, sizeof(pwd)) == NULL) 140272343Sngie err(EXIT_FAILURE, "getcwd"); 141272343Sngie if (strcmp(pwd, makepath("adir")) != 0) 142272343Sngie errx(EXIT_FAILURE, "strcmp"); 143272343Sngie} 144272343Sngie 145272343Sngieint 146272343Sngiemain(int argc, char *argv[]) 147272343Sngie{ 148272343Sngie 149272343Sngie if (argc != 4) 150272343Sngie errx(1, "usage"); 151272343Sngie 152272343Sngie prefix = argv[1]; 153272343Sngie prefixlen = strlen(argv[1]); 154272343Sngie 155272343Sngie if (strcmp(argv[3], "chdir") == 0) 156272343Sngie thechdir = dochdir; 157272343Sngie else if (strcmp(argv[3], "fchdir") == 0) 158272343Sngie thechdir = dofchdir; 159272343Sngie else 160272343Sngie errx(EXIT_FAILURE, "invalid chdir type"); 161272343Sngie 162272343Sngie if (strcmp(argv[2], "simple") == 0) 163272343Sngie simple(); 164272343Sngie if (strcmp(argv[2], "symlink") == 0) 165272343Sngie symlinktest(); 166272343Sngie 167272343Sngie return EXIT_SUCCESS; 168272343Sngie} 169