ascii is working
[ascii.git] / ascii.c
diff --git a/ascii.c b/ascii.c
index 5eeac21c7c52b719f0ad4d6df202d58ea5ff29ce..c15d71bc1021bcbcb5c3bbc253a00f07584e0636 100644 (file)
--- a/ascii.c
+++ b/ascii.c
@@ -1,16 +1,16 @@
 /* depend: */
 /* cflags: */
-/* linker: */
+/* linker: atoi.o fdprintf.o */
 
-#include <assert.h>
-#include <getopt.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
+#include <stddef.h>
+
+#include "atoi.h"
+#include "fdprintf.h"
 
 /* macros */
 
 #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
+#define VERSION "0.9"
 
 /* gobal variables */
 
@@ -38,46 +38,63 @@ char tablechars[256][4] = {
 
 /* help function */
 
-void usage (int ret)
+int usage (int ret)
 {
-    FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s\n", progname);
-    fprintf (fd, " -c : number of columns (%d)\n", nbcols);
-    fprintf (fd, " -h : help message\n");
-    fprintf (fd, " -v : verbose level (%d)\n", verbose);
+    int fd = ret ? _fd_stderr : _fd_stdout;
+    fdprintf (fd, "usage: %s\n", progname);
+    fdprintf (fd, " -c : number of columns (%d)\n", nbcols);
+    fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -v : show version\n");
 
-    exit (ret);
+    return ret;
 }
 
 /* main function */
 
 int main (int argc, char *argv[]) 
 {
-    int i;
+    int i = 0;
 
     progname = argv[0];
+    while (progname[i] != '\0') {
+        if ((progname[i] == '/') || (progname[i] == '\\')) {
+            progname += i + 1;
+            i = 0;
+        } else {
+            i++;
+        }
+    }
 
-    int c;
-    while ((c = getopt(argc, argv, "c:hv:")) != EOF) {
+    while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            PRINTERR ("%s: invalid option -- %s\n", progname, arg);
+            return usage (1);
+        }
+        char c = arg[1];
         switch (c) {
         case 'c':
-            nbcols = atoi (optarg);
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                PRINTERR ("%s: missing number of columns\n", progname);
+                return usage (1);
+            }
+            nbcols = atoi (arg);
             break;
         case 'v':
-            verbose = atoi (optarg);
+            PRINTOUT ("%s: version %s\n", progname, VERSION);
+            return 0;
             break;
         case 'h':
         default:
-            usage (c != 'h');
+            return usage (c != 'h');
         }
     }
-    if (argc - optind != 0) {
-        fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
-        usage (1);
-    }
+
+    /* main */
 
     for (i = 0; i < 256; i++) {
-        char line[] = "   :  :    x";
+        char line[] = "   [  ]    x";
 
         if (i > 99) line[0] = '0' + i / 100;
         if (i > 9) line[1] = '0' + (i / 10) % 10;
@@ -98,7 +115,7 @@ int main (int argc, char *argv[])
         }
         line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' ';
 
-        write (STDOUT_FILENO, line, 12);
+        fdprintf (_fd_stdout, "%s", line);
     }
 
     return 0;