From d45fa19d5cc4375b9706cf7b6ea55ba791a1ba8b Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 27 Jul 2024 23:59:32 -0600 Subject: [PATCH 01/10] dj(1): fixes extraneous ternary for program_name --- src/dj.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dj.c b/src/dj.c index c4ebf38..8dc066a 100644 --- a/src/dj.c +++ b/src/dj.c @@ -165,7 +165,6 @@ int main(int argc, char *argv[]) { size_t i; /* side of io being modified */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ struct Io io[2 /* { in, out } */]; - program_name = (argv[0] == NULL ? program_name : argv[0]); /* Set defaults. */ align = -1; From aed64840ea9adcf332ba482cc0fb53f27cc9a5cd Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 00:12:34 -0600 Subject: [PATCH 02/10] STYLE: fixes some concerns --- STYLE | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/STYLE b/STYLE index 4e35af6..76ecc99 100644 --- a/STYLE +++ b/STYLE @@ -65,7 +65,7 @@ opening curly brace and before a closing one: 8. If a control flow statement is short enough to be easily understood in a glance, it may be placed on a single line: - if (!argc < 0) { usage(program_name); } + if !(argc < 0) { usage(program_name); } 9. In C, note everything you use from a library in a comment subsequent to its #include statement: @@ -86,13 +86,13 @@ library crates. Group alike statements: 11. Do not use do while loops in C. -12. Follow the rules from the paper The Power of 10: Rules for Developing -Safety-Critical Code [0]: +12. Follow the following rules from the paper The Power of 10: Rules for +Developing Safety-Critical Code [0]: 1. Avoid complex flow constructs, such as goto and recursion. 2. All loops must have fixed bounds. This prevents runaway code. 3. Avoid heap memory allocation. - 4. Restrict functions to a single printed page. - 5. Use a minimum of two runtime assertions per function. + 4. Restrict functions to the length of a single printed page. + 6. Restrict the scope of data to the smallest possible. 7. Check the return value of all non-void functions, or cast to void to indicate the return value is useless. @@ -100,7 +100,8 @@ Safety-Critical Code [0]: 9. Limit pointer use to a single dereference, and do not use function pointers. 10. Compile with all possible warnings active; all warnings should then be - addressed before release of the software. + addressed before release of the software (for C compilers, compile with + -Wpedantic). References From d5d13b84a76a07182814bf87be92d05b9fe45cc1 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 00:34:42 -0600 Subject: [PATCH 03/10] STYLE: repition & Kernighan quote --- STYLE | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/STYLE b/STYLE index 76ecc99..5322661 100644 --- a/STYLE +++ b/STYLE @@ -86,7 +86,7 @@ library crates. Group alike statements: 11. Do not use do while loops in C. -12. Follow the following rules from the paper The Power of 10: Rules for +12. Adhere to the following rules from the paper The Power of 10: Rules for Developing Safety-Critical Code [0]: 1. Avoid complex flow constructs, such as goto and recursion. 2. All loops must have fixed bounds. This prevents runaway code. @@ -95,7 +95,8 @@ Developing Safety-Critical Code [0]: 6. Restrict the scope of data to the smallest possible. 7. Check the return value of all non-void functions, or cast to void to - indicate the return value is useless. + indicate the return value is useless (such as in the case of using + fprintf(3p) to print to the standard error). 8. Use the preprocessor sparingly. 9. Limit pointer use to a single dereference, and do not use function pointers. @@ -103,6 +104,12 @@ Developing Safety-Critical Code [0]: addressed before release of the software (for C compilers, compile with -Wpedantic). +13. Remember this quote from The Elements of Programming Style by Brian +Kernighan: + Everyone knows that debugging is twice as hard as writing a program in the + first place. So if you're as clever as you can be when you write it, how + will you ever debug it? + References ========== From 09193a14d5dcdc324387081667c02aa4854bee0b Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 00:35:55 -0600 Subject: [PATCH 04/10] fop(1): fixes unhandled i/o error --- src/fop.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fop.rs b/src/fop.rs index 061815e..0faa827 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -70,7 +70,10 @@ fn main() { }); let mut buf = String::new(); - let _ = stdin().read_to_string(&mut buf); + if let Err(e) = stdin().read_to_string(&mut buf) { + eprintln!("{}: {}", argv[0], e.strerror); + exit(EX_IOERR); + }; /* split the buffer by the delimiter (by default, '\u{1E}') */ let mut fields = buf.split(&d).collect::>(); From 0f121cbac735fb5a43c8106a42f4b38e30fcb269 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 00:40:16 -0600 Subject: [PATCH 05/10] fop(1): more descriptive command arguments variable name --- src/fop.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 0faa827..fc45248 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -79,7 +79,7 @@ fn main() { let mut fields = buf.split(&d).collect::>(); /* collect arguments for the operator command */ - let opts = argv + let command_args = argv .iter() .clone() .skip(command_arg + 1) /* skip the command name */ @@ -87,7 +87,7 @@ fn main() { /* spawn the command to operate on the field */ let mut spawned = Command::new(operator) - .args(opts) /* spawn with the specified arguments */ + .args(command_args) /* spawn with the specified arguments */ .stdin(Stdio::piped()) .stdout(Stdio::piped()) /* piped stdout to handle output ourselves */ .spawn() From d2ae35eac9eca030d9b96252706da8494b7f5b18 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 01:15:08 -0600 Subject: [PATCH 06/10] fop(1): fixes glaring issue with newline handling --- src/fop.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index fc45248..06024d5 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -71,7 +71,7 @@ fn main() { let mut buf = String::new(); if let Err(e) = stdin().read_to_string(&mut buf) { - eprintln!("{}: {}", argv[0], e.strerror); + eprintln!("{}: {}", argv[0], e.strerror()); exit(EX_IOERR); }; @@ -120,8 +120,13 @@ fn main() { /* get the output with which the original field will be replaced */ let mut replace = output.stdout.clone(); - /* as long as it’s not a newline, set the replacement to the output */ - if replace.pop() != Some(b'\n') { replace = output.stdout; } + /* pop trailing newline out if the input did not contain it */ + if fields[index].chars().last() != Some('\n') /* no newline */ + && replace.pop() != Some(b'\n') { /* pop last char of replacement */ + /* restore replacement to original command output if popped char was not + * a newline */ + replace = output.stdout; + } /* convert the output of the program to UTF-8 */ let new_field = String::from_utf8(replace).unwrap_or_else(|e| { From 68e31058a864052d03605b859fa96459167824b8 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 17:49:39 -0600 Subject: [PATCH 07/10] intcmp(1): move program_name ternary --- src/intcmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/intcmp.c b/src/intcmp.c index 35fecf3..88356b4 100644 --- a/src/intcmp.c +++ b/src/intcmp.c @@ -44,10 +44,10 @@ int main(int argc, char *argv[]) { size_t i; unsigned char mode; int r; /* reference integer */ - program_name = (argv[0] == NULL ? program_name : argv[0]); - mode = 0; + program_name = (argv[0] == NULL ? program_name : argv[0]); + if (argc == 0) { return usage(program_name); } while ((c = getopt(argc, argv, "egl")) != -1) { From 6b9d13b8a0dc6a8a072148954e0e222a085c57d8 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 18:27:22 -0600 Subject: [PATCH 08/10] fop(1): better opt matching --- src/fop.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index 06024d5..bbca85e 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -42,12 +42,12 @@ fn main() { while let Some(opt) = argv.getopt("d:") { match opt.opt() { - Ok(_) => { + Ok("d") => { /* delimiter */ d = opt.arg().unwrap(); optind = opt.ind(); }, - Err(_) => { + _ => { eprintln!("{}", usage); exit(EX_USAGE); } From 338a3e715550c84d4049fb802bf000e1bf26a9ad Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 18:31:01 -0600 Subject: [PATCH 09/10] intcmp(1): move program_name ternary --- src/intcmp.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/intcmp.c b/src/intcmp.c index 88356b4..3e329b9 100644 --- a/src/intcmp.c +++ b/src/intcmp.c @@ -46,20 +46,20 @@ int main(int argc, char *argv[]) { int r; /* reference integer */ mode = 0; - program_name = (argv[0] == NULL ? program_name : argv[0]); - - if (argc == 0) { return usage(program_name); } + if (argc < 3) { + return usage(argv[0] == NULL ? program_name : argv[0]); + } while ((c = getopt(argc, argv, "egl")) != -1) { switch (c) { case 'e': mode |= EQUAL; break; case 'g': mode |= GREATER; break; case 'l': mode |= LESSER; break; - default: return usage(program_name); + default: return usage(argv[0]); } } - if (optind + 2 /* ref cmp */ > argc) { return usage(program_name); } + if (optind + 2 /* ref cmp */ > argc) { return usage(argv[0]); } i = optind; @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) { fprintf( stderr, "%s: argument #%d: Invalid integer\n", - program_name, + argv[0], (int)i ); return EX_USAGE; From 44d461fb16ddc9bfb91565ad7417fe7e9f912548 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 18:33:49 -0600 Subject: [PATCH 10/10] scrut(1): return program_name ternary to former position --- src/scrut.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/scrut.c b/src/scrut.c index 4d5a932..39fa698 100644 --- a/src/scrut.c +++ b/src/scrut.c @@ -39,16 +39,15 @@ usage(char *argv0) { int main(int argc, char *argv[]) { char sel[(sizeof opts) / (sizeof *opts)]; - program_name = (argv[0] == NULL ? program_name : argv[0]); - if (argc < 2) { return usage(program_name); } + if (argc < 2) { return usage(argv[0] == NULL ? program_name : argv[0]); } { /* option parsing */ char *p; memset(sel, '\0', sizeof sel); for (int c; (c = getopt(argc, argv, opts)) != -1;) { - if ((p = strchr(opts, c)) == NULL) { return usage(program_name); } + if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); } else { sel[p - opts] = c; } } @@ -62,7 +61,7 @@ int main(int argc, char *argv[]) { } } - if (optind == argc) { return usage(program_name); } + if (optind == argc) { return usage(argv[0]); } for (argv += optind ; *argv != NULL; ++argv) { struct stat buf;