Commit Graph

1864 Commits

Author SHA1 Message Date
Pieter Kockx
5b66e55a83 which: handle relative/absolute pathnames 2017-10-21 12:44:09 -07:00
Pieter Kockx
d15f49e715 which: fix whitespace 2017-10-21 12:44:09 -07:00
Pieter Kockx
6eec2eb3b4 tr: Fix infinite loop
When `makeset` got a string containing square brackets
followed by at least one extra character, e.g. "[abc]d",
it entered an infinite loop because it was assumed
`j` could not exceed `len` without having been equal to `len`.
It can, however, when `m == len` and subsequently `j = m + 1`.
2017-10-21 12:44:09 -07:00
David Phillips
e1c56a6321 chmod: Use DIRFIRST
Previously, running `chmod 777` on a directory that had no read or
execute access (e.g. 111 or 000) would cause chmod to throw its
toys since it was trying to opendir before having added read permission
to the directory.
2017-10-01 11:54:24 -07:00
David Phillips
69b9c2444b libutil/recurse: only opendir if recursing
Previous behaviour was to call opendir regardless of if we are actually going
to be recursing into the directory. Additionally, some utilities that use
DIRFIRST benefit from running the function pointed to by fn before the call
to opendir. One such example is `chmod [-R] 777 dir` on a directory with mode
000, where it will be expected for chmod to first give itself rwx before
optionally listing the directory to traverse it.
2017-10-01 09:34:47 -07:00
Michael Forney
e6d3032131 tar: Fix a few weprintf/eprintf format strings
Thanks to Jim Beveridge for spotting the incorrect utimensat error.

Turns out there are a few other instances of this in the vicinity.
2017-09-09 17:34:43 -07:00
Pieter Kockx
d90ba15f29 Fix segmentation fault in paste(1)
libutil/unescape received a pointer to a a string in
read-only memory.
2017-09-07 22:58:24 -07:00
Eric Pruitt
fb936d5ba5 Add missing getconf.h dependency to sbase-box 2017-09-05 23:00:42 -07:00
Michael Forney
b524eb6f3d getconf: Ensure output was written successfully 2017-09-03 17:26:04 -07:00
Michael Forney
c225d88d49 getconf: Report invalid variables as errors
Printing `undefined` to stdout means that the variable is valid but not
set. Instead, report an error in this case.

linux 4.13 appends the result of `getconf LFS_CFLAGS` to HOSTCFLAGS,
even if it prints `undefined`. This is arguably a bug in the Makefile,
but even so, getconf shouldn't report that unsupported variables are
valid.
2017-09-03 17:23:28 -07:00
John Vogel
ed78aef5b1 date: add date/time setting capability
[Michael Forney: Moved functionality to setdate, other minor tweaks]
2017-09-03 12:36:15 -07:00
Michael Forney
1b41610a82 date: Just set TZ to handle -u flag
This simplifies things a tiny bit, and is also necessary for setting the
date since mktime operates based on the value of TZ.
2017-09-03 11:39:12 -07:00
Michael Forney
772a40188b tar: Explain reason for malformed header 2017-08-22 13:35:29 +02:00
David Phillips
46495ea902 ls: abort a directory if we cannot opendir it
We should not try and perform operations on an invalid DIR* stream.
Instead, we shall let the error message be printed, and the return
code set (existing behaviour) and abort afterwards.
2017-08-22 13:33:40 +02:00
Laslo Hunhold
e92a1aef54 Handle { NULL } argv[] properly in manual arg-reduction
Thanks izabera for reporting this!
2017-08-05 23:50:39 +02:00
Michael Forney
ea8622a4ce tail: Process bytes with -c option, and add -m option for runes
POSIX says that -c specifies a number of bytes, not characters. This
flag is commonly used by scripts that operate on binary files to things
like extract a header. Treating the offsets as character offsets will
break things in mysterious ways.

Instead, add a -m option (chosen to match `wc -m`, which also operates
on characters) to handle character offsets.
2017-07-14 07:50:54 +02:00
Michael Forney
1ab4ac60ff tail: Use fstat in case file is removed 2017-07-14 07:50:52 +02:00
Michael Forney
51e432cc44 cp: Only call chmod with -p or -a
Previously, when the destination file was created with fopen, we needed
to use fchmod to set its permissions.

Now that we pass in the mode to creat, we already get the desired
behavior of creating the file with the same mode as the source file
modified by the user's file creation mask.

This fixes the issue where a directory or special file created with
mkdir/mknod does not end up with the appropriate mode with -p or -a
(since it may have been narrowed by the umask).

