commit 4863f6fd6ec7f2717be692641b88a88bce9db8df
parent a1553cc74c7c646df407a21ae515773214fde8dc
Author: Tim Kuijsten <info+git@netsend.nl>
Date: Fri, 13 Dec 2019 17:36:01 +0100
more idiomatic printing of usage
Diffstat:
M | rpass.c | | | 26 | ++++++++++++++++++++++---- |
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/rpass.c b/rpass.c
@@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <libgen.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
@@ -28,26 +29,43 @@ static const char secon[] = "aeiou"; /* vowels, 2.3 bpc */
static const char third[] = "fhjklmnprstxz"; /* drop b, c, d, g, q, v, w,
* y = 3.7 bpc */
+static const char *progname;
+
+static void
+printusage(int d)
+{
+ dprintf(d, "usage: %s [bitlen]\n", progname);
+}
+
int
main(int argc, char **argv)
{
double bits, fbpc, sbpc, tbpc;
int c;
+ if ((progname = basename(argv[0])) == NULL) {
+ perror("basename");
+ exit(1);
+ }
+
while ((c = getopt(argc, argv, "h")) != -1)
switch (c) {
case 'h':
- case '?':
- fputs("usage: rpass [bitlen]\n", stdout);
+ printusage(STDOUT_FILENO);
exit(0);
default:
- fputs("usage: rpass [bitlen]\n", stderr);
+ printusage(STDERR_FILENO);
exit(1);
}
argc -= optind;
argv += optind;
+ if (argc > 1) {
+ printusage(STDERR_FILENO);
+ exit(1);
+ }
+
fbpc = log2(sizeof first - 1);
sbpc = log2(sizeof secon - 1);
tbpc = log2(sizeof third - 1);
@@ -59,7 +77,7 @@ main(int argc, char **argv)
}
if (bits <= 0) {
- fputs("usage: rpass [bitlen]\n", stderr);
+ printusage(STDERR_FILENO);
exit(1);
}