0708,LINUX目录相关操作

主要是冷气太足感冒了,加上少吃药抗药性差,全天昏迷,学傻了学傻了

cat t_chdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_chdir dir 
    if(argc!=2){
        error(1,errno,"Usage:%s dir",argv[0]);
    }
    //打印当前工作目录
    char buf[128];
    printf("%s\n",getcwd(buf,128));

    //改变当前工作目录
    if(chdir(argv[1])==-1){
        error(1,errno,"chdir %s ",argv[1]);
    }

    printf("%s\n",getcwd(buf,128));
    return 0;
}

cat t_getcwd.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>

int main(int argc,char *argv[])
{
    // ./t_getcwd
    //char buf[128];
    //char *cwd=getcwd(buf,128);

    char* cwd=getcwd(NULL,0);
    //如果传入的 buf为 NULL,且size为0
    //,则 getcwd 会调用 malloc 申请合适大小的内存空间,
    //填入当前工作目录的绝对路径
    //,然后返回alloc申请的空间的地址。
    if(!cwd){
        error(1,errno,"getcwd");
    }
    printf("%s\n",cwd);
    return 0;
}

cat t_mkdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_mkfir dir mode
    if(argc!=3){
        error(1,errno,"Usage:%s dir mode",argv[0]);
    }
    mode_t mode;
    sscanf(argv[2],"%o",&mode);
    
    if(mkdir(argv[1],mode)==-1){
        error(1,errno,"mkdir %s ",argv[1]);
    }

    return 0;
}

cat t_readdir.c

cpp 复制代码
#include <func.h>


int main(int argc,char *argv[])
{
    // ./t_readdir dir 
    if(argc!=2){
        error(1,0,"Usage:%s dir mode",argv[0]);
    }
    //打开目录流
    DIR * dirp=opendir(argv[1]);
    if(dirp==NULL){
        error(1,errno,"opendir :%s ",argv[1]);
    }

    //依次读取每一个目录流
    errno=0;
    struct dirent *p;
    while((p=readdir(dirp))!=NULL){
        //打印目录项
        printf("d_ino=%ld   ,d_off=%ld   ,d_reclen=%hu  ,d_type=%u   ,d_name=%s\n",
               p->d_ino,
               p->d_off,
               p->d_reclen,
               p->d_type,
               p->d_name );
    }//p==NULL
    if(errno){
        error(0,errno,"readdir");
    }
    //关闭目录流
    closedir(dirp);
    return 0;
}

cat t_rmdir.c

cpp 复制代码
#include <stdio.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    // ./t_rmfir dir
    if(argc!=2){
        error(1,errno,"Usage:%s dir mode",argv[0]);
    }


    
    if(rmdir(argv[1])==-1){
        error(1,errno,"rmdir %s ",argv[1]);
    }

    return 0;
}
相关推荐
花海如潮淹4 分钟前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
2301_780789664 小时前
UDP和TCP的主要区别是什么
服务器·网络协议·web安全·网络安全·udp
_丿丨丨_5 小时前
XSS(跨站脚本攻击)
前端·网络·xss
天天进步20155 小时前
前端安全指南:防御XSS与CSRF攻击
前端·安全·xss
Dcs5 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java
一个龙的传说7 小时前
linux 常用命令
linux·服务器·zookeeper
拾光拾趣录7 小时前
括号生成算法
前端·算法
fouryears_234178 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc