new getconf shell builtin

This commit is contained in:
Chet Ramey
2021-11-30 10:41:22 -05:00
parent 25e43d2c0a
commit fffe80d438
40 changed files with 10423 additions and 8881 deletions
+5 -1
View File
@@ -104,7 +104,7 @@ INC = -I. -I.. -I$(topdir) -I$(topdir)/lib -I$(topdir)/builtins -I${srcdir} \
ALLPROG = print truefalse sleep finfo logname basename dirname fdflags \
tty pathchk tee head mkdir rmdir mkfifo mktemp printenv id whoami \
uname sync push ln unlink realpath strftime mypid setpgid seq rm \
accept csv cut stat
accept csv cut stat getconf
OTHERPROG = necho hello cat pushd asort
all: $(SHOBJ_STATUS)
@@ -192,6 +192,9 @@ head: head.o
printenv: printenv.o
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ printenv.o $(SHOBJ_LIBS)
getconf: getconf.o
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ getconf.o $(SHOBJ_LIBS)
id: id.o
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ id.o $(SHOBJ_LIBS)
@@ -295,6 +298,7 @@ truefalse.o: truefalse.c
accept.o: accept.c
sleep.o: sleep.c
finfo.o: finfo.c
getconf.o: getconf.c getconf.h
logname.o: logname.c
basename.o: basename.c
dirname.o: dirname.c
File diff suppressed because it is too large Load Diff
+119
View File
@@ -0,0 +1,119 @@
/* getconf.h -- replacement definitions for ones the system doesn't provide
and don't appear in <typemax.h> */
#ifndef _GETCONF_H
#define _GETCONF_H
/* Some systems do not define these; use POSIX.2 minimum recommended values. */
#ifndef _POSIX2_COLL_WEIGHTS_MAX
# define _POSIX2_COLL_WEIGHTS_MAX 2
#endif
/* If we're on a posix system, but the system doesn't define the necessary
constants, use posix.1 minimum values. */
#if defined (_POSIX_VERSION)
#ifndef _POSIX_ARG_MAX
# define _POSIX_ARG_MAX 4096
#endif
#ifndef _POSIX_CHILD_MAX
# define _POSIX_CHILD_MAX 6
#endif
#ifndef _POSIX_LINK_MAX
# define _POSIX_LINK_MAX 8
#endif
#ifndef _POSIX_MAX_CANON
# define _POSIX_MAX_CANON 255
#endif
#ifndef _POSIX_MAX_INPUT
# define _POSIX_MAX_INPUT 255
#endif
#ifndef _POSIX_NAME_MAX
# define _POSIX_NAME_MAX 14
#endif
#ifndef _POSIX_NGROUPS_MAX
# define _POSIX_NGROUPS_MAX 0
#endif
#ifndef _POSIX_OPEN_MAX
# define _POSIX_OPEN_MAX 16
#endif
#ifndef _POSIX_PATH_MAX
# define _POSIX_PATH_MAX 255
#endif
#ifndef _POSIX_PIPE_BUF
# define _POSIX_PIPE_BUF 512
#endif
#ifndef _POSIX_SSIZE_MAX
# define _POSIX_SSIZE_MAX 32767
#endif
#ifndef _POSIX_STREAM_MAX
# define _POSIX_STREAM_MAX 8
#endif
#ifndef _POSIX_TZNAME_MAX
# define _POSIX_TZNAME_MAX 3
#endif
#ifndef _POSIX2_BC_BASE_MAX
# define _POSIX2_BC_BASE_MAX 99
#endif
#ifndef _POSIX2_BC_DIM_MAX
# define _POSIX2_BC_DIM_MAX 2048
#endif
#ifndef _POSIX2_BC_SCALE_MAX
# define _POSIX2_BC_SCALE_MAX 99
#endif
#ifndef _POSIX2_BC_STRING_MAX
# define _POSIX2_BC_STRING_MAX 1000
#endif
#ifndef _POSIX2_EQUIV_CLASS_MAX
# define _POSIX2_EQUIV_CLASS_MAX 2
#endif
#ifndef _POSIX2_EXPR_NEST_MAX
# define _POSIX2_EXPR_NEST_MAX 32
#endif
#ifndef _POSIX2_LINE_MAX
# define _POSIX2_LINE_MAX 2048
#endif
#ifndef _POSIX2_RE_DUP_MAX
# define _POSIX2_RE_DUP_MAX 255
#endif
#endif /* _POSIX_VERSION */
/* ANSI/ISO C, POSIX.1-200x, XPG 4.2, and C language type limits.
Defined only if the system include files and <typemax.h> don't. */
#ifndef CHAR_MAX
# define CHAR_MAX 127
#endif
#ifndef CHAR_MIN
# define CHAR_MIN -128
#endif
#ifndef SCHAR_MAX
# define SCHAR_MAX 127
#endif
#ifndef SCHAR_MIN
# define SCHAR_MIN -128
#endif
#ifndef INT_BIT
# define INT_BIT (sizeof (int) * CHAR_BIT)
#endif
#ifndef LONG_BIT
# define LONG_BIT (sizeof (long int) * CHAR_BIT)
#endif
#ifndef WORD_BIT
# define WORD_BIT (sizeof (int) * CHAR_BIT)
#endif
#if !defined (PRIdMAX)
# if HAVE_LONG_LONG
# define PRIdMAX "lld"
# else
# define PRIdMAX "ld"
# endif
#endif
#endif /* _GETCONF_H */
+86 -15
View File
@@ -2,10 +2,12 @@
* sleep -- sleep for fractions of a second
*
* usage: sleep seconds[.fraction]
*
* as an extension, we support the GNU time interval format (2m20s)
*/
/*
Copyright (C) 1999-2020 Free Software Foundation, Inc.
Copyright (C) 1999-2021 Free Software Foundation, Inc.
This file is part of GNU Bash.
Bash is free software: you can redistribute it and/or modify
@@ -44,13 +46,80 @@
#include <stdio.h>
#include "chartypes.h"
#include "shell.h"
#include "builtins.h"
#include "common.h"
#include "loadables.h"
#define S_SEC 1
#define S_MIN (60*S_SEC)
#define S_HOUR (60*S_MIN)
#define S_DAY (24*S_HOUR)
static int
parse_gnutimefmt (char *string, long *sp, long *up)
{
int c, r;
char *s, *ep;
long tsec, tusec, accumsec, accumusec, t;
int mult;
tsec = tusec = 0;
accumsec = accumusec = 0;
mult = 1;
for (s = string; s && *s; s++) {
r = uconvert(s, &accumsec, &accumusec, &ep);
if (r == 0 && *ep == 0)
return r;
c = *ep;
mult = 1;
switch (c) {
case '\0':
case 's':
mult = S_SEC;
break;
case 'm':
mult = S_MIN;
break;
case 'h':
mult = S_HOUR;
break;
case 'd':
mult = S_DAY;
break;
default:
return 0;
}
/* multiply the accumulated value by the multiplier */
t = accumusec * mult;
accumsec = accumsec * mult + t / 1000000;
accumusec = t % 1000000;
/* add to running total */
tsec += accumsec;
tusec += accumusec;
if (tusec >= 1000000) {
tsec++;
tusec -= 1000000;
}
/* reset and continue */
accumsec = accumusec = 0;
mult = 1;
if (c == 0)
break;
s = ep;
}
if (sp)
*sp = tsec;
if (up)
*up = tusec;
return 1;
}
int
sleep_builtin (list)
WORD_LIST *list;
sleep_builtin (WORD_LIST *list)
{
long sec, usec;
char *ep;
@@ -72,21 +141,22 @@ WORD_LIST *list;
}
r = uconvert(list->word->word, &sec, &usec, &ep);
/* Maybe postprocess conversion failures here based on EP */
/*
* Maybe postprocess conversion failures here based on EP
*
* A heuristic: if the conversion failed, but the argument appears to
* contain a GNU-like interval specifier (e.g. "1m30s"), try to parse
* it. If we can't, return the right exit code to tell
* execute_builtin to try and execute a disk command instead.
*/
if (r == 0 && (strchr ("dhms", *ep) || strpbrk (list->word->word, "dhms")))
r = parse_gnutimefmt (list->word->word, &sec, &usec);
if (r) {
fsleep(sec, usec);
QUIT;
return(EXECUTION_SUCCESS);
}
/*
* A heuristic: if the conversion failed, but the argument appears to
* contain a GNU-like interval specifier (e.g. "1m30s"), return the
* right exit code to tell execute_builtin to try and execute a disk
* command instead.
*/
if (strpbrk (list->word->word, "dhms"))
return(EX_DISKFALLBACK);
builtin_error("%s: bad sleep interval", list->word->word);
return (EXECUTION_FAILURE);
}
@@ -95,6 +165,7 @@ static char *sleep_doc[] = {
"Suspend execution for specified period.",
""
"sleep suspends execution for a minimum of SECONDS[.FRACTION] seconds.",
"As an extension, sleep accepts GNU-style time intervals (e.g., 2m30s).",
(char *)NULL
};