unix环境高级编程

来源:Linux认证    发布时间:2012-11-10    Linux认证视频    评论

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

  进程通过调用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终止符,否则返回出错。

  char * getcwd (char *buffer, size t size) [Function] The getcwd function returns an absolute file name representing the current working directory, storing it in the character array buffer that you provide. The size argument is how you tell the system the allocation size of buffer. The GNU library version of this function also permits you to specify a null pointer for the buffer argument. Then getcwd allocates a buffer automatically, as with malloc(see Section 3.2.2 [Unconstrained Allocation], page 33)。 If the size is greater than zero, then the buffer is that large; otherwise, the buffer is as large as necessary to hold the result. The return value is buffer on success and a null pointer on failure. The following errno error conditions are defined for this function:EINVAL The size argument is zero and buffer is not a null pointer. ERANGE The size argument is less than the length of the working directory name. You need to allocate a bigger array and try again. EACCES Permission to read or search a component of the file name was denied. You could implement the behavior of GNU‘s getcwd (NULL, 0) using only the standard behavior of getcwd:

  view plain char * gnu_getcwd ()

  { size_t size = 100;

  while (1)

  { char *buffer = (char *) xmalloc (size);if (getcwd (buffer, size) == buffer)

  return buffer;free (buffer);if (errno != ERANGE)

  return 0;size *= 2;}

  实例

  view plain #include "apue.h" char*path_alloc(int* size) { char *p = NULL; if(!size) return NULL; p = malloc(256); if(p) *size = 256; else *size = 0; return p; } int main(void)

  { char * ptr;int size;

  if(chdir("/devis/wangchenglin")<0)

  err_sys("chdir  failed");

  ptr=path_alloc(&size);if(getcwd(ptr,size)==NULL)

  err_sys("getcwd failed");

  printf("cwd=%s/n",ptr);exit(0);

  }

  考试大温馨提示:本内容来源于网络,仅代表作者个人观点,与本站立场无关,仅供您学习交流使用。其中可能有部分文章经过多次转载而造成文章内容缺失、错误或文章作者不详等问题,请您谅解。如有侵犯您的权利,请联系我们,本站会立即予以处理。

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答