History log of /openbsd-current/bin/cat/cat.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.34 09-Feb-2022 cheloha

cat(1): drop "rpath" promise in no-file case

If we're only working with the standard input we don't need "rpath".

Tweaked by mestre@.

Thread: https://marc.info/?l=openbsd-tech&m=163941848104274&w=2

No objections on tech@ after several weeks.


# 1.33 09-Feb-2022 cheloha

cat(1): refactor cook_args()/raw_args() into single function, cat_file()

- Combine the open/close portions of cook_args()/raw_args() into a single
function, cat_file().

- Push the flag-checking conditional in main() down into cat_file().

- Pull the argv loop in cat_file() up into main().

These changes -- especially pulling the argv look up into main() --
will allow us to drop the "rpath" promise in a single spot in a
subsequent patch.

Tweaked by mestre@. Descriptor leak in earlier version spotted by
Matthew Martin.

Thread: https://marc.info/?l=openbsd-tech&m=163941848104274&w=2

No objections on tech@ after several weeks.


# 1.32 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.31 11-Dec-2020 cheloha

cat(1): -n flag: correctly enumerate files with more than INT_MAX lines

If we bump 'lines' from an int to an unsigned long long we can
trivially support files with more than INT_MAX lines.

ok millert@


# 1.30 04-Dec-2020 cheloha

cat(1): misc. style(9)

- Use getprogname(3) instead of __progname.
- Sprinkle in some missing braces.
- Prefer err(..., NULL) when malloc(3) fails.
- Remove an unnecessary cast to from ssize_t to size_t.


# 1.29 04-Dec-2020 cheloha

cat(1): simplify argv processing loops

cook_args() and raw_args() do some peculiar things in order to avoid
calling cook_buf() and raw_cat(), respectively, in more than one
place. The result is a convoluted. If we isolate the special cases
from the normal case and just call these functions in multiple places
the loops will be easier to read.

Three things:

1. Pull the no-args case out of the loop. If *argv is NULL when we
get into the argv processing function we just want to operate on
the standard input and return early. It makes no sense to handle
this case *in* the loop.

2. Isolate the "-" case from the filename case. If *argv is "-" we want
to operate on the standard input. We can then do any stdin-specific
cleanup within the same branch, which makes it easier to understand
both the "-" case and the normal filename case. This also allows us
to remove the 'filename' intermediate variable from both argv processing
functions.

3. While we're here, use a for-loop and iterate argv in the loop header.
Now argv is incremented in one place.

ok martijn@


# 1.28 03-Dec-2020 cheloha

cat(1): remove global 'filename' variable

There is no need for the global filename variable. Both cook_buf()
and raw_cat() can accept a filename variable from the caller to use
for printing warnings.

ok martijn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.27 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.32 24-Oct-2021 deraadt

For open/openat, if the flags parameter does not contain O_CREAT, the
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past
have passed mode_t (0, 044, 0644, or such), which might lead future people
to copy this broken idiom, and perhaps even believe this parameter has some
meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from
mode_t on a number of system calls as a safety factor, and his bewilderment
that this appeared to be happening against valid modes (at least visually),
but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef.
ok millert


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.31 11-Dec-2020 cheloha

cat(1): -n flag: correctly enumerate files with more than INT_MAX lines

If we bump 'lines' from an int to an unsigned long long we can
trivially support files with more than INT_MAX lines.

ok millert@


# 1.30 04-Dec-2020 cheloha

cat(1): misc. style(9)

- Use getprogname(3) instead of __progname.
- Sprinkle in some missing braces.
- Prefer err(..., NULL) when malloc(3) fails.
- Remove an unnecessary cast to from ssize_t to size_t.


# 1.29 04-Dec-2020 cheloha

cat(1): simplify argv processing loops

cook_args() and raw_args() do some peculiar things in order to avoid
calling cook_buf() and raw_cat(), respectively, in more than one
place. The result is a convoluted. If we isolate the special cases
from the normal case and just call these functions in multiple places
the loops will be easier to read.

Three things:

1. Pull the no-args case out of the loop. If *argv is NULL when we
get into the argv processing function we just want to operate on
the standard input and return early. It makes no sense to handle
this case *in* the loop.

