加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Advanced Programming in UNIX Environment Episode 19

发布时间:2020-12-15 09:09:34 所属栏目:安全 来源:网络整理
导读:进程调用chdir或fchdir函数可以更改当前工作目录 #include unistd.h int chdir( const char *pathname); int fchdir( int fd); 在这两个函数中,分别用pathname或打开文件描述符来指定新的当前工作目录。 #include "apue.h" int main(void){ if ( chdir ( "/

进程调用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 succeededn");
    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=%sn",ptr);
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读