linux之getcwd函数解析
2013-08-06 16:07
204 查看
[lingyun@localhost getcwd]$ cat getcwd.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: getcwd.c
* Description: This file
*
* Version: 1.0.0(08/06/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "08/06/2013 03:56:30 PM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char *ptr;
int size = 50;
if(chdir("/usr/tmp/yum-lingyun-sYjnfB") < 0)
{
perror("chdir");
exit(1);
}
ptr = malloc(size);
if(getcwd(ptr, size) == NULL)
{
perror("getcwd");
exit(1);
}
printf("cwd = %s\n", ptr);
exit(0);
}
[lingyun@localhost getcwd]$ ll /usr/
total 224
dr-xr-xr-x. 2 root root 69632 Apr 29 03:32 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 39 root root 4096 Apr 15 15:44 include
dr-xr-xr-x. 29 root root 4096 Apr 22 03:18 lib
dr-xr-xr-x. 132 root root 86016 Apr 29 03:32 lib64
drwxr-xr-x. 30 root root 12288 Apr 29 03:32 libexec
drwxr-xr-x. 15 root root 4096 Jun 5 19:57 local
drwxr-xr-x 3 root root 4096 Apr 21 20:38 man
dr-xr-xr-x. 2 root root 12288 Jul 30 03:42 sbin
drwxr-xr-x. 265 root root 12288 Jun 9 09:34 share
drwxr-xr-x. 4 root root 4096 Jul 6 2012 src
lrwxrwxrwx. 1 root root 10 Jul 6 2012 tmp -> ../var/tmp
[lingyun@localhost getcwd]$ ls /usr/tmp/yum-lingyun-sYjnfB/
[lingyun@localhost getcwd]$ gcc getcwd.c
[lingyun@localhost getcwd]$ ./a.out
cwd = /var/tmp/yum-lingyun-sYjnfB
[lingyun@localhost getcwd]$
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: getcwd.c
* Description: This file
*
* Version: 1.0.0(08/06/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "08/06/2013 03:56:30 PM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char *ptr;
int size = 50;
if(chdir("/usr/tmp/yum-lingyun-sYjnfB") < 0)
{
perror("chdir");
exit(1);
}
ptr = malloc(size);
if(getcwd(ptr, size) == NULL)
{
perror("getcwd");
exit(1);
}
printf("cwd = %s\n", ptr);
exit(0);
}
[lingyun@localhost getcwd]$ ll /usr/
total 224
dr-xr-xr-x. 2 root root 69632 Apr 29 03:32 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 39 root root 4096 Apr 15 15:44 include
dr-xr-xr-x. 29 root root 4096 Apr 22 03:18 lib
dr-xr-xr-x. 132 root root 86016 Apr 29 03:32 lib64
drwxr-xr-x. 30 root root 12288 Apr 29 03:32 libexec
drwxr-xr-x. 15 root root 4096 Jun 5 19:57 local
drwxr-xr-x 3 root root 4096 Apr 21 20:38 man
dr-xr-xr-x. 2 root root 12288 Jul 30 03:42 sbin
drwxr-xr-x. 265 root root 12288 Jun 9 09:34 share
drwxr-xr-x. 4 root root 4096 Jul 6 2012 src
lrwxrwxrwx. 1 root root 10 Jul 6 2012 tmp -> ../var/tmp
[lingyun@localhost getcwd]$ ls /usr/tmp/yum-lingyun-sYjnfB/
[lingyun@localhost getcwd]$ gcc getcwd.c
[lingyun@localhost getcwd]$ ./a.out
cwd = /var/tmp/yum-lingyun-sYjnfB
[lingyun@localhost getcwd]$
相关文章推荐
- linux之getcwd函数解析
- module_init宏解析 linux驱动的入口函数module_init的加载和释放
- Linux 中的 fork() 函数实例解析
- Intel平台下linux中ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- linux之unlink函数解析
- Linux 多线程函数解析
- Linux多线程函数解析
- linux-011文件系统节点操作函数解析
- Linux 字符设备驱动开发基础(四)—— ioctl() 函数解析
- Linux系统调用之sysinfo函数解析
- 全面解析Linux 内核 3.10.x - 内核入口函数__kernel_entry
- 深入解析Linux中的fork函数
- linux之getopt_long函数解析
- 【Linux】dup和dup2函数解析与文件描述符
- linux-011文件系统超级块的操作函数(挂载卸载系统)解析
- Linux 多线程函数解析
- Linux进程(之)进程切换函数switch_to()解析
- linux-0.11中进程睡眠函数sleep_on()解析
- linux c strtol 函数解析
- 深入解析Linux中的fork函数