Move the big gnarly functions like system() and popen() into sys, and make them
build.
This commit is contained in:
parent
6a729b846a
commit
bb78fd158c
|
@ -46,13 +46,9 @@ for _, plat in ipairs(vars.plats) do
|
|||
"./sys/malloc/*.c",
|
||||
"./sys/exit/*.c",
|
||||
"./sys/stdio/*.c",
|
||||
"./assert/*.c",
|
||||
"./sys/misc/*.c",
|
||||
"./stdio/*.c",
|
||||
"./stdlib/*.c",
|
||||
"./string/*.c",
|
||||
"./time/*.c",
|
||||
"./misc/getpass.c",
|
||||
"./misc/isatty.c",
|
||||
},
|
||||
hdrs = {}, -- must be empty
|
||||
deps = {
|
||||
|
|
|
@ -29,6 +29,26 @@
|
|||
#define ACKCONF_WANT_EMULATED_RAISE 1
|
||||
#endif
|
||||
|
||||
#ifndef ACKCONF_WANT_EMULATED_REMOVE
|
||||
/* Implement remove() as unlink(). */
|
||||
#define ACKCONF_WANT_EMULATED_REMOVE 1
|
||||
#endif
|
||||
|
||||
#ifndef ACKCONF_WANT_EMULATED_SYSTEM
|
||||
/* Implement system() as fork()/execve()/wait(). */
|
||||
#define ACKCONF_WANT_EMULATED_SYSTEM 1
|
||||
#endif
|
||||
|
||||
#ifndef ACKCONF_WANT_EMULATED_SLEEP
|
||||
/* Implement sleep() with SIGALRM. */
|
||||
#define ACKCONF_WANT_EMULATED_SLEEP 1
|
||||
#endif
|
||||
|
||||
#ifndef ACKCONF_WANT_EMULATED_POPEN
|
||||
/* Implement popen() with fork()/dup2() etc. */
|
||||
#define ACKCONF_WANT_EMULATED_POPEN 1
|
||||
#endif
|
||||
|
||||
#ifndef ACKCONF_WANT_MALLOC
|
||||
/* Uses sbrk() to get memory from the system. */
|
||||
#define ACKCONF_WANT_MALLOC 1
|
||||
|
|
|
@ -43,8 +43,11 @@
|
|||
#define SIG_DFL ((sighandler_t) 0) /* Default action. */
|
||||
#define SIG_IGN ((sighandler_t) 1) /* Ignore signal. */
|
||||
|
||||
#define SIGINT 2 /* Terminal interrupt */
|
||||
#define SIGQUIT 3 /* Terminal quit */
|
||||
#define SIGABRT 6 /* Abort (ANSI) */
|
||||
#define SIGILL 11 /* Illegal instruction */
|
||||
#define SIGALRM 14 /* Alarm clock */
|
||||
|
||||
#define _NSIG 16 /* Biggest signal number + 1
|
||||
(not including real-time signals). */
|
||||
|
@ -79,6 +82,9 @@ extern char** environ;
|
|||
extern int brk(void* ptr);
|
||||
extern int close(int d);
|
||||
extern int creat(const char* path, mode_t mode);
|
||||
extern int dup(int oldfd);
|
||||
extern int dup2(int oldfd, int newfd);
|
||||
extern int execl(const char *path, const char* arg, ...);
|
||||
extern int execve(const char *path, char *const argv[], char *const envp[]);
|
||||
extern int fcntl(int fd, int op, ...);
|
||||
extern int gettimeofday(struct timeval* tv, struct timezone* tz);
|
||||
|
@ -86,6 +92,8 @@ extern int isatty(int d);
|
|||
extern int isatty(int d);
|
||||
extern int kill(pid_t old, int sig);
|
||||
extern int open(const char* path, int access, ...);
|
||||
extern int pause(void);
|
||||
extern int pipe(int pipefd[2]);
|
||||
extern int raise(int signum);
|
||||
extern int settimeofday(const struct timeval* tv, const struct timezone* tz);
|
||||
extern int sigaction(int, const struct sigaction *, struct sigaction *);
|
||||
|
@ -94,6 +102,7 @@ extern int unlink(const char* path);
|
|||
extern off_t lseek(int fildes, off_t offset, int whence);
|
||||
extern pid_t fork(void);
|
||||
extern pid_t getpid(void);
|
||||
extern unsigned int alarm(unsigned int seconds);
|
||||
extern pid_t wait(int* wstatus);
|
||||
extern sighandler_t signal(int signum, sighandler_t handler);
|
||||
extern ssize_t read(int fd, void* buffer, size_t count);
|
||||
|
@ -101,5 +110,4 @@ extern ssize_t write(int fd, void* buffer, size_t count);
|
|||
extern void _exit(int);
|
||||
extern void* sbrk(int increment);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,119 +0,0 @@
|
|||
/*
|
||||
* popen - open a pipe
|
||||
*/
|
||||
/* $Id$ */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#if defined(__BSD4_2)
|
||||
union wait {
|
||||
int w_status;
|
||||
};
|
||||
typedef union wait wait_arg;
|
||||
#else
|
||||
typedef int wait_arg;
|
||||
#endif /* __BSD4_2 */
|
||||
#include "../stdio/loc_incl.h"
|
||||
|
||||
int _close(int d);
|
||||
#if defined(__USG)
|
||||
static
|
||||
#endif
|
||||
int
|
||||
_dup2(int oldd, int newd); /* not present in System 5 */
|
||||
int _execl(const char* name, ...);
|
||||
int _fork(void);
|
||||
int _pipe(int fildes[2]);
|
||||
int _wait(wait_arg* status);
|
||||
void _exit(int status);
|
||||
|
||||
static int pids[FOPEN_MAX];
|
||||
|
||||
FILE* popen(const char* command, const char* type)
|
||||
{
|
||||
int piped[2];
|
||||
int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2;
|
||||
int pid;
|
||||
|
||||
if (Xtype == 2 || _pipe(piped) < 0 || (pid = _fork()) < 0)
|
||||
return 0;
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
/* child */
|
||||
register int* p;
|
||||
|
||||
for (p = pids; p < &pids[FOPEN_MAX]; p++)
|
||||
{
|
||||
if (*p)
|
||||
_close(p - pids);
|
||||
}
|
||||
_close(piped[Xtype]);
|
||||
_dup2(piped[!Xtype], !Xtype);
|
||||
_close(piped[!Xtype]);
|
||||
_execl("/bin/sh", "sh", "-c", command, (char*)0);
|
||||
_exit(127); /* like system() ??? */
|
||||
}
|
||||
|
||||
pids[piped[Xtype]] = pid;
|
||||
_close(piped[!Xtype]);
|
||||
return fdopen(piped[Xtype], type);
|
||||
}
|
||||
|
||||
#if defined(__BSD4_2)
|
||||
#define ret_val status.w_status
|
||||
#else
|
||||
#define ret_val status
|
||||
#endif
|
||||
|
||||
int pclose(FILE* stream)
|
||||
{
|
||||
int fd = fileno(stream);
|
||||
wait_arg status;
|
||||
int wret;
|
||||
void (*intsave)(int) = signal(SIGINT, SIG_IGN);
|
||||
void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN);
|
||||
|
||||
fclose(stream);
|
||||
while ((wret = _wait(&status)) != -1)
|
||||
{
|
||||
if (wret == pids[fd])
|
||||
break;
|
||||
}
|
||||
if (wret == -1)
|
||||
ret_val = -1;
|
||||
signal(SIGINT, intsave);
|
||||
signal(SIGQUIT, quitsave);
|
||||
pids[fd] = 0;
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
#if defined(__USG)
|
||||
int _dup(int fildes);
|
||||
|
||||
static int
|
||||
_dup2(int oldd, int newd)
|
||||
{
|
||||
int i = 0, fd, tmp;
|
||||
int fdbuf[FOPEN_MAX];
|
||||
|
||||
/* ignore the error on the close() */
|
||||
tmp = errno;
|
||||
(void)_close(newd);
|
||||
errno = tmp;
|
||||
while ((fd = _dup(oldd)) != newd)
|
||||
{
|
||||
if (fd == -1)
|
||||
break;
|
||||
fdbuf[i++] = fd;
|
||||
}
|
||||
tmp = errno;
|
||||
while (--i >= 0)
|
||||
{
|
||||
_close(fdbuf[i]);
|
||||
}
|
||||
errno = tmp;
|
||||
return -(fd == -1);
|
||||
}
|
||||
#endif /* __USG */
|
|
@ -1,25 +0,0 @@
|
|||
ext_fmt.h
|
||||
abort.c
|
||||
abs.c
|
||||
atof.c
|
||||
atoi.c
|
||||
atol.c
|
||||
bsearch.c
|
||||
div.c
|
||||
atexit.c
|
||||
exit.c
|
||||
getenv.c
|
||||
labs.c
|
||||
ldiv.c
|
||||
malloc.c
|
||||
mblen.c
|
||||
mbstowcs.c
|
||||
mbtowc.c
|
||||
qsort.c
|
||||
rand.c
|
||||
strtod.c
|
||||
strtol.c
|
||||
system.c
|
||||
wcstombs.c
|
||||
wctomb.c
|
||||
ext_comp.c
|
|
@ -1,14 +0,0 @@
|
|||
clean:
|
||||
rm -f abort.o abs.o atof.o atoi.o atol.o bsearch.o div.o \
|
||||
atexit.o exit.o getenv.o labs.o ldiv.o malloc.o mblen.o \
|
||||
mbstowcs.o mbtowc.o qsort.o rand.o strtod.o strtol.o \
|
||||
system.o wcstombs.o wctomb.o ext_comp.o malloc.c OLIST
|
||||
|
||||
malloc/malloc.c:
|
||||
-(cd malloc; make)
|
||||
|
||||
malloc.c: malloc/malloc.c
|
||||
-cp malloc/malloc.c malloc.c
|
||||
|
||||
distr: malloc.c
|
||||
|
68
lang/cem/libcc.ansi/sys/misc/popen.c
Normal file
68
lang/cem/libcc.ansi/sys/misc/popen.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* popen - open a pipe
|
||||
*/
|
||||
/* $Id$ */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if ACKCONF_WANT_EMULATED_POPEN
|
||||
|
||||
static int pids[FOPEN_MAX];
|
||||
|
||||
FILE* popen(const char* command, const char* type)
|
||||
{
|
||||
int piped[2];
|
||||
int Xtype = *type == 'r' ? 0 : *type == 'w' ? 1 : 2;
|
||||
int pid;
|
||||
|
||||
if (Xtype == 2 || pipe(piped) < 0 || (pid = fork()) < 0)
|
||||
return 0;
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
/* child */
|
||||
register int* p;
|
||||
|
||||
for (p = pids; p < &pids[FOPEN_MAX]; p++)
|
||||
{
|
||||
if (*p)
|
||||
close(p - pids);
|
||||
}
|
||||
close(piped[Xtype]);
|
||||
dup2(piped[!Xtype], !Xtype);
|
||||
close(piped[!Xtype]);
|
||||
execl("/bin/sh", "sh", "-c", command, (char*)0);
|
||||
_exit(127); /* like system() ??? */
|
||||
}
|
||||
|
||||
pids[piped[Xtype]] = pid;
|
||||
close(piped[!Xtype]);
|
||||
return fdopen(piped[Xtype], type);
|
||||
}
|
||||
|
||||
int pclose(FILE* stream)
|
||||
{
|
||||
int fd = fileno(stream);
|
||||
int status;
|
||||
int wret;
|
||||
void (*intsave)(int) = signal(SIGINT, SIG_IGN);
|
||||
void (*quitsave)(int) = signal(SIGQUIT, SIG_IGN);
|
||||
|
||||
fclose(stream);
|
||||
while ((wret = wait(&status)) != -1)
|
||||
{
|
||||
if (wret == pids[fd])
|
||||
break;
|
||||
}
|
||||
if (wret == -1)
|
||||
status = -1;
|
||||
signal(SIGINT, intsave);
|
||||
signal(SIGQUIT, quitsave);
|
||||
pids[fd] = 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -7,7 +7,11 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if ACKCONF_WANT_EMULATED_REMOVE
|
||||
|
||||
int remove(const char* filename)
|
||||
{
|
||||
return unlink(filename);
|
||||
}
|
||||
|
||||
#endif
|
|
@ -3,11 +3,12 @@
|
|||
*/
|
||||
/* $Id$ */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
int _alarm(int n);
|
||||
void _pause(void);
|
||||
#if ACKCONF_WANT_EMULATED_SLEEP
|
||||
|
||||
static jmp_buf setjmpbuf;
|
||||
|
||||
|
@ -28,10 +29,10 @@ void sleep(int n)
|
|||
if (setjmp(setjmpbuf))
|
||||
{
|
||||
signal(SIGALRM, oldsig);
|
||||
_alarm(oldalarm);
|
||||
alarm(oldalarm);
|
||||
return;
|
||||
}
|
||||
oldalarm = _alarm(5000); /* Who cares how long, as long
|
||||
oldalarm = alarm(5000); /* Who cares how long, as long
|
||||
* as it is long enough
|
||||
*/
|
||||
if (oldalarm > n)
|
||||
|
@ -42,10 +43,12 @@ void sleep(int n)
|
|||
oldalarm = 1;
|
||||
}
|
||||
oldsig = signal(SIGALRM, alfun);
|
||||
_alarm(n);
|
||||
alarm(n);
|
||||
for (;;)
|
||||
{
|
||||
/* allow for other handlers ... */
|
||||
_pause();
|
||||
pause();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -9,6 +9,8 @@
|
|||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if ACKCONF_WANT_EMULATED_SYSTEM
|
||||
|
||||
#define FAIL 127
|
||||
|
||||
static const char* exec_tab[] = {
|
||||
|
@ -56,3 +58,5 @@ int system(const char* str)
|
|||
}
|
||||
return exitstatus;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1,20 +0,0 @@
|
|||
/*
|
||||
* rename.c - rename a file
|
||||
*/
|
||||
/* $Id$ */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Disabled, dtrg: rename is a system call these days. */
|
||||
#if 0
|
||||
int _link(const char *name1, const char *name2);
|
||||
|
||||
int
|
||||
rename(const char *old, const char *new) {
|
||||
if (!_link(old, new))
|
||||
return remove(old);
|
||||
else return -1;
|
||||
}
|
||||
#endif
|
Loading…
Reference in a new issue