Newer
Older
src / c / rl.c
/*
 * rl: remove line(s)
 */

/*
 * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

const char usagemsg[] =
	"usage: rl [-t separator] [-n lines]\n"
	"\n"
	"options:\n"
	"    -t C   use character C as separator instead of \\n\n"
	"    -n N   remove N lines\n"
	" also: rl -v    show version\n"
	"       rl -h    display this help\n"
	"       rl -l    display (BSD) license\n"
	;

const char versionmsg[] = "rl 0.6\n";

const char licensemsg[] =
	"rl is copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>\n"
	"All rights reserved.\n"
	"\n"
	"Redistribution and use in source and binary forms, with or without\n"
	"modification, are permitted provided that the following conditions\n"
	"are met:\n"
	"1. Redistributions of source code must retain the above copyright\n"
	"   notice, this list of conditions and the following disclaimer.\n"
	"2. Redistributions in binary form must reproduce the above copyright\n"
	"   notice, this list of conditions and the following disclaimer in the\n"
	"   documentation and/or other materials provided with the distribution.\n"
	"3. The name of the author may not be used to endorse or promote products\n"
	"   derived from this software without specific prior written permission.\n"
	"\n"
	"THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
	"IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
	"OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
	"IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
	"INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n"
	"NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
	"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
	"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
	"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n"
	"THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
	;

void usage(void);
void version(void);
void license(void);
void rl(int n, char sep);

int main ( int argc, char *argv[] ) {
	int n = 1;
	int r = -1;
	char sep = '\n';

	while ( (r = getopt(argc, argv, "n:t:hlv")) != -1 ) {
		switch ( r ) {
			case 'h':
				usage();
				exit(0);
			case 'l':
				license();
				exit(0);
			case 'v':
				version();
				exit(0);
			case 'n':
				n = atoi(optarg);

				if ( n < 0 ) {
					usage();
					exit(1);
				}
				break;
			case 't':
				sep = optarg[0];

				if ( optarg[1] != '\0' ) {
					usage();
					exit(1);
				}
				break;
			default:
				usage();
				exit(1);
		}
	}

	rl(n, sep);

	exit(0);
}

void rl(int n, char sep) {
	char buf;

	if ( n > 0 ) {
		do {
			if ( getchar() == sep ) {
				n--;
			}
		} while ( n > 0 && !feof(stdin) );
	}

	if ( !feof(stdin) ) {
		buf = getchar();
	}

	while ( !feof(stdin) ) {
		printf("%c",buf);
		buf = getchar();
	}
}

void usage(void) {
	fprintf(stdout, usagemsg);
}

void version(void) {
	fprintf(stdout, versionmsg);
}

void license(void) {
	fprintf(stdout, licensemsg);
}