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 
27 int 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 }
S_ISDIR
#define S_ISDIR(m)
Test for a directory.
Definition: stat.h:155
stat
The <sys/stat.h> header shall define the stat structure.
Definition: stat.h:176
FileSystemPath
Simple filesystem path parser.
Definition: FileSystemPath.h:37
FileSystemPath.h
errno
C int errno
The lvalue errno is used by many functions to return error values.
String::length
Size length() const
Same as count().
Definition: String.cpp:105
string.h
getcwd
char * getcwd(char *buf, size_t size)
Get the pathname of the current working directory.
Definition: getcwd.cpp:24
String
Abstraction of strings.
Definition: String.h:41
FileSystemClient
FileSystemClient provides a simple interface to a FileSystemServer.
Definition: FileSystemClient.h:42
PATH_MAX
#define PATH_MAX
Maximum file path length.
Definition: limits.h:37
strcpy
int strcpy(char *dest, const char *src)
Copy a string.
Definition: strcpy.cpp:20
limits.h
List::remove
virtual int remove(T t)
Remove all items which are equal to the given item.
Definition: List.h:166
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
FileSystemClient::setCurrentDirectory
void setCurrentDirectory(const String &directory)
Set new current directory.
Definition: FileSystemClient.cpp:151
FileSystemPath::split
const List< String > & split() const
Returns a List of separate path elements.
Definition: FileSystemPath.cpp:63
List::append
void append(T t)
Insert an item at the end of the list.
Definition: List.h:139
strcat
char * strcat(char *dest, const char *src)
Concatenate two strings.
Definition: strcat.cpp:20
stat::st_mode
mode_t st_mode
Mode of file.
Definition: stat.h:203
stat
int stat(const char *path, struct stat *buf)
Get file status.
Definition: stat.cpp:25
stat.h
unistd.h
FileSystemClient.h
chdir
int chdir(const char *filepath)
Change working directory.
Definition: chdir.cpp:27
String.h
memset
void * memset(void *dest, int ch, size_t count)
Fill memory with a constant byte.
Definition: memset.cpp:20
List< String >
ENOTDIR
#define ENOTDIR
Not a directory.
Definition: errno.h:214
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
snprintf
int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition: snprintf.cpp:22
List.h
ListIterator
Iterate through a List.
Definition: ListIterator.h:37