[comp.os.minix] p.c for Minix

wayne@csri.toronto.edu (Wayne Hayes) (07/23/90)

Here is p(1) for Minix.  It is a simple replacement for more(1), because
I'm sick of the long delay it takes more to start up.  Has anyone else
wondered why such a simple program takes so loooong to start up?  Sheesh.

    For p(1), you just press return for each new screen or press q{return}
to quit.  It accepts piped input, and accepts an optional single option
to set the screen size in lines (default 23, which allows 1 or 2 lines
overlap between screens).  It'll also take an arbitrary number of files on
the command line.  It should also compile on any UNIX box.  Hey, now that
I think of it, it'll probably compile *anywhere*, it's not that specialized...

USAGE: p [-linesPerPage] [file_list]

---
"The number of programs that can be done with the Hubble Space Telescope has
always greatly exceeded the time available for their execution, and this
remains true even with the telescope in its current state." -- HST Science
Working Group and User's Commitee Report, 1990 June 29.
Wayne Hayes	INTERNET: wayne@csri.utoronto.ca	CompuServe: 72401,3525

----------------------------- cut here -----------------------------------
/*********** p.c for Minix, by Wayne Hayes  wayne@csri.utoronto.ca  ********/

#include <stdio.h>
#include <ctype.h>

char USAGE[] = "USAGE: p [-linesPerPage] [filename1] [filename2] [...]";

void Fatal(s)
char *s;
{
	printf("%s\n", s);
	exit(1);
}

int cookedstrlen(s)
char *s;
{	int len;
	for(len = 0; *s; s++)
		if(*s == '\t') len += 8 - len % 8;
		else len++;
	return len;
}

#define MAXLINELEN 80
int MAXLINES = 23;

static FILE *con;

void Wait(fp)
FILE *fp;
{
	int c = getc(con);	/* each char moves a screen */
	if(c == 'q')
	{
		fclose(fp);
		fclose(con);
		exit(0);
	}
}

void p(fp)
FILE *fp;
{	char line[MAXLINELEN];
	int n = 0, len;
	while(fgets(line, sizeof(line), fp))
	{	len = strlen(line);
		if(line[len - 1] == '\n') line[len - 1] = '\0';
		len = cookedstrlen(line);
		n += 1 + len / sizeof(line);
		printf("%s", line);
		if(n < MAXLINES) putchar('\n');
		else
		{	n = 0;
			Wait(fp);
		}
	}
}

main(argc, argv)
char *argv[];
{	FILE *in;
	int i = 1;

	con = fopen("/dev/tty", "r");

	if(argc > 1 && argv[1][0] == '-')
	{
		MAXLINES = atoi(argv[1] + 1);
		if(argv[1][1] == 'h' || argv[1][1] == '?' || MAXLINES < 1)
			Fatal(USAGE);
		i++;
	}

	if(argc == i) p(stdin);
	else for( ; i < argc; i++)
	{
		if((in=fopen(argv[i], "r")) == NULL) perror(argv[i]);
		else
		{
			printf("%s\n", argv[i]);
			p(in);
			if(i < argc - 1)
			{
				printf("--EOF--");
				Wait(in);
				fclose(in);
			}
		}
	}
	fclose(con);
}

paula@bcsaic.UUCP (Paul Allen) (07/24/90)

In article <1990Jul23.011015.15237@jarvis.csri.toronto.edu> wayne@csri.toronto.edu (Wayne Hayes) writes:
>Here is p(1) for Minix.  It is a simple replacement for more(1), because
>I'm sick of the long delay it takes more to start up.  Has anyone else
>wondered why such a simple program takes so loooong to start up?  Sheesh.

I used to see this kind of behavior back in 1.5.2.  There was a bug 
someplace that caused the buffer cache to thrash if you tried to 
read a file that was larger than the cache.  (Or something like that.
I didn't actually track down the bug.)  More(1) takes less than a 
second to start up under 1.5.10, but it took many seconds under 1.5.2.

P(1) looks interesting.  I'll put it on my list of stuff to try out.

Paul Allen
-- 
------------------------------------------------------------------------
Paul L. Allen                       | pallen@atc.boeing.com
Boeing Advanced Technology Center   | ...!uw-beaver!bcsaic!pallen

nall@sun8.scri.fsu.edu (John Nall) (07/25/90)

In article <28577@bcsaic.UUCP> paula@bcsaic.UUCP (Paul Allen) writes:
>In article <1990Jul23.011015.15237@jarvis.csri.toronto.edu> wayne@csri.toronto.edu (Wayne Hayes) writes:
>>Here is p(1) for Minix.  It is a simple replacement for more(1), because
>>I'm sick of the long delay it takes more to start up.  Has anyone else
>>wondered why such a simple program takes so loooong to start up?  Sheesh.
>
>I used to see this kind of behavior back in 1.5.2.  There was a bug 
>someplace that caused the buffer cache to thrash if you tried to 
>read a file that was larger than the cache.  (Or something like that.
>I didn't actually track down the bug.)  More(1) takes less than a 
>second to start up under 1.5.10, but it took many seconds under 1.5.2.
>

There is a nagging memory that the behavior back in 1.5.2 etc was 
cured (for me, anyway) with a chmem of something.  Perhaps "more", I
dunno.  (General rule for Minix novices: try a chmem!)  When I fixed
it, it cured several things.



--
John W. Nall		| Supercomputation Computations Research Institute
nall@sun8.scri.fsu.edu  | Florida State University, Tallahassee, FL 32306
     "Los dioses eran computadores.  ?Que otra cosa podian ser??"