黑马《linux基础编程》学习笔记(81到84)

八十一. lstat和stat函数的区别

 八十二. 文件属性相关的函数

 首先是access.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("a.out filename\n");
        exit(1);
    }

    int ret = access(argv[1], W_OK);
    if(ret == -1)
    {
        perror("access");
        exit(1);
    }
    printf("you can write this file.\n");
    return 0;
}

 运行一下

[root@VM_0_15_centos file_op]# gcc access.c
[root@VM_0_15_centos file_op]# ls
access.c  chown.c      mode_stat    rename.c     stat.c      type_stat
a.out     english.txt  mode_stat.c  size_stat    strtol.c    type_stat.c
chmod.c   ls-l.c       newstat      size_stat.c  truncate.c
[root@VM_0_15_centos file_op]# ./a.out english.txt
you can write this file.

 

 chmod.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
    if(argc < 3)
    {
        printf("a.out filename mode\n");
        exit(1);
    }
    int mode = strtol(argv[2], NULL, 8);
    int ret = chmod(argv[1], mode);
    if(ret == -1)
    {
        perror("chmod");
        exit(1);
    }

    ret = chown(argv[1], 1001, 1002);
    if(ret == -1)
    {
        perror("chown");
        exit(1);
    }
    return 0;
}

 chown.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("a.out filename!\n");
        exit(1);
    }

    // user->ftp  group->ftp
    int ret = chown(argv[1], 1002, 1001);
    if(ret == -1)
    {
        perror("chown");
        exit(1);
    }
    return 0;
}

 

 八十三. 目录操作相关的函数

 chdir的程序chdir.c

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("a.out dir\n");
        exit(1);
    }

    int ret = chdir(argv[1]);
    if(ret == -1)
    {
        perror("chdir");
        exit(1);
    }

    int fd = open("____chdir.txt", O_CREAT | O_RDWR, 0777);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }
    close(fd);

    char buf[128];
    getcwd(buf, sizeof(buf));
    printf("current dir: %s\n", buf);

    return 0;
}

运行一下,果然在/home下找到了

[root@VM_0_15_centos dir_op]# vim chdir.c
[root@VM_0_15_centos dir_op]# ls
chdir.c  mkdir.c  opendir.c  readfileNum.c
[root@VM_0_15_centos dir_op]# gcc chdir.c
[root@VM_0_15_centos dir_op]# ls
a.out  chdir.c  mkdir.c  opendir.c  readfileNum.c
[root@VM_0_15_centos dir_op]# ./a.out /home
current dir: /home
[root@VM_0_15_centos dir_op]# cd /home
[root@VM_0_15_centos home]# ls
]              c.txt       mycal                      TestDjango
a1.txt         date.txt    myhome.tar.gz              test_rar.rar
a2.txt         experiment  orange.txt                 test_tar.tar.gz
apple.txt      file.c      poem                       tom
a.tar.gz       hello       Pointer                    virtualenvs
a.txt          hello.txt   rar                        zf
____chdir.txt  hello.zip   rarlinux-x64-5.4.0.tar.gz  zsf
cjz            LinkToRoot  test

 

 

 

 八十四. 目录遍历函数介绍

 

 

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85146176