2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/unistd/lib_chdir.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/stat.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
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: _trimdir
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static inline void _trimdir(char *path)
|
|
|
|
{
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Skip any trailing '/' characters (unless it is also the leading '/') */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
int len = strlen(path) - 1;
|
|
|
|
while (len > 0 && path[len] == '/')
|
|
|
|
{
|
2012-11-10 17:06:01 +01:00
|
|
|
path[len] = '\0';
|
|
|
|
len--;
|
2015-10-04 23:04:00 +02:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define _trimdir(p)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: chdir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The chdir() function causes the directory named by the pathname pointed
|
|
|
|
* to by the 'path' argument to become the current working directory; that
|
|
|
|
* is, the starting point for path searches for pathnames not beginning
|
|
|
|
* with '/'.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-11-10 17:06:01 +01:00
|
|
|
* path - A pointer to a directory to use as the new current working
|
|
|
|
* directory
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0(OK) on success; -1(ERROR) on failure with errno set appropriately:
|
|
|
|
*
|
|
|
|
* EACCES
|
|
|
|
* Search permission is denied for any component of the pathname.
|
|
|
|
* ELOOP
|
|
|
|
* A loop exists in symbolic links encountered during resolution of the
|
|
|
|
* 'path' argument OR more that SYMLOOP_MAX symbolic links in the
|
|
|
|
* resolution of the 'path' argument.
|
|
|
|
* ENAMETOOLONG
|
2021-03-02 14:33:27 +01:00
|
|
|
* The length of the path argument exceeds PATH_MAX or a pathname
|
|
|
|
* component is longer than NAME_MAX.
|
2012-11-10 17:06:01 +01:00
|
|
|
* ENOENT
|
|
|
|
* A component of 'path' does not name an existing directory or path is
|
|
|
|
* an empty string.
|
|
|
|
* ENOTDIR
|
|
|
|
* A component of the pathname is not a directory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int chdir(FAR const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
char *oldpwd;
|
|
|
|
char *alloc;
|
2022-02-11 09:15:01 +01:00
|
|
|
char *abspath;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2012-11-10 17:06:01 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify the input parameters */
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOENT;
|
2012-11-10 17:06:01 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that 'path' refers to a directory */
|
|
|
|
|
|
|
|
ret = stat(path, &buf);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOENT;
|
2012-11-10 17:06:01 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Something exists here... is it a directory? */
|
|
|
|
|
|
|
|
if (!S_ISDIR(buf.st_mode))
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOTDIR;
|
2012-11-10 17:06:01 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Yes, it is a directory.
|
|
|
|
* Remove any trailing '/' characters from the path
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
_trimdir(path);
|
|
|
|
|
|
|
|
/* Replace any preceding OLDPWD with the current PWD (this is to
|
|
|
|
* support 'cd -' in NSH)
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
oldpwd = getenv("PWD");
|
|
|
|
if (!oldpwd)
|
|
|
|
{
|
2021-08-01 09:27:08 +02:00
|
|
|
oldpwd = CONFIG_LIBC_HOMEDIR;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alloc = strdup(oldpwd); /* kludge needed because environment is realloc'ed */
|
|
|
|
setenv("OLDPWD", alloc, TRUE);
|
|
|
|
lib_free(alloc);
|
|
|
|
|
|
|
|
/* Set the cwd to the input 'path' */
|
|
|
|
|
2022-02-11 09:15:01 +01:00
|
|
|
abspath = realpath(path, NULL);
|
|
|
|
if (abspath == NULL)
|
|
|
|
{
|
|
|
|
errcode = ENOENT;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
setenv("PWD", abspath, TRUE);
|
|
|
|
lib_free(abspath);
|
2012-11-10 17:06:01 +01:00
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2012-11-10 17:06:01 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
2019-02-11 19:09:26 +01:00
|
|
|
#endif /* !CONFIG_DISABLE_ENVIRON */
|