Commit Graph

61 Commits

Author SHA1 Message Date
Quentin Rameau f1bbe6529a ls: detect infinite loop with recursivity
populate an history of visited directories inodes and search it before
recursing
2015-08-31 18:10:25 +01:00
Quentin Rameau 74b5aa151c ls: correctly handle recursivity
the path is now carried through ls() and lsdir(), directory entries are
listed before recursing.
2015-08-31 18:10:25 +01:00
Quentin Rameau 1905b83cc9 ls: list operands in correct order
sort operands by name, list files first then directory entries
2015-08-31 18:10:25 +01:00
Quentin Rameau b88ed2ab2d ls: fix -q
Move qflag handling to output() instead of modifying the actual entity
name which broke recursivity.
2015-08-31 18:10:25 +01:00
Alexandre Niveau 6be7eaff1b ls: fix option -S for it to work without -lnpFi 2015-05-15 12:07:43 +01:00
FRIGN 5595af5742 Convert humansize() to accept a size_t instead of a double
General convention is to use size_t to store sizes of all kinds.
Internally, the function uses double anyway, but at least this
doesn't clobber up the API any more and there's a chance in the
future to make this function a bit cleaner and not use this dirty
static buffer hack any more.
2015-04-25 11:43:14 +01:00
FRIGN 11e2d472bf Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-05 09:13:56 +01:00
Quentin Rameau 9fdef90feb ls: add -f and -S options 2015-03-19 10:12:12 +00:00
FRIGN a68c2a9e6e Remove apathmax() and implicitly agetcwd()
pathconf() is just an insane interface to use. All sane operating-
systems set sane values for PATH_MAX. Due to the by-runtime-nature of
pathconf(), it actually weakens the programs depending on its values.

Given over 3 years it has still not been possible to implement a sane
and easy to use apathmax()-utility-function, and after discussing this
on IRC, we'll dump this garbage.

We are careful enough not to overflow PATH_MAX and even if, any user
is able to set another limit in config.mk if he so desires.
2015-03-18 15:20:35 +01:00
FRIGN 833c2aebb4 Remove mallocarray(...) and use reallocarray(NULL, ...)
After a short correspondence with Otto Moerbeek it turned out
mallocarray() is only in the OpenBSD-Kernel, because the kernel-
malloc doesn't have realloc.
Userspace applications should rather use reallocarray with an
explicit NULL-pointer.

Assuming reallocarray() will become available in c-stdlibs in the
next few years, we nip mallocarray() in the bud to allow an easy
transition to a system-provided version when the day comes.
2015-03-11 10:50:18 +01:00
FRIGN 3c33abc520 Implement mallocarray()
A function used only in the OpenBSD-Kernel as of now, but it surely
provides a helpful interface when you just don't want to make sure
the incoming pointer to erealloc() is really NULL so it behaves
like malloc, making it a bit more safer.

Talking about *allocarray(): It's definitely a major step in code-
hardening. Especially as a system administrator, you should be
able to trust your core tools without having to worry about segfaults
like this, which can easily lead to privilege escalation.

How do the GNU coreutils handle this?
$ strings -n 4611686018427387903
strings: invalid minimum string length -1
$ strings -n 4611686018427387904
strings: invalid minimum string length 0

They silently overflow...

In comparison, sbase:

$ strings -n 4611686018427387903
mallocarray: out of memory
$ strings -n 4611686018427387904
mallocarray: out of memory

The first out of memory is actually a true OOM returned by malloc,
whereas the second one is a detected overflow, which is not marked
in a special way.
Now tell me which diagnostic error-messages are easier to understand.
2015-03-10 22:19:19 +01:00
FRIGN 3b825735d8 Implement reallocarray()
Stateless and I stumbled upon this issue while discussing the
semantics of read, accepting a size_t but only being able to return
ssize_t, effectively lacking the ability to report successful
reads > SSIZE_MAX.
The discussion went along and we came to the topic of input-based
memory allocations. Basically, it was possible for the argument
to a memory-allocation-function to overflow, leading to a segfault
later.
The OpenBSD-guys came up with the ingenious reallocarray-function,
and I implemented it as ereallocarray, which automatically returns
on error.
Read more about it here[0].

A simple testcase is this (courtesy to stateless):
$ sbase-strings -n (2^(32|64) / 4)

This will segfault before this patch and properly return an OOM-
situation afterwards (thanks to the overflow-check in reallocarray).

[0]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/calloc.3
2015-03-10 21:23:36 +01:00
Quentin Rameau 7264acf7ed ls: add -n option 2015-02-22 12:30:47 +00:00
Tai Chi Minh Ralph Eastwood dacd523ec1 ls: add support for -R flag 2015-02-21 11:46:04 +00:00
sin 9c30fbf018 Add ls -A implementation
Thanks joshua@cubesolving.com!
2015-02-21 09:31:01 +00:00
Quentin Rameau 51b707e91e ls: add support for -p 2015-02-19 18:52:13 +00:00
sin e635760742 Fix ls when -q is not set
Use strdup() unconditionally.
2015-02-18 16:48:07 +00:00
sin d40333e702 Fix ls -q UTF-8 parsing
Thanks FRIGN for the input.
2015-02-18 16:43:18 +00:00
sin 2a3f82a5cd Implement -u support for ls(1) 2015-02-18 16:29:46 +00:00
sin 9bd094193d Simplify ls -q implementation 2015-02-18 15:55:57 +00:00
FRIGN 6b719faade Add q-flag to ls(1)
of course, UTF-8-aware. ;)
2015-02-17 18:59:01 +01:00
FRIGN bec3c32dbd Refactor ls(1) 2015-02-16 19:20:06 +01:00
FRIGN 31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
sin dc70eb7976 ls: Implement -c and update manpage
Update usage + README as well.  Apparently some of the options (
-H and -L) had already been implemented but not reflected in the
program usage line.
2015-01-20 16:50:37 +00:00
sin bb59d2eb34 ls: No need to set errno to zero
Consider the following code:

