📄 example23.c
字号:
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#define SERV_FIFO "/tmp/fifo.serv"
#define LINE 1024
int
main()
{
int readf, writef, dummyfd, fd;
char *ptr, buff[LINE], fifoname[LINE];
pid_t pid;
ssize_t n;
/* create fifo use wellknow pathname */
if ((mkfifo(SERV_FIFO, 666 < 0) && (errno != EEXIST)))
printf("can't create fifo");
readf = open(SERV_FIFO, O_RDONLY, 0); /* open the fifo with read only mode */
dummyfd = open(SERV_FIFO, O_WRONLY, 0); /* open the fifo with write only mode */
while ((n = getline(buff, 100, readf)) > 0) {
if (buff[n-1] == '\n')
n--; /* delete the return character */
buff[n] = '\0'; /* end the pathname with null character */
if ((ptr = strchr(buff, ' ')) == NULL) {
printf("bogus request: %s", buff);
continue;
}
*ptr++ = 0;
pid = atoi(buff);
/* create the pathname of the client's fifo */
snprintf(fifoname, sizeof(fifoname), "/tmp/fifo.%ld", (long)pid);
if ((writef = open(fifoname, O_WRONLY, 0)) < 0) {
printf("cannot open: %s", fifoname);
continue;
}
if ((fd = open(ptr, O_WRONLY)) < 0) {
snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",strerror(errno));
n = strlen(ptr);
write(writef, ptr, n); /* write the file to the client's fifo */
close(writef);
}
else {
while ((n = read(fd, buff, LINE)) > 0)
write(writef, buff, n);
close(fd);
close(writef);
}
}
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -