Commit Graph

1384 Commits

Author SHA1 Message Date
Hiltjo Posthuma
9f97430143 libutil/getlines: fix crash with no lines
because b->lines and b->nlines would be 0 with no lines read.

reproduce: printf '' | sort or cols

bug was introduced by commit: 66a5ea722d
2015-03-29 21:48:49 +02:00
Hiltjo Posthuma
1e0c3a0ba6 whitespace fixes 2015-03-27 22:48:05 +01:00
Hiltjo Posthuma
8e486d5282 xargs: minor optimizations 2015-03-27 22:48:05 +01:00
Hiltjo Posthuma
67ca2428d5 xargs: fix sysconf(_SC_ARG_MAX)
the minimum space required by environment variables isn't clearly defined by
POSIX afaik.
2015-03-27 22:48:05 +01:00
Hiltjo Posthuma
7ebf02d749 find: fgetc() returns int 2015-03-27 22:48:05 +01:00
Hiltjo Posthuma
a9bedca038 fix some signed/unsigned warnings and style fixes 2015-03-27 22:48:05 +01:00
FRIGN
57dc7b94b0 POSIX 2013 audit
Mostly manpage-shuffling according to the changes in the corrigendum,
wording changes and more idiomatic expressions.
All this is finished up by marking the POSIX 2013 conformant tools
with
.St -p1003.1-2013
which is not available in older mandoc builds or nroff, but which
reflects what we actually did, so who cares?

This is a huge step and it's not far until we can release sbase 0.1.
2015-03-27 17:25:22 +01:00
Hiltjo Posthuma
06b663234f sed: declare functions and global vars static (some still missing) 2015-03-27 16:20:50 +01:00
Hiltjo Posthuma
244539e473 sed: style improvements
- declare variables at the top of a function.
- free(NULL) is valid.
- avoid VLA.
2015-03-27 16:01:55 +01:00
Hiltjo Posthuma
0547e72441 sed: show specific error strings (strerror) and minor style fixes 2015-03-27 15:59:09 +01:00
Hiltjo Posthuma
fea0a34e13 grep: use len returned from getline and check fmemopen() 2015-03-27 15:54:41 +01:00
Hiltjo Posthuma
44298e54e6 comm: minor optimization 2015-03-27 15:53:46 +01:00
FRIGN
9144d51594 Check getline()-return-values properly
It's not useful when 0 is returned anyway, so be sure that we have a
string with length > 0, this also solves some indexing-gotchas like
"len - 1" and so on.
Also, add checked getline()'s whenever it has been forgotten and
clean up the error-messages.
2015-03-27 14:49:48 +01:00
Hiltjo Posthuma
a516338581 nl.1: fix mandoc warning 2015-03-27 12:21:28 +01:00
Hiltjo Posthuma
c5a51123b1 printf: no need to nul-terminate after strndup 2015-03-27 12:21:13 +01:00
FRIGN
096da1297b Properly handle width and precision in printf(1) 2015-03-26 23:01:26 +01:00
Evan Gates
b7886f3e67 change estrlcat back to strlcat
strlcat is used to here to purposely truncate the string
2015-03-24 22:50:26 +00:00
FRIGN
8db1632409 Drop maxdigits in expr(1)
This just begs for off-by-ones. Just drop it and use BUFSIZ.
We'll survive it.
2015-03-23 22:56:26 +01:00
FRIGN
f668afa1ff Style fix in expr(1)
(*valp). --> valp->
2015-03-23 22:44:07 +01:00
FRIGN
50ecbd43ad Can't use sizeof on malloc'ed array in expr(1)
Thanks emg for reporting this!
2015-03-23 22:31:36 +01:00
FRIGN
df8529f0a1 Fix syntax error in sort(1)
Somehow went unnoticed...
2015-03-23 20:30:07 +01:00
FRIGN
4750b233ce Prepare POSIX 2013 audit
I can't believe we've come this far! The idea is to look at the
2013 POSIX corrigendum for each tool and deep-test features before
making the first 0.1 release.
To keep the noise low, I'll do this in batches, not on a per-tool-
basis (as many of these are trivial to test).
In the meantime, I'll also think of a fitting STANDARDS section
for the non-POSIX tools. Now that the audits are pretty much done,
I can also have a more relaxed view on standards compliance instead
of having to dig through some uncleaned mess.

To mark this "new beginning", the README has gotten a liftover.
The POSIX 2008-column was more or less useless and as I expect the
checks to go along pretty quickly, I "reset" the compliance state
of all but the non-POSIX tools and will then go along and check every
single one of them in the next few days.

Apart from the few missing flags and audits, sbase should then be
ready to hit the world with the first release after 4 years of work.
2015-03-23 16:22:00 +01:00
FRIGN
49e27c1b0c Add -m and -o flags to sort(1)
Sort comes pretty much automatically, as no script relies on the
undefined behaviour of the input _not_ being sorted, we might as well
sort the sorted input already.
The only downside is memory usage, which can be an issue for large
files.
The o-flag was trivial to implement.
2015-03-22 23:39:48 +01:00
FRIGN
71adaed519 Add s-, t-, x-flags to and audit xargs(1)
The flexible design already allowed to add these flags trivially.
Drop the -I and -L-flags, which are XSI-extensions.
The audit generally consisted of style-changes, dropping kitchen-
sink functions, updating the usage and using estrtonum instead of
strtol.
2015-03-22 22:57:58 +01:00
FRIGN
521f324319 Audit col(1), add UTF-8-support
Nothing special here, only renaming of variables and adding the
Rune-utility-functions.
Also, I refactored the manpage.
2015-03-22 21:45:32 +01:00
sin
a0e5b565a7 nl: Fix leading white-space 2015-03-22 19:23:57 +00:00
FRIGN
b96c9d06c5 Don't indent non-numbered lines in nl(1)
This is yet another GNU-specific behaviour we fell for.
POSIX explicitly states that for non-numbered lines, all indentation-
crap should be dropped immediately. Well, we didn't listen, so this
commit will set things straight again.
Honestly speaking, this also looks much cleaner than before.
Fuck GNU!
2015-03-22 18:30:45 +01:00
FRIGN
3ccfa1877d Don't print section-control-lines as empty lines in nl(1)
GNU coreutils may behave this way, but one can think of these lines
as consumed control lines.
Fuck GNU!
2015-03-22 17:38:22 +01:00
FRIGN
377b5499e9 Fix small typo in nl.1 2015-03-22 17:25:11 +01:00
FRIGN
587575dcb4 Audit nl(1)
1) Refactor the manpage to use the num-syntax and concise wording.
2) Build format instead of having a list of static strings.
3) BUGFIX: if (!buf[0] || buf[0] == '\n') Process last-read-line
           properly.
4) BUGFIX: In case we hit a formatting line, print a newline instead
           of just dropping it.
5) Use a switch instead of having spaghetti-cases.
6) Don't use printf-magic but explicitly do a putchar(' ')-loop.
7) Update usage(), indent properly.
8) BUGFIX: strchr is not NULL when type[0] is \0. Check for \0
           separately beforehand.
9) Reorder arg.h-cases for better readability.
2015-03-22 16:46:01 +01:00
FRIGN
d49f6f2044 Audit expr(1)
No bugs found, but I changed intmax_t to long long to make it more
predictable and removed some of the kitchen-sinking.
Don't return structs themselves, as this is not very elegant.
Do it like functions like stat(), which take a pointer to a
struct to fill.
2015-03-22 14:35:15 +01:00
FRIGN
1f0f1dd320 Show usage() when filtermode is given for tar-creation
We only allow decompression for extraction. Thus, it may be confusing
for the user and break scripts silently when the j- or z-flag are given
even though this is not supported.
2015-03-21 14:04:49 +01:00
FRIGN
b6b977f63d Audit tar(1), add DIRFIRST-flag to recurse()
I've been wanting to do this for a while now, as tar(1) used to
be one of messiest and cruftiest tools.
First off, before walking through the audit, I'll talk about
what the DIRFIRST-flag for recurse() does.
It basically calls fn() on the first-level-dir before calling
it's subentries. It's necessary here, because else the order
of the tar-files would've been wrong (it would try to create
dir/file before creating dir/).

Now, to the audit:
1)  Update manpage, fix mistake that compression is also available
    for compressing. It's only available for extracting.
2)  Define the major, minor and makedev macros from glibc by ourselves.
    No need to rely on them, as they are common sense.

decomp()
3)  Simple refactorization.

putoctal()
4)  Add a truncation check for snprintf().

archive()
5)  BUGFIX: Add checks to any checkable function, don't blindly call
    them, this is harmful and there are 100 ways to exploit that.
6)  Use estrlcpy() instead of snprintf() wherever possible, fix
    alignment.
7)  BUGFIX: Terminate the result-buffer of readlink(), check if
    it even succeeded.
8)  Fix sizeof()-formatting.

unarchive()
9)  BUGFIX: Add checks to any checkable function, don't blindly call
    them, this is harmful and there are 100 ways to exploit that.
10) BUGFIX: strtoul can happily return negative numbers. Add checks
    for that and also if the full string has been processed.
11) Remove calls to perror(). We have eprintf, use it.
12) BUGFIX: "minor = strtoul(h->mode, 0, 8);". We need h->minor of
    course.
13) Fix typo "usupported", remove fprintf-call.

print()
14) Check fread().

xt()
15) Get rid of snprintf-magic. Use estrlcat().
16) BUGFIX: check for ferror() on the tarfile.

usage()
17) Update it. The old usage() was like 1000 years old.

main()
18) Add DIRFIRST-flag to the recursor.
19) Don't print usage() when a mode is re-set. We allow this in
    general.
20) Add function checks and fix error messages.
21) Add tarfilename-global for proper error-messages.
2015-03-21 01:30:47 +01:00
FRIGN
a531865fe5 Audit readlink(1)
1) Properly document e, f and m-flags in the manpage.
2) Clear up the code for the m-flag-handling. Add idiomatic
   '/'-path-traversal as already seen in mkdir(1).
