设为首页收藏本站

LUPA开源社区

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

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

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

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

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);

}


酷毙

雷人

鲜花

鸡蛋

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

最新评论

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

返回顶部