Unix Network Programming Episode 98

#include "unp.h"

int my_open(const char *, int);

int main(int argc, char **argv)
{
    int fd, n;
    char buff[BUFFSIZE];

    if(argc!=2)
        err_quit("usage: mycat <pathname>");
    
    if((fd=my_open(argv[1],O_RDONLY))<0)
        err_sys("cannot open %s", argv[1]);
    
    while((n=Read(fd, buff, BUFFSIZE))>0)
        Write(STDOUT_FILENO, buff, n);
    
    return 0;
}

mycat program: copies a file to standard output

#include "unp.h"

int my_open(const char *, int);

int my_open(const char *pathname, int mode)
{
    int fd, sockfd[2], status;
    pid_t childpid;
    char c, argsockfd[10], argmode[10];

    Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);

    if((childpid=Fork())==0)
    {
        Close(sockfd[0]);
        snprintf(argsockfd, sizeof(argsockfd),"%d", sockfd[1]);
        snprintf(argmode, sizeof(argmode), "%d", mode);
        execl("./openfile", "openfile", argsockfd, pathname, argmode, (char *)NULL);
        err_sys("execl error");
    }

    Close(sockfd[1]);

    Waitpid(childpid, &status, 0);
    if(WIFEXITED(status)==0)
        err_quit("child did not terminate");
    if((status=WEXITSTATUS(status))==0)
        Read_fd(sockfd[0], &c, 1, &fd);
    else
    {
        errno=status;
        fd=-1;
    }

    Close(sockfd[0]);
    
    return fd;
}

my_open function: opens a file and returns a descriptor

在这里插入代码片

read_fd function: receives data and a descriptor

相关推荐
大鹅同志10 分钟前
在服务器上开Juypter Lab教程(远程访问)
运维·服务器·pytorch·jupyter·cuda·云服务器
驯龙高手_追风36 分钟前
Ubuntu下安装最新版本Apache2文件服务器
linux·服务器·ubuntu
炸裂狸花猫1 小时前
Linux技术04-IPVS
linux·运维·服务器
快快小毛毛1 小时前
CC攻击防御策略要怎么调整?使用游戏盾有效解决
运维·服务器·网络·tcp/ip·游戏·udp
繁依Fanyi1 小时前
828 华为云征文|华为 Flexus 云服务器搭建 PicGo 图床
服务器·华为·华为云
两仪式quq2 小时前
服务网关Gateway快速入门
服务器·网络·gateway
亦舒.2 小时前
魔方财务迁移指南
服务器
终末圆2 小时前
MyBatis 增删改查【后端 17】
java·服务器·数据库·b树·mysql·spring·mybatis
~kiss~3 小时前
QUIC的丢包处理
服务器·网络
limengshi1383923 小时前
通信工程学习:什么是AN-SMF接入网系统管理功能
服务器·网络·网络协议·学习·信息与通信