DataMuseum.dk

Presents historical artifacts from the history of:

Commodore CBM-900

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about Commodore CBM-900

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦468030354⟧ TextFile

    Length: 1185 (0x4a1)
    Types: TextFile
    Notes: UNIX file
    Names: »from.c«

Derivation

└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
    └─⟦f4b8d8c84⟧ UNIX V7 Filesystem
        └─ ⟦this⟧ »cmd/from.c« 

TextFile

/*
 * from - generate a list of numbers
 * usage - from number to number [ by number ]
 */
#include <stdio.h>

#define sign(x)	(x<0?-1:1)	/* returns sign of number */

char usage[] = "Usage: from number to number [by number]\n";
char signmsg[] = "Increment has wrong sign\n";

main(argc, argv)
char *argv[];
{
	int i;
	int start, end, incr;

	switch ( argc ) {

	case 4:
		incr = 1;
		break;

	case 6:
		if ( strcmp(argv[4], "by") )
			error(usage);
		else
			if ( (incr = numeric(argv[5])) == 0 )
				error("Increment must be non-zero\n");
		break;

	default:
		error(usage);
		break;
	}
	if ( strcmp(argv[2], "to") )
		error(usage);
	start = numeric(argv[1]);
	end = numeric(argv[3]);
	if ( sign(end-start) * sign(incr) < 0 )
		error(signmsg);

	if ( incr > 0 )
		for ( i = start; i <= end; i += incr )
			printf("%d\n", i);
	else
		for ( i = start; i >= end; i += incr )
			printf("%d\n", i);
}

/*
 * Return the value of a numeric arg;
 * otherwise, call usage().
 */
numeric(s)
register char *s;
{
	register int n;

	n = atoi(s);
	if ( *s == '-' )
		s++;
	for ( ; *s; s++)
		if (*s<'0' || *s>'9')
			error(usage);
	return(n);
}

error(x)
{
	fprintf(stderr, "%r", &x);
	exit(1);
}