3 #include <sys/dirent.h>
13 int _syscall_set_errno(struct _reent *ptr, int err) {
18 extern int _stat_r(struct _reent *, const char *, struct stat *);
19 int _access_r(struct _reent *ptr, const char *pathname, int mode) {
21 if(_stat_r(ptr,pathname,&statbuf) < 0) return -1;
25 /* NestedVM doesn't, and probably never will, support this security related stuff */
26 uid_t getuid() { return 0; }
27 gid_t getgid() { return 0; }
28 uid_t geteuid() { return 0; }
29 gid_t getegid() { return 0; }
30 int getgroups(int gidsetlen, gid_t *gidset) {
31 if(gidsetlen) *gidset = 0;
34 mode_t umask(mode_t new) { return 0022; }
35 int _chmod_r(struct _reent *ptr, const char *f, mode_t mode) { return 0; }
36 int _fchmod_r(struct _reent *ptr, int fd, mode_t mode) { return 0; }
37 int _chown_r(struct _reent *ptr, const char *f, uid_t uid, gid_t gid) { return 0; }
38 int _fchown_r(struct _reent *ptr, int fd, uid_t uid, gid_t gid) { return 0; }
40 #define REENT_WRAPPER0R(f,rt) \
41 extern rt _##f##_r(struct _reent *ptr); \
42 rt f() { return _##f##_r(_REENT); }
43 #define REENT_WRAPPER0(f) REENT_WRAPPER0R(f,int)
45 #define REENT_WRAPPER1R(f,rt,t1) \
46 extern rt _##f##_r(struct _reent *ptr, t1 a); \
47 rt f(t1 a) { return _##f##_r(_REENT,a); }
48 #define REENT_WRAPPER1(f,t1) REENT_WRAPPER1R(f,int,t1)
50 #define REENT_WRAPPER2R(f,rt,t1,t2) \
51 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b); \
52 rt f(t1 a, t2 b) { return _##f##_r(_REENT,a,b); }
53 #define REENT_WRAPPER2(f,t1,t2) REENT_WRAPPER2R(f,int,t1,t2)
55 #define REENT_WRAPPER3R(f,rt,t1,t2,t3) \
56 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c); \
57 rt f(t1 a, t2 b, t3 c) { return _##f##_r(_REENT,a,b,c); }
58 #define REENT_WRAPPER3(f,t1,t2,t3) REENT_WRAPPER3R(f,int,t1,t2,t3)
60 #define REENT_WRAPPER4R(f,rt,t1,t2,t3,t4) \
61 extern rt _##f##_r(struct _reent *ptr, t1 a, t2 b, t3 c, t4 d); \
62 rt f(t1 a, t2 b, t3 c, t4 d) { return _##f##_r(_REENT,a,b,c,d); }
63 #define REENT_WRAPPER4(f,t1,t2,t3,t4) REENT_WRAPPER4R(f,int,t1,t2,t3,t4)
65 REENT_WRAPPER2(mkdir,const char *,mode_t)
66 REENT_WRAPPER2(access,const char *,int)
67 REENT_WRAPPER1(rmdir,const char *)
68 REENT_WRAPPER1R(sysconf,long,int)
69 REENT_WRAPPER1(chdir,const char*)
70 REENT_WRAPPER2(utime,const char *,const struct utimbuf *)
71 REENT_WRAPPER1(pipe,int *)
72 REENT_WRAPPER2(dup2,int,int)
73 REENT_WRAPPER3(waitpid,pid_t,int *,int)
74 REENT_WRAPPER2R(getcwd,char *,char *,size_t)
75 REENT_WRAPPER2R(_getcwd,char *,char *,size_t)
76 REENT_WRAPPER2(symlink,const char *,const char *)
77 REENT_WRAPPER3(readlink,const char *, char *,int)
78 REENT_WRAPPER3(chown,const char *,uid_t,gid_t)
79 REENT_WRAPPER3(fchown,int,uid_t,gid_t)
80 REENT_WRAPPER3(lchown,const char *,uid_t,gid_t)
81 REENT_WRAPPER2(chmod,const char *,mode_t)
82 REENT_WRAPPER2(fchmod,int,mode_t)
83 REENT_WRAPPER2(lstat,const char *,struct stat *)
84 REENT_WRAPPER4(getdents,int, char *, size_t,long *)
85 REENT_WRAPPER1(dup,int)
86 REENT_WRAPPER2R(pathconf,long,const char *,int)
88 REENT_WRAPPER1(chroot,const char *)
89 REENT_WRAPPER3(mknod,const char *,mode_t,dev_t)
90 REENT_WRAPPER2(ftruncate,int,off_t)
91 REENT_WRAPPER1(usleep,unsigned int)
92 REENT_WRAPPER2(mkfifo,const char *, mode_t)
94 extern int __execve_r(struct _reent *ptr, const char *path, char *const argv[], char *const envp[]);
95 int _execve(const char *path, char *const argv[], char *const envp[]) {
96 return __execve_r(_REENT,path,argv,envp);
99 char *_getcwd_r(struct _reent *ptr, char *buf, size_t size) {
101 buf = __getcwd_r(ptr,buf,size);
102 return (long)buf == -1 ? NULL : buf;
108 char *ret = __getcwd_r(ptr,buf,size);
109 if((long)ret != -1) return ret;
112 if(ptr->_errno != ERANGE) return NULL;
116 pid_t _wait_r(struct _reent *ptr, int *status) {
117 return _waitpid_r(ptr,-1,status,0);
120 long _pathconf_r(struct _reent *ptr,const char *path, int name) {
123 fprintf(stderr,"WARNING: pathconf: Unknown \"name\": %d\n",name);
124 ptr->_errno = EINVAL;
138 char *ttyname(int fd) {
139 return isatty(fd) ? "/dev/console" : NULL;
142 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) {
146 if(act->sa_flags || act->sa_mask != ~((sigset_t)0)) { errno = EINVAL; return -1; }
147 old = signal(sig,act->sa_handler);
149 old = signal(sig,SIG_DFL);
153 oact->sa_handler = old;
155 oact->sa_mask = ~((sigset_t)0);
160 int sigfillset(sigset_t *set) {
161 *set = ~((sigset_t)0);
165 int sigemptyset(sigset_t *set) {
170 DIR *opendir(const char *path) {
175 fd = open(path,O_RDONLY);
176 if(fd < 0) return NULL;
178 if(fstat(fd,&sb) < 0 || !S_ISDIR(sb.st_mode)) {
184 dir = malloc(sizeof(*dir));
191 dir->dd_buf = malloc(sizeof(struct dirent));
192 dir->dd_size = sizeof(struct dirent);
193 if(dir->dd_buf == NULL) {
203 struct dirent *readdir(DIR *dir) {
206 if(dir->dd_loc == 0 || dir->dd_loc == dir->dd_len) {
207 dir->dd_len = getdents(dir->dd_fd,dir->dd_buf,dir->dd_size,NULL);
209 if(dir->dd_len <= 0) { dir->dd_len = 0; return NULL; }
211 dp = (struct dirent*) (dir->dd_buf + dir->dd_loc);
212 if(dp->d_reclen == 0 || dp->d_reclen > dir->dd_len - dir->dd_loc) return NULL;
213 dir->dd_loc += dp->d_reclen;
217 int closedir(DIR *dir) {
225 * Other People's Code
228 /* FreeBSD's dirname/basename */
230 /* FIXME: Put these in a header */
233 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
234 * All rights reserved.
236 * Redistribution and use in source and binary forms, with or without
237 * modification, are permitted provided that the following conditions
239 * 1. Redistributions of source code must retain the above copyright
240 * notice, this list of conditions and the following disclaimer.
241 * 2. Redistributions in binary form must reproduce the above copyright
242 * notice, this list of conditions and the following disclaimer in the
243 * documentation and/or other materials provided with the distribution.
244 * 3. The name of the author may not be used to endorse or promote products
245 * derived from this software without specific prior written permission.
247 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
248 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
249 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
250 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
251 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
252 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
253 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
254 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
255 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
256 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263 static char bname[MAXPATHLEN];
264 register const char *endp;
266 /* Empty or NULL string gets treated as "." */
267 if (path == NULL || *path == '\0') {
268 (void)strcpy(bname, ".");
272 /* Strip trailing slashes */
273 endp = path + strlen(path) - 1;
274 while (endp > path && *endp == '/')
277 /* Find the start of the dir */
278 while (endp > path && *endp != '/')
281 /* Either the dir is "/" or there are no slashes */
283 (void)strcpy(bname, *endp == '/' ? "/" : ".");
288 } while (endp > path && *endp == '/');
291 if (endp - path + 2 > sizeof(bname)) {
292 errno = ENAMETOOLONG;
295 (void)strncpy(bname, path, endp - path + 1);
296 bname[endp - path + 1] = '\0';
304 static char bname[MAXPATHLEN];
305 register const char *endp, *startp;
307 /* Empty or NULL string gets treated as "." */
308 if (path == NULL || *path == '\0') {
309 (void)strcpy(bname, ".");
313 /* Strip trailing slashes */
314 endp = path + strlen(path) - 1;
315 while (endp > path && *endp == '/')
318 /* All slashes becomes "/" */
319 if (endp == path && *endp == '/') {
320 (void)strcpy(bname, "/");
324 /* Find the start of the base */
326 while (startp > path && *(startp - 1) != '/')
329 if (endp - startp + 2 > sizeof(bname)) {
330 errno = ENAMETOOLONG;
333 (void)strncpy(bname, startp, endp - startp + 1);
334 bname[endp - startp + 1] = '\0';