getcwd(char *buf, size_t size)

來源:互聯網
上載者:User
 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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.