// A version of unjust’s “glaze.c” stripped down to an absolute // minimum: // #include #include #include #include int usage(char *me) { fprintf(stderr, "Usage: %s -w 53 [-h 768] [-x 33] [-j 17]\n" "Copies 768 53-byte records from stdin to stdout,\n" "seeking ahead 17 bytes in stdout between records,\n" "and seeking ahead 33 bytes in stdin before starting.\n" "The records are contiguous in stdin, but generally not in stdout.\n" "This copies a rectangle into an output framebuffer.\n" "Works for any values of 53, 768, 33, and 17. 768 defaults to ∞.\n" "33 and 17 default to 0.\n", me); return 1; } int main(int argc, char **argv) { size_t width = 0, height = 0, offset = 0, skip = 0; char *me = *argv, **argend = argv + argc; while (++argv < argend) { if (0 == strcmp(*argv, "-w")) { width = atoi(*++argv); } else if (0 == strcmp(*argv, "-h")) { height = atoi(*++argv); } else if (0 == strcmp(*argv, "-x")) { offset = atoi(*++argv); } else if (0 == strcmp(*argv, "-j")) { skip = atoi(*++argv); } else { return usage(me); } } if (!width) return usage(me); if (offset) lseek(1, offset, SEEK_CUR); for (size_t i = 0; !height || i != height; i++) { char buf[width]; char *bufp = buf; ssize_t size = 0; while (bufp != buf + width) { size = read(0, bufp, buf + width - bufp); if (0 == size) break; if (size < 0) { perror("read"); return -1; } bufp += size; } write(1, buf, bufp - buf); if (0 == size) break; if (skip) lseek(1, skip, SEEK_CUR); } return 0; }