2. Isolate the "-" case from the filename case. If *argv is "-" we want
to operate on the standard input. We can then do any stdin-specific
cleanup within the same branch, which makes it easier to understand
both the "-" case and the normal filename case. This also allows us
to remove the 'filename' intermediate variable from both argv processing
functions.

3. While we're here, use a for-loop and iterate argv in the loop header.
Now argv is incremented in one place.

ok martijn@


# 1.28 03-Dec-2020 cheloha

cat(1): remove global 'filename' variable

There is no need for the global filename variable. Both cook_buf()
and raw_cat() can accept a filename variable from the caller to use
for printing warnings.

ok martijn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.27 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.31 11-Dec-2020 cheloha

cat(1): -n flag: correctly enumerate files with more than INT_MAX lines

If we bump 'lines' from an int to an unsigned long long we can
trivially support files with more than INT_MAX lines.

ok millert@


# 1.30 04-Dec-2020 cheloha

cat(1): misc. style(9)

- Use getprogname(3) instead of __progname.
- Sprinkle in some missing braces.
- Prefer err(..., NULL) when malloc(3) fails.
- Remove an unnecessary cast to from ssize_t to size_t.


# 1.29 04-Dec-2020 cheloha

cat(1): simplify argv processing loops

cook_args() and raw_args() do some peculiar things in order to avoid
calling cook_buf() and raw_cat(), respectively, in more than one
place. The result is a convoluted. If we isolate the special cases
from the normal case and just call these functions in multiple places
the loops will be easier to read.

Three things:

1. Pull the no-args case out of the loop. If *argv is NULL when we
get into the argv processing function we just want to operate on
the standard input and return early. It makes no sense to handle
this case *in* the loop.

2. Isolate the "-" case from the filename case. If *argv is "-" we want
to operate on the standard input. We can then do any stdin-specific
cleanup within the same branch, which makes it easier to understand
both the "-" case and the normal filename case. This also allows us
to remove the 'filename' intermediate variable from both argv processing
functions.

3. While we're here, use a for-loop and iterate argv in the loop header.
Now argv is incremented in one place.

ok martijn@


# 1.28 03-Dec-2020 cheloha

cat(1): remove global 'filename' variable

There is no need for the global filename variable. Both cook_buf()
and raw_cat() can accept a filename variable from the caller to use
for printing warnings.

ok martijn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.27 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.30 04-Dec-2020 cheloha

cat(1): misc. style(9)

- Use getprogname(3) instead of __progname.
- Sprinkle in some missing braces.
- Prefer err(..., NULL) when malloc(3) fails.
- Remove an unnecessary cast to from ssize_t to size_t.


# 1.29 04-Dec-2020 cheloha

cat(1): simplify argv processing loops

cook_args() and raw_args() do some peculiar things in order to avoid
calling cook_buf() and raw_cat(), respectively, in more than one
place. The result is a convoluted. If we isolate the special cases
from the normal case and just call these functions in multiple places
the loops will be easier to read.

Three things:

1. Pull the no-args case out of the loop. If *argv is NULL when we
get into the argv processing function we just want to operate on
the standard input and return early. It makes no sense to handle
this case *in* the loop.

2. Isolate the "-" case from the filename case. If *argv is "-" we want
to operate on the standard input. We can then do any stdin-specific
cleanup within the same branch, which makes it easier to understand
both the "-" case and the normal filename case. This also allows us
to remove the 'filename' intermediate variable from both argv processing
functions.

3. While we're here, use a for-loop and iterate argv in the loop header.
Now argv is incremented in one place.

ok martijn@


# 1.28 03-Dec-2020 cheloha

cat(1): remove global 'filename' variable

There is no need for the global filename variable. Both cook_buf()
and raw_cat() can accept a filename variable from the caller to use
for printing warnings.

ok martijn@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE
# 1.27 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.27 28-Jun-2019 deraadt

When system calls indicate an error they return -1, not some arbitrary
value < 0. errno is only updated in this case. Change all (most?)
callers of syscalls to follow this better, and let's see if this strictness
helps us in the future.


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_1_BASE OPENBSD_6_2_BASE
# 1.26 19-Oct-2016 schwarze

