+++ /dev/null
-/* depend: */
-/* cflags: */
-/* linker: atoi.o fdprintf.o */
-
-#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 */
-
-char *progname = NULL;
-int verbose = 0;
-int nbcols = 4;
-
-char tablechars[256][4] = {
- "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "DEL",
- "DS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
- "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
- "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
- "SP", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "",
- "", "", "", "", "", "", "", "DEL"};
-
-/* help function */
-
-int usage (int ret)
-{
- int fd = ret ? stdfderr : stdfdout;
- 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");
-
- return ret;
-}
-
-/* main function */
-
-int main (int argc, char *argv[])
-{
- int i = 0;
-
- /* programm name */
-
- progname = argv[0];
- while (progname[i] != '\0') {
- if ((progname[i] == '/') || (progname[i] == '\\')) {
- progname += i + 1;
- i = 0;
- } else {
- i++;
- }
- }
-
- /* argument processing */
-
- 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':
- 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':
- PRINTOUT ("%s: version %s\n", progname, VERSION);
- return 0;
- break;
- case 'h':
- default:
- return usage (c != 'h');
- }
- }
-
- /* main */
-
- int n = (256 + nbcols - 1) / nbcols;
- for (i = 0; i < n * nbcols; i++) {
- char line[] = " [ ] x";
- int j = i / nbcols + (i % nbcols) * n;
-
- if (j > 255) {
- PRINTOUT ("\n");
- continue;
- }
-
- if (j > 99) line[0] = '0' + j / 100;
- if (j > 9) line[1] = '0' + (j / 10) % 10;
- line[2] = '0' + j % 10;
- line[4] = '0' + j / 16;
- if (line[4] > '9') line[4] += 'a' - '0' - 10;
- line[5] = '0' + j % 16;
- if (line[5] > '9') line[5] += 'a' - '0' - 10;
-
- if (tablechars[j][0] != '\0') {
- int k = 0;
- while (tablechars[j][k] != '\0') {
- line[8 + k] = tablechars[j][k];
- k++;
- }
- } else {
- line[8] = j;
- }
- line[11] = ((i + 1) % nbcols == 0) ? '\n' : ' ';
-
- PRINTOUT ("%s", line);
- }
-
- return 0;
-}
-
-// test: ascii.exe -h
-// test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
-// test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-
-// test: ascii.exe | awk '/ 64\[40\] @/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe | awk '/127\[7f\] DEL/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 4 | tail -1 | awk '/63/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 5 | tail -1 | awk '/51/ { rc=1 } END { exit (1-rc) }'
-// test: ascii.exe -c 6 | tail -1 | awk '/42/ { rc=1 } END { exit (1-rc) }'
-
-/* vim: set ts=4 sw=4 et: */
/* depend: */
/* cflags: */
-/* linker: */
+/* linker: atoi.o fdprintf.o */
-#include <assert.h>
-#include <getopt.h>
-#include <malloc.h>
+//#include <malloc.h>
+#include <stddef.h>
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
+#include "atoi.h"
+#include "fdprintf.h"
+
/* constants */
//#define BUFFER_SIZE 4096
/* gobal variables */
char *progname = NULL;
-int verbose = 0;
+int verbose = 2;
/* help function */
-void usage (int ret)
+int usage (int ret)
{
- FILE *fd = ret ? stderr : stdout;
- fprintf (fd, "usage: %s\n", progname);
- fprintf (fd, " -h : help message\n");
- fprintf (fd, " -v : verbose level (%d)\n", verbose);
+ int fd = ret ? stdfderr : stdfdout;
+ fdprintf (fd, "usage: %s\n", progname);
+ fdprintf (fd, " -h : help message\n");
+ fdprintf (fd, " -v : verbose level (%d)\n", verbose);
- exit (ret);
+ return ret;
}
/* main function */
{
char buffer[BUFFER_SIZE + 1] = {0};
char *pt = buffer;
- int i, j = 0, n;
+ int i = 0, j = 0, n;
+
+ /* program name */
progname = argv[0];
+ while (progname[i] != '\0') {
+ if ((progname[i] == '/') || (progname[i] == '\\')) {
+ progname += i + 1;
+ i = 0;
+ } else {
+ i++;
+ }
+ }
+
+ /* argument processing */
- int c;
- while ((c = getopt(argc, argv, "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 'v':
- verbose = atoi (optarg);
+ arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+ if (arg == NULL) {
+ PRINTERR ("%s: missing verbose level\n", progname);
+ return usage (1);
+ }
+ verbose = atoi (arg);
break;
case 'h':
- VERBOSE (INFO, usage (0));
- break;
default:
- VERBOSE (ERROR, usage (1));
+ return usage (c != 'h');
}
}
- if (argc - optind != 0) {
- VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]));
- VERBOSE (ERROR, usage (1));
- }
/* read from input stream */
- while ((n = read (STDIN_FILENO, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
- VERBOSE (DEBUG, fprintf (stdout, "read %d bytes\n", n));
+
+ while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) {
+ VERBOSE (DEBUG, PRINTOUT ("read %d bytes\n", n));
n += (pt - buffer);
/* look for end of line */
for (i = 0, j = 0; i < n; i++) {
if (buffer[i] == '\n') {
buffer[i] = 0;
- VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
+ VERBOSE (DEBUG, PRINTOUT ("line(%d): %s\n", j, buffer + j));
+ //fsync (stdfdout);
+ fflush (stdout);
j = i + 1;
}
}
}
}
- /* check that nothing is left behind */
-
- VERBOSE (DEBUG, fprintf (stdout, "last\n"));
- VERBOSE (DEBUG, fprintf (stdout, "line(%d): %s\n", j, buffer + j));
-
return 0;
}