为生存而奔跑

   :: 首页 :: 联系 :: 聚合  :: 管理
  271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

留言簿(5)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 319507
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜

转自http://blogold.chinaunix.net/u3/105349/showart_2099730.html

一、
   每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以斜杠开始的路径名为相对路径名)。当用户登录到 *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);
 }

posted on 2011-03-09 11:46 baby-fly 阅读(1096) 评论(0)  编辑 收藏 引用 所属分类: Ubuntu&Linux

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理