summaryrefslogtreecommitdiff
path: root/nuttx/lib
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-23 15:16:10 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2008-08-23 15:16:10 +0000
commit872cf9f99f8b6e2a64781c0c6bc8aee6e35f5cb7 (patch)
tree27c66a47011dea19170420a26a954591f02e8c53 /nuttx/lib
parentb588aaaa96a83162f43a4ea409be382a01aa3812 (diff)
downloadpx4-nuttx-872cf9f99f8b6e2a64781c0c6bc8aee6e35f5cb7.tar.gz
px4-nuttx-872cf9f99f8b6e2a64781c0c6bc8aee6e35f5cb7.tar.bz2
px4-nuttx-872cf9f99f8b6e2a64781c0c6bc8aee6e35f5cb7.zip
Added ch and pwd to NSH
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@841 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib')
-rw-r--r--nuttx/lib/Makefile7
-rw-r--r--nuttx/lib/lib_chdir.c88
-rw-r--r--nuttx/lib/lib_cwdsem.c86
-rw-r--r--nuttx/lib/lib_getcwd.c79
-rw-r--r--nuttx/lib/lib_internal.h10
5 files changed, 110 insertions, 160 deletions
diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile
index cf35a0541..a56cde66f 100644
--- a/nuttx/lib/Makefile
+++ b/nuttx/lib/Makefile
@@ -57,7 +57,7 @@ STDIO_SRCS = lib_printf.c lib_rawprintf.c lib_lowprintf.c lib_dbg.c \
lib_lowstream.c lib_nullstream.c lib_sscanf.c
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
-STDIO_SRCS += lib_rawstream.c lib_chdir.c lib_getcwd.c lib_cwdsem.c
+STDIO_SRCS += lib_rawstream.c
ifneq ($(CONFIG_NFILE_STREAMS),0)
STDIO_SRCS += lib_fopen.c lib_fclose.c lib_fread.c lib_libfread.c lib_fseek.c \
lib_fgetc.c lib_fgets.c lib_gets.c lib_fwrite.c lib_libfwrite.c \
@@ -72,6 +72,11 @@ STDLIB_SRCS = lib_rand.c
MATH_SRCS = lib_rint.c
UNISTD_SRCS = lib_getopt.c
+ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
+ifneq ($(CONFIG_DISABLE_ENVIRON),y)
+UNISTD_SRCS += lib_chdir.c lib_getcwd.c
+endif
+endif
NET_SRCS = lib_htons.c lib_htonl.c lib_inetntoa.c lib_etherntoa.c
diff --git a/nuttx/lib/lib_chdir.c b/nuttx/lib/lib_chdir.c
index 008e923ec..0db29829d 100644
--- a/nuttx/lib/lib_chdir.c
+++ b/nuttx/lib/lib_chdir.c
@@ -40,6 +40,8 @@
#include <nuttx/config.h>
#include <sys/types.h>
+#include <sys/stat.h>
+
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -47,13 +49,35 @@
#include "lib_internal.h"
+#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
+
/****************************************************************************
* Public Variables
****************************************************************************/
-#if CONFIG_NFILE_DESCRIPTORS > 0
-char *g_cwd = NULL;
-char *g_prevcwd = NULL;
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: _trimdir
+ ****************************************************************************/
+
+#if 0
+static inline void _trimdir(char *path)
+{
+ /* Skip any trailing '/' characters (unless it is also the leading '/') */
+
+ int len = strlen(path) - 1;
+ while (len > 0 && path[len] == '/')
+ {
+ path[len] = '\0';
+ len--;
+ }
+}
+#else
+# define _trimdir(p)
+#endif
/****************************************************************************
* Public Functions
@@ -94,38 +118,64 @@ char *g_prevcwd = NULL;
int chdir(FAR const char *path)
{
- char *duppath;
+ struct stat buf;
+ char *oldpwd;
+ char *alloc;
+ int err;
+ int ret;
/* Verify the input parameters */
if (!path)
{
- errno = ENOENT;
- return ERROR;
+ err = ENOENT;
+ goto errout;
}
/* Verify that 'path' refers to a directory */
- /* (To be provided) */
- /* Make a persistent copy of 'path' */
+ ret = stat(path, &buf);
+ if (ret != 0)
+ {
+ err = ENOENT;
+ goto errout;
+ }
+
+ /* Something exists here... is it a directory? */
- duppath = strdup(path);
+ if (!S_ISDIR(buf.st_mode))
+ {
+ err = ENOTDIR;
+ goto errout;
+ }
- /* Free any preceding cwd and set the previous to the cwd (this
- * is to support 'cd -' in NSH
+ /* Yes, it is a directory. Remove any trailing '/' characters from the path */
+
+ _trimdir(path);
+
+ /* Replace any preceding OLDPWD with the current PWD (this is to
+ * support 'cd -' in NSH)
*/
- cwd_semtake();
- if (g_prevcwd)
+ sched_lock();
+ oldpwd = getenv("PWD");
+ if (!oldpwd)
{
- free(g_prevcwd);
+ oldpwd = CONFIG_LIB_HOMEDIR;
}
- g_prevcwd = g_cwd;
- /* Set the cwd to a persistent copy of the input 'path' */
+ alloc = strdup(oldpwd); /* kludge needed because environment is realloc'ed */
+ setenv("OLDPWD", alloc, TRUE);
+ free(alloc);
- g_cwd = duppath;
- cwd_semgive();
+ /* Set the cwd to the input 'path' */
+
+ setenv("PWD", path, TRUE);
+ sched_unlock();
return OK;
+
+errout:
+ errno = err;
+ return ERROR;
}
-#endif /* CONFIG_NFILE_DESCRIPTORS */
+#endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_DISABLE_ENVIRON */
diff --git a/nuttx/lib/lib_cwdsem.c b/nuttx/lib/lib_cwdsem.c
deleted file mode 100644
index d06487890..000000000
--- a/nuttx/lib/lib_cwdsem.c
+++ /dev/null
@@ -1,86 +0,0 @@
-/****************************************************************************
- * lib/lib_cwdsem.c
- *
- * Copyright (C) 2008 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- * used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <assert.h>
-#include <semaphore.h>
-#include <errno.h>
-
-/****************************************************************************
- * Private Variables
- ****************************************************************************/
-
-#if CONFIG_NFILE_DESCRIPTORS > 0
-static sem_t g_cwdsem = { 1 };
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: cwd_semtake
- ****************************************************************************/
-
-void cwd_semtake(void)
-{
- /* Take the semaphore (perhaps waiting) */
-
- while (sem_wait(&g_cwdsem) != 0)
- {
- /* The only case that an error should occr here is if the wait was
- * awakened by a signal.
- */
-
- ASSERT(errno == EINTR);
- }
-}
-/****************************************************************************
- * Name: cwd_semgive
- ****************************************************************************/
-
-void cwd_semgive(void)
-{
- /* Give the semaphore */
-
- (void)sem_post(&g_cwdsem);
-}
-#endif /* CONFIG_NFILE_DESCRIPTORS */
diff --git a/nuttx/lib/lib_getcwd.c b/nuttx/lib/lib_getcwd.c
index a0db05c09..20f7cef48 100644
--- a/nuttx/lib/lib_getcwd.c
+++ b/nuttx/lib/lib_getcwd.c
@@ -40,12 +40,15 @@
#include <nuttx/config.h>
#include <sys/types.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "lib_internal.h"
+#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON)
+
/****************************************************************************
* Definitions
****************************************************************************/
@@ -90,50 +93,38 @@
*
****************************************************************************/
-#if CONFIG_NFILE_DESCRIPTORS > 0
FAR char *getcwd(FAR char *buf, size_t size)
{
- const char *ptr;
- int err;
-
- /* Verify input parameters */
-
- if (!buf || !size)
- {
- err = EINVAL;
- goto errout;
- }
-
- /* If no working directory is defined, then default to the home directory */
-
- cwd_semtake();
- if (g_cwd)
- {
- ptr = g_cwd;
- }
- else
- {
- ptr = CONFIG_LIB_HOMEDIR;
- }
-
- /* Verify that the cwd will fit into the user-provided buffer */
-
- if (strlen(ptr) + 1 > size)
- {
- err = ERANGE;
- goto errout_with_sem;
- }
-
- /* Copy the cwd to the user buffer */
-
- strcpy(buf, ptr);
- cwd_semgive();
- return buf;
-
-errout_with_sem:
- cwd_semgive();
-errout:
- errno = err;
- return NULL;
+ char *pwd;
+
+ /* Verify input parameters */
+
+ if (!buf || !size)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ /* If no working directory is defined, then default to the home directory */
+
+ pwd = getenv("PWD");
+ if (!pwd)
+ {
+ pwd = CONFIG_LIB_HOMEDIR;
+ }
+
+ /* Verify that the cwd will fit into the user-provided buffer */
+
+ if (strlen(pwd) + 1 > size)
+ {
+ errno = ERANGE;
+ return NULL;
+ }
+
+ /* Copy the cwd to the user buffer */
+
+ strcpy(buf, pwd);
+ sched_unlock();
+ return buf;
}
-#endif /* CONFIG_NFILE_DESCRIPTORS */
+#endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_DISABLE_ENVIRON */
diff --git a/nuttx/lib/lib_internal.h b/nuttx/lib/lib_internal.h
index 067b64c24..2f776321a 100644
--- a/nuttx/lib/lib_internal.h
+++ b/nuttx/lib/lib_internal.h
@@ -102,11 +102,6 @@ struct lib_rawstream_s
* Public Variables
****************************************************************************/
-#if CONFIG_NFILE_DESCRIPTORS > 0
-extern char *g_cwd; /* Defined in lib_chdir.c */
-extern char *g_prevcwd; /* Defined in lib_chdir.c */
-#endif
-
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@@ -193,9 +188,4 @@ extern void lib_give_semaphore(FAR struct file_struct *stream);
extern int lib_getbase(const char *nptr, const char **endptr);
-/* Defined in lib_cwdsem.c */
-
-extern void cwd_semtake(void);
-extern void cwd_semgive(void);
-
#endif /* __LIB_INTERNAL_H */