Plan 9 from Bell Labs’s /usr/web/sources/contrib/mycroftiv/root/sys/src/cmd/iosrv/fdutils.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

/* makes pipe (3) files */
int
makepipe(char *pipename)
{
	int pipefd;
	char *pipe;
	ulong mode = 0777L;
	pipe="#|";
	if(verbosity == UP){
		print("-making pipe %s-", pipename);
	}
	if((pipefd = create(pipename, OREAD, DMDIR | mode)) < 0){
		sysfatal("couldnt create pipe!\n");
	}
	bind(pipe, pipename, MREPL);
	close(pipefd);
	return pipefd;
}

/* creates a pipe and opens name/data1 for reading, allowing user processes to claim name/data for writing */
int
mkrdfd(char *name)
{
	int fd;
	char fullname[SMBUF];
	sprint(fullname, "%s/data1", name);
	makepipe(name);
	if((fd = open(fullname, OREAD)) < 0){
		sysfatal("cant make pipe for reading\n");
	}
	if(verbosity == UP){
		print("-opened %s for reading on fd %d-", fullname, fd);
	}
	if((nohold != 'y') && (fdcounter < MAXHOLD)){
		sprint(fullname, "%s/data",name);
		if(verbosity == UP){
			print("holding %s-", fullname);
		}
		extrafds[fdcounter] = open(fullname, OREAD);
		if(extrafds[fdcounter] < 0){
			print("problems grabbing spare fd to hold \n");
		}
		fdcounter++;
	}
	return fd;
}

/* creates a pipe and opens name/data for writing, allowing user processes to claim name/data1 for reading */
int
mkwrfd(char *name)
{
	int fd;
	char fullname[SMBUF];
	sprint(fullname, "%s/data", name);
	makepipe(name);
	if ((fd = open(fullname, OWRITE)) < 0){
		sysfatal("cant make pipe for writing\n");
	}
	if(verbosity == UP){
		print("-opened %s for writing on fd %d-", fullname, fd);
	}
	if((nohold != 'y') && (fdcounter < MAXHOLD)){
		strcat(fullname, "1");
		if(verbosity == UP){
			print("holding %s-", fullname);
		}
		extrafds[fdcounter] = open(fullname, OREAD);
		if(extrafds[fdcounter] < 0){
			print("problems grabbing spare fd to hold \n");
		}
		fdcounter++;
	}
	return fd;
}

/* connects a new fd to a Hub for output from the Hub */
int
newout(Hub *h, char *name)
{
	if(h->outfdcount == MAXFDS - 1){
		print("TOO MANY FILE OUTPUT DESCRIPTORS!\n");
		return -1;
	}
	h->outfds[h->outfdcount] = mkwrfd(name);
	h->outfdcount++;
	return 0;
}

/* connects a new fd to a Hub for input to the Hub */
int
newin(Hub *h, char *name)
{
	if(h->infdcount == MAXFDS - 1){
		print("TOO MANY INPUT FILE DESCRIPTORS!\n");
		return -1;
	}
	h->infds[h->infdcount] = mkrdfd(name);
	h->infdcount++;
	return 0;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].