[Toybox] [CLEANUP] More fun with find.

Rob Landley rob at landley.net
Wed Apr 10 21:00:57 PDT 2013


It occurs to me I should have a tag for these.

   http://landley.net/hg/toybox/rev/849

The only user of toybuf[] was copying constant strings into it.  
Environment strings never get freed, and constant "." never gets freed  
either, so we can just use a char * and assign to it. Since it's  
getting used in multiple functions, allocate a GLOBALS() block and add  
"char *dir;" to it.

(This frees up toybuf for use in whatever exec_buf[] was doing, if that  
comes back and turns out to be a good idea.)

In do_exec, initialize path to NULL when declared, so we don't need an  
else case to initialize it. (The assignment code is there either way,  
extra cache-local assignment takes essentially no time, and a single  
conditional assignment is actually faster on desktop processors.)

Combine the fork/if/xexec into a single line. Note that xexec() does  
not return (it perror_exit()s if it can't run the command), so we don't  
need an "else" case because the child will never fall through. Move the  
free() earlier so we don't need a variable for the WEXITSTATUS() and  
can just directly return it.

Remove the curly brackets from a lot of:

   if (test) {
     only_one_line();
   }

Moving the second line to the end of the first where it won't go over  
80 chars.

Turn some /* single line comments */ into // single line comments
(Not a huge deal, but as long as we've got 'em in c99...)

I mentioned this trick earlier:

-    if (filter->op==ACTION_PRINT) {
-      terminator = '\n';
-    } else {
-      terminator = 0;
-    }
+    terminator = (filter->op==ACTION_PRINT)*'\n';

Since == return 0 or 1 according to c99, I can multiple the result by a  
constant, and '\n' is an integer constant...

(I could have done ? : instead but that puts a branch in and this  
avoids a branch.)

In build_filter_list(), combine a bunch of "struct filter_node"  
declarations into a single comma-separated list.

Remove \n from the end of error_exit() calls. Those functions already  
add a newline to the end of the string.

Replace a big switch() statement where each case checks for letter and  
assigns a corresponding integer to the same variable. Instead, glue  
those characters together into a string, and declare a mode_t types[]  
array with the values in the same order as the letter occur in the  
string. Call stridx() on the string, doing the error_exit() from the  
default case if it returns -1 (character not found), otherwise assign  
the appropriate entry from the array.

Simplify error_exit() strings for non-english speakers:

   "Missing ';' in exec command\n" -> "need ';' in exec"
   "Unknown option '%s'\n" -> "bad option '%s'"
   "Missing right paren\n" -> "need ')'"

Rob


More information about the Toybox mailing list