/* Copyright (C) 2004 * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include int main(int argc, char** argv) { int i, ret; int beg, end, pos, next; int seek, count; unsigned char buf[4096]; if (argc != 3) { fprintf(stderr, "Usage: range START END\n"); return EXIT_SUCCESS; } else { next = 0; beg = atoi(argv[1]); end = atoi(argv[2]); if (beg < 0) beg = 0; } while (1) { ret = read(0, buf, 4096); if (ret == -1) { perror("read"); return EXIT_FAILURE; } else if (ret == 0) { return EXIT_SUCCESS; } else { pos = next; next += ret; /* Fall through. */ } if (beg > pos) { /* Don't send beginning of block. */ if (beg >= pos + ret) { /* Send nothing. */ count = 0; } else { seek = beg - pos; if (end >= pos + ret) { /* Send end of block. */ count = ret - seek; } else { /* Send middle of block. */ count = end - beg + 1; } } } else if (end >= pos) { seek = 0; if (end >= pos + ret) { /* Send entire block. */ count = ret; } else { /* Send beginning of block. */ count = end - pos + 1; } } else { /* Send nothing. */ count = 0; } while (count > 0) { ret = write(1, buf + seek, count); if (ret == -1) { perror("write"); return EXIT_FAILURE; } else if (ret == 0) { fprintf(stderr, "write: stdout closed?\n"); return EXIT_FAILURE; } else { seek += ret; count -= ret; } } } return EXIT_SUCCESS; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */