Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pathconf and fpathconf #1278

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
* Pre-processor Definitions
****************************************************************************/

/* The number of functions that may be registered to be called
* at program exit.
*/

#define ATEXIT_MAX 1

/* Values for seeking */

#define SEEK_SET 0 /* From the start of the file */
Expand Down Expand Up @@ -109,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.
*/

Expand Down Expand Up @@ -371,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 */

Expand Down
2 changes: 1 addition & 1 deletion libs/libc/unistd/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
132 changes: 132 additions & 0 deletions libs/libc/unistd/lib_pathconf.c
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 8 additions & 0 deletions libs/libc/unistd/lib_sysconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <nuttx/config.h>

#include <unistd.h>
#include <sched.h>
#include <errno.h>

/****************************************************************************
Expand Down Expand Up @@ -225,6 +226,13 @@ long sysconf(int name)
case _SC_OPEN_MAX:
return CONFIG_NFILE_DESCRIPTORS;

case _SC_ATEXIT_MAX:
#ifdef CONFIG_SCHED_EXIT_MAX
return CONFIG_SCHED_EXIT_MAX;
#else
return 0;
#endif

default:
#if 0 /* Assume valid but not implemented for the time being */
errcode = EINVAL;
Expand Down