3) Unwrap the SWAP_BUF()-macro.
4) BUGFIX: Actually handle the f-flag properly. Only resolve
   the dirname and append the basename later.
5) Use fputs() instead of printf("%s", ...).
2015-03-20 22:14:31 +01:00
Quentin Rameau
e6c20fe367 nl: UTF-8 support and removed the two characters limit for delimiter 2015-03-20 20:21:20 +00:00
FRIGN
b8c9a88371 Reorder variables in seq(1) 2015-03-20 16:37:29 +01:00
FRIGN
7e7d15b7a5 Audit seq(1)
Mostly manpage-stuff and style-changes in the code.
2015-03-20 16:05:31 +01:00
FRIGN
f3224add37 Update README on feature-list
Add "none" to ls, as all pending flags are optional.
sed is feature-complete, so I marked it like that. It needs an audit
though.
seq is implicitly UTF-8-ready, will be audited later.
2015-03-20 15:17:55 +01:00
FRIGN
58098575e7 Audit cp() in libutil
1) Rename cp_HLPflag -> cp_follow for consistency.
2) Use function-pointers for stat to clear up the code.
3) BUGFIX: TERMINATE THE RESULT BUFFER OF READLINK !!!
   It's something I noticed earlier and it actually lead to some
   pretty insane behaviour on our side using glibc (musl somehow
   magically solves this).
   Basically, symlinks used to contain the data of the file they
   pointed to. I wondered for weeks where this came from and now
   this has finally been solved.
4) BUGFIX: Do not unconditionally unlink target-files. Even GNU
   coreutils do it wrong.
   The basic idea is this:
   If fflag == 0 --> don't touch target files if they exist.
   If fflag == 1 --> unlink all and don't error out when we try
                     to unlink a file which doesn't exist.
5) Use estrlcpy and estrlcat instead of snprintf for path building.
6) Make it clearer what happens in preserve.
2015-03-19 17:57:12 +01:00
sin
756f729172 kill: No need for const int 2015-03-19 10:45:58 +00:00
Quentin Rameau
9fdef90feb ls: add -f and -S options 2015-03-19 10:12:12 +00:00
Quentin Rameau
787d99d896 nl: add -d -p -f -h options 2015-03-19 10:10:55 +00:00
FRIGN
3111908b03 Refactor recurse() again
Okay, why yet another recurse()-refactor?
The last one added the recursor-struct, which simplified things
on the user-end, but there was still one thing that bugged me a lot:
Previously, all fn()'s were forced to (l)stat the paths themselves.
This does not work well when you try to keep up with H-, L- and P-
flags at the same time, as each utility-function would have to set
the right function-pointer for (l)stat every single time.

This is not desirable. Furthermore, recurse should be easy to use
and not involve trouble finding the right (l)stat-function to do it
right.
So, what we needed was a stat-argument for each fn(), so it is
directly accessible. This was impossible to do though when the
fn()'s are still directly called by the programs to "start" the
recurse.
Thus, the fundamental change is to make recurse() the function to
go, while designing the fn()'s in a way they can "live" with st
being NULL (we don't want a null-pointer-deref).

What you can see in this commit is the result of this work. Why
all this trouble instead of using nftw?
The special thing about recurse() is that you tell the function
when to recurse() in your fn(). You don't need special flags to
tell nftw() to skip the subtree, just to give an example.

The only single downside to this is that now, you are not allowed
to unconditionally call recurse() from your fn(). It has to be
a directory.
However, that is a cost I think is easily weighed up by the
advantages.

Another thing is the history: I added a procedure at the end of
the outmost recurse to free the history. This way we don't leak
memory.

A simple optimization on the side:

-		if (h->dev == st.st_dev && h->ino == st.st_ino)
+		if (h->ino == st.st_ino && h->dev == st.st_dev)

First compare the likely difference in inode-numbers instead of
checking the unlikely condition that the device-numbers are
different.
2015-03-19 01:08:19 +01:00
FRIGN
b3e8b17235 Audit concat() in libutil
Be more pedantic about the error-checking, fread can also return
values > 0 even though there has been a read-error.
We want to write the last incoming data and then bail.
2015-03-18 22:58:42 +01: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
833670e06c Add notice on flags put in parentheses in README 2015-03-18 01:11:55 +01:00
FRIGN
2602681232 ls has explicit UTF-8-support (q-flag) 2015-03-18 01:10:47 +01:00
FRIGN
74dcd46c70 Update README in regard to ls(1)
I marked out -m, -s and -x, because they are either visual flags
for interactive mode, which are better solved with tools made for this
job, or superfluous in another sense.
For example, -s basically "steals" the job from du.
In general, some of these options might still be easy to implement.
The options -S and -f are important though, as they are sorting-options
with real use.
2015-03-18 01:00:13 +01:00
FRIGN
7bacd98b0e Audit wc(1)
Style cleanup, Manpage refactoring.
2015-03-18 00:20:19 +01:00
FRIGN
960bccbe9d Remove comma from uuencode.1 2015-03-18 00:18:28 +01:00