You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/06/23 16:35:15 UTC

[incubator-nuttx] 02/02: libc: Implement pathconf and fpathconf

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit d24bd782a9a773480e9ecd4fe0340b5a143996fb
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Jun 22 14:34:59 2020 +0800

    libc: Implement pathconf and fpathconf
    
    specified here:
    https://pubs.opengroup.org/onlinepubs/009695399/functions/fpathconf.html
    note: only _PC_PATH_MAX is handled now
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: Idd323dc10e8f31f10dd1fc64f467227808c11dbd
---
 include/unistd.h                |  28 ++++++++-
 libs/libc/unistd/Make.defs      |   2 +-
 libs/libc/unistd/lib_pathconf.c | 132 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+), 2 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index e51ab9f..1e3351e 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -103,7 +103,31 @@
 #undef  _POSIX_ASYNC_IO
 #undef  _POSIX_PRIO_IO
 
-/* Constants used with POSIX sysconf().  sysconf() will return -2 and set
+/* Constants used with POSIX pathconf().  pathconf() will return -1 and set
+ * errno to ENOSYS for most of these.
+ */
+
+#define _PC_2_SYMLINKS                   0x0001
+#define _PC_ALLOC_SIZE_MIN               0x0002
+#define _PC_ASYNC_IO                     0x0003
+#define _PC_CHOWN_RESTRICTED             0x0004
+#define _PC_FILESIZEBITS                 0x0005
+#define _PC_LINK_MAX                     0x0006
+#define _PC_MAX_CANON                    0x0007
+#define _PC_MAX_INPUT                    0x0008
+#define _PC_NAME_MAX                     0x0009
+#define _PC_NO_TRUNC                     0x000a
+#define _PC_PATH_MAX                     0x000b
+#define _PC_PIPE_BUF                     0x000c
+#define _PC_PRIO_IO                      0x000d
+#define _PC_REC_INCR_XFER_SIZE           0x000e
+#define _PC_REC_MIN_XFER_SIZE            0x000f
+#define _PC_REC_XFER_ALIGN               0x0010
+#define _PC_SYMLINK_MAX                  0x0011
+#define _PC_SYNC_IO                      0x0012
+#define _PC_VDISABLE                     0x0013
+
+/* Constants used with POSIX sysconf().  sysconf() will return -1 and set
  * errno to ENOSYS for most of these.
  */
 
@@ -365,6 +389,8 @@ int     sethostname(FAR const char *name, size_t size);
 /* Get configurable system variables */
 
 long    sysconf(int name);
+long    fpathconf(int fildes, int name);
+long    pathconf(FAR const char *path, int name);
 
 /* User and group identity management */
 
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index b3b2a82..760c3b7 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -35,7 +35,7 @@
 
 # Add the unistd C files to the build
 
-CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_sysconf.c
+CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_pathconf.c lib_sysconf.c
 CSRCS += lib_getopt.c lib_getoptargp.c lib_getoptindp.c lib_getoptoptp.c
 CSRCS += lib_alarm.c lib_sleep.c lib_usleep.c
 CSRCS += lib_seteuid.c lib_setegid.c lib_geteuid.c lib_getegid.c
