免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3696 | 回复: 0
打印 上一主题 下一主题

chdir、fchdir 和 getcwd 函数(APUE2 4.22节) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-20 23:55 |只看该作者 |倒序浏览
一、
   每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以斜杠开始的路径名为相对路径名)。当用户登录到 *NIX 系统时,其当前工作目录通常是口令文件(/etc/passwd)中该用户登录项的第 6 个字段--用户的起始目录(home directory)。当前工作目录是进程的一个属性,起始目录则是登录名的一个属性。
更改当前工作目录的函数。
#include unistd.h>
int chdir(const char *pathname);
int fchdir(int filedes);
                                Both return: 0 if OK, 1 on error
在这两个函数中,分别用pathname 或打开的文件描述符来指定新的当前工作目录。
二、
因为当前工作目录是进程的一个属性,所以它只影响调用 chdir 或 fchdir 的进程本身,而不影响其他进程。例如:在 shell 中执行程序 mychdir, 如果在 mychdir 中修改了当前工作路径,当 mychdir 执行完成后,该 shell 的自己的当前工作目录并没有任何改变。其原因是:shell 创建了一个子进程,由该子进程具体处理 mychdir 程序。由此可见,为了改变 shell 进程自己的工作目录,shell 应当直接调用 chdir 函数,为此 cd 命令的执行程序直接包含在 shell 程序中。
三、
获得“当前工作目录”完整的绝对路径名。
#include unistd.h>
char *getcwd(char *buf, size_t size);
                                 Returns: buf if OK, NULL on error
此函数将当前工作目录存放在缓冲区 buf 中,此缓冲区长度为 size 字节。 该缓冲区必须有足够的长度以容纳绝对路径名在加上一个 null 终止字符,否则返回出错。
四、示例代码:
    当一个应用程序需要在文件系统中返回到其工作的起点时,getcwd 函数是很有用的。在更换工作目录之前,可以调用 getcwd 函数先将其保存起来。在完成了处理后,就可将从getcwd 获得的路径名作为调用参数传送给 chdir,这样就返回到了文件系统中的起点。
注意:getcwd 的第一参数,其长度必须足以容纳下当前工作路径名 + 1个'\0' 字符。
    fchdir 函数提供了一种完成此任务的便捷方法。在更换到文件系统中的不同位置前,无需调用 getcwd 函数(这就避免了缓冲区大小不足的问题),而是使用 open 函数 打开当前工作目录,然后保存文件描述符。当希望回到原工作目录时,只要简单地将该文件描述符传递给 fchdir。
1) chdir 函数。
#include stdio.h>
#include stdlib.h>
int main(int argc, char *argv[])
{
        /* 在这里,为了简单,直接用了数组存放当前工作目录 */
        /* 可以使用 中的 char *path_alloc(unsigned int *sizep); 函数为存放路径名的缓冲区动态分配内存 */
        char save_cwd[1024];
        char buf[1024];
        /* 保存当前工作目录 */
        if (getcwd(save_cwd, sizeof(save_cwd)) == NULL)
                perror("getcwd error");
        printf("before chdir, cwd = %s\n", save_cwd);
        /* change cwd */
        if (chdir("/")  0)
                perror("chdir error");
        /* 下面调用 getcwd 函数,只是为了显示 当前工作目录 */
        if (getcwd(buf, sizeof(buf)) == NULL)
                perror("getcwd error");
        printf("after chdir, cwd = %s\n", buf);
        /* 返回到先前的工作目录,需要使用更换目录前保存的路径名 */
        if (chdir(save_cwd)  0)
                perror("chdir error");
        /* 下面调用 getcwd 函数,只是为了显示 当前工作目录 */
        if (getcwd(buf, sizeof(buf)) == NULL)
                perror("getcwd error");
        printf("previous chdir, cwd = %s\n", buf);
        exit(0);
}
2) fchdir 函数
#include stdio.h>
#include stdlib.h>
#include fcntl.h>
int main(int argc, char *argv[])
{
        char buf[1024];
        int fd;
        /* 保存当前工作目录 */
        if ((fd = open(".", O_RDONLY))  0)
                perror("open error");
        /* 下面调用 getcwd 函数,只是为了显示 当前工作目录 */
        /* show current work directory */
        if (getcwd(buf, sizeof(buf)) == NULL)
                perror("getcwd error");
        printf("before chdir, cwd = %s\n", buf);
        /* change cwd */
        if (chdir("/")  0)
                perror("chdir error");
        /* 下面调用 getcwd 函数,只是为了显示 当前工作目录 */
        if (getcwd(buf, sizeof(buf)) == NULL)
                perror("getcwd error");
        printf("after chdir, cwd = %s\n", buf);
        /* 返回到先前的工作目录,需要使用更换目录前保存的文件描述符 */
        if (fchdir(fd)  0)
                perror("fchdir error");
        /* 下面调用 getcwd 函数,只是为了显示 当前工作目录 */
        if (getcwd(buf, sizeof(buf)) == NULL)
                perror("getcwd error");
        printf("previous chdir, cwd = %s\n", buf);
        exit(0);
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/105349/showart_2099730.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP