2 #include <sys/dirent.h>
11 int _syscall_set_errno(struct _reent *ptr, int err) {
16 extern int _stat_r(struct _reent *ptr, const char *path, struct stat *sb);
17 int _lstat_r(struct _reent *ptr, const char *path, struct stat *sb) {
18 return _stat_r(ptr,path,sb);
21 uid_t getuid() { return 0; }
22 gid_t getgid() { return 0; }
23 uid_t geteuid() { return 0; }
24 gid_t getegid() { return 0; }
25 int getgroups(int gidsetlen, gid_t *gidset) {
26 if(gidsetlen) *gidset = 0;
29 mode_t umask(mode_t new) { return 0022; }
31 static int syscall_nosys(struct _reent *ptr) {
36 int _access_r(struct _reent *ptr, const char *pathname, int mode) {
38 if(_stat_r(ptr,pathname,&statbuf) < 0) return -1;
42 /* FIXME: These should be in newlib */
43 int access(const char *pathname, int mode) { return _access_r(_REENT,pathname,mode); }
44 extern int _rmdir_r(struct _reent *ptr, const char *pathname);
45 int rmdir(const char *pathname) { return _rmdir_r(_REENT,pathname); }
46 extern long _sysconf_r(struct _reent *ptr, int n);
47 long sysconf(int n) { return _sysconf_r(_REENT,n); }
49 #define SYSCALL_NOSYS_R(name) int _##name##_r(struct _reent *ptr) { return syscall_nosys(ptr); }
52 SYSCALL_NOSYS_R(symlink)
53 SYSCALL_NOSYS_R(readlink)
54 SYSCALL_NOSYS_R(chown)
55 SYSCALL_NOSYS_R(fchown)
56 SYSCALL_NOSYS_R(chmod)
57 SYSCALL_NOSYS_R(fchmod)
59 static int read_fully(int fd, void *buf, size_t size) {
62 n = read(fd,buf,size);
70 DIR *opendir(const char *path) {
75 fd = open(path,O_RDONLY);
76 if(fd < 0) return NULL;
78 if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
84 dir = malloc(sizeof(*dir));
95 static int readdir_r(DIR *dir,struct dirent *entry, struct dirent **result) {
100 if(dir->dd_fd < 0) return -1;
102 if(read_fully(dir->dd_fd,&h,sizeof(h)) < 0) goto fail;
103 if(h.name_len < 0 || h.name_len >= sizeof(entry->d_name)-1) goto fail;
105 entry->d_ino = h.inode;
106 if(read_fully(dir->dd_fd,entry->d_name,h.name_len) < 0) goto fail;
108 entry->d_name[h.name_len] = '\0';
109 dir->dd_pos += h.name_len + 8;
111 if(result) *result = entry;
114 if(result) *result = NULL;
118 struct dirent *readdir(DIR *dir) { return readdir_r(dir,&dir->ent,NULL) == 0 ? &dir->ent : NULL; }
120 int closedir(DIR *dir) {