1/*
2 * Copyright (c) 2007, 2008, 2009, 2011, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9
10#define _USE_XOPEN // for strdup()
11#include <string.h>
12#include <unistd.h>
13#include <barrelfish/barrelfish.h>
14#include <vfs/vfs.h>
15#include <vfs/vfs_path.h>
16#include "posixcompat.h"
17
18int chdir(const char *pathname)
19{
20    errval_t err;
21
22    char *newcwd = vfs_path_mkabs(pathname);
23    assert(newcwd != NULL);
24
25    // ensure directory exists, by attempting to open it
26    vfs_handle_t dh;
27    err = vfs_opendir(newcwd, &dh);
28    if (err_is_fail(err)) {
29        POSIXCOMPAT_DEBUG("chdir('%s') -> '%s' FAILED\n", pathname, newcwd);
30        free(newcwd);
31        return -1;
32    }
33    vfs_closedir(dh);
34
35    POSIXCOMPAT_DEBUG("chdir('%s') -> '%s'\n", pathname, newcwd);
36
37    // ok!
38    return setenv("PWD", newcwd, 1);
39}
40