pw = getpwuid(uid);
if (!pw) {
	if (errno)
		...
	else
		...
}

If the entry was not found then as per POSIX errno is not set
because that is not considered to be a failing condition.  errno
is only set if an internal error occurred.

If errno happened to be non-zero before the getpwuid() call
because of a previous error then we'll report a bogus error.

In this case, we have to set errno to zero before the call to
getpwuid().

However in ls(1) we only really care if the password entry was found
and we do not report any errors so setting errno to 0 is not necessary.
2014-12-22 11:28:20 +00:00
Hiltjo Posthuma 32651cb2da ls: getpwuid, getgrgid, use return code of NULL is an error, not errno 2014-12-22 11:28:20 +00:00
Michael Forney 573ef00c91 ls: Always leave room for the NULL byte in the link target
Otherwise, if the length of the link target is the same as BUFSIZ, we
will try to write past the end of buf.
2014-12-08 10:02:28 +00:00
Michael Forney 46ea55a258 ls: Handle symlinks to directories properly
Also, implement the -H and -L options.
2014-12-08 10:02:09 +00:00
FRIGN 1436518f9d Use < 0 instead of == -1 2014-11-19 20:09:29 +00:00
FRIGN e17b9cdd0a Convert codebase to use emalloc.c utility-functions
This also definitely increases readability and makes OOM-conditions
more consistent.
2014-11-16 10:22:39 +00:00
FRIGN ec8246bbc6 Un-boolify sbase
It actually makes the binaries smaller, the code easier to read
(gems like "val == true", "val == false" are gone) and actually
predictable in the sense of that we actually know what we're
working with (one bitwise operator was quite adventurous and
should now be fixed).

This is also more consistent with the other suckless projects
around which don't use boolean types.
2014-11-14 10:54:20 +00:00
FRIGN 7d2683ddf2 Sort includes and more cleanup and fixes in util/ 2014-11-14 10:54:10 +00:00
FRIGN eee98ed3a4 Fix coding style
It was about damn time. Consistency is very important in such a
big codebase.
2014-11-13 18:08:43 +00:00
Hiltjo Posthuma 4d4e2608c1 ls: add -h flag
for util/human don't show "B" for bytes.
2014-10-19 12:54:19 +01:00
sin 0c5b7b9155 Stop using EXIT_{SUCCESS,FAILURE} 2014-10-02 23:46:59 +01:00
Hiltjo Posthuma 35959cd1c4 ls: improve default format
truncate username and group for better alignment for now. in the future
better tabular printing will be implemented.
2014-07-21 16:44:15 +01:00
Hiltjo Posthuma 7fdbdd0770 ls: add -1 for compatibility (ignore) 2014-07-05 16:26:27 +01:00
Hiltjo Posthuma 953ebf3573 code style
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-06-01 18:02:30 +01:00
sin e3c62682ff Don't bail out if the username/group name does not exist in ls(1) 2014-04-05 20:23:40 +01:00
Daniel Bainton 080db637f8 Add the -F flag to ls 2014-02-18 12:54:16 +00:00
Markus Wichmann a55cc8c95e Make chmod and ls recognize sticky bit (S_ISVTX). 2013-10-20 10:41:32 +01:00
sin b5a511dacf Exit with EXIT_SUCCESS/EXIT_FAILURE instead of 0 and 1
Fixed for consistency purposes.
2013-10-07 16:44:22 +01:00
Markus Teich 952fa19362 ls: rework implementation for reversing the sort order
Update the usage line as well.
2013-10-06 19:57:21 +01:00
Markus Teich 3a3cd24092 ls: add option to reverse the sort order
Now you can list directory contents ordered by mtime with the
latest file displayed last in the output.
2013-10-05 14:58:55 +01:00
sin 8c87c20054 Implement -i support for ls 2013-10-05 13:51:45 +01:00
sin 582511d57b Fix some warnings about strcpy() etc. on OpenBSD 2013-10-05 13:50:57 +01:00
David Galos cd1526715f Opimizing tar, adding U flag 2013-07-18 15:14:53 -04:00
stateless 7216a53a7e Remove unnecessary exit(1) in usage()
Signed-off-by: Christoph Lohmann <20h@r-36.net>
2013-06-19 19:58:19 +02:00
Christoph Lohmann 4d38f60685 Eliminating the getopt disgrace. 2013-06-14 20:20:47 +02:00
anonymous 4192b13768 ls: allocate proper space for args 2012-01-19 00:04:04 +00:00