getcwd(char *buf, size_t size)

 SYNOPSIS
       #include <unistd.h>

       char *getcwd(char *buf, size_t size);
       char *get_current_dir_name(void);
       char *getwd(char *buf);

DESCRIPTION
       The  getcwd() function copies an absolute pathname of the current work-
       ing directory to the array pointed to by buf, which is of length  size.

       If  the  current  absolute  pathname would require a buffer longer than
       size elements, NULL is returned, and errno is set to ERANGE; an  appli-
       cation  should  check  for  this error, and allocate a larger buffer if
       necessary.

       If buf is NULL, the behaviour of getcwd() is undefined.

       As an extension to the  POSIX.1-2001  standard,  Linux  (libc4,  libc5,
       glibc)  getcwd() allocates the buffer dynamically using malloc() if buf
       is NULL on call.  In this case, the allocated  buffer  has  the  length
       size  unless  size  is zero, when buf is allocated as big as necessary.
       It is possible (and, indeed, advisable) to free() the buffers  if  they
       have been obtained this way.

       get_current_dir_name(),  which  is  only  prototyped  if _GNU_SOURCE is
       defined, will malloc(3) an array big enough to hold the current  direc-
       tory  name.   If  the environment variable PWD is set, and its value is
       correct, then that value will be returned.

       getwd(),   which    is    only    prototyped    if    _BSD_SOURCE    or
       _XOPEN_SOURCE_EXTENDED  is  defined, will not malloc(3) any memory. The
       buf argument should be a pointer to an array at  least  PATH_MAX  bytes
       long.   getwd() does only return the first PATH_MAX bytes of the actual
       pathname.  Note that PATH_MAX need not be a compile-time  constant;  it
       may depend on the filesystem and may even be unlimited. For portability
       and security reasons, use of getwd() is deprecated.

RETURN VALUE
       NULL on failure with errno set accordingly, and  buf  on  success.  The
       contents of the array pointed to by buf is undefined on error.

ERRORS
       EACCES Permission  to  read  or  search a component of the filename was
              denied.

       EFAULT buf points to a bad address.

       EINVAL The size argument is zero and buf is not a null pointer.

       ENOENT The current working directory has been unlinked.

       ERANGE The size argument is less than the length of the working  direc-
       descriptors are available, especially on platforms other than Linux.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值