Delete useless setlocale(3) in src/bin/.
Minor cleanup while here: return from main, static void __dead usage, etc.
Based on a patch from Jan Stary <hans at stare dot cz>.
Feedback and OK tb@, OK millert@.


Revision tags: OPENBSD_6_0_BASE
# 1.25 01-Jul-2016 schwarze

For -be, indent the $ on blank lines.
Patch from Giles Lean (NetBSD PR bin/4841), tweaked by kleink at
NetBSD (rev. 1.17 1998-01-27), version for OpenBSD sent in by Sevan
Janiyan <venture37 at geeklan dot co dot uk>.
OK deraadt@


Revision tags: OPENBSD_5_9_BASE
# 1.24 04-Nov-2015 tedu

replace setbuf with setvbuf, from Frederic Nowak


# 1.23 09-Oct-2015 deraadt

Change all tame callers to namechange to pledge(2).


# 1.22 03-Oct-2015 deraadt

obvious tame "stdio rpath"
ok semarie


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE
# 1.21 16-Jan-2015 deraadt

Replace <sys/param.h> with <limits.h> and other less dirty headers where
possible. Annotate <sys/param.h> lines with their current reasons. Switch
to PATH_MAX, NGROUPS_MAX, HOST_NAME_MAX+1, LOGIN_NAME_MAX, etc. Change
MIN() and MAX() to local definitions of MINIMUM() and MAXIMUM() where
sensible to avoid pulling in the pollution. These are the files confirmed
through binary verification.
ok guenther, millert, doug (helped with the verification protocol)


Revision tags: OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE OPENBSD_5_4_BASE OPENBSD_5_5_BASE OPENBSD_5_6_BASE
# 1.20 27-Oct-2009 deraadt

rcsid[] and sccsid[] and copyright[] are essentially unmaintained (and
unmaintainable). these days, people use source. these id's do not provide
any benefit, and do hurt the small install media
(the 33,000 line diff is essentially mechanical)
ok with the idea millert, ok dms


Revision tags: OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.19 17-Jul-2007 jmc

sync usage(); from Tobias Stoeckmann


# 1.18 22-Mar-2007 millert

Fix handling of multiple stdin arguments in cooked mode, closes PR 5418.
From Johan Veenhuizen


Revision tags: OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE
# 1.17 05-Apr-2005 jaredy

change incorrect filename in output on fstat() failure

from Cedric Berger <cedric@berger.to>

ok otto


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE
# 1.16 21-May-2004 jolan

minor cleanups / shut up lint, ok pedro tedu


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE
# 1.15 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.14 04-Jul-2002 deraadt

ansi


Revision tags: OPENBSD_3_1_BASE
# 1.13 14-Mar-2002 millert

Fix 'cat -se'; the '$' was not being printed for blank lines.
Based on a patch from Denis Afonin.


# 1.12 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.11 05-Nov-2001 mpech

kill more registers;

millert@ ok


Revision tags: OPENBSD_3_0_BASE
# 1.10 06-Sep-2001 mpech

o) __progname aria;

millert@ ok.


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE OPENBSD_2_9_BASE
# 1.9 22-Jan-2000 deraadt

remove extra externs not needed because of unistd.h (rest of tree will be done later.. contact me if you want to help)


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE
# 1.8 18-Jun-1997 kstailey

(foo *)NULL -> NULL


Revision tags: OPENBSD_2_1_BASE
# 1.7 14-Dec-1996 mickey

-Wall'ing.


Revision tags: OPENBSD_2_0_BASE
# 1.6 28-Sep-1996 imp

Back out last change, getopt on OpenBSD returns -1, not EOF


# 1.5 28-Sep-1996 imp

Fix the return value of test of getopt. It is documented to return
EOF when there are no more args to process. This happens also to be a
trivial difference between FreeBSD and OpenBSD which FreeBSD has
right. Shouldn't break anything because EOF is #defined to be -1 on
BSD systems.


# 1.4 02-Aug-1996 tholo

Exit with status >0 if any error occured; from FreeBSD


# 1.3 02-Aug-1996 deraadt

zap getopt() case of -?, come on, it is the default!


# 1.2 23-Jun-1996 deraadt

update rcsid


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision