Commit Graph

4014 Commits

Author SHA1 Message Date
simonb c115fd08c1 Set totals.f_frsize to DEV_BSIZE instead of totals.f_bsize so that
addstat() uses an initialised value for total size calculations.
Fixes core dump for "df -c".
2021-11-29 05:59:58 +00:00
kre 16d8557100 PR bin/53550
Here we go again...   One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen).   Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process.  This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost.   We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs.   Now we're all the way with LBJ ... (or something like that).
2021-11-22 05:17:43 +00:00
kre 9b13765f1a Improve the however-many-negatives wording even more. 2021-11-21 16:23:20 +00:00
rillig 87e5866877 sh.1: replace triple negation with single negation, fix typo 2021-11-20 17:18:31 +00:00
kre c426788907 Improve the wording of the "Argument List Processing" section (where
all the sh options, also used with "set", are listed) in response to
a discussion on icb conveyed to me by Darrin B. Jewell.
A few improvements to the description of the "set" built-in as well.

Bump Dd to cover all of this month's changes (so far).
2021-11-20 01:52:51 +00:00
rillig 4c9c11dcf9 sh.1: fix typos 2021-11-16 23:39:34 +00:00
rillig 982b8df7ec echo: fix lint error due to strict bool mode
No functional change.
2021-11-16 21:38:29 +00:00
kre b6dd340d3c Make pwd (both /bin/pwd and the /bin/sh built-in version) check for
write errors on stdout, and indicate an error if that happens.
2021-11-16 16:57:15 +00:00
kre 8ea2ac47d5 PR bin/56491
Make "hash" exit(!=0) (ie: exit(1)) if it writes an error message to
stderr as required by POSIX (it was writing "not found" errors, yet
still doing exit(0)).

Whether, when doing "hash foobar", and "foobar" is not found as a command
(not a built-in, not a function, and not found via a PATH search), that
should be considered an error differs between shells.  All of the ksh
descendant shells say "no", write no error message in this case, and
exit(0) if no other errors occur.   Other shells (essentially all) do
consider it an error, write a message to stderr, and exit(1) when this happens.

POSIX isn't clear, the bug report:
     https://austingroupbugs.net/view.php?id=1460
which is not yet resolved, suggests that the outcome will be that
this is to be unspecified.   Given the diversity, there might be no
other choice.

Have a foot in both camps - default to the "other shell" behaviour,
but add a -e option (no errors ... applies only to these "not found"
errors) to generate the ksh behaviour.   Without other errors (like an
unknown option, etc) "hash -e anyname" will always exit(0).

See the PR for details on how it all works now, or read the updated man page.

While here, when hash is in its other mode (reporting what is in the
table) check for I/O errors on stdout, and exit(1) (with an error
message!) if any occurred.   This does not apply to output generated
by the -v option when command names are given (that output is incidental).

In sh.1 document all of this.   Also add documentation for a bunch of
other options the hash command has had for years, but which were never
documented.   And while there, clean up some other sections I noticed
needed improving (either formatting or content or both).
2021-11-16 11:28:29 +00:00
kre b65a5b9068 Detect write errors to stdout, and exit(1) from some built-in
commands which (primarily) are used just to generate output
(or with a particular option combination do so).
2021-11-16 11:27:50 +00:00
kre a41dbcb01d Fix value of ${LINENO} in "for" commands.
This affects (as best I can tell) only uses of ${LINENO} in PS4
when -x is enabled (and perhaps only when the list contains no
expansions).   "for" like "case" (which was already handled) is
special in that it generates trace output before actually executing
any kind of simple command.
2021-11-16 11:25:44 +00:00
kre a18d86b7be Ensure that all of the POSIX standard utilities are correctly
identified with the -u flag (that is, I hope I identified all
the ones that were missing it).

This change is a no-op (NFC) as the -u flag does nothing.

Still, just in case we find a use for it one day, and just as a
matter of general principle, we should get this correct.
2021-11-10 18:25:52 +00:00
kre 40f303e024 With -Wall compiling this was giving:
echo.c: In function 'main':
	echo.c:74:1: warning: control reaches end of non-void function

