设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客

Linux认证学习辅导之unix环境高级编程

2012-2-1 15:54| 发布者: 红黑魂| 查看: 6466| 评论: 0|来自: 帮考网

摘要: 为了方便广大考生更好的复习,帮考网综合整理提供了Linux认证学习辅导之unix环境高级编程,以供各位考生考试复习参考,希望对考生复习有所帮助。unix环境高级编程每个进程都有一个当前的工作目录,此目录是搜索所有相 ...

为了方便广大考生更好的复习,帮考网综合整理提供了Linux认证学习辅导之unix环境高级编程,以供各位考生考试复习参考,希望对考生复习有所帮助。

unix环境高级编程

每个进程都有一个当前的工作目录,此目录是搜索所有相对路径的起点。

进程通过调用chdir或fcdir函数可以更改当前工作目录。

进程通过调用chdir或者fchdir函数可以更改当前工作目录。

view plain #include int chdir(const char* pathname);int fchdir(int filedes);

这两个函数分别用文件名和文件描述符来制定新的工作目录。

先查看GNU C手册。

int chdir (const char *filename) [Function] This function is used to set the process‘s working directory to filename. The normal, successful return value from chdir is 0. A value of -1 is returned to indicate an error. The errno error conditions defined for this function are the usual file name syntax errors (see Section 11.2.3 [File Name Errors], page 224), plus ENOTDIR if the file filename is not a directory. int fchdir (int filedes) [Function] This function is used to set the process’s working directory to directory associated with the file descriptor filedes. The normal, successful return value from fchdir is 0. A value of -1 is returned to indicate an error. The following errno error conditions are defined for this function:EACCES Read permission is denied for the directory named by dirname. EBADF The filedes argument is not a valid file descriptor. ENOTDIR The file descriptor filedes is not associated with a directory. EINTR The function call was interrupt by a signal.

实例;

view plain #include“apue.h”

int main(void)

{ if(chdir(“/devis/wangchenglin”)《0)

err_sys(“chdir failed”);printf(“chdir to /devis/wangchenglin succeeded\n”);exit(0);

}

运行结果:

看到并没有改变工作目录,这是因为shell创建了一个子进程,又该自己子进程执行这个命令。可知,为了改变shell进程自己的工作目录,shell应该直接调用chdir函数,为此cd命令的执行程序直接包含在shell程序中。

函数getcwd这样的功能,它从当前工作目录(。目录项开始),用……目录项找到其上一级的目录,然后赌气目录项,知道该目录项中的i节点编号与工作目录i节点编号相同,这就就找到了其对应文件名。按照这种方法,组成上一,直到遇到跟。

view plain #include

char *getcwd(char* buf,size_t size);

向此函数传递两个参数,一个是缓冲地址buf,领个是缓冲长度size.缓冲必须有足够长度以容纳绝对路径名再加上一个null终止符,否则返回出错。


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部