Linux getcwd()的实现【转】
转自:http://www.cnblogs.com/alan-forever/p/3721908.html
通过getcwd()可以获取当前工作目录。
1 #include <unistd.h>
2
3 char *getcwd(char *cwdbuf, size_t size);
成功调用返回指向cwdbuf的指针,失败返回NULL。
getcwd()的实现是《Linux/Unix系统编程手册》的练习18.5,题目如下:
实现一个功能与getcwd()相当的函数。提示:要获取当前工作目录的名称,可调用opendir()和readdir()来遍历其父目录(..)中的各个条目,查找其中与当前工作目录具有相同i-node编号及设备号的一项。如此这般,沿着目录树层层拾级而上(chdir(..))并进行扫描,就能构建出完整的目录路径。当前目录与当前工作目录相同时,就结束遍历。无论调用该函数成功与否,都应将调用者遣回其起始目录(使用open()和fchdir()能方便地实现这一功能)
1、通过stat获取文件信息,根据文件信息中的i-node编号和设备号来找到正确的目录
2、运用opendir()、readdir()来获取目录的信息,目录不能通过read()来获取信息。
PS:tlpi_hdr.h头文件为《Linux/Unix系统编程手册》的头文件,可以去作者的网站下载,其中的errExit()为错误处理函数。。。。

1 /*
2 * =====================================================================================
3 *
4 * Filename: 18.5.c
5 *
6 * Description:
7 *
8 * Version: 1.0
9 * Created: 2014年05月11日 14时04分35秒
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: alan (), alan19920626@gmail.com
14 * Organization:
15 *
16 * =====================================================================================
17 */
18
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <dirent.h>
22 #include <sys/types.h>
23 #include "tlpi_hdr.h"
24
25 #define BUF_MAX 4096
26
27 extern int errno;
28
29 char *Getcwd(char *cwdbuf, size_t size){
30 char path[BUF_MAX], cwd[BUF_MAX];
31 DIR *dirp;
32 struct dirent *dp;
33 struct stat sb, sb_d, sb_1;
34 dev_t dev;
35 ino_t ino;
36
37 while(1){
38 //获取当前目录的文件信息
39 if(stat(".", &sb) == -1)
40 errExit("stat");
41 dev = sb.st_dev;
42 ino = sb.st_ino;
43
44 //获取父目录的对应的目录流和父目录的文件信息
45 if((dirp = opendir("..")) == NULL)
46 errExit("opendir");
47 if(stat("..", &sb_1) == -1)
48 errExit("stat");
49
50 //判断当前目录是否与父目录相同
51 if(sb_1.st_dev == dev && sb_1.st_ino == ino)
52 break;
53
54 errno = 0;
55
56 //在父目录对应的目录流读取条目
57 while((dp = readdir(dirp)) != NULL){
58 snprintf(path, BUF_MAX, "../%s", dp->d_name);
59
60 if(stat(path, &sb_d) == -1)
61 errExit("stat");
62
63 //得到当前目录对应的条目并将目录逐渐完善
64 if(dev == sb_d.st_dev && ino == sb_d.st_ino){
65 memset(cwd, 0, sizeof(cwd));
66 if(strcat(cwd, "/") == NULL)
67 errExit("strcat");
68 if(strcat(cwd, dp->d_name) == NULL)
69 errExit("strcat");
70 if(strcat(cwd, cwdbuf) == NULL)
71 errExit("strcat");
72
73 if(strncpy(cwdbuf, cwd, BUF_MAX) == NULL)
74 errExit("strncpy");
75 break;
76 }
77
78 }
79
80 if(dp == NULL && errno != 0)
81 errExit("readdir");
82
83 closedir(dirp);
84 chdir(".."); //改变当前目录
85 }
86
87 return cwdbuf;
88 }
89
90 int main(int argc, char *argv[]){
91 char buf[BUF_MAX];
92 char t_buf[BUF_MAX];
93 char *p;
94 int fd;
95
96 if((fd = open(".", O_RDONLY)) == -1)
97 errExit("open");
98
99 if(argc != 1)
100 usageErr("%s", argv[0]);
101
102 p = Getcwd(buf, BUF_MAX);
103 if(p == NULL)
104 errExit("My getcwd");
105 printf("My getcwd: %s\n", p);
106 fchdir(fd); //遣回最初的目录
107
108 p = getcwd(t_buf, BUF_MAX);
109 if(p == NULL)
110 errExit("getcwd");
111 printf("getcwd: %s\n", p);
112
113 exit(EXIT_SUCCESS);
114 }

