Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / core / fs / chdir.c
blobbfce9bce2c7e0475275b1142f0e8147f277a693b
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <string.h>
4 #include "fs.h"
5 #include "cache.h"
7 /*
8 * Convert a relative pathname to an absolute pathname
9 * In the future this might also resolve symlinks...
11 void pm_realpath(com32sys_t *regs)
13 const char *src = MK_PTR(regs->ds, regs->esi.w[0]);
14 char *dst = MK_PTR(regs->es, regs->edi.w[0]);
16 realpath(dst, src, FILENAME_MAX);
19 size_t realpath(char *dst, const char *src, size_t bufsize)
21 if (this_fs->fs_ops->realpath) {
22 return this_fs->fs_ops->realpath(this_fs, dst, src, bufsize);
23 } else {
24 /* Filesystems with "common" pathname resolution */
25 return snprintf(dst, bufsize, "%s%s",
26 src[0] == '/' ? "" : this_fs->cwd_name,
27 src);
31 int chdir(const char *src)
33 int rv;
34 struct file *file;
35 char *p;
37 if (this_fs->fs_ops->chdir)
38 return this_fs->fs_ops->chdir(this_fs, src);
40 /* Otherwise it is a "conventional filesystem" */
41 rv = searchdir(src);
42 if (rv < 0)
43 return rv;
45 file = handle_to_file(rv);
46 if (file->inode->mode != DT_DIR) {
47 _close_file(file);
48 return -1;
51 put_inode(this_fs->cwd);
52 this_fs->cwd = get_inode(file->inode);
53 _close_file(file);
55 /* Save the current working directory */
56 realpath(this_fs->cwd_name, src, CURRENTDIR_MAX);
57 p = strchr(this_fs->cwd_name, '\0');
59 /* Make sure the cwd_name ends in a slash, it's supposed to be a prefix */
60 if (p < this_fs->cwd_name + CURRENTDIR_MAX - 1 &&
61 (p == this_fs->cwd_name || p[1] != '/')) {
62 p[0] = '/';
63 p[1] = '\0';
65 return 0;