aboutsummaryrefslogtreecommitdiff
path: root/bin/echo
diff options
context:
space:
mode:
authorAlfred Perlstein <alfred@FreeBSD.org>2002-03-04 05:30:04 +0000
committerAlfred Perlstein <alfred@FreeBSD.org>2002-03-04 05:30:04 +0000
commit6188858ae1dc3fdf5e3c21357ccfb1c5e72973c9 (patch)
tree29c7e3a2dd5211dfaa3f12b7e3e00367631742fa /bin/echo
parent5573db3f0b8e0eff55e660469eacfc05934ccef1 (diff)
downloadsrc-6188858ae1dc3fdf5e3c21357ccfb1c5e72973c9.tar.gz
src-6188858ae1dc3fdf5e3c21357ccfb1c5e72973c9.zip
clarify code:
add comments. don't get the length of each arg passed, only the last one. check against == or != NULL rather than using a pointer value as truth test.
Notes
Notes: svn path=/head/; revision=91614
Diffstat (limited to 'bin/echo')
-rw-r--r--bin/echo/echo.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/bin/echo/echo.c b/bin/echo/echo.c
index 2e294c8eb062..989aaa360d5c 100644
--- a/bin/echo/echo.c
+++ b/bin/echo/echo.c
@@ -53,7 +53,7 @@ static const char rcsid[] =
int
main(int argc __unused, char *argv[])
{
- int nflag;
+ int nflag; /* if not set, output a trailing newline. */
/* This utility may NOT do getopt(3) option parsing. */
if (*++argv && !strcmp(*argv, "-n")) {
@@ -63,12 +63,25 @@ main(int argc __unused, char *argv[])
else
nflag = 0;
- while (argv[0]) {
- size_t len = strlen(argv[0]);
+ while (argv[0] != NULL) {
- if (len >= 2 && !argv[1] && argv[0][len - 2] == '\\' && argv[0][len - 1] == 'c') {
- argv[0][len - 2] = '\0';
- nflag = 1;
+ /*
+ * If the next argument is NULL then this is this
+ * the last argument, therefore we need to check
+ * for a trailing \c.
+ */
+ if (argv[1] == NULL) {
+ size_t len;
+
+ len = strlen(argv[0]);
+ /* is there room for a '\c' and is there one? */
+ if (len >= 2 &&
+ argv[0][len - 2] == '\\' &&
+ argv[0][len - 1] == 'c') {
+ /* chop it and set the no-newline flag. */
+ argv[0][len - 2] = '\0';
+ nflag = 1;
+ }
}
(void)printf("%s", argv[0]);
if (*++argv)