Cygwin: access: Fix X_OK behaviour for backup operators and admins
[newlib-cygwin.git] / newlib / libc / sys / sh / syscalls.c
blob3c41ecee9f1a3e2e9e08bb0a84bd9fc88df00f08
1 #include <_ansi.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/time.h>
5 #include "sys/syscall.h"
6 #include <errno.h>
7 #undef errno
8 extern int errno;
10 void abort (void) __attribute__((__noreturn__));
12 extern int __trap34 (int function, ...);
14 /* This is used by _sbrk. */
15 register char *stack_ptr asm ("r15");
17 extern int main (int, char**);
19 int
20 _read (int file,
21 char *ptr,
22 int len)
24 return __trap34 (SYS_read, file, ptr, len);
27 int
28 _lseek (int file,
29 int ptr,
30 int dir)
32 return __trap34 (SYS_lseek, file, ptr, dir);
35 int
36 _write ( int file,
37 char *ptr,
38 int len)
40 return __trap34 (SYS_write, file, ptr, len);
43 int
44 _close (int file)
46 return __trap34 (SYS_close, file, 0, 0);
49 int
50 _link (char *old, char *new)
52 return -1;
55 caddr_t
56 _sbrk (int incr)
58 extern char end; /* Defined by the linker */
59 static char *heap_end;
60 char *prev_heap_end;
62 if (heap_end == 0)
64 heap_end = &end;
66 prev_heap_end = heap_end;
67 if (heap_end + incr > stack_ptr)
69 _write (1, "Heap and stack collision\n", 25);
70 abort ();
72 heap_end += incr;
73 return (caddr_t) prev_heap_end;
76 int
77 _fstat (int file,
78 struct stat *st)
80 st->st_mode = S_IFCHR;
81 return 0;
84 int
85 _open (const char *path,
86 int flags)
88 return __trap34 (SYS_open, path, flags, 0);
91 int
92 _creat (const char *path,
93 int mode)
95 return __trap34 (SYS_creat, path, mode, 0);
98 int
99 _unlink ()
101 return -1;
105 isatty (int fd)
107 return 1;
111 _isatty (int fd)
113 return 1;
116 _ATTRIBUTE ((__noreturn__)) void
117 _exit (int n)
119 __trap34 (SYS_exit, n, 0, 0);
123 _kill (int pid,
124 int sig)
126 return __trap34 (SYS_exit, 0xdead, 0, 0);
130 _getpid ()
132 return 1;
135 void
136 _raise ()
141 _stat (const char *path, struct stat *st)
144 return __trap34 (SYS_stat, path, st, 0);
148 _chmod (const char *path, short mode)
150 return __trap34 (SYS_chmod, path, mode);
154 _chown (const char *path, short owner, short group)
156 return __trap34 (SYS_chown, path, owner, group);
160 _utime (const char *path,
161 char *times)
163 return __trap34 (SYS_utime, path, times);
167 _fork ()
169 return __trap34 (SYS_fork);
173 _wait (int *statusp)
175 return __trap34 (SYS_wait);
179 _execve (const char *path, char *const argv[], char *const envp[])
181 return __trap34 (SYS_execve, path, argv, envp);
185 _execv (const char *path, char *const argv[])
187 return __trap34 (SYS_execv, path, argv);
191 _pipe (int *fd)
193 return __trap34 (SYS_pipe, fd);
196 /* This is only provided because _gettimeofday_r and _times_r are
197 defined in the same module, so we avoid a link error. */
198 clock_t
199 _times (struct tms *tp)
201 return -1;
205 _gettimeofday (struct timeval *tv, void *tz)
207 tv->tv_usec = 0;
208 tv->tv_sec = __trap34 (SYS_time);
209 return 0;
212 static inline int
213 __setup_argv_for_main (int argc)
215 char **argv;
216 int i = argc;
218 argv = __builtin_alloca ((1 + argc) * sizeof (*argv));
220 argv[i] = NULL;
221 while (i--) {
222 argv[i] = __builtin_alloca (1 + __trap34 (SYS_argnlen, i));
223 __trap34 (SYS_argn, i, argv[i]);
226 return main (argc, argv);
230 __setup_argv_and_call_main ()
232 int argc = __trap34 (SYS_argc);
234 if (argc <= 0)
235 return main (argc, NULL);
236 else
237 return __setup_argv_for_main (argc);