/* 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 */
/* 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;
}
line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' ';
- write (STDOUT_FILENO, line, 12);
+ fdprintf (_fd_stdout, "%s", line);
}
return 0;