This also allows us to clear the SUID and SGID bits from the mode if the
chown fails, as specified by POSIX.
2017-07-14 07:50:50 +02:00
Michael Forney
3276fbea1c concat: Use plain read/write instead of buffered stdio
If we are just copying data from one file to another, we don't need to
fill a complete buffer, just read a chunk at a time, and write it to the
output.
2017-07-14 07:50:47 +02:00
Michael Forney
9a3b12525b Don't use buffered IO (fread) when not appropriate
fread reads the entire requested size (BUFSIZ), which causes tools to
block if only small amounts of data are available at a time. At best,
this causes unnecessary copies and inefficiency, at worst, tools like
tee and cat are almost unusable in some cases since they only display
large chunks of data at a time.
2017-07-03 21:04:14 +02:00
Michael Forney
5cb3a1eba1 libutil: Add writeall utility function
writeall makes successive write calls to write an entire buffer to the
output file descriptor. It returns the number of bytes written, or -1 on
the first error.
2017-07-03 21:04:12 +02:00
Michael Forney
529e50a7ad mkdir: Fix created directory permissions
Previously, with -p, the specified directory and all of its parents
would be 0777&~filemask (regardless of the -m flag). POSIX says parent
directories must created as (0300|~filemask)&0777, and of course if -m
is set, the specified directory should be created with those
permissions.

Additionally, POSIX says that for symbolic_mode strings, + and - should
be interpretted relative to a default mode of 0777 (not 0).

Without -p, previously the directory would be created first with
0777&~filemask (before a chmod), but POSIX says that the directory shall
at no point in time have permissions less restrictive than the -m mode
argument.

Rather than dealing with mkdir removing the filemask bits by calling
chmod afterward, just clear the umask and remove the bits manually.
2017-07-03 21:03:11 +02:00
Michael Forney
6ac5f01cc9 mkdir -p: Fail if argument exists, but is not a directory
If it is a directory, we can just return straightaway.
2017-07-03 21:03:09 +02:00
Michael Forney
e795946971 mkdir: Fail on EEXIST with no -p flag
mkdir without the -p flag should only exit success if it actually made
the directory.
2017-07-03 21:03:07 +02:00
Michael Forney
830ca05c25 du: Don't print 0 entry if stat of named file fails
Previously:

	$ du doesntexist
	du: lstat doesntexist: No such file or directory
	0       doesntexist

Now:

	$ du doesntexist
	du: lstat doesntexist: No such file or directory

Also, just call nblks one time.
2017-07-03 21:03:04 +02:00
Michael Forney
a5612b0d08 Remove st != NULL checks from recursor functions
In the description of 3111908b03, it says
that the functions must be able to handle st being NULL, but recurse
always passes a valid pointer. The only function that was ever passed
NULL was rm(), but this was changed to go through recurse in
2f4ab52739, so now the checks are
pointless.
2017-07-03 21:03:02 +02:00
Hiltjo Posthuma
af392d1a76 libutil: fix leaks 2017-05-07 13:50:26 +02:00
Michael Forney
fa0e5d6378 libutil/unescape: NULL terminate unescaped string
In commit 30fd43d7f3, unescape was
simplified significantly, but the new version failed to NULL terminate
the resulting string.

This causes bad behavior in various utilities, for example tr:

Broken:

  $ echo b2 | tr '\142' '\143'
  c3

Fixed:

  $ echo b2 | tr '\142' '\143'
  c2

This bug breaks libtool's usage of tr, causing gcc to fail to build with
sbase.
2017-03-24 10:40:32 +01:00
Michael Forney
72b49a065b test: Use complete prototypes in func field of struct test
Function declarators with empty parentheses is an obsolescent feature in
C99, and it is not clear to me that the standard allows assigning
assigning a function pointer declared in this way to a function declared
in prototype-format.

