C语言实现pwd,ls命令

实现pwd

实现pwd打印当前目录

所需函数 getcwd

函数原型:char *getcwd( char *buffer, int maxlen );

  • 功 能:获取当前工作目录
  • 参数说明:getcwd()会将当前工作目录的绝对路径复制到参数buffer所指的内存空间中,参数maxlen为buffer的空间大小。
  • 返 回 值:成功则返回当前工作目录,失败返回 FALSE。
  • 在某些 Unix 的变种下,如果任何父目录没有设定可读或搜索模式,即使当前目录设定了,getcwd()还是会返回 FALSE。有关模式与权限的更多信息见 chmod()。
  • 头文件:unistd.h(windows下为direct.h)

    实现

    1
    2
    3
    4
    5
    6
    #include <unistd.h>
    #include <stdio.h>
    int main(void){
    printf("%s\n",getcwd(NULL,0)); //输出获取路径
    return 0;
    }

实现ls