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
27DIR * opendir(const char *dirname)
28{
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
90
91 // Success
92 return dir;
93}
u8 type
Definition IntelACPI.h:0
C size_t strlcpy(char *dst, const char *src, size_t siz)
Copy src to string dst of size siz.
Definition strlcpy.cpp:19
#define DT_DIR
This is a directory.
Definition dirent.h:44
#define DT_BLK
This is a block device.
Definition dirent.h:47
C int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition open.cpp:26
C char * dirname(char *path)
Return the directory portion of a pathname.
Definition dirname.cpp:22
#define ESUCCESS
Reports a success operation.
Definition errno.h:43
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 DT_FIFO
This is a named pipe (FIFO).
Definition dirent.h:38
#define DT_CHR
This is a character device.
Definition dirent.h:41
C ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition read.cpp:22
#define DT_REG
This is a regular file.
Definition dirent.h:50
#define DIRLEN
Maximum length of a directory entry name.
Definition dirent.h:59
#define DT_LNK
This is a symbolic link.
Definition dirent.h:53
#define DT_SOCK
This is a Unix domain socket.
Definition dirent.h:56
DIR * opendir(const char *dirname)
Open directory associated with file descriptor.
Definition opendir.cpp:27
C int closedir(DIR *dirp)
Close a directory stream.
Definition closedir.cpp:22
slong Error
Error code defined in Error.h.
Definition Types.h:159
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ZERO
Zero value.
Definition Macros.h:43
unsigned char u8
Unsigned 8-bit number.
Definition Types.h:59
A type representing a directory stream.
Definition dirent.h:95
Size count
Number of direct structures in the buffer.
Definition dirent.h:106
bool eof
End-of-file reached?
Definition dirent.h:109
struct dirent * buffer
Input buffer.
Definition dirent.h:100
Size current
Index of the current dirent.
Definition dirent.h:103
int fd
File descriptor returned by opendir().
Definition dirent.h:97
Describes an entry inside a Directory.
Definition Directory.h:40
Represents a directory entry.
Definition dirent.h:65
u8 d_type
Type of file.
Definition dirent.h:70
The <sys/stat.h> header shall define the stat structure.
Definition stat.h:177