This is an automated email from the git hooks/post-receive script. daube-guest pushed a commit to branch master in repository hail.
commit a8a6abfeef5edc6ab3157bef3d0d8b0c01903cec Author: Kevin Murray <[email protected]> Date: Sun Jul 12 16:11:13 2015 +1000 Imported Upstream version 0.1.0 --- .gitignore | 2 ++ Makefile | 17 +++++++++++++++ README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hail.1 | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ hail.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 206 insertions(+) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d0a49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +hail +*.sw* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..09b4197 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +CC ?= gcc +CFLAGS +=-O3 -std=gnu99 +PREFIX ?= $(HOME) + +all: hail + +hail: hail.c + $(CC) $(CFLAGS) -o $@ $< + +install: hail hail.1 + mkdir -p $(PREFIX)/bin + mkdir -p $(PREFIX)/man/man1 + cp hail $(PREFIX)/bin + cp hail.1 $(PREFIX)/man/man1 + +clean: + rm -f hail diff --git a/README.md b/README.md new file mode 100644 index 0000000..db18ebd --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +hail +==== + +Extract lines from a file + + +Installation +------------ + +Hail has no dependencies other than a C compiler which implements the GNU-C99 +standard. To install: + + git clone https://github.com/kdmurray91/hail.git + cd hail + make + make install + + +About +----- + +Hail gets its name from a contraction of `head` and `tail`, the common +alternative to this program. It extracts lines from a file, taken on stdin, +and prints them on stdout. It's so simple that the Unix wizards of old never +bothered. But I'm lazy and prone to procrastination. + +Hail takes any number of `x`-`y` pairings as arguments. These pairs are +integral numbers, corresponding to the first and last lines to print. The +final number may be excluded to indicate that the remainder of the file +should be printed + + +Examples +-------- + + # prints 1 through 10, i.e. does nothing + seq 1 10 | hail 1- + + # prints 1 through 3, i.e. equivalent to `head -n 3` + seq 1 10 | hail 1-3 + + # prints 3 through 5, i.e. equivalent to `head -n 5 | tail -n 3` + seq 1 10 | hail 3-5 + + # prints 5 through 10, i.e. equivalent to `tail -n 6` + seq 1 10 | hail 5- + + # prints 2, 3, 5 and 7, which is where I'll give up on my comparisons to + # head and tail + seq 1 10 | hail 2-3 5-5 7-7 + + +License +------- + +Copyright 2015 Kevin Murray <[email protected]> + +Licensed under the GNU General Public License, version 3 or (at your option) +any later version. diff --git a/hail.1 b/hail.1 new file mode 100644 index 0000000..a5795e7 --- /dev/null +++ b/hail.1 @@ -0,0 +1,58 @@ +.TH "HAIL" "1" "February 2015" "" "" +. +.SH "NAME" +\fBhail\fR \- Extract lines from a file +. +.SH "SYNOPSIS" +\fBhail\fR \fIFIRST\fR\-\fILAST\fR \.\.\. +. +.SH "DESCRIPTION" +Hail gets its name from a contraction of \fBhead\fR and \fBtail\fR, the common +alternative to this program\. It extracts lines from a file, taken on stdin, +and prints them on stdout\. It\'s so simple that the Unix wizards of old never +bothered\. But I\'m lazy and prone to procrastination\. +. +.SH "SYNTAX" +Hail takes any number of \fBx\fR\-\fBy\fR pairings as arguments\. These pairs +are (1-based) integral numbers, corresponding to the first and last lines to +print\. The final number may be excluded to indicate that the remainder of the +file should be printed +. +.SH "EXAMPLES" +# prints 1 through 10, i\.e\. does nothing +. +.P +seq 1 10 | hail 1\- +. +.P +# prints 1 through 3, i\.e\. equivalent to \fBhead \-n 3\fR +. +.P +seq 1 10 | hail 1\-3 +. +.P +# prints 3 through 5, i\.e\. equivalent to \fBhead \-n 5 | tail \-n 3\fR +. +.P +seq 1 10 | hail 3\-5 +. +.P +# prints 5 through 10, i\.e\. equivalent to \fBtail \-n 6\fR +. +.P +seq 1 10 | hail 5\- +. +.P +# prints 2, 3, 5 and 7, which is where I\'ll give up on my comparisons to # head and tail +. +.P +seq 1 10 | hail 2\-3 5\-5 7\-7 +. +.SH "AUTHOR" +Kevin Murray (daube) \fIspam@kdmurray\.id\.au\fR +. +.SH "COPYRIGHT" +Copyright 2015 Kevin Murray \fIspam@kdmurray\.id\.au\fR +. +.P +Licensed under the GNU General Public License, version 3 or (at your option) any later version\. diff --git a/hail.c b/hail.c new file mode 100644 index 0000000..0d010f0 --- /dev/null +++ b/hail.c @@ -0,0 +1,70 @@ +/* + * ============================================================================ + * + * Filename: hail.c + * Description: Extract lines x to y + * License: GPLv3+ + * Author: Kevin Murray, [email protected] + * + * ============================================================================ + */ + +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> + + +/* Defined as a macro to allow use of argv[0]. */ +#define usage() \ + fprintf(stderr, "USAGE: %s <first>-<last> [<first>-<last ...]\n", \ + argv[0]) + +int +main(int argc, const char **argv) +{ + FILE *fp = stdin; + char *buf = NULL; + size_t len = 0; + ssize_t res = 0; + size_t lineno = 0; + size_t first, last; + int i = 0; + + /* We expect to have at least one pair of start-finish line nums. */ + if (argc < 2) { + usage(); + return EXIT_FAILURE ; + } + + /* For each pair of <start>-<finish> */ + for (i = 1; i < argc; i++) { + /* Parse the <x>-<y> from argv, storing how many elements we parsed. */ + res = sscanf(argv[i], "%zu-%zu", &first, &last); + /* we expect at least one value, so error out if we don't have one. */ + if (res < 1) { + putchar('\n'); + usage(); + fflush(stdout); + free(buf); + return EXIT_FAILURE ; + } + /* We have something like 2-, so we go till the last line. */ + if (res == 1) { + last = SIZE_MAX; + } + /* Main brains of hail: Grab line and print it if it's in the range. */ + while (lineno < last) { + if (getline(&buf, &len, fp) < 1) { + /* EOF or error */ + break; + } + if (++lineno >= first) { + fputs(buf, stdout); + } + } + } + /* Clean up and exit nicely */ + fflush(stdout); + free(buf); + return EXIT_SUCCESS ; +} -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/hail.git _______________________________________________ debian-med-commit mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/debian-med-commit
