Commit Graph

79 Commits

Author SHA1 Message Date
Quentin Rameau
ee8701d501 ls: check for the index size before using it in visit()
Thanks to k0ga for noticing it!
2016-02-18 14:18:49 +00:00
Quentin Rameau
1024369f2d ls: make the opendir() warning message more consistent
Print the total file path in the warning message.
2016-02-18 14:17:21 +00:00
Quentin Rameau
ac05587385 ls: set a return error code when skipping an already visited path. 2016-02-18 14:17:21 +00:00
Quentin Rameau
340b163563 ls: do not exit when a directory isn't accessible
Just print a warning and process next item instead,
and return 1 to report this error.
2016-02-18 11:31:58 +00:00
pekka.jylha.ollila@gmail.com
bb83eade39 Fix showing directories when -R flag is set in ls(1)
Here's a better version of the patch.
When the R flag is used with a single directory, the given directory name is
omitted. With multiple directories each directory name is listed.
Directories that start with './' and '../' are now also printed.
2016-02-17 19:49:07 +00:00
tty0
b107489bf2 ls: only display directory headers when more than one directory is specified 2016-02-15 09:59:09 +00:00
Quentin Rameau
4859f4c825 ls: fix time handling by augmenting time resolution
ls was using (old) UNIX spec (struct stat).st_[acm]time.
It now uses POSIX (struct stat).(struct timespec st_[acm]tim) which
gives time resolution in seconds and nanoseconds.
If two files have the same time in seconds, we extend the comparision to
nanoseconds.
2015-11-01 17:29:39 +00:00
Quentin Rameau
57d220b3dc ls: fix entities arrays handling in main()
Entities arrays in main() were arrays of pointer to entities and were
not compatible with entcmp().
They have been changed to being arrays of entities.
Thanks to Michael Forney <mforney@mforney.org> for having seen that.
2015-11-01 17:29:39 +00:00
sin
aa8112a408 Revert "ls: Fix sorting of named entries"
This reverts commit ff78d03791cc7c24dfc407c39dd2158451c9ad12.
2015-11-01 17:29:39 +00:00
sin
2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00:00
Michael Forney
674d1636f8 ls: Fix sorting of named entries
Previously, entcmp was being passed struct entry **, when it expected
struct entry *.

Many autoconf-generated configure scripts use `ls -t` to determine whether or
not the system clock is behaving correctly. If they are sorted in the wrong
order, it produces an error.

  checking whether build environment is sane... configure: error: newly created file is older than distributed files!
  Check your system clock
2015-11-01 09:36:41 +00:00
Quentin Rameau
0e2f509883 ls: handle character/block files in long format
Although major() and minor() are not POSIX, we don't want to have macros
there so we rely on their implementation by the target system.
2015-10-04 16:45:51 +01:00
Hiltjo Posthuma
521c3734af ls: fix usage line, add f, n and p flag 2015-09-30 19:44:10 +01:00
Hiltjo Posthuma
aba450fd1a ls: printf("%s", s) -> fputs(s, stdout) 2015-09-30 19:44:10 +01:00
Hiltjo Posthuma
874d131728 ls: LEN(pw|grname) -> sizeof() 2015-09-30 19:44:10 +01:00
Hiltjo Posthuma
53be158979 code-style: whitespace fixes 2015-09-30 19:44:10 +01:00
FRIGN
93f1fc4614 Remove trailing whitespace inside of ls(1) 2015-08-31 18:10:25 +01:00
Quentin Rameau
365e1a324a ls: group sorting options
tflag and Sflag are now grouped under 'sort' var, as suggested by cls.
r-flag handling has been moved inside entcmp().
2015-08-31 18:10:25 +01:00
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