In any case, using a union for the functions is just as simple and
enforces that we pass the correct types to the functions.
2017-03-24 10:39:19 +01:00
Mattias Andrée
30fd43d7f3 libutil/unescape.c: simplify and add \E
Signed-off-by: Mattias Andrée <maandree@kth.se>
2017-02-06 15:47:01 -08:00
Mattias Andrée
9a903c63de libutil/unescape.c: only print argv0 once on error
Signed-off-by: Mattias Andrée <maandree@kth.se>
2017-02-06 14:13:40 -08:00
Mattias Andrée
2c424c078a cp.1: source and dest are not optional
Signed-off-by: Mattias Andrée <maandree@kth.se>
2017-01-31 10:32:30 -08:00
Mattias Andrée
d301322cf2 getconf: fail if any other flag than -v is used
Signed-off-by: Mattias Andrée <maandree@kth.se>
2017-01-31 10:27:59 -08:00
Roberto E. Vargas Caballero
9ab1478f1e ed: fix commit 2ccc1e8
The patch was wrong because the prototype of
strcpy is different to the prototype of strlcpy
2017-01-10 11:30:34 +01:00
Roberto E. Vargas Caballero
b95c8ed79e ed: Don't use strlcpy()
All the buffers related to files have FILENAME_MAX size, so it is impossible
to have any buffer overrun.
2017-01-10 08:49:17 +01:00
Roberto E. Vargas Caballero
78bfd8978e Revert "ed: remove double free in join()"
This reverts commit 30da327fbd.
The double free is needed to avoid memory leaks when signals are caught
2017-01-10 08:41:35 +01:00
Thomas Mannay
89f5f84316 ed: Treat addresses of 0 as 1 for insert
From 6665eaa1d2c25a95b44a4f4fb3d24a3bd5c1180f Mon Sep 17 00:00:00 2001
From: Thomas Mannay <audiobarrier@openmailbox.org>
Date: Thu, 3 Nov 2016 15:16:32 +0000
Subject: [PATCH] Treat addresses of 0 as 1 for insert
2017-01-02 20:57:00 +01:00
Quentin Rameau
441f73a518 ls: print filenames on the fly rather than in a buffer 2016-12-28 12:23:03 -08:00
Robert Karl
60da4fb049 paste: fix warning on indentation in parallel()
_Bug_
Got the following error after cloning and running make:

paste.c: In function ‘parallel’:
paste.c:70:4: warning: this ‘else’ clause does not guard... [-Wmisleading-indentation]
    else
    ^~~~
paste.c:72:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘else’
     last++;
     ^~~~
This patch preserves the same functionality and just adjusts indentation to squelch the warning.

_Test plan_
Used the following 'script' to convince myself output looked correct for inputs
where the latter arguments to paste had fewer lines.

	make && printf "1\n2\n" > two.txt && printf "" > zero.txt && ./paste -d, two.txt zero.txt
2016-12-27 15:02:15 +01:00
Michael Forney
2481042651 cp: Also preserve atime/mtime for symlinks
Laslo: Fixed style a bit and added comment
2016-12-27 14:50:58 +01:00
Michael Forney
e03a57df92 cp: Check result of utimensat
POSIX says that if duplicating the modification/access times fails, then
an error should be written to stderr.
2016-12-27 14:46:11 +01:00
Michael Forney
a8a9b3bae9 xinstall: Check result of fchmod 2016-12-27 14:37:20 +01:00
Michael Forney
5e4e6aeb3e od: Fix buffer overflow if -N flag is larger than BUFSIZ
Previously, if max was specified, od will call read with that size,
potentially overflowing buf with data read from the file.
2016-12-27 14:32:04 +01:00
Laslo Hunhold
9e594a986e Add a TODO-note about crypt.c 2016-12-27 14:30:51 +01:00
Michael Forney
52e49329e5 crypt: Add some missing error checks for cryptsum
Previously, if a file failed to read in a checksum list, it would be
reported as not matched rather than a read failure.

Also, if reading from stdin failed, previously a bogus checksum would be
printed anyway.
2016-12-27 14:02:32 +01:00
Mattias Andrée
609169d600 install: ignore -s
The -s flag previously called strip(1) on the installed file.
This patch changes install(1)'s behaviour to ignore -s.

Many makefiles use the -s flag, so it has to be recognised for
compatibility, however it does not have to do anything because
symbols do not negatively affect the functionallity of binaries.

Ignoring -s have the added benefit that the user do not need
to edit makefiles if they want the symbols that are useful for
debugging. If the user wants to strip away symbols, it can be
done manually or automatically by the package manager.

Laslo: Update the man-date and remove -s from usage()

Signed-off-by: Mattias Andrée <maandree@kth.se>
2016-12-27 13:56:27 +01:00
Michael Forney
b7fb3a58e7 xinstall: Fix broken memmove with -t
memmove moves a number of bytes, not pointers, so if you passed a number
of arguments that is larger than the pointer byte size, you could
end up crashing or skipping the install of a file and installing another
twice.

Also, argv was never decreased to match the moved arguments, so the -t
parameter was added in the NULL argv slot.
2016-12-27 13:48:59 +01:00
Michael Forney
87f40834a3 parsemode: No need to return after eprintf
Also, since parsemode exits on failure, don't bother checking return
value in xinstall (this would never trigger anyway because mode_t can be
unsigned).
2016-12-27 13:33:35 +01:00
parazyd
aded902891 remove install.1 on make uninstall
Just a minor fix since xinstall.1 is installed as install.1.
2016-12-27 13:30:08 +01:00
Evan Gates
123f784ccc printf: handle \0 in %b arguments
The %b case was using fputs after unescape to print the argument, which
meant that it could not handle nul bytes. Instead, store the length
returned from unescape and use fwrite to properly handle them.
2016-12-27 13:25:38 +01:00