Newer
Older
src / c / rs.c
/*
 * rs: reverse stream
 */

/*
 * 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>

struct _cb;

typedef struct _cb* cb;

typedef struct _cb {
	char *dat;
	int n;
	cb ne;
} _cb;

const char usagemsg[] =
	"usage: rs\n"
	"\n"
	"options:\n"
	"   -n N   set size of internal buffer to N (default is 512)\n"
	" also: rs -v    show version\n"
	"       rs -h    display this help\n"
	"       rs -l    display (BSD) license\n"
	;

const char versionmsg[] = "rs 0.7\n";

const char licensemsg[] =
	"rs 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 rs1(void);
void rs(int ds);

int main ( int argc, char *argv[] ) {
	int ds = 512;

	switch ( getopt(argc, argv, "n:hlv") ) {
		case 'h':
			usage();
			break;
		case 'l':
			license();
			break;
		case 'v':
			version();
			break;
		case 'n':
			ds = atoi(optarg);

			if ( ds < 1 ) {
				usage();
				exit(1);
			}
		case -1:
			if ( ds == 1 ) {
				rs1();
			} else {
				rs(ds);
			}
			break;
		default:
			usage();
			exit(1);
	}
	exit(0);
}

void rs1(void) {
	long int an = 0;
	char buf;
	char *sp = NULL;

	buf = getchar();

	while ( !feof(stdin) ) {
		an++;
		sp = (char*)realloc(sp, sizeof(char)*an);
		*(sp+an) = buf;
		buf = getchar();
	}

	while ( an > 0 ) {
		printf("%c", *(sp+an));
		an--;
	}

	free(sp);
}

void rs(int ds) {
	int cn = 0;
	cb bl = (cb) NULL;
	cb tl = (cb) NULL;
	char *buf = (char*) NULL;
	int lr = 0;
	int i = 0;

	buf = (char*)malloc(sizeof(char)*ds);

	while ( !feof(stdin) ) {
		cn++;
		tl = bl;
		bl = (cb) malloc(sizeof(_cb));
		lr = fread(buf, sizeof(char), ds, stdin);
		bl->dat = (char*)malloc(sizeof(char)*ds);

		for ( i = 0; i < ds && i < lr; i++ ) {
			*(bl->dat+i) = *(buf+i);
		}

		bl->n = lr;
		bl->ne = tl;
	}

	while ( cn > 0 ) {
		for ( i = bl->n; i > 0; i-- ) {
			printf("%c", *(bl->dat+i-1));
		}

		free(bl->dat);

		tl = bl->ne;

		free(bl);
		bl = tl;

		cn--;
	}

	free(bl);
}

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

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

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