IO/作业/2024/8/7

#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

int main(int argc, const char *argv[])
{
    //打开读取的文件
    int fd1=open("04_test.c",O_RDONLY);
    if(fd1==-1)
    {
        perror("fd1 open error");
        return 1;
    }

    //打开要写入的文件
    int fd2=open("fd2.c",O_WRONLY | O_TRUNC | O_CREAT,0666);
    if(fd2==-1)
    {
        perror("fd2 open error");
        return 1;
    }
    //读取文件写入文件
    while(1)
    {                                                                    
        //定义存储的容器
        char buf[32]={0};
        int res=read(fd1,buf,32);
        char buf2[32]={0};
        int res2=read(fd1,buf2,32);
        if(res==0 && res2 ==0)
        {
            break;
        }
        //保留输出文件描述符
        int bkp=dup(STDOUT_FILENO);
        //输出到文件中
        dup2(fd2,STDOUT_FILENO);
        printf("%s",buf);
        fflush(stdout);
        //输出到终端
        dup2(bkp,STDOUT_FILENO);
        printf("%s",buf2);
    }
    //关闭文件
    close(fd1);
    close(fd2);
    return 0;
}

2.

#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

int main(int argc, const char *argv[])
{
    //定义stat指向的结构体
    struct stat buf={0};
    //判断其具有的权限
    stat(argv[1],&buf);
    mode_t mode=buf.st_mode;//获取结构体中,文件属性
    if((mode | S_IWGRP)==mode)
    {
        printf("%s文件拥有同组人可写权限\n",argv[1]);
    }
    else{printf("不拥有同组人可写权限\n");}


    if((mode | S_IWUSR)==mode)
    {
        printf("%s文件拥有用户可写权限\n",argv[1]);
    }
    else{printf("不拥有用户可写权限\n");}

                                                                      
    if((mode | S_IWOTH)==mode)
    {
        printf("%s文件拥有其他人可写权限\n",argv[1]);
    }
    else{printf("不拥有其他人可写权限\n");}

    if((mode | S_IWGRP | S_IWUSR | S_IWOTH)==mode)
    {
        mode_t new_mode=0444;
        chmod(argv[1],new_mode);
    }
    if((mode | S_IWGRP | S_IWURP | S_IWOTH)==0)
    {
        mode_t new_mode=0666;
        chmod(argv[1],new_mode);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值