add pipe(...)

This commit is contained in:
bebbo 2025-03-11 12:19:46 +01:00
parent f29b8c4ee8
commit 520edc6c25
3 changed files with 51 additions and 10 deletions

View File

@ -7,6 +7,7 @@
extern StdFileDes **__stdfiledes;
extern unsigned __stdfilesize;
extern unsigned __stdfilesize_max;
#ifdef __posix_threads__
unsigned __stdLock[2];
#endif
@ -67,7 +68,7 @@ static StdFileDes *__allocfd2(void) {
StdFileDes *fp, **sfd;
int file, max;
for (sfd = __stdfiledes, max = __stdfilesize, file = 0; file < max; sfd++, file++)
for (sfd = __stdfiledes, max = __stdfilesize_max, file = 0; file < max; sfd++, file++)
if (!sfd[0] || !sfd[0]->lx_inuse)
break;

View File

@ -27,7 +27,6 @@ struct filenode {
/* own stuff */
extern struct MinList ___filelist; /* List of all fopen'ed files */
extern struct MinList __memorylist; /* List of memory puddles */
extern struct ExecBase * SysBase;
@ -39,9 +38,6 @@ extern unsigned *__BUFSIZE;
extern int __fflush(FILE *);
extern void __seterrno(void);
extern FILE * ___stdin[4];
extern int __errno__data;
extern FILE * __sF__data[3];
extern int * __errno;
extern FILE **__sF;
/*
@ -49,6 +45,7 @@ extern FILE **__sF;
*/
StdFileDes **__stdfiledes;
unsigned __stdfilesize;
unsigned __stdfilesize_max;
#ifdef __posix_threads__
unsigned __stdLock[2];
#endif
@ -159,7 +156,7 @@ int __fclose(FILE * stream) {
return error;
}
static StdFileDes * stdfiledes(BPTR fh) {
static StdFileDes * __mk_stdfiledes(BPTR fh) {
StdFileDes *sfd = (StdFileDes *) malloc(sizeof(StdFileDes));
if (sfd) {
__stdfiledes[__stdfilesize] = sfd;
@ -169,7 +166,7 @@ static StdFileDes * stdfiledes(BPTR fh) {
_setup_file(sfd);
sfd->lx_flags |= LX_SYS;
}
KPrintF("stdfiledes [__stdfiledes=%ld, __stdfilecount=%ld]\n", __stdfiledes, __stdfilesize);
KPrintF("__mk_stdfiledes [__stdfiledes=%ld, __stdfilecount=%ld]\n", __stdfiledes, __stdfilesize);
return sfd;
}
@ -181,13 +178,14 @@ void __initstdio(void) {
#ifdef __KICK13__
__stdfilesize = 0;
#endif
__stdfilesize_max = 4;
if ((__stdfiledes = (StdFileDes **) malloc(4 * sizeof(StdFileDes *)))) {
unsigned __bufsiz = *__BUFSIZE;
*__BUFSIZE = 512;
if ((sfd = stdfiledes(Input()))) {
if ((sfd = __mk_stdfiledes(Input()))) {
sfd->lx_oflags = O_RDONLY;
*__BUFSIZE = __bufsiz;
if ((sfd = stdfiledes(Output()))) {
if ((sfd = __mk_stdfiledes(Output()))) {
BPTR bstderr;
struct Process * proc = (struct Process *) SysBase->ThisTask;
#ifdef __KICK13__
@ -200,7 +198,7 @@ void __initstdio(void) {
(!proc->pr_CLI || (bstderr = stderrdes = Open((CONST_STRPTR)"*", MODE_OLDFILE)) == 0))
bstderr = __stdfiledes[STDOUT_FILENO]->lx_fh;
if ((sfd = stdfiledes(bstderr))) {
if ((sfd = __mk_stdfiledes(bstderr))) {
__stdfiledes[3] = 0; // have a free one
{
short flags;

View File

@ -0,0 +1,42 @@
#include <proto/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
extern StdFileDes *__allocfd(void);
extern void _setup_file(StdFileDes *fp);
int pipe(int pipefd[2]) {
char tmp[32];
sprintf(tmp, "PIPE:libnix-%08lx-%08lx", time(0), rand());
BPTR bout = Open(tmp, MODE_NEWFILE);
BPTR bin = Open(tmp, MODE_OLDFILE);
if (bin && bout) {
Write(bin, tmp, 0); // no longer blocks!?
StdFileDes* in = __allocfd();
StdFileDes* out = __allocfd();
if (in && out) {
in->lx_oflags = O_RDONLY;
in->lx_fh = bin;
_setup_file(in);
out->lx_flags = O_APPEND;
out->lx_fh = bout;
_setup_file(out);
pipefd[0] = in->lx_pos;
pipefd[1] = out->lx_pos;
return 0;
}
if (in)
in->lx_inuse = 0;
if (out)
out->lx_inuse = 0;
}
if (bin) Close(bin);
if (bout) Close(bout);
return -1;
}