mirror of
https://https.git.savannah.gnu.org/git/bash.git
synced 2026-07-08 21:00:48 +02:00
bash-5.0 distribution sources and documentation
This commit is contained in:
@@ -100,10 +100,10 @@ INC = -I. -I.. -I$(topdir) -I$(topdir)/lib -I$(topdir)/builtins -I${srcdir} \
|
||||
$(SHOBJ_CC) $(SHOBJ_CFLAGS) $(CCFLAGS) $(INC) -c -o $@ $<
|
||||
|
||||
|
||||
ALLPROG = print truefalse sleep finfo logname basename dirname \
|
||||
ALLPROG = print truefalse sleep finfo logname basename dirname fdflags \
|
||||
tty pathchk tee head mkdir rmdir printenv id whoami \
|
||||
uname sync push ln unlink realpath strftime mypid setpgid
|
||||
OTHERPROG = necho hello cat pushd
|
||||
uname sync push ln unlink realpath strftime mypid setpgid seq
|
||||
OTHERPROG = necho hello cat pushd stat rm
|
||||
|
||||
all: $(SHOBJ_STATUS)
|
||||
|
||||
@@ -142,6 +142,15 @@ finfo: finfo.o
|
||||
cat: cat.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ cat.o $(SHOBJ_LIBS)
|
||||
|
||||
rm: rm.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ rm.o $(SHOBJ_LIBS)
|
||||
|
||||
fdflags: fdflags.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ fdflags.o $(SHOBJ_LIBS)
|
||||
|
||||
seq: seq.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ seq.o $(SHOBJ_LIBS)
|
||||
|
||||
logname: logname.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ logname.o $(SHOBJ_LIBS)
|
||||
|
||||
@@ -202,10 +211,12 @@ strftime: strftime.o
|
||||
mypid: mypid.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ mypid.o $(SHOBJ_LIBS)
|
||||
|
||||
|
||||
setpgid: setpgid.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ setpgid.o $(SHOBJ_LIBS)
|
||||
|
||||
stat: stat.o
|
||||
$(SHOBJ_LD) $(SHOBJ_LDFLAGS) $(SHOBJ_XLDFLAGS) -o $@ stat.o $(SHOBJ_LIBS)
|
||||
|
||||
# pushd is a special case. We use the same source that the builtin version
|
||||
# uses, with special compilation options.
|
||||
#
|
||||
@@ -236,6 +247,7 @@ installdirs:
|
||||
|
||||
install-dev: installdirs
|
||||
@$(INSTALL_DATA) Makefile.inc $(DESTDIR)$(loadablesdir)/Makefile.inc
|
||||
@$(INSTALL_DATA) $(srcdir)/loadables.h $(DESTDIR)$(loadablesdir)/loadables.h
|
||||
@( cd $(BUILD_DIR) && ${MAKE} ${MFLAGS} DESTDIR="$(DESTDIR)" install-headers)
|
||||
|
||||
install-supported: all installdirs install-dev
|
||||
@@ -246,7 +258,7 @@ install-supported: all installdirs install-dev
|
||||
done
|
||||
|
||||
uninstall-dev:
|
||||
-$(RM) $(DESTDIR)$(loadablesdir)/Makefile.inc
|
||||
-$(RM) $(DESTDIR)$(loadablesdir)/Makefile.inc $(DESTDIR)$(loadablesdir)/loadables.h
|
||||
-( cd $(BUILD_DIR) && ${MAKE} ${MFLAGS} DESTDIR="$(DESTDIR)" uninstall-headers)
|
||||
|
||||
uninstall-supported: uninstall-dev
|
||||
@@ -283,3 +295,6 @@ mkdir.o: mkdir.c
|
||||
realpath.o: realpath.c
|
||||
strftime.o: strftime.c
|
||||
setpgid.o: setpgid.c
|
||||
stat.o: stat.c
|
||||
fdflags.o: fdflags.c
|
||||
seq.o: seq.c
|
||||
|
||||
@@ -32,28 +32,39 @@ the canonical example. There is no real `builtin writers' programming
|
||||
guide'. The file template.c provides a template to use for creating
|
||||
new loadable builtins.
|
||||
|
||||
The file "Makefile.inc" is created using the same values that configure
|
||||
writes into Makefile.in, and is installed in the same directory as the
|
||||
rest of the example builtins. It's intended to be a start at something
|
||||
that can be modified or included to help you build your own loadables
|
||||
without having to search for the right CFLAGS and LDFLAGS.
|
||||
|
||||
basename.c Return non-directory portion of pathname.
|
||||
cat.c cat(1) replacement with no options - the way cat was intended.
|
||||
dirname.c Return directory portion of pathname.
|
||||
fdflags.c Change the flag associated with one of bash's open file desriptors.
|
||||
finfo.c Print file info.
|
||||
head.c Copy first part of files.
|
||||
hello.c Obligatory "Hello World" / sample loadable.
|
||||
id.c POSIX.2 user identity.
|
||||
ln.c Make links.
|
||||
loadables.h Start at a file loadable builtins can include for shell definitions
|
||||
loadables.h File loadable builtins can include for shell definitions.
|
||||
logname.c Print login name of current user.
|
||||
Makefile.in Simple makefile for the sample loadable builtins.
|
||||
Makefile.inc.in Sample makefile to use for loadable builtin development.
|
||||
mkdir.c Make directories.
|
||||
mypid.c Add $MYPID variable, demonstrate use of unload hook function
|
||||
mypid.c Add $MYPID variable, demonstrate use of unload hook functio.n
|
||||
necho.c echo without options or argument interpretation.
|
||||
pathchk.c Check pathnames for validity and portability.
|
||||
print.c Loadable ksh-93 style print builtin.
|
||||
printenv.c Minimal builtin clone of BSD printenv(1).
|
||||
push.c Anyone remember TOPS-20?
|
||||
README README
|
||||
realpath.c Canonicalize pathnames, resolving symlinks.
|
||||
rm.c Remove files and directories.
|
||||
rmdir.c Remove directory.
|
||||
seq.c Print a sequence of decimal or floating point numbers.
|
||||
setpgid.c Set a process's pgrp; example of how to wrap a system call.
|
||||
sleep.c sleep for fractions of a second.
|
||||
stat.c populate an associative array with information about a file
|
||||
strftime.c Loadable builtin interface to strftime(3).
|
||||
sync.c Sync the disks by forcing pending filesystem writes to complete.
|
||||
tee.c Duplicate standard input.
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "builtins.h"
|
||||
#include "shell.h"
|
||||
#include "common.h"
|
||||
#include "bashgetopt.h"
|
||||
|
||||
int
|
||||
basename_builtin (list)
|
||||
@@ -46,6 +47,7 @@ basename_builtin (list)
|
||||
|
||||
if (no_options (list))
|
||||
return (EX_USAGE);
|
||||
list = loptend;
|
||||
|
||||
string = list->word->word;
|
||||
suffix = (char *)NULL;
|
||||
|
||||
@@ -56,6 +56,7 @@ int fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
cat_main (argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
@@ -88,6 +89,7 @@ char **argv;
|
||||
return (r);
|
||||
}
|
||||
|
||||
int
|
||||
cat_builtin(list)
|
||||
WORD_LIST *list;
|
||||
{
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "builtins.h"
|
||||
#include "shell.h"
|
||||
#include "common.h"
|
||||
#include "bashgetopt.h"
|
||||
|
||||
int
|
||||
dirname_builtin (list)
|
||||
@@ -38,15 +39,16 @@ dirname_builtin (list)
|
||||
int slen;
|
||||
char *string;
|
||||
|
||||
if (no_options (list))
|
||||
return (EX_USAGE);
|
||||
list = loptend;
|
||||
|
||||
if (list == 0 || list->next)
|
||||
{
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
|
||||
if (no_options (list))
|
||||
return (EX_USAGE);
|
||||
|
||||
string = list->word->word;
|
||||
slen = strlen (string);
|
||||
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
/* Loadable builtin to get and set file descriptor flags. */
|
||||
|
||||
/* See Makefile for compilation details. */
|
||||
|
||||
/*
|
||||
Copyright (C) 2017 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash.
|
||||
Bash is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if defined (HAVE_UNISTD_H)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include "bashansi.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "loadables.h"
|
||||
|
||||
static const struct
|
||||
{
|
||||
const char *name;
|
||||
int value;
|
||||
} file_flags[] =
|
||||
{
|
||||
#ifdef O_APPEND
|
||||
{ "append", O_APPEND },
|
||||
#endif
|
||||
#ifdef O_ASYNC
|
||||
{ "async", O_ASYNC },
|
||||
#endif
|
||||
#ifdef O_SYNC
|
||||
{ "sync", O_SYNC },
|
||||
#endif
|
||||
#ifdef O_NONBLOCK
|
||||
{ "nonblock", O_NONBLOCK },
|
||||
#endif
|
||||
#ifdef O_FSYNC
|
||||
{ "fsync", O_FSYNC },
|
||||
#endif
|
||||
#ifdef O_DSYNC
|
||||
{ "dsync", O_DSYNC },
|
||||
#endif
|
||||
#ifdef O_RSYNC
|
||||
{ "rsync", O_RSYNC },
|
||||
#endif
|
||||
#ifdef O_ALT_IO
|
||||
{ "altio", O_ALT_IO },
|
||||
#endif
|
||||
#ifdef O_DIRECT
|
||||
{ "direct", O_DIRECT },
|
||||
#endif
|
||||
#ifdef O_NOATIME
|
||||
{ "noatime", O_NOATIME },
|
||||
#endif
|
||||
#ifdef O_NOSIGPIPE
|
||||
{ "nosigpipe", O_NOSIGPIPE },
|
||||
#endif
|
||||
#ifdef O_CLOEXEC
|
||||
{ "cloexec", O_CLOEXEC },
|
||||
#endif
|
||||
};
|
||||
|
||||
#define N_FLAGS (sizeof (file_flags) / sizeof (file_flags[0]))
|
||||
|
||||
#ifndef errno
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
/* FIX THIS */
|
||||
static int
|
||||
getallflags ()
|
||||
{
|
||||
int i, allflags;
|
||||
|
||||
for (i = allflags = 0; i < N_FLAGS; i++)
|
||||
allflags |= file_flags[i].value;
|
||||
return allflags;
|
||||
}
|
||||
|
||||
static int
|
||||
getflags(int fd, int p)
|
||||
{
|
||||
int c, f;
|
||||
int allflags;
|
||||
|
||||
if ((c = fcntl(fd, F_GETFD)) == -1)
|
||||
{
|
||||
if (p)
|
||||
builtin_error("can't get status for fd %d: %s", fd, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((f = fcntl(fd, F_GETFL)) == -1)
|
||||
{
|
||||
if (p)
|
||||
builtin_error("Can't get flags for fd %d: %s", fd, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (c)
|
||||
f |= O_CLOEXEC;
|
||||
|
||||
return f & getallflags();
|
||||
}
|
||||
|
||||
static void
|
||||
printone(int fd, int p, int verbose)
|
||||
{
|
||||
int f;
|
||||
size_t i;
|
||||
|
||||
if ((f = getflags(fd, p)) == -1)
|
||||
return;
|
||||
|
||||
printf ("%d:", fd);
|
||||
|
||||
for (i = 0; i < N_FLAGS; i++)
|
||||
{
|
||||
if (f & file_flags[i].value)
|
||||
{
|
||||
printf ("%s%s", verbose ? "+" : "", file_flags[i].name);
|
||||
f &= ~file_flags[i].value;
|
||||
}
|
||||
else if (verbose)
|
||||
printf ( "-%s", file_flags[i].name);
|
||||
else
|
||||
continue;
|
||||
|
||||
if (f || (verbose && i != N_FLAGS - 1))
|
||||
putchar (',');
|
||||
}
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
static int
|
||||
parseflags(char *s, int *p, int *n)
|
||||
{
|
||||
int f, *v;
|
||||
size_t i;
|
||||
|
||||
f = 0;
|
||||
*p = *n = 0;
|
||||
|
||||
for (s = strtok(s, ","); s; s = strtok(NULL, ","))
|
||||
{
|
||||
switch (*s)
|
||||
{
|
||||
case '+':
|
||||
v = p;
|
||||
s++;
|
||||
break;
|
||||
case '-':
|
||||
v = n;
|
||||
s++;
|
||||
break;
|
||||
default:
|
||||
v = &f;
|
||||
break;
|
||||
}
|
||||
|
||||
for (i = 0; i < N_FLAGS; i++)
|
||||
if (strcmp(s, file_flags[i].name) == 0)
|
||||
{
|
||||
*v |= file_flags[i].value;
|
||||
break;
|
||||
}
|
||||
if (i == N_FLAGS)
|
||||
builtin_error("invalid flag `%s'", s);
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
static void
|
||||
setone(int fd, char *v, int verbose)
|
||||
{
|
||||
int f, n, pos, neg, cloexec;
|
||||
|
||||
f = getflags(fd, 1);
|
||||
if (f == -1)
|
||||
return;
|
||||
|
||||
parseflags(v, &pos, &neg);
|
||||
|
||||
cloexec = -1;
|
||||
if ((pos & O_CLOEXEC) && (f & O_CLOEXEC) == 0)
|
||||
cloexec = FD_CLOEXEC;
|
||||
if ((neg & O_CLOEXEC) && (f & O_CLOEXEC))
|
||||
cloexec = 0;
|
||||
if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1)
|
||||
builtin_error("can't set status for fd %d: %s", fd, strerror(errno));
|
||||
|
||||
pos &= ~O_CLOEXEC;
|
||||
neg &= ~O_CLOEXEC;
|
||||
f &= ~O_CLOEXEC;
|
||||
|
||||
n = f;
|
||||
n |= pos;
|
||||
n &= ~neg;
|
||||
|
||||
if (n != f && fcntl(fd, F_SETFL, n) == -1)
|
||||
builtin_error("can't set flags for fd %d: %s", fd, strerror(errno));
|
||||
}
|
||||
|
||||
static int
|
||||
getmaxfd ()
|
||||
{
|
||||
int maxfd, ignore;
|
||||
|
||||
#ifdef F_MAXFD
|
||||
maxfd = fcntl (0, F_MAXFD);
|
||||
if (maxfd > 0)
|
||||
return maxfd;
|
||||
#endif
|
||||
|
||||
maxfd = getdtablesize ();
|
||||
if (maxfd <= 0)
|
||||
maxfd = HIGH_FD_MAX;
|
||||
for (maxfd--; maxfd > 0; maxfd--)
|
||||
if (fcntl (maxfd, F_GETFD, &ignore) != -1)
|
||||
break;
|
||||
|
||||
return maxfd;
|
||||
}
|
||||
|
||||
int
|
||||
fdflags_builtin (WORD_LIST *list)
|
||||
{
|
||||
int opt, maxfd, i, num, verbose, setflag;
|
||||
char *setspec;
|
||||
WORD_LIST *l;
|
||||
intmax_t inum;
|
||||
|
||||
setflag = verbose = 0;
|
||||
reset_internal_getopt ();
|
||||
while ((opt = internal_getopt (list, "s:v")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 's':
|
||||
setflag = 1;
|
||||
setspec = list_optarg;
|
||||
break;
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
|
||||
}
|
||||
list = loptend;
|
||||
|
||||
/* Maybe we could provide some default here, but we don't yet. */
|
||||
if (list == 0 && setflag)
|
||||
return (EXECUTION_SUCCESS);
|
||||
|
||||
if (list == 0)
|
||||
{
|
||||
maxfd = getmaxfd ();
|
||||
if (maxfd < 0)
|
||||
{
|
||||
builtin_error ("can't get max fd: %s", strerror (errno));
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
for (i = 0; i < maxfd; i++)
|
||||
printone (i, 0, verbose);
|
||||
return (EXECUTION_SUCCESS);
|
||||
}
|
||||
|
||||
opt = EXECUTION_SUCCESS;
|
||||
for (l = list; l; l = l->next)
|
||||
{
|
||||
if (legal_number (l->word->word, &inum) == 0 || inum < 0)
|
||||
{
|
||||
builtin_error ("%s: invalid file descriptor", l->word->word);
|
||||
opt = EXECUTION_FAILURE;
|
||||
continue;
|
||||
}
|
||||
num = inum; /* truncate to int */
|
||||
if (setflag)
|
||||
setone (num, setspec, verbose);
|
||||
else
|
||||
printone (num, 1, verbose);
|
||||
}
|
||||
|
||||
return (opt);
|
||||
}
|
||||
|
||||
char *fdflags_doc[] =
|
||||
{
|
||||
"Display and modify file descriptor flags.",
|
||||
"",
|
||||
"Display or, if the -s option is supplied, set flags for each file",
|
||||
"descriptor supplied as an argument. If the -v option is supplied,",
|
||||
"the display is verbose, including each settable option name in the",
|
||||
"form of a string such as that accepted by the -s option.",
|
||||
"",
|
||||
"The -s option accepts a string with a list of flag names, each preceded",
|
||||
"by a `+' (set) or `-' (unset). Those changes are applied to each file",
|
||||
"descriptor supplied as an argument.",
|
||||
"",
|
||||
"If no file descriptor arguments are supplied, the displayed information",
|
||||
"consists of the status of flags for each of the shell's open files.",
|
||||
(char *)NULL
|
||||
};
|
||||
|
||||
/* The standard structure describing a builtin command. bash keeps an array
|
||||
of these structures. The flags must include BUILTIN_ENABLED so the
|
||||
builtin can be used. */
|
||||
struct builtin fdflags_struct = {
|
||||
"fdflags", /* builtin name */
|
||||
fdflags_builtin, /* function implementing the builtin */
|
||||
BUILTIN_ENABLED, /* initial flags for builtin */
|
||||
fdflags_doc, /* array of long documentation strings. */
|
||||
"fdflags [-v] [-s flags_string] [fd ...]", /* usage synopsis; becomes short_doc */
|
||||
0 /* reserved for internal use */
|
||||
};
|
||||
@@ -336,9 +336,9 @@ int flags;
|
||||
} else if (flags & OPT_DEV)
|
||||
printf("%d\n", st->st_dev);
|
||||
else if (flags & OPT_INO)
|
||||
printf("%d\n", st->st_ino);
|
||||
printf("%lu\n", (unsigned long)st->st_ino);
|
||||
else if (flags & OPT_FID)
|
||||
printf("%d:%ld\n", st->st_dev, st->st_ino);
|
||||
printf("%d:%lu\n", st->st_dev, (unsigned long)st->st_ino);
|
||||
else if (flags & OPT_NLINK)
|
||||
printf("%d\n", st->st_nlink);
|
||||
else if (flags & OPT_LNKNAM) {
|
||||
|
||||
@@ -117,6 +117,7 @@ head_builtin (list)
|
||||
return (EX_USAGE);
|
||||
}
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -91,6 +91,7 @@ id_builtin (list)
|
||||
case 'n': id_flags |= ID_USENAME; break;
|
||||
case 'r': id_flags |= ID_USEREAL; break;
|
||||
case 'u': id_flags |= ID_USERONLY; break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -76,6 +76,7 @@ ln_builtin (list)
|
||||
case 'n':
|
||||
flags |= LN_NOFOLLOW;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -52,12 +52,12 @@ int
|
||||
mkdir_builtin (list)
|
||||
WORD_LIST *list;
|
||||
{
|
||||
int opt, pflag, omode, rval, nmode, parent_mode;
|
||||
int opt, pflag, mflag, omode, rval, nmode, parent_mode;
|
||||
char *mode;
|
||||
WORD_LIST *l;
|
||||
|
||||
reset_internal_getopt ();
|
||||
pflag = 0;
|
||||
pflag = mflag = 0;
|
||||
mode = (char *)NULL;
|
||||
while ((opt = internal_getopt(list, "m:p")) != -1)
|
||||
switch (opt)
|
||||
@@ -66,8 +66,10 @@ mkdir_builtin (list)
|
||||
pflag = 1;
|
||||
break;
|
||||
case 'm':
|
||||
mflag = 1;
|
||||
mode = list_optarg;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage();
|
||||
return (EX_USAGE);
|
||||
@@ -114,7 +116,7 @@ mkdir_builtin (list)
|
||||
|
||||
for (rval = EXECUTION_SUCCESS, l = list; l; l = l->next)
|
||||
{
|
||||
if (pflag && make_path (l->word->word, nmode, parent_mode))
|
||||
if (pflag && make_path (l->word->word, mflag, nmode, parent_mode))
|
||||
{
|
||||
rval = EXECUTION_FAILURE;
|
||||
continue;
|
||||
@@ -132,8 +134,9 @@ mkdir_builtin (list)
|
||||
this changes the process's umask; make sure that all paths leading to a
|
||||
return reset it to ORIGINAL_UMASK */
|
||||
static int
|
||||
make_path (path, nmode, parent_mode)
|
||||
make_path (path, user_mode, nmode, parent_mode)
|
||||
char *path;
|
||||
int user_mode;
|
||||
int nmode, parent_mode;
|
||||
{
|
||||
int oumask;
|
||||
@@ -148,7 +151,7 @@ make_path (path, nmode, parent_mode)
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (chmod (path, nmode))
|
||||
if (user_mode && chmod (path, nmode))
|
||||
{
|
||||
builtin_error ("%s: %s", path, strerror (errno));
|
||||
return 1;
|
||||
@@ -172,13 +175,20 @@ make_path (path, nmode, parent_mode)
|
||||
*p = '\0';
|
||||
if (stat (npath, &sb) != 0)
|
||||
{
|
||||
if (mkdir (npath, parent_mode))
|
||||
if (mkdir (npath, 0))
|
||||
{
|
||||
builtin_error ("cannot create directory `%s': %s", npath, strerror (errno));
|
||||
umask (original_umask);
|
||||
free (npath);
|
||||
return 1;
|
||||
}
|
||||
if (chmod (npath, parent_mode) != 0)
|
||||
{
|
||||
builtin_error ("cannot chmod directory `%s': %s", npath, strerror (errno));
|
||||
umask (original_umask);
|
||||
free (npath);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if (S_ISDIR (sb.st_mode) == 0)
|
||||
{
|
||||
|
||||
@@ -38,7 +38,7 @@ WORD_LIST *list;
|
||||
char *necho_doc[] = {
|
||||
"Display arguments.",
|
||||
"",
|
||||
"Print the arguments to the standard ouput separated",
|
||||
"Print the arguments to the standard output separated",
|
||||
"by space characters and terminated with a newline.",
|
||||
(char *)NULL
|
||||
};
|
||||
|
||||
@@ -112,6 +112,7 @@ pathchk_builtin (list)
|
||||
case 'p':
|
||||
pflag = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
@@ -135,7 +136,7 @@ char *pathchk_doc[] = {
|
||||
"Check pathnames for validity.",
|
||||
"",
|
||||
"Check each pathname argument for validity (i.e., it may be used to",
|
||||
"create or access a file without casuing syntax errors) and portability",
|
||||
"create or access a file without causing syntax errors) and portability",
|
||||
"(i.e., no filename truncation will result). If the `-p' option is",
|
||||
"supplied, more extensive portability checks are performed.",
|
||||
(char *)NULL
|
||||
|
||||
@@ -122,6 +122,7 @@ print_builtin (list)
|
||||
case 'f':
|
||||
pfmt = list_optarg;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -46,6 +46,7 @@ printenv_builtin (list)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -51,6 +51,7 @@ push_builtin (list)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -86,15 +86,19 @@ WORD_LIST *list;
|
||||
case 'v':
|
||||
vflag = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
}
|
||||
|
||||
list = loptend;
|
||||
|
||||
if (list == 0)
|
||||
if (list == 0) {
|
||||
builtin_usage();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
|
||||
for (es = EXECUTION_SUCCESS; list; list = list->next) {
|
||||
p = list->word->word;
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/* rm - remove files and directories with -r */
|
||||
|
||||
/* See Makefile for compilation details. */
|
||||
|
||||
/*
|
||||
Copyright (C) 2016 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash.
|
||||
Bash is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#include "builtins.h"
|
||||
#include "shell.h"
|
||||
#include "common.h"
|
||||
#include "bashgetopt.h"
|
||||
|
||||
#if !defined (errno)
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
static int rm_file(const char *fname);
|
||||
|
||||
static int force, recursive;
|
||||
|
||||
static int
|
||||
_remove_directory(const char *dirname)
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *dp;
|
||||
size_t dirlen;
|
||||
int err;
|
||||
|
||||
dirlen = strlen (dirname);
|
||||
err = 0;
|
||||
|
||||
if ((dir = opendir(dirname)))
|
||||
{
|
||||
while ((dp = readdir(dir)))
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
char fname[dirlen + 1 + strlen (dp->d_name) + 1];
|
||||
#else
|
||||
char *fname;
|
||||
int fnsize;
|
||||
#endif
|
||||
|
||||
if (*dp->d_name == '.' && (dp->d_name[1] == 0 || (dp->d_name[1] == '.' && dp->d_name[2] == 0)))
|
||||
continue;
|
||||
|
||||
#ifdef __GNUC__
|
||||
snprintf(fname, sizeof (fname), "%s/%s", dirname, dp->d_name);
|
||||
#else
|
||||
fnsize = dirlen + 1 + strlen (dp->d_name) + 1;
|
||||
fname = xmalloc (fnsize);
|
||||
snprintf(fname, fnsize, "%s/%s", dirname, dp->d_name);
|
||||
#endif
|
||||
|
||||
if (rm_file (fname) && force == 0)
|
||||
err = 1;
|
||||
#ifndef __GNUC__
|
||||
free (fname);
|
||||
#endif
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
|
||||
if (err == 0 && rmdir (dirname) && force == 0)
|
||||
err = 1;
|
||||
}
|
||||
else if (force == 0)
|
||||
err = 1;
|
||||
|
||||
if (err)
|
||||
builtin_error ("%s: %s", dirname, strerror (errno));
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int
|
||||
rm_file(const char *fname)
|
||||
{
|
||||
if (unlink (fname) == 0)
|
||||
return 0;
|
||||
|
||||
/* If FNAME is a directory glibc returns EISDIR but correct POSIX value
|
||||
would be EPERM. If we get that error and FNAME is a directory and -r
|
||||
was supplied, recursively remove the directory and its contents */
|
||||
if ((errno == EISDIR || errno == EPERM) && recursive && file_isdir (fname))
|
||||
return _remove_directory(fname);
|
||||
else if (force)
|
||||
return 0;
|
||||
|
||||
builtin_error ("%s: %s", fname, strerror (errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
rm_builtin (list)
|
||||
WORD_LIST *list;
|
||||
{
|
||||
const char *name;
|
||||
WORD_LIST *l;
|
||||
int rval, opt;
|
||||
|
||||
recursive = force = 0;
|
||||
rval = EXECUTION_SUCCESS;
|
||||
|
||||
reset_internal_getopt ();
|
||||
while ((opt = internal_getopt (list, "Rrfi")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'R':
|
||||
case 'r':
|
||||
recursive = 1;
|
||||
break;
|
||||
case 'f':
|
||||
force = 1;
|
||||
break;
|
||||
case 'i':
|
||||
return (EX_DISKFALLBACK);
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
}
|
||||
list = loptend;
|
||||
|
||||
if (list == 0)
|
||||
{
|
||||
builtin_usage ();
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
for (l = list; l; l = l->next)
|
||||
{
|
||||
if (rm_file(l->word->word) && force == 0)
|
||||
rval = EXECUTION_FAILURE;
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
char *rm_doc[] = {
|
||||
"Remove files.",
|
||||
"",
|
||||
"rm removes the files specified as arguments.",
|
||||
(char *)NULL
|
||||
};
|
||||
|
||||
/* The standard structure describing a builtin command. bash keeps an array
|
||||
of these structures. */
|
||||
struct builtin rm_struct = {
|
||||
"rm", /* builtin name */
|
||||
rm_builtin, /* function implementing the builtin */
|
||||
BUILTIN_ENABLED, /* initial flags for builtin */
|
||||
rm_doc, /* array of long documentation strings. */
|
||||
"rm [-rf] file ...", /* usage synopsis; becomes short_doc */
|
||||
0 /* reserved for internal use */
|
||||
};
|
||||
@@ -0,0 +1,490 @@
|
||||
/* seq - print sequence of numbers to standard output.
|
||||
Copyright (C) 2018 Free Software Foundation, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
||||
|
||||
/* Written as bash builtin by Chet Ramey. Portions from seq.c by Ulrich Drepper. */
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "bashansi.h"
|
||||
#include "loadables.h"
|
||||
#include "bashintl.h"
|
||||
|
||||
#ifndef errno
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
#if defined (HAVE_LONG_DOUBLE) && HAVE_DECL_STRTOLD && !defined(STRTOLD_BROKEN)
|
||||
typedef long double floatmax_t;
|
||||
# define FLOATMAX_CONV "L"
|
||||
# define strtofltmax strtold
|
||||
# define FLOATMAX_FMT "%Lg"
|
||||
# define FLOATMAX_WFMT "%0.Lf"
|
||||
# define USE_LONG_DOUBLE
|
||||
#else
|
||||
typedef double floatmax_t;
|
||||
# define FLOATMAX_CONV ""
|
||||
# define strtofltmax strtod
|
||||
# define FLOATMAX_FMT "%g"
|
||||
# define FLOATMAX_WFMT "%0.f"
|
||||
#endif
|
||||
static floatmax_t getfloatmax __P((const char *));
|
||||
static char *genformat __P((floatmax_t, floatmax_t, floatmax_t));
|
||||
|
||||
#define MAX(a, b) (((a) < (b))? (b) : (a))
|
||||
|
||||
static int conversion_error = 0;
|
||||
|
||||
/* If true print all number with equal width. */
|
||||
static int equal_width;
|
||||
|
||||
/* The string used to separate two numbers. */
|
||||
static char const *separator;
|
||||
|
||||
/* The string output after all numbers have been output. */
|
||||
static char const terminator[] = "\n";
|
||||
|
||||
static char decimal_point;
|
||||
|
||||
/* Pretty much the same as the version in builtins/printf.def */
|
||||
static floatmax_t
|
||||
getfloatmax (arg)
|
||||
const char *arg;
|
||||
{
|
||||
floatmax_t ret;
|
||||
char *ep;
|
||||
|
||||
errno = 0;
|
||||
ret = strtofltmax (arg, &ep);
|
||||
|
||||
if (*ep)
|
||||
{
|
||||
sh_invalidnum ((char *)arg);
|
||||
conversion_error = 1;
|
||||
}
|
||||
else if (errno == ERANGE)
|
||||
{
|
||||
builtin_error ("warning: %s: %s", arg, strerror(ERANGE));
|
||||
conversion_error = 1;
|
||||
}
|
||||
|
||||
if (ret == -0.0)
|
||||
ret = 0.0;
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/* If FORMAT is a valid printf format for a double argument, return
|
||||
its long double equivalent, allocated from dynamic storage. This
|
||||
was written by Ulrich Drepper, taken from coreutils:seq.c */
|
||||
static char *
|
||||
long_double_format (char const *fmt)
|
||||
{
|
||||
size_t i;
|
||||
size_t length_modifier_offset;
|
||||
int has_L;
|
||||
|
||||
for (i = 0; ! (fmt[i] == '%' && fmt[i + 1] != '%'); i += (fmt[i] == '%') + 1)
|
||||
{
|
||||
if (!fmt[i])
|
||||
{
|
||||
builtin_error ("format %s has no %% directive", fmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
i += strspn (fmt + i, "-+#0 '"); /* zero or more flags */
|
||||
i += strspn (fmt + i, "0123456789"); /* optional minimum field width */
|
||||
if (fmt[i] == '.') /* optional precision */
|
||||
{
|
||||
i++;
|
||||
i += strspn (fmt + i, "0123456789");
|
||||
}
|
||||
|
||||
length_modifier_offset = i; /* optional length modifier */
|
||||
/* we could ignore an 'l' length modifier here */
|
||||
has_L = (fmt[i] == 'L');
|
||||
i += has_L;
|
||||
switch (fmt[i])
|
||||
{
|
||||
case '\0':
|
||||
builtin_error ("format %s ends in %%", fmt);
|
||||
return 0;
|
||||
case 'A':
|
||||
case 'a':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'g':
|
||||
case 'G':
|
||||
break;
|
||||
default:
|
||||
builtin_error ("format %s has unknown `%%%c' directive", fmt, fmt[i]);
|
||||
return 0;
|
||||
}
|
||||
for (i++; ; i += (fmt[i] == '%') + 1)
|
||||
if (fmt[i] == '%' && fmt[i + 1] != '%')
|
||||
{
|
||||
builtin_error ("format %s has too many %% directives", fmt);
|
||||
return 0;
|
||||
}
|
||||
else if (fmt[i] == 0)
|
||||
{
|
||||
size_t format_size = i + 1;
|
||||
char *ldfmt = xmalloc (format_size + 1);
|
||||
memcpy (ldfmt, fmt, length_modifier_offset);
|
||||
#ifdef USE_LONG_DOUBLE
|
||||
ldfmt[length_modifier_offset] = 'L';
|
||||
strcpy (ldfmt + length_modifier_offset + 1,
|
||||
fmt + length_modifier_offset + has_L);
|
||||
#else
|
||||
strcpy (ldfmt + length_modifier_offset, fmt + length_modifier_offset);
|
||||
#endif
|
||||
return ldfmt;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return the number of digits following the decimal point in NUMBUF */
|
||||
static int
|
||||
getprec (numbuf)
|
||||
const char *numbuf;
|
||||
{
|
||||
int p;
|
||||
char *dp;
|
||||
|
||||
if (dp = strchr (numbuf, decimal_point))
|
||||
dp++; /* skip over decimal point */
|
||||
for (p = 0; dp && *dp && ISDIGIT (*dp); dp++)
|
||||
p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Return the default format given FIRST, INCR, and LAST. */
|
||||
static char *
|
||||
genformat (first, incr, last)
|
||||
floatmax_t first, incr, last;
|
||||
{
|
||||
static char buf[6 + 2 * INT_STRLEN_BOUND (int)];
|
||||
int wfirst, wlast, width;
|
||||
int iprec, fprec, lprec, prec;
|
||||
|
||||
if (equal_width == 0)
|
||||
return (FLOATMAX_FMT);
|
||||
|
||||
/* OK, we have to figure out the largest number of decimal places. This is
|
||||
a little more expensive than using the original strings. */
|
||||
snprintf (buf, sizeof (buf), FLOATMAX_FMT, incr);
|
||||
iprec = getprec (buf);
|
||||
|
||||
wfirst = snprintf (buf, sizeof (buf), FLOATMAX_FMT, first);
|
||||
fprec = getprec (buf);
|
||||
|
||||
prec = MAX (fprec, iprec);
|
||||
|
||||
wlast = snprintf (buf, sizeof (buf), FLOATMAX_FMT, last);
|
||||
lprec = getprec (buf);
|
||||
|
||||
/* increase first width by any increased precision in increment */
|
||||
wfirst += (prec - fprec);
|
||||
|
||||
/* adjust last width to use precision from first/incr */
|
||||
wlast += (prec - lprec);
|
||||
|
||||
if (lprec && prec == 0)
|
||||
wlast--; /* no decimal point */
|
||||
if (lprec == 0 && prec)
|
||||
wlast++; /* include decimal point */
|
||||
if (fprec == 0 && prec)
|
||||
wfirst++; /* include decimal point */
|
||||
|
||||
width = MAX (wfirst, wlast);
|
||||
if (width)
|
||||
sprintf (buf, "%%0%d.%d%sf", width, prec, FLOATMAX_CONV);
|
||||
else
|
||||
sprintf (buf, "%%.%d%sf", prec, FLOATMAX_CONV);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int
|
||||
print_fltseq (fmt, first, last, incr)
|
||||
const char *fmt;
|
||||
floatmax_t first, last, incr;
|
||||
{
|
||||
int n;
|
||||
floatmax_t next;
|
||||
const char *s;
|
||||
|
||||
n = 0; /* interation counter */
|
||||
s = "";
|
||||
for (next = first; incr >= 0 ? (next <= last) : (next >= last); next = first + n * incr)
|
||||
{
|
||||
QUIT;
|
||||
if (*s && fputs (s, stdout) == EOF)
|
||||
return (sh_chkwrite (EXECUTION_FAILURE));
|
||||
if (printf (fmt, next) < 0)
|
||||
return (sh_chkwrite (EXECUTION_FAILURE));
|
||||
s = separator;
|
||||
n++;
|
||||
}
|
||||
|
||||
if (n > 0 && fputs (terminator, stdout) == EOF)
|
||||
return (sh_chkwrite (EXECUTION_FAILURE));
|
||||
return (sh_chkwrite (EXECUTION_SUCCESS));
|
||||
}
|
||||
|
||||
/* must be <= INT_STRLEN_BOUND(intmax_t) */
|
||||
int
|
||||
width_needed (num)
|
||||
intmax_t num;
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = num < 0; /* sign */
|
||||
if (ret)
|
||||
num = -num;
|
||||
do
|
||||
ret++;
|
||||
while (num /= 10);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
print_intseq (ifirst, ilast, iincr)
|
||||
intmax_t ifirst, ilast, iincr;
|
||||
{
|
||||
char intwfmt[6 + INT_STRLEN_BOUND(int) + sizeof (PRIdMAX)];
|
||||
const char *s;
|
||||
intmax_t i, next;
|
||||
|
||||
/* compute integer format string */
|
||||
if (equal_width) /* -w supplied */
|
||||
{
|
||||
int wfirst, wlast, width;
|
||||
|
||||
wfirst = width_needed (ifirst);
|
||||
wlast = width_needed (ilast);
|
||||
width = MAX(wfirst, wlast);
|
||||
|
||||
/* The leading %s is for the separator */
|
||||
snprintf (intwfmt, sizeof (intwfmt), "%%s%%0%u" PRIdMAX, width);
|
||||
}
|
||||
|
||||
/* We could use braces.c:mkseq here but that allocates lots of memory */
|
||||
s = "";
|
||||
for (i = ifirst; (ifirst <= ilast) ? (i <= ilast) : (i >= ilast); i = next)
|
||||
{
|
||||
QUIT;
|
||||
/* The leading %s is for the separator */
|
||||
if (printf (equal_width ? intwfmt : "%s%" PRIdMAX, s, i) < 0)
|
||||
return (sh_chkwrite (EXECUTION_FAILURE));
|
||||
s = separator;
|
||||
next = i + iincr;
|
||||
}
|
||||
|
||||
if (fputs (terminator, stdout) == EOF)
|
||||
return (sh_chkwrite (EXECUTION_FAILURE));
|
||||
return (sh_chkwrite (EXECUTION_SUCCESS));
|
||||
}
|
||||
|
||||
int
|
||||
seq_builtin (list)
|
||||
WORD_LIST *list;
|
||||
{
|
||||
floatmax_t first, last, incr;
|
||||
intmax_t ifirst, ilast, iincr;
|
||||
WORD_LIST *l;
|
||||
int opt, nargs, intseq, freefmt;
|
||||
char *first_str, *incr_str, *last_str;
|
||||
char const *fmtstr; /* The printf(3) format used for output. */
|
||||
|
||||
equal_width = 0;
|
||||
separator = "\n";
|
||||
fmtstr = NULL;
|
||||
|
||||
first = 1.0;
|
||||
last = 0.0;
|
||||
incr = 0.0; /* set later */
|
||||
ifirst = ilast = iincr = 0;
|
||||
first_str = incr_str = last_str = 0;
|
||||
|
||||
intseq = freefmt = 0;
|
||||
opt = 0;
|
||||
|
||||
reset_internal_getopt ();
|
||||
while (opt != -1)
|
||||
{
|
||||
l = lcurrent ? lcurrent : list;
|
||||
if (l && l->word && l->word->word && l->word->word[0] == '-' &&
|
||||
(l->word->word[1] == '.' || DIGIT (l->word->word[1])))
|
||||
{
|
||||
loptend = l;
|
||||
break; /* negative number */
|
||||
}
|
||||
if ((opt = internal_getopt (list, "f:s:w")) == -1)
|
||||
break;
|
||||
|
||||
switch (opt)
|
||||
{
|
||||
case 'f':
|
||||
fmtstr = list_optarg;
|
||||
break;
|
||||
case 's':
|
||||
separator = list_optarg;
|
||||
break;
|
||||
case 'w':
|
||||
equal_width = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
}
|
||||
list = loptend;
|
||||
|
||||
if (list == 0)
|
||||
{
|
||||
builtin_usage ();
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
for (nargs = 1, l = list; l->next; l = l->next)
|
||||
nargs++;
|
||||
if (nargs > 3)
|
||||
{
|
||||
builtin_usage ();
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
/* LAST */
|
||||
conversion_error = 0;
|
||||
last = getfloatmax (last_str = l->word->word);
|
||||
if (conversion_error)
|
||||
return (EXECUTION_FAILURE);
|
||||
|
||||
/* FIRST LAST */
|
||||
if (nargs > 1)
|
||||
{
|
||||
conversion_error = 0;
|
||||
first = getfloatmax (first_str = list->word->word);
|
||||
if (conversion_error)
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
/* FIRST INCR LAST */
|
||||
if (nargs > 2)
|
||||
{
|
||||
conversion_error = 0;
|
||||
incr = getfloatmax (incr_str = list->next->word->word);
|
||||
if (conversion_error)
|
||||
return (EXECUTION_FAILURE);
|
||||
if (incr == 0.0)
|
||||
{
|
||||
builtin_error ("zero %screment", (first < last) ? "in" : "de");
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Sanitize arguments */
|
||||
if (incr == 0.0)
|
||||
incr = (first <= last) ? 1.0 : -1.0;
|
||||
if ((incr < 0.0 && first < last) || (incr > 0 && first > last))
|
||||
{
|
||||
builtin_error ("incorrect %screment", (first < last) ? "in" : "de");
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
/* validate format here */
|
||||
if (fmtstr)
|
||||
{
|
||||
fmtstr = long_double_format (fmtstr);
|
||||
freefmt = 1;
|
||||
if (fmtstr == 0)
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
if (fmtstr != NULL && equal_width)
|
||||
{
|
||||
builtin_warning ("-w ignored when the format string is specified");
|
||||
equal_width = 0;
|
||||
}
|
||||
|
||||
/* Placeholder for later additional conditions */
|
||||
if (last_str && all_digits (last_str) &&
|
||||
(first_str == 0 || all_digits (first_str)) &&
|
||||
(incr_str == 0 || all_digits (incr_str)) &&
|
||||
fmtstr == NULL)
|
||||
intseq = 1;
|
||||
|
||||
if (intseq)
|
||||
{
|
||||
ifirst = (intmax_t)first; /* truncation */
|
||||
ilast = (intmax_t)last;
|
||||
iincr = (intmax_t)incr;
|
||||
|
||||
return (print_intseq (ifirst, ilast, iincr));
|
||||
}
|
||||
|
||||
decimal_point = locale_decpoint ();
|
||||
if (fmtstr == NULL)
|
||||
fmtstr = genformat (first, incr, last);
|
||||
|
||||
print_fltseq (fmtstr, first, last, incr);
|
||||
|
||||
if (freefmt)
|
||||
free ((void *)fmtstr);
|
||||
return sh_chkwrite (EXECUTION_SUCCESS);
|
||||
}
|
||||
|
||||
/* Taken largely from GNU seq. */
|
||||
char *seq_doc[] = {
|
||||
"Print numbers from FIRST to LAST, in steps of INCREMENT.",
|
||||
"",
|
||||
"-f FORMAT use printf style floating-point FORMAT",
|
||||
"-s STRING use STRING to separate numbers (default: \\n)",
|
||||
"-w equalize width by padding with leading zeroes",
|
||||
"",
|
||||
"If FIRST or INCREMENT is omitted, it defaults to 1. However, an",
|
||||
"omitted INCREMENT defaults to -1 when LAST is smaller than FIRST.",
|
||||
"The sequence of numbers ends when the sum of the current number and",
|
||||
"INCREMENT would become greater than LAST.",
|
||||
"FIRST, INCREMENT, and LAST are interpreted as floating point values.",
|
||||
"",
|
||||
"FORMAT must be suitable for printing one argument of type 'double';",
|
||||
"it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point",
|
||||
"decimal numbers with maximum precision PREC, and to %g otherwise.",
|
||||
(char *)NULL
|
||||
};
|
||||
|
||||
struct builtin seq_struct = {
|
||||
"seq",
|
||||
seq_builtin,
|
||||
BUILTIN_ENABLED,
|
||||
seq_doc,
|
||||
"seq [-f format] [-s separator] [-w] [FIRST [INCR]] LAST",
|
||||
0
|
||||
};
|
||||
@@ -48,13 +48,6 @@
|
||||
#include "builtins.h"
|
||||
#include "common.h"
|
||||
|
||||
#define RETURN(x) \
|
||||
do { \
|
||||
if (sp) *sp = sec; \
|
||||
if (usp) *usp = usec; \
|
||||
return (x); \
|
||||
} while (0)
|
||||
|
||||
int
|
||||
sleep_builtin (list)
|
||||
WORD_LIST *list;
|
||||
|
||||
@@ -0,0 +1,430 @@
|
||||
/* stat - load up an associative array with stat information about a file */
|
||||
|
||||
/* See Makefile for compilation details. */
|
||||
|
||||
/*
|
||||
Copyright (C) 2016 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash.
|
||||
Bash is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Bash is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Bash. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#if defined (HAVE_UNISTD_H)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include "posixstat.h"
|
||||
#include <stdio.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <errno.h>
|
||||
#include "posixtime.h"
|
||||
|
||||
#include "bashansi.h"
|
||||
#include "shell.h"
|
||||
#include "builtins.h"
|
||||
#include "common.h"
|
||||
#include "bashgetopt.h"
|
||||
|
||||
#ifndef errno
|
||||
extern int errno;
|
||||
#endif
|
||||
|
||||
#define ST_NAME 0
|
||||
#define ST_DEV 1
|
||||
#define ST_INO 2
|
||||
#define ST_MODE 3
|
||||
#define ST_NLINK 4
|
||||
#define ST_UID 5
|
||||
#define ST_GID 6
|
||||
#define ST_RDEV 7
|
||||
#define ST_SIZE 8
|
||||
#define ST_ATIME 9
|
||||
#define ST_MTIME 10
|
||||
#define ST_CTIME 11
|
||||
#define ST_BLKSIZE 12
|
||||
#define ST_BLOCKS 13
|
||||
#define ST_CHASELINK 14
|
||||
#define ST_PERMS 15
|
||||
|
||||
#define ST_END 16
|
||||
|
||||
static char *arraysubs[] =
|
||||
{
|
||||
"name", "device", "inode", "type", "nlink", "uid", "gid", "rdev",
|
||||
"size", "atime", "mtime", "ctime", "blksize", "blocks", "link", "perms",
|
||||
0
|
||||
};
|
||||
|
||||
static int
|
||||
getstat (fname, flags, sp)
|
||||
const char *fname;
|
||||
int flags;
|
||||
struct stat *sp;
|
||||
{
|
||||
intmax_t lfd;
|
||||
int fd, r;
|
||||
|
||||
if (strncmp (fname, "/dev/fd/", 8) == 0)
|
||||
{
|
||||
if ((legal_number(fname + 8, &lfd) == 0) || (int)lfd != lfd)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
fd = lfd;
|
||||
r = fstat(fd, sp);
|
||||
}
|
||||
#ifdef HAVE_LSTAT
|
||||
else if (flags & 1)
|
||||
r = lstat(fname, sp);
|
||||
#endif
|
||||
else
|
||||
r = stat(fname, sp);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static char *
|
||||
statlink (fname, sp)
|
||||
char *fname;
|
||||
struct stat *sp;
|
||||
{
|
||||
#if defined (HAVE_READLINK)
|
||||
char linkbuf[PATH_MAX];
|
||||
int n;
|
||||
|
||||
if (fname && S_ISLNK (sp->st_mode) && (n = readlink (fname, linkbuf, PATH_MAX)) > 0)
|
||||
{
|
||||
linkbuf[n] = '\0';
|
||||
return (savestring (linkbuf));
|
||||
}
|
||||
else
|
||||
#endif
|
||||
return (savestring (fname));
|
||||
}
|
||||
|
||||
static char *
|
||||
octalperms (m)
|
||||
int m;
|
||||
{
|
||||
int operms;
|
||||
char *ret;
|
||||
|
||||
operms = 0;
|
||||
|
||||
if (m & S_IRUSR)
|
||||
operms |= 0400;
|
||||
if (m & S_IWUSR)
|
||||
operms |= 0200;
|
||||
if (m & S_IXUSR)
|
||||
operms |= 0100;
|
||||
|
||||
if (m & S_IRGRP)
|
||||
operms |= 0040;
|
||||
if (m & S_IWGRP)
|
||||
operms |= 0020;
|
||||
if (m & S_IXGRP)
|
||||
operms |= 0010;
|
||||
|
||||
if (m & S_IROTH)
|
||||
operms |= 0004;
|
||||
if (m & S_IWOTH)
|
||||
operms |= 0002;
|
||||
if (m & S_IXOTH)
|
||||
operms |= 0001;
|
||||
|
||||
if (m & S_ISUID)
|
||||
operms |= 04000;
|
||||
if (m & S_ISGID)
|
||||
operms |= 02000;
|
||||
if (m & S_ISVTX)
|
||||
operms |= 01000;
|
||||
|
||||
ret = (char *)xmalloc (16);
|
||||
snprintf (ret, 16, "%04o", operms);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *
|
||||
statperms (m)
|
||||
int m;
|
||||
{
|
||||
char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
|
||||
int i;
|
||||
char *ret;
|
||||
|
||||
i = 0;
|
||||
if (m & S_IRUSR)
|
||||
ubits[i++] = 'r';
|
||||
if (m & S_IWUSR)
|
||||
ubits[i++] = 'w';
|
||||
if (m & S_IXUSR)
|
||||
ubits[i++] = 'x';
|
||||
ubits[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
if (m & S_IRGRP)
|
||||
gbits[i++] = 'r';
|
||||
if (m & S_IWGRP)
|
||||
gbits[i++] = 'w';
|
||||
if (m & S_IXGRP)
|
||||
gbits[i++] = 'x';
|
||||
gbits[i] = '\0';
|
||||
|
||||
i = 0;
|
||||
if (m & S_IROTH)
|
||||
obits[i++] = 'r';
|
||||
if (m & S_IWOTH)
|
||||
obits[i++] = 'w';
|
||||
if (m & S_IXOTH)
|
||||
obits[i++] = 'x';
|
||||
obits[i] = '\0';
|
||||
|
||||
if (m & S_ISUID)
|
||||
ubits[2] = (m & S_IXUSR) ? 's' : 'S';
|
||||
if (m & S_ISGID)
|
||||
gbits[2] = (m & S_IXGRP) ? 's' : 'S';
|
||||
if (m & S_ISVTX)
|
||||
obits[2] = (m & S_IXOTH) ? 't' : 'T';
|
||||
|
||||
ret = (char *)xmalloc (32);
|
||||
snprintf (ret, 32, "u=%s,g=%s,o=%s", ubits, gbits, obits);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *
|
||||
statmode(mode)
|
||||
int mode;
|
||||
{
|
||||
char *modestr, *m;
|
||||
|
||||
modestr = m = (char *)xmalloc (8);
|
||||
if (S_ISBLK (mode))
|
||||
*m++ = 'b';
|
||||
if (S_ISCHR (mode))
|
||||
*m++ = 'c';
|
||||
if (S_ISDIR (mode))
|
||||
*m++ = 'd';
|
||||
if (S_ISREG(mode))
|
||||
*m++ = '-';
|
||||
if (S_ISFIFO(mode))
|
||||
*m++ = 'p';
|
||||
if (S_ISLNK(mode))
|
||||
*m++ = 'l';
|
||||
if (S_ISSOCK(mode))
|
||||
*m++ = 's';
|
||||
|
||||
#ifdef S_ISDOOR
|
||||
if (S_ISDOOR (mode))
|
||||
*m++ = 'D';
|
||||
#endif
|
||||
#ifdef S_ISWHT
|
||||
if (S_ISWHT(mode))
|
||||
*m++ = 'W';
|
||||
#endif
|
||||
#ifdef S_ISNWK
|
||||
if (S_ISNWK(mode))
|
||||
*m++ = 'n';
|
||||
#endif
|
||||
#ifdef S_ISMPC
|
||||
if (S_ISMPC (mode))
|
||||
*m++ = 'm';
|
||||
#endif
|
||||
|
||||
*m = '\0';
|
||||
return (modestr);
|
||||
}
|
||||
|
||||
static char *
|
||||
stattime (t)
|
||||
time_t t;
|
||||
{
|
||||
char *tbuf, *ret;
|
||||
size_t tlen;
|
||||
|
||||
tbuf = ctime (&t);
|
||||
tlen = strlen (tbuf);
|
||||
ret = savestring (tbuf);
|
||||
ret[tlen-1] = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
static char *
|
||||
statval (which, fname, flags, sp)
|
||||
int which;
|
||||
char *fname;
|
||||
int flags;
|
||||
struct stat *sp;
|
||||
{
|
||||
int temp;
|
||||
|
||||
switch (which)
|
||||
{
|
||||
case ST_NAME:
|
||||
return savestring (fname);
|
||||
case ST_DEV:
|
||||
return itos (sp->st_dev);
|
||||
case ST_INO:
|
||||
return itos (sp->st_ino);
|
||||
case ST_MODE:
|
||||
return (statmode (sp->st_mode));
|
||||
case ST_NLINK:
|
||||
return itos (sp->st_nlink);
|
||||
case ST_UID:
|
||||
return itos (sp->st_uid);
|
||||
case ST_GID:
|
||||
return itos (sp->st_gid);
|
||||
case ST_RDEV:
|
||||
return itos (sp->st_rdev);
|
||||
case ST_SIZE:
|
||||
return itos (sp->st_size);
|
||||
case ST_ATIME:
|
||||
return ((flags & 2) ? stattime (sp->st_atime) : itos (sp->st_atime));
|
||||
case ST_MTIME:
|
||||
return ((flags & 2) ? stattime (sp->st_mtime) : itos (sp->st_mtime));
|
||||
case ST_CTIME:
|
||||
return ((flags & 2) ? stattime (sp->st_ctime) : itos (sp->st_ctime));
|
||||
case ST_BLKSIZE:
|
||||
return itos (sp->st_blksize);
|
||||
case ST_BLOCKS:
|
||||
return itos (sp->st_blocks);
|
||||
case ST_CHASELINK:
|
||||
return (statlink (fname, sp));
|
||||
case ST_PERMS:
|
||||
temp = sp->st_mode & (S_IRWXU|S_IRWXG|S_IRWXO|S_ISUID|S_ISGID);
|
||||
return (flags & 2) ? statperms (temp) : octalperms (temp);
|
||||
default:
|
||||
return savestring ("42");
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
loadstat (vname, var, fname, flags, sp)
|
||||
char *vname;
|
||||
SHELL_VAR *var;
|
||||
char *fname;
|
||||
int flags;
|
||||
struct stat *sp;
|
||||
{
|
||||
int i;
|
||||
char *key, *value;
|
||||
SHELL_VAR *v;
|
||||
|
||||
for (i = 0; arraysubs[i]; i++)
|
||||
{
|
||||
key = savestring (arraysubs[i]);
|
||||
value = statval (i, fname, flags, sp);
|
||||
v = bind_assoc_variable (var, vname, key, value, ASS_FORCE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
stat_builtin (list)
|
||||
WORD_LIST *list;
|
||||
{
|
||||
int opt, flags;
|
||||
char *aname, *fname;
|
||||
struct stat st;
|
||||
SHELL_VAR *v;
|
||||
|
||||
aname = "STAT";
|
||||
flags = 0;
|
||||
|
||||
reset_internal_getopt ();
|
||||
while ((opt = internal_getopt (list, "A:Ll")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'A':
|
||||
aname = list_optarg;
|
||||
break;
|
||||
case 'L':
|
||||
flags |= 1; /* operate on links rather than resolving them */
|
||||
break;
|
||||
case 'l':
|
||||
flags |= 2;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
}
|
||||
|
||||
list = loptend;
|
||||
if (list == 0)
|
||||
{
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
}
|
||||
|
||||
fname = list->word->word;
|
||||
|
||||
if (getstat (fname, flags, &st) < 0)
|
||||
{
|
||||
builtin_error ("%s: cannot stat: %s", fname, strerror (errno));
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
unbind_variable (aname);
|
||||
v = make_new_assoc_variable (aname);
|
||||
if (v == 0)
|
||||
{
|
||||
builtin_error ("%s: cannot create variable", aname);
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
if (loadstat (aname, v, fname, flags, &st) < 0)
|
||||
{
|
||||
builtin_error ("%s: cannot assign file status information", aname);
|
||||
unbind_variable (aname);
|
||||
return (EXECUTION_FAILURE);
|
||||
}
|
||||
|
||||
return (EXECUTION_SUCCESS);
|
||||
}
|
||||
|
||||
/* An array of strings forming the `long' documentation for a builtin xxx,
|
||||
which is printed by `help xxx'. It must end with a NULL. By convention,
|
||||
the first line is a short description. */
|
||||
char *stat_doc[] = {
|
||||
"Load an associative array with file status information.",
|
||||
"",
|
||||
"Take a filename and load the status information returned by a",
|
||||
"stat(2) call on that file into the associative array specified",
|
||||
"by the -A option. The default array name is STAT. If the -L",
|
||||
"option is supplied, stat does not resolve symbolic links and",
|
||||
"reports information about the link itself. The -l option results",
|
||||
"in longer-form listings for some of the fields. The exit status is 0",
|
||||
"unless the stat fails or assigning the array is unsuccessful.",
|
||||
(char *)NULL
|
||||
};
|
||||
|
||||
/* The standard structure describing a builtin command. bash keeps an array
|
||||
of these structures. The flags must include BUILTIN_ENABLED so the
|
||||
builtin can be used. */
|
||||
struct builtin stat_struct = {
|
||||
"stat", /* builtin name */
|
||||
stat_builtin, /* function implementing the builtin */
|
||||
BUILTIN_ENABLED, /* initial flags for builtin */
|
||||
stat_doc, /* array of long documentation strings. */
|
||||
"stat [-lL] [-A aname] file", /* usage synopsis; becomes short_doc */
|
||||
0 /* reserved for internal use */
|
||||
};
|
||||
@@ -84,6 +84,7 @@ tee_builtin (list)
|
||||
case 'i':
|
||||
nointr = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -31,6 +31,7 @@ template_builtin (list)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -46,6 +46,7 @@ tty_builtin (list)
|
||||
case 's':
|
||||
sflag = 1;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -95,6 +95,7 @@ uname_builtin (list)
|
||||
case 'v':
|
||||
uname_flags |= FLAG_VERSION;
|
||||
break;
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
@@ -39,6 +39,7 @@ whoami_builtin (list)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
CASE_HELPOPT;
|
||||
default:
|
||||
builtin_usage ();
|
||||
return (EX_USAGE);
|
||||
|
||||
Reference in New Issue
Block a user