This raises 2 issues.

First, why with WARNS set to 6, which should include just about
everything, was this not causing problems with everyday builds?
Surely falling off the end of a non-void function without returning
a specific value is one of the more basic errors that should be fixed.
(Whatever the name of the function).   Is there a missing -Wxxx option?

And second, does C99 really promise:
	Remove unnecessary call to exit(0); returning from main is equivalent
	since C99.
in the sense that simply falling out of main() is exit(0)?  Or is it
simply saying that the return value of main() is the exit status (which
has been true for much longer than since c99)?
2021-11-10 17:35:45 +00:00
kre a3710ae449 Add a couple of macro definitions for dealing with shell I/O errors.
(Macros akin to stdio's ferror() and clearerr()).

NFC: the macros are defined, but not yet used anywhere.   Uses coming soonish.
2021-11-10 15:58:38 +00:00
kre b8bee70d9c DEBUG mode changes only. NFC (NC) for any normally compiled shell.
Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
2021-11-10 15:26:34 +00:00
msaitoh f81569c702 s/writting/writing/ 2021-11-06 06:40:33 +00:00
msaitoh 763440c947 s/maxiumum/maximum/ 2021-11-06 06:38:03 +00:00
gutteridge 45137c4698 pax.1: minor grammar fixes 2021-11-06 01:19:19 +00:00
nia 56978e9154 ed(1): use reallocarr instead of realloc(x * y) 2021-11-02 08:04:20 +00:00
kre 609916a1bc PR bin/45390
Be explicit about what happens to PWD after a successful cd command.
Also be very clear  that "cd" and "cd -P" are the same thing, and
the only cd variant implemented.

Also, when it is appropriate to print the new directory after a cd
command, note that it happens if interactive (as it always has here)
and also if the posix option is set (for POSIX compat, where "interactive"
is irrelevant).  Mention that "cd -" is a case where the new directory
is printed (along with paths relative to a non-empty CDPATH entry,
and where the "cd old new" (string replacement in curdir) is used.

While here document the new -e option to cd.

XXX pullup -9
2021-10-31 02:12:08 +00:00
kre 82386044cc PR bin/45390 - fix for folly four
In the pwd builtin, verify that curdir names '.' before
simply printing it.   Never alter PWD or OLDPWD in the
pwd command.

Also while here, implement the (new: coming in POSIX, but has existed
for a while in several other shells) -e option to cd (with -e, cd -P
will exit(1) if the chdir() succeeds, but PWD cannot be discovered).

cd now prints the directory name used (if different from that given,
or cdprint is on) if interactive or (the new bit)in posix mode.

Some additional/changed comments added, and a DEBUG mode trace call
that was accidentally put inside an #if 0 block moved to where it
can do some good.

XXX pullup -9
2021-10-31 02:12:01 +00:00
andvar 56d66eb030 "no affect" -> "no effect" in man pages and comments. 2021-10-30 21:08:58 +00:00
kre 4a9058f97b Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type.   Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.

Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack).  Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
2021-10-26 10:07:20 +00:00
kre 4ca0efa9b1 PR bin/56464
After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...)   All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges.  Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9   (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
2021-10-26 00:05:38 +00:00
rillig 27943b9954 echo: clean up, increase WARNS to 6
Lint can handle __COPYRIGHT and __RCSID, so there is no need to hide
them anymore.

Use proper type 'bool' for nflag, ensure correct types via lint's strict
bool mode.

Remove unnecessary call to exit(0); returning from main is equivalent
since C99.

No functional change.
2021-10-10 19:07:19 +00:00
rillig 4453f5596f sh: remove useless do-while-0 loop
28 years was more than enough for the useless 'continue' statement in
this do-while-0 "loop". Without the 'continue' statement, there is no
need for the "loop" anymore. The comment at its top was confusing since
the word 'while' suggested a loop, but there was none, so remove that as
well.

Pointed out by Tom Ivar Helbekkmo on source-changes-d.

No change to the resulting binary.
2021-10-10 18:46:25 +00:00
rillig 074335e1f1 sh: remove no-op 'continue' from do-while-0 loop
With Clang, the only change to the binary are the line number changes
from __LINE__, GCC generates a bit different code.

No functional change.
2021-10-10 08:35:34 +00:00
rillig 16296115f5 sh: make find_command simpler
Lint complained about the do-while-0 loop that contained a continue. It
didn't state the reason for it, but indeed the code looked complicated.
Rewrite the code to be less verbose and to use common coding patterns.

No functional change.
2021-10-10 08:19:02 +00:00
rillig ac96e5dd30 dd: fix lint warnings about effectively discarding 'const'
args.c(153): warning: call to 'bsearch' effectively discards 'const'
    from argument [346]
args.c(383): dito
args.c(475): dito

No functional change.
2021-10-09 21:31:36 +00:00
rillig 3e5654ee02 bin: in builds with MKLINT=yes, run lint on all programs 2021-10-09 21:06:31 +00:00
rillig c59e0a480e sh: ignore lint error about 'continue' in 'do while' loop
exec.c(575): error: continue in 'do ... while (0)' loop [323]
jobs.c(203): error: continue in 'do ... while (0)' loop [323]

It is certainly a rarely used feature, I saw it the first time today and
had to look up its meaning in the C standard. But after that, I don't
see why a 'continue' statement in a 'do while' loop should be an error.
Maybe a warning since up to now I thought that 'continue' would jump
back to the top of the loop, while it really jumps to the bottom of the
loop body, for all 3 kinds of loops.
2021-10-09 21:02:53 +00:00
rillig 4cea8ecbb7 dd: clean up function summary
No functional change. Ideally the binary would have stayed the same, but
GCC 10.3.0 inserts a nop and reorders the code.
2021-10-09 20:44:55 +00:00
rillig 2dcff222a4 dd: fix lint error about 'return expr' from void function
No change to the resulting binary. The 'return' statements are necessary
for GCC to generate the exact same object code, even though they can be
removed without affecting the functionality, as seen before the 'else'.
2021-10-09 20:29:23 +00:00
christos 45fcea9556 PR/56443: RVP: dd conv=swab doesn't always work 2021-10-08 14:44:48 +00:00
christos 1b2514eee4 It is either teaching gcc about aaah or adding FALLTHROUGH. 2021-09-16 19:44:01 +00:00
christos b8d28f3621 support -Z 2021-09-16 19:43:33 +00:00
christos e79ccca6dc provide a way to reset setproctitle. 2021-09-16 19:34:21 +00:00
kre 51a7b24283 Fix an ordering error in the previous (and even earlier, going back
a way, but made more serious with the recent changes).

The n>&n operation (more or less a no-op, except it clears CLOEXEC)
should precede almost everything else - and simply be made to fail if
an attempt is made to apply it to a sh internal fd.

We were renumbering the internal fd (the n> part considered first)
which was dumb, but OK, before, but now rejecting the operation
(the >&n) part when n should not be visible to the script.  That
made something of a mess (and could lead to the shell believing its
job control tty was at a fd it never got moved to).

Do things in the correct order, and simply fail that case for internal
fds (for every other n>xxx for any xxx sh simply renumbers its internal fd
n to some other fd before attempting the operation, even n>&- ... those are
all fine).

[In all the above the '>' is used in place of any redirect operator].
2021-09-15 20:21:47 +00:00
kre c15b76c2fd Have the ulimit command watch for ulimit -n (alter number of available fds)
and keep the rest of the shell aware of any changes.

While here, modify 'ulimit -aSH' to print both the soft and hard limits
for the resources, rather than just (in this case, as H comes last) the
hard limit.   In any other case when both S and H are present, and we're
examining a limit, use the soft limit (just as if neither were given).

No change for setting limits (both are set, unless exactly one of -H
or -S is given).   However, we now check for overflow when converting
the value to be assigned, rather than just truncating the value however
it happens to work out...
2021-09-15 18:30:57 +00:00
kre 52967993c0 Improve the solution for the 2nd access to a fd which shouldn't
be available ("13") issue reported by Jan Schaumann on netbsd-users.

This fixes a bug in the earlier fix (a day or so ago) which could allow the
shell's idea of which fd range was in use by the script to get wildly
incorrect, but now also actually looks to see which fd's are in use as
renamed other user fd's during the lifetime of a redirection which needs
to be able to be undone (most redirections occur after a fork and are
permanent in the child process).   Attempting to access such a fd (as with
attempts to access a fd in use by the shell for its own purposes) is treated
as an attempt to access a closed fd (EBADF).  Attempting to reuse the fd
for some other purpose (which should be rare, even for scripts attempting
to cause problems, since the shell generally knows which fds the script
wants to use, and avoids them) will cause the renamed (renumbered) fd
to be renamed again (moved aside to some other available fd), just as
happens with the shell's private fds.

Also, when a generic fd is required, don't give up because of EMFILE
or similar unless there are no available fds at all (we might prefer >10
or bigger, but if there are none there, use anything).  This avoids
redirection errors when ulimit -n has been set small, and all the fds >10
that are available have been used, but we need somewhere to park the old
user of a fd while we reuse that fd for the redirection.
2021-09-15 18:29:45 +00:00
christos a83514c691 Don't assign v to newvar, so we can still access the original length. 2021-09-15 13:16:57 +00:00
christos bd3ed7c8b3 reflect reality on flags. 2021-09-14 22:14:11 +00:00
christos 9dc307c463 use emalloc and friends, add ktrace flag printing. 2021-09-14 22:01:17 +00:00
christos c879b655cb Provide symbolic printing of some keywords by capitalizing them. 2021-09-14 17:09:18 +00:00
christos 8298e812fc Quote the filenames like before 2021-09-14 15:04:09 +00:00
kre c88e74eff8 Deal with some issues where fds intended only for internal use
by the shell were available for manipulation by scripts (or the user).
These issues were reported by Jan Schaumann on netbsd-users.

The first allows the user to reference sh internal fds, and is
a simple fix - any sh internal fd is simply treated as if it were closed
when referenced by the script.  These fds can be discovered by
examining /proc/N/fd so it is not difficult for a script to discover
which fd it should attempt to access.

The second allows the user to reference a user level fd which is
one that is normally available to it, but at a point where it should
no longer be visible (when that fd has been redirected, for a built
in command, so the original fd needs to be saved so it can be restored,
the saving fd should not be accessible).   It is not as easy for the
script to determine which fd to attempt here, as the relevant one
exists only during the lifetime of a built-in command (and similar),
but there are ways in some cases (aside from looking at /proc from
another process).

Fix this one by watching which fds the user script is attempting
to use, and avoid using those as temporary fds.   This is possible in
this case as we know what command is being run, before we need to
save the fds it uses.   That's different from the earlier case where
when the shell allocates its fds we have no idea what it might
reference later.

Also clean up a couple of other minor code issues (NFC intended) that
I noticed while here...
2021-09-14 14:49:39 +00:00
kre 683de06b06 PR bin/56398
The final component of both a/b/c and a/b/c/ is "c", so that's the one
to which the -m arg applies.
2021-09-13 22:46:02 +00:00
wiz b90ee919d5 Mark up NULL with Dv. 2021-09-12 06:53:08 +00:00
kre bc2fe2a15e Improve the formatting of the list of Built-in commands for those
commands with multiple synopsis lines (eg: trap).

But there really must be a better way to achieve this effect than
the way it is accomplished here, and I'm hoping some wizard who
understands mdoc much better than I do will revert this change and
do it using some inspired magic incantation instead.
2021-09-12 02:20:36 +00:00
kre 777df0a3cd Don't dereference NULL on "jobs -Z" (with no title given), instead
do setproctitle(NULL) (which is not the same thing at all).  Do the
same with jobs -Z '' as setting the title to "sh: " isn't useful.

Improve the way this is documented, and note that it is only done
this way because zsh did it first (ie: pass on the balme, doing this
in the jobs command is simply absurd.)
2021-09-12 01:30:41 +00:00