diff --git a/libs/libc/unistd/lib_pathconf.c b/libs/libc/unistd/lib_pathconf.c
new file mode 100644
index 0000000..5b05a4f
--- /dev/null
+++ b/libs/libc/unistd/lib_pathconf.c
@@ -0,0 +1,132 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_pathconf.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fpathconf/pathconf
+ *
+ * Description:
+ *   The fpathconf() and pathconf() functions shall determine the current
+ *   value of a configurable limit or option (variable) that is associated
+ *   with a file or directory.
+ *
+ *   For pathconf(), the path argument points to the pathname of a file or
+ *   directory.
+ *
+ *   For fpathconf(), the fildes argument is an open file descriptor.
+ *
+ *   The name argument represents the variable to be queried relative to that
+ *   file or directory. Implementations shall support all of the variables
+ *   listed in the following table and may support others. The variables in
+ *   the following table come from <limits.h> or <unistd.h> and the symbolic
+ *   constants, defined in <unistd.h>, are the corresponding values used for
+ *   name.
+ *
+ *     Variable                           Value of Name
+ *
+ *     {FILESIZEBITS}                     _PC_FILESIZEBITS
+ *     {LINK_MAX}                         _PC_LINK_MAX
+ *     {MAX_CANON}                        _PC_MAX_CANON
+ *     {MAX_INPUT}                        _PC_MAX_INPUT
+ *     {NAME_MAX}                         _PC_NAME_MAX
+ *     {PATH_MAX}                         _PC_PATH_MAX
+ *     {PIPE_BUF}                         _PC_PIPE_BUF
+ *     {POSIX2_SYMLINKS}                  _PC_2_SYMLINKS
+ *     {POSIX_ALLOC_SIZE_MIN}             _PC_ALLOC_SIZE_MIN
+ *     {POSIX_REC_INCR_XFER_SIZE}         _PC_REC_INCR_XFER_SIZE
+ *     {POSIX_REC_MAX_XFER_SIZE}          _PC_REC_MAX_XFER_SIZE
+ *     {POSIX_REC_MIN_XFER_SIZE}          _PC_REC_MIN_XFER_SIZE
+ *     {POSIX_REC_XFER_ALIGN}             _PC_REC_XFER_ALIGN
+ *     {SYMLINK_MAX}                      _PC_SYMLINK_MAX
+ *     _POSIX_CHOWN_RESTRICTED            _PC_CHOWN_RESTRICTED
+ *     _POSIX_NO_TRUNC                    _PC_NO_TRUNC
+ *     _POSIX_VDISABLE                    _PC_VDISABLE
+ *     _POSIX_ASYNC_IO                    _PC_ASYNC_IO
+ *     _POSIX_PRIO_IO                     _PC_PRIO_IO
+ *     _POSIX_SYNC_IO                     _PC_SYNC_IO
+ *
+ * Returned Value:
+ *   If name is an invalid value, both pathconf() and fpathconf() shall
+ *   return -1 and set errno to indicate the error.
+ *
+ *   If the variable corresponding to name has no limit for the path or file
+ *   descriptor, both pathconf() and fpathconf() shall return -1 without
+ *   changing errno. If the implementation needs to use path to determine the
+ *   value of name and the implementation does not support the association of
+ *   name with the file specified by path, or if the process did not have
+ *   appropriate privileges to query the file specified by path, or path does
+ *   not exist, pathconf() shall return -1 and set errno to indicate the
+ *   error.
+ *
+ *   If the implementation needs to use fildes to determine the value of name
+ *   and the implementation does not support the association of name with the
+ *   file specified by fildes, or if fildes is an invalid file descriptor,
+ *   fpathconf() shall return -1 and set errno to indicate the error.
+ *
+ *   Otherwise, pathconf() or fpathconf() shall return the current variable
+ *   value for the file or directory without changing errno. The value
+ *   returned shall not be more restrictive than the corresponding value
+ *   available to the application when it was compiled with the
+ *   implementation's <limits.h> or <unistd.h>.
+ *
+ *   If the variable corresponding to name is dependent on an unsupported
+ *   option, the results are unspecified.
+ *
+ ****************************************************************************/
+
+long fpathconf(int fildes, int name)
+{
+  /* NOTE:  The initialize implementation of this interface is very sparse.
+   * It was originally created to support only the functionality of libcxx
+   * but can be extended to support as much of the standard POSIX as is
+   * necessary.
+   */
+
+  switch (name)
+    {
+      case _PC_PATH_MAX:
+        return PATH_MAX;
+
+      default:
+
+        /* Assume valid but not implemented for the time being */
+
+        set_errno(ENOSYS);
+        return ERROR;
+    }
+}
+
+long pathconf(FAR const char *path, int name)
+{
+  return fpathconf(-1, name);
+}