FreeNOS
chdir.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <FileSystemClient.h>
19#include <FileSystemPath.h>
20#include <String.h>
21#include <List.h>
22#include "limits.h"
23#include "string.h"
24#include "sys/stat.h"
25#include "unistd.h"
26
27int chdir(const char *filepath)
28{
29 String last;
30 List<String> lst;
31 char cwd[PATH_MAX], buf[PATH_MAX], *path = ZERO;
32 FileSystemClient filesystem;
33
34 struct stat st;
35
36 // What's the current working dir?
37 getcwd(cwd, PATH_MAX);
38
39 // Relative or absolute?
40 if (filepath[0] != '/')
41 {
42 snprintf(buf, sizeof(buf), "%s/%s", cwd, filepath);
43 FileSystemPath fspath(buf);
44
45 // Process '..'
46 for (ListIterator<String> i(fspath.split()); i.hasCurrent(); i++)
47 {
48 if ((*i.current())[0] != '.')
49 {
50 lst.append(i.current());
51 last = i.current();
52 }
53 else if ((*i.current())[1] == '.' && last.length() > 0)
54 {
55 lst.remove(last);
56 }
57 }
58
59 memset(buf, 0, sizeof(buf));
60
61 // Construct final path
62 for (ListIterator<String> i(&lst); i.hasCurrent(); i++)
63 {
64 strcat(buf, "/");
65 strcat(buf, *i.current());
66 }
67 path = buf;
68 }
69 else
70 path = (char *) filepath;
71
72 // Fall back to slash?
73 if (!path[0])
74 {
75 strcpy(buf, "/");
76 path = buf;
77 }
78
79 // Stat the file
80 if (stat(path, &st) != 0)
81 {
82 return -1;
83 }
84
85 // Must be a directory
86 if (!S_ISDIR(st.st_mode))
87 {
88 errno = ENOTDIR;
89 return -1;
90 }
91
92 // Set current directory
93 filesystem.setCurrentDirectory(path);
94
95 // Done
96 errno = ZERO;
97 return 0;
98}
FileSystemClient provides a simple interface to a FileSystemServer.
void setCurrentDirectory(const String &directory)
Set new current directory.
Simple filesystem path parser.
const List< String > & split() const
Returns a List of separate path elements.
Iterate through a List.
virtual bool hasCurrent() const
Check if there is a current item on the List.
Simple linked list template class.
Definition List.h:37
void append(T t)
Insert an item at the end of the list.
Definition List.h:139
virtual int remove(T t)
Remove all items which are equal to the given item.
Definition List.h:166
Abstraction of strings.
Definition String.h:42
Size length() const
Same as count().
Definition String.cpp:105
C int errno
The lvalue errno is used by many functions to return error values.
C void * memset(void *dest, int ch, size_t count)
Fill memory with a constant byte.
Definition memset.cpp:20
#define S_ISDIR(m)
Test for a directory.
Definition stat.h:155
C char * strcat(char *dest, const char *src)
Concatenate two strings.
Definition strcat.cpp:20
#define ENOTDIR
Not a directory.
Definition errno.h:214
C int strcpy(char *dest, const char *src)
Copy a string.
Definition strcpy.cpp:20
int chdir(const char *filepath)
Change working directory.
Definition chdir.cpp:27
C char * getcwd(char *buf, size_t size)
Get the pathname of the current working directory.
Definition getcwd.cpp:24
C int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition snprintf.cpp:22
#define PATH_MAX
Maximum file path length.
Definition limits.h:37
#define ZERO
Zero value.
Definition Macros.h:43
The <sys/stat.h> header shall define the stat structure.
Definition stat.h:177
mode_t st_mode
Mode of file.
Definition stat.h:203