码迷,mamicode.com
首页 > 其他好文 > 详细

1111

时间:2018-10-13 16:47:17      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:bre   lse   ice   rdo   null   UNC   close   exe   its   

#include<stdio.h> //标准输入输出函数
#include<stdlib.h> //C、C++语言的最常用的系统函数
#include<sys/types.h>
#include<sys/stat.h>
#include<syslog.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define MAX 10
int chmd()
{
    int c;
    printf("Please input your choice of change the mode of the file :");
    scanf("%d", &c);
    switch(c)
    {
        case 0:chmod("file1", S_IRWXU); break;
            // Mode flag: Read, write, execute by user.
            //依据自己的理解定义其他case中文件权限的情况。
        case 1:
            chmod("file1", S_IRUSR); break;
        case 2:
            chmod("file1", S_IWUSR); break;
        case 3:
            chmod("file1", S_IXUSR); break;
            
        default:printf("You have a wrong choice! \n");
    }
    return 0;
}

int main()
{
    int fd;
    int num;
    int choice;
    char buffer[MAX];
    struct stat st;
    char* path="/bin/ls";
    char* argv[4]={"ls","-l","file1",NULL};
    while(1)
    {
        printf("*************************\n");
        printf("0. 退出\n");
        printf("1. 创建新文件\n");
        printf("2. 写文件\n");
        printf("3. 读文件\n");
        printf("4. 修改文件权限\n");
        printf("5. 查看当前文件的权限并退出\n");
        printf("*************************\n");
        printf("Please input your choice(0-6):");
        scanf("%d", &choice);
        
        switch(choice)
        {
            case 0:close(fd);
                exit(0);
            case 1:
                fd=open("file1",O_RDWR|O_TRUNC|O_CREAT,0750);
                num = 0;
                /*
                 O_RDWR : file open mode: Read/Write
                 O_TRUNC : file open mode: Truncate file to length 0
                 O_CREAT : file open mode: Create if file does not yet exist.
                 0750: file access permission bits -rwxr-x---当前用户rwx;同组用户r-x; 其他用户无权限
                 */
                
                if(fd==-1)
                    printf("File Create Failed!\n");
                else {
                    printf("fd=%d\n",fd);
            close(fd);
        }
                break;

            case 2:
        fd = open("file1", O_WRONLY|O_APPEND);
                scanf("%s", buffer);
        int len = (int)strlen(buffer);
        if(len + num < MAX)
                    num += write(fd, buffer, len);
        else printf("Write failed!\n");
        close(fd);
                break;
                //补充代码:从键盘里面读取信息,写到file1里面
            case 3:
        fd = open("file1", O_RDONLY);
        memset(buffer, ‘\0‘, sizeof buffer);
        if(read(fd, buffer, MAX) >= 0)
                    printf("%s\n", buffer);
        else printf("Read failed!\n");
        close(fd);
                break;
                //补充代码:把file1文件的内容在屏幕上输出
            case 4:
                chmd();
                printf("Change mode success!\n");
                break;
            case 5:
                execv(path, argv); //-ls -l 显示文件完整的信息,包括权限,拥有者,拥有组,文件名,大小等
                break;
            default:
                printf("You have a wrong choice!\n");
        }
    }
}

1111

标签:bre   lse   ice   rdo   null   UNC   close   exe   its   

原文地址:https://www.cnblogs.com/HazelNut/p/9783212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!