FreeNOS
opendir.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 <FreeNOS/User.h>
19 #include <FileSystem.h>
20 #include <Directory.h>
21 #include <errno.h>
22 #include "dirent.h"
23 #include "fcntl.h"
24 #include "unistd.h"
25 #include "sys/stat.h"
26 
27 DIR * opendir(const char *dirname)
28 {
29  Dirent *dirent;
30  DIR *dir;
31  int fd;
32  struct stat st;
33  Error e;
34 
35  // First stat the directory
36  if (stat(dirname, &st) < 0)
37  {
38  return (ZERO);
39  }
40 
41  // Try to open the directory
42  if ((fd = open(dirname, ZERO)) < 0)
43  {
44  return (ZERO);
45  }
46 
47  // Allocate Dirents
48  dirent = new Dirent[1024];
49  memset(dirent, 0, 1024 * sizeof(Dirent));
50 
51  // Allocate DIR object
52  dir = new DIR;
53  dir->fd = fd;
54  dir->buffer = new struct dirent[1024];
55  memset(dir->buffer, 0, 1024 * sizeof(struct dirent));
56  dir->current = 0;
57  dir->count = 0;
58  dir->eof = false;
59 
60  // Read them all
61  if ((e = read(fd, dirent, sizeof(Dirent) * 1024)) < 0)
62  {
63  e = errno;
64  closedir(dir);
65  errno = e;
66  return (ZERO);
67  }
68 
69  // Fill in the dirent structs
70  for (Size i = 0; i < e / sizeof(Dirent); i++)
71  {
72  u8 types[] =
73  {
74  DT_REG,
75  DT_DIR,
76  DT_BLK,
77  DT_CHR,
78  DT_LNK,
79  DT_FIFO,
80  DT_SOCK,
81  };
82  strlcpy((dir->buffer)[i].d_name, dirent[i].name, DIRLEN);
83  (dir->buffer)[i].d_type = types[dirent[i].type];
84  }
85  dir->count = e / sizeof(Dirent);
86  delete dirent;
87 
88  // Set errno
89  errno = ESUCCESS;
90 
91  // Success
92  return dir;
93 }
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Copy src to string dst of size siz.
Definition: strlcpy.cpp:19
stat
The <sys/stat.h> header shall define the stat structure.
Definition: stat.h:176
FileSystem.h
fcntl.h
errno
C int errno
The lvalue errno is used by many functions to return error values.
DIRLEN
#define DIRLEN
Maximum length of a directory entry name.
Definition: dirent.h:59
Dirent
struct Dirent Dirent
Describes an entry inside a Directory.
closedir
int closedir(DIR *dirp)
Close a directory stream.
Definition: closedir.cpp:22
DT_REG
#define DT_REG
This is a regular file.
Definition: dirent.h:50
ESUCCESS
#define ESUCCESS
Reports a success operation.
Definition: errno.h:43
DT_BLK
#define DT_BLK
This is a block device.
Definition: dirent.h:47
open
int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition: open.cpp:26
opendir
DIR * opendir(const char *dirname)
Open directory associated with file descriptor.
Definition: opendir.cpp:27
read
ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition: read.cpp:22
dirent.h
DIR::eof
bool eof
End-of-file reached?
Definition: dirent.h:109
DIR::buffer
struct dirent * buffer
Input buffer.
Definition: dirent.h:100
dirent
Represents a directory entry.
Definition: dirent.h:64
DIR::count
Size count
Number of direct structures in the buffer.
Definition: dirent.h:106
Error
slong Error
Error code defined in Error.h.
Definition: Types.h:159
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
DT_SOCK
#define DT_SOCK
This is a Unix domain socket.
Definition: dirent.h:56
stat
int stat(const char *path, struct stat *buf)
Get file status.
Definition: stat.cpp:25
DIR
struct DIR DIR
A type representing a directory stream.
DIR::current
Size current
Index of the current dirent.
Definition: dirent.h:103
stat.h
DIR
A type representing a directory stream.
Definition: dirent.h:94
DT_DIR
#define DT_DIR
This is a directory.
Definition: dirent.h:44
unistd.h
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
DT_CHR
#define DT_CHR
This is a character device.
Definition: dirent.h:41
DIR::fd
int fd
File descriptor returned by opendir().
Definition: dirent.h:97
memset
void * memset(void *dest, int ch, size_t count)
Fill memory with a constant byte.
Definition: memset.cpp:20
DT_FIFO
#define DT_FIFO
This is a named pipe (FIFO).
Definition: dirent.h:38
DT_LNK
#define DT_LNK
This is a symbolic link.
Definition: dirent.h:53
Directory.h
type
u8 type
Definition: IntelACPI.h:63
Dirent
Describes an entry inside a Directory.
Definition: Directory.h:39
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
dirname
char * dirname(char *path)
Return the directory portion of a pathname.
Definition: dirname.cpp:22
errno.h
dirent::d_type
u8 d_type
Type of file.
Definition: dirent.h:70