Advanced Programming in UNIX Environment Episode 19

进程调用chdir或fchdir函数可以更改当前工作目录

#include <unistd.h>

int chdir(const char *pathname);
int fchdir(int fd);

在这两个函数中,分别用pathname或打开文件描述符来指定新的当前工作目录。

#include "apue.h"

int main(void)
{
    if(chdir("/tmp")<0)
        err_sys("chdir failed");
    printf("chdir to /tmp succeeded\n");
    return 0;
}

得到当前工作目录完整的绝对路径

#include <unistd.h>

char *getcwd(char *buf,size_t size);

必须向此函数传递两个参数,一个是缓冲区地址buf,另一个是缓冲区的长度size(以字节为单位)。该缓冲区必须有足够的长度以容纳绝对路径名再加上一个终止null字节。

#include "apue.h"

int main(void)
{
    char *ptr;
    size_t size;
    if(chdir("/usr/spool/uucppublic")<0)
    {
        err_sys("chdir failed");
    }
    ptr=path_alloc(&size);
    if(getcwd(ptr,size)==NULL)
        err_sys("getcwd failed");
    printf("cwd=%s\n",ptr);
    return 0;
}

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...