2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/unistd/lib_getcwd.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2019-02-11 19:09:26 +01:00
|
|
|
#ifndef CONFIG_DISABLE_ENVIRON
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getwcd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* getcwd() function places the absolute pathname of the current working
|
|
|
|
* directory in the array pointed to by 'buf', and returns 'buf.' The
|
|
|
|
* pathname copied to the array shall contain no components that are
|
|
|
|
* symbolic links. The 'size' argument is the size in bytes of the
|
|
|
|
* character array pointed to by the 'buf' argument.
|
|
|
|
*
|
2021-01-25 05:40:27 +01:00
|
|
|
* As an extension to the POSIX.1-2001 standard, getcwd() allocates
|
|
|
|
* the buffer dynamically using lib_malloc if buf is NULL. In this case,
|
|
|
|
* the allocated buffer has the length size unless size is zero, when buf
|
|
|
|
* is allocated as big as necessary. The caller should free the
|
|
|
|
* returned buffer.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-11-10 17:06:01 +01:00
|
|
|
* buf - a pointer to the location in which the current working directory
|
2019-09-19 14:14:53 +02:00
|
|
|
* pathname is returned.
|
|
|
|
* size - The size in bytes available at 'buf'
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Upon successful completion, getcwd() returns the 'buf' argument.
|
|
|
|
* Otherwise, getcwd() returns a null pointer and sets errno to indicate
|
|
|
|
* the error:
|
|
|
|
*
|
|
|
|
* EINVAL
|
2021-01-25 05:40:27 +01:00
|
|
|
* The 'size' argument is 0 and the 'buf' argument is not NULL.
|
2012-11-10 17:06:01 +01:00
|
|
|
* ERANGE
|
|
|
|
* The size argument is greater than 0, but is smaller than the length
|
2019-09-19 14:14:53 +02:00
|
|
|
* of the current working directory pathname +1.
|
2012-11-10 17:06:01 +01:00
|
|
|
* EACCES
|
|
|
|
* Read or search permission was denied for a component of the pathname.
|
|
|
|
* ENOMEM
|
2019-09-19 14:14:53 +02:00
|
|
|
* Insufficient storage space is available.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR char *getcwd(FAR char *buf, size_t size)
|
|
|
|
{
|
|
|
|
char *pwd;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
2021-01-25 05:40:27 +01:00
|
|
|
if (buf && size == 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
set_errno(EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-01-25 05:40:27 +01:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
size = PATH_MAX + 1;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* If no working directory is defined, then default to the home directory */
|
|
|
|
|
|
|
|
pwd = getenv("PWD");
|
2021-01-25 05:40:27 +01:00
|
|
|
if (pwd == NULL)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2021-08-01 09:27:08 +02:00
|
|
|
pwd = CONFIG_LIBC_HOMEDIR;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the cwd will fit into the user-provided buffer */
|
|
|
|
|
|
|
|
if (strlen(pwd) + 1 > size)
|
|
|
|
{
|
|
|
|
set_errno(ERANGE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-25 05:40:27 +01:00
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
buf = lib_malloc(size);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Copy the cwd to the user buffer */
|
|
|
|
|
|
|
|
strcpy(buf, pwd);
|
|
|
|
return buf;
|
|
|
|
}
|
2019-02-11 19:09:26 +01:00
|
|
|
#endif /* !CONFIG_DISABLE_ENVIRON */
|