6414c2df28f98b7b5b8ffdf1574a6abbda7d3960
3 /* linker: atoi.o fdprintf.o */
15 //#define BUFFER_SIZE 4096
16 #define BUFFER_SIZE 256
20 #define CEIL(x, y) (((x) + (y) - 1) / (y))
21 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
22 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
31 #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
35 char *progname
= NULL
;
42 int fd
= ret
? stdfderr
: stdfdout
;
43 fdprintf (fd
, "usage: %s\n", progname
);
44 fdprintf (fd
, " -h : help message\n");
45 fdprintf (fd
, " -v : verbose level (%d)\n", verbose
);
52 int main (int argc
, char *argv
[])
54 char buffer
[BUFFER_SIZE
+ 1] = {0};
61 while (progname
[i
] != '\0') {
62 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
70 /* argument processing */
73 char *arg
= *(++argv
);
75 PRINTERR ("%s: invalid option -- %s\n", progname
, arg
);
81 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
83 PRINTERR ("%s: missing verbose level\n", progname
);
90 return usage (c
!= 'h');
94 /* read from input stream */
96 while ((n
= read (stdfdin
, pt
, BUFFER_SIZE
- (pt
- buffer
))) != 0) {
97 VERBOSE (DEBUG
, PRINTOUT ("read %d bytes\n", n
));
100 /* look for end of line */
101 for (i
= 0, j
= 0; i
< n
; i
++) {
102 if (buffer
[i
] == '\n') {
104 VERBOSE (DEBUG
, PRINTOUT ("line(%d): %s\n", j
, buffer
+ j
));
111 /* keep remainding */
113 for (i
= 0; i
< n
- j
; i
++) {
114 buffer
[i
] = buffer
[i
+ j
];
117 for (i
= n
- j
; i
< BUFFER_SIZE
; i
++) {
127 // test: calc.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
128 // test: calc.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
129 // test: calc.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
130 // test: echo "foo\nbar\nfoobar" | calc.exe -v3
132 /* vim: set ts=4 sw=4 et: */