> Dear Quentin, Hi Laslo,
> > I agree, yes is neither a benchmark tool nor a data generator, it just > > outputs 'y' for piping into other commands. > > Keep it simple. > > I agree with you in general, but are we really "optimizing" yes(1) here > for the sake of performance? This looks to me like a case of premature > optimization. To be honest yes, this reads as bikeshedding, but on the other hand, a simple tool as yes is quite complicated, at least for an sbase implementation. > We don't know if a script relies on the behaviour of yes(1) printing > all passed strings repeatedly. So, even though the tool is not > standardized by Posix the common "consensus" is that > > - with no arguments, it shall print y and a newline repeatedly. > - with arguments, it shall print them, comma separated, followed by > newline, repeatedly. Where did you get this was the common consensus? This is the GNU behaviour, ie not a consensus. The yes(1) rationale is to output a 'y' character for piping into interactive tool's stdin and make them batchable. The tool lets you output a custom string which would be passed to its argument, so when a user wants yes to output anything else, it's a simple matter of passing it on the command line. The consensus here is that yes takes a parameter, at least one. So if one wants to output a string, that's simple: yes 'my very long string with arguments' or yes "$foo $bar $baz" Done. > The point about readability is a good one. I will even take the blame > for writing it as it is now. However, you could greatly improve > readability without sacrificing features/performance. The line > > argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; > > is used in many places in sbase and is more or less an idiom. The > entire tool itself could then be written as follows. We can even > remove the checks consequently within the loop, which could be regarded > as a "performance" improvement. > > int i; > > argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; > > if (argc == 0) { > for (;;) { > fputs("y\n", stdout); > } > } else { > for (;;) { > for (i = 0; i < argc; i++) { > fputs(argv[i], stdout); > fputc(' ', stdout); > } > fputc('\n', stdout); > } > } And then this becomes quite simple, if yes gets argc > 1, printf argv[1], otherwise print 'y'. > What do you think? Patch is attached. I think this answers the question ;)