测试结果:
lancelot@debian:~/Code/tlpi$ pwd
/home/lancelot/Code/tlpi
lancelot@debian:~/Code/tlpi$ ./18.5
My getcwd: /home/lancelot/Code/tlpi
getcwd: /home/lancelot/Code/tlpi
Linux getcwd()的实现【转】的更多相关文章
- Linux getcwd()的实现
通过getcwd()可以获取当前工作目录. #include <unistd.h> char *getcwd(char *cwdbuf, size_t size); 成功调用返回指向cwd ...
- C/C++ Windows移植到Linux
近期写了有关Socket的程序,需要从windows移植到linux.现把有用的东东收集整理记录下来. 1.头文件windows下winsock.h或winsock2.h:linux下netinet/ ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- 20155325 加分作业 实现pwd
要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 准备 思路 问题 1.如何获取当前目录的节点号 Linux ...
- linux的getcwd和readlink的区别
针对linux下的程序,有两个路径: 1>运行程序的路径; 2>可执行文件所在的路径 例如: 如果我在/home/yongchao下执行 $ ./temp/test 那么 运行程序的 ...
- linux之getcwd函数解析
[lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...
- Linux gcc getcwd()的实现 zhuan
通过getcwd()可以获取当前工作目录. 1 #include <unistd.h> 2 3 char *getcwd(char *cwdbuf, size_t size);
- 软件素材---linux C语言:linux下获取可执行文件的绝对路径--getcwd函数
//头文件:#include <unistd.h> //定义函数:char * getcwd(char * buf, size_t size); //函数说明:getcwd()会将当前的工 ...
- linux服务器开发一 基础
注:本文仅限交流使用,请务用于商业用途,否则后果自负! Linux 1.Linux介绍 Linux是类Unix计算机操作系统的统称. Linux操作系统的内核的名字也是“Linux”. Linux这个 ...
随机推荐
- Leetcode 337. 打家劫舍 III
题目链接 https://leetcode.com/problems/house-robber-iii/description/ 题目描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可 ...
- python-2函数
http://docs.python.org/3/library/functions.html 或者菜鸟中文资料 1-使用函数 abs(-20)#求绝对值 max(1,4,200,3,2) #求最大的 ...
- python基础之生成器表达式形式、面向过程编程、内置函数部分
生成器表达式形式 直接上代码 1 # yield的表达式形式 2 def foo(): 3 print('starting') 4 while True: 5 x=yield #默认返回为空,实际上为 ...
- CSS3 Shape ---使用形状改变旁边内容的布局
注意 本文所实现的功能,在浏览器的支持上并不好,除chrome浏览器外其余的大部分浏览器均不支持,虽然可以使用polyfill解决,但也不能很好的支持,有时也会出错 polyfill使用方法 下载po ...
- Kafka实践、升级和新版本(0.10)特性预研
本文来自于网易云社区 一.消息总线MQ和Kafka (挡在请求的第一线) 1. 几个应用场景 case a:上游系统往下游系统推送消息,而不关心处理结果: case b:一份新数据生成,需要实时保存到 ...
- centos 6.4安装杀毒软件clamAV 0.98[转]
原文出处: http://dnuser.blog.51cto.com/4863891/1303829 1.查看系统版本 [root@local]# lsb_release -a LSB Versi ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目4
2014-04-23 18:17 题目:设计一个停车位的类. 解法:停车位,就要有停车.取车的功能了.另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用. 代码: // 8.4 Des ...
- Pascal “内存病毒”(骗人的)
Pascal内存病毒 Chaobs从互联网上获得 pascal病毒虽然说只用Ctrl+Pause Break就可以关闭,但是像有些程序一旦启动,不用等你找到那两个键,就自己运行结束关闭了.比如下面一个 ...
- python - web自动化测试 - 元素操作 - 定位
# -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: find_ele.py @ide: PyCharm Community ...
- 5.修改haproxy配置文件
需求: 1.查 输入:www.oldboy.org 获取当前backend下的所有记录 2.新建 输入: arg = { 'backend': 'www.oldboy.org', 'record':{ ...