[comp.sources.misc] v05i038: screens - displays randomly selected file

russ%uokmax@uokmax.ecn.uoknor.edu (Russ 'Random' Smith) (11/09/88)

Posting-number: Volume 5, Issue 38
Submitted-by: "Russ 'Random' Smith" <russ%uokmax@uokmax.ecn.uoknor.edu>
Archive-name: screens

[A quick scan reveals one BSDism (random()).  Otherwise only the terminal
dependency should provide a problem.  ++bsa]

This is an interesting little "toy" that will dsiplay a randomly selected
file from a directory $HOME/.screens.  I wrote it with .logout files in
mind (I still use it in mine), but some enterprising mind might come up with
another use (a terminal lock, perhaps).

There is an interesting feature for Zenith terminal users (check the README
and the man page), which may be expanded for use with ANSI people someday.

---- Cut Here and unpack ----
#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  Makefile
#	  README
#	  screens.6
#	  screens.c
#	  screens.sh
#
sed 's/^X//' << 'SHAR_EOF' > Makefile &&
XDESTBIN = /usr/local
XDESTMAN = /usr/local/doc
X
X# you will probably want to chown screens to bin or root, but it isn't necessary
X
Xcreate:		screens screens.man
X
Xinstall:	screens screens.man
X		mv screens $(DESTBIN)
X		mv screens.man $(DESTMAN)
X		
Xscreens:	screens.c
X		cc -O -o screens screens.c
X		strip screens
X		chmod 711 screens
X
Xscreens.man:	screens.6
X		nroff -man screens.6 > screens.man
SHAR_EOF
chmod 0640 Makefile || echo "restore of Makefile fails"
sed 's/^X//' << 'SHAR_EOF' > README &&
XScreens is a simple utility to grab a random "screen" from the directory
Xgiven by $HOME/.screens and put it on the terminal screen.  Normally, it's
Xan ascii file, but if you have files in that directory that end in ".g",
Xand have a terminal with Zenith emulation, it will display that file in
XZenith graphics mode.  Naturally, you can always put the graphics escape
Xcodes for whatever terminal in your file.
X
XAny bug reports go to russ@uokmax.UUCP or texsun!uokmax!russ.  Thanks.
X
X
X
SHAR_EOF
chmod 0640 README || echo "restore of README fails"
sed 's/^X//' << 'SHAR_EOF' > screens.6 &&
X.TH SCREENS 6 "Local Manual                 7/19/88"
X.SH NAME
Xscreens \- Random screen displayer
X.SH SYNOPSIS
X.B screens
X.SH DESCRIPTION
X.I Screens 
Xscans your
X.BI $HOME/.screens
Xdirectory, and picks a random file to display
Xon the screen.  If the filename ends with
X.I .g,
Xit turns on z29a graphics
Xmode first.
X.SH FILES
X$HOME/.screens/*
X.SH BUGS
XIf you have some mutant terminal type which starts with z but isn't zenith
Xgraphics compatible,
X.B screens
Xmay select a zenith screen (if you have one).
X.SH AUTHOR
XRuss Smith
SHAR_EOF
chmod 0640 screens.6 || echo "restore of screens.6 fails"
sed 's/^X//' << 'SHAR_EOF' > screens.c &&
X#include <stdio.h>
X#include <strings.h>
X#include <sys/types.h>
X#include <sys/dir.h>
X#include <sys/stat.h>
X
Xmain()
X{
X	int i,q,filno;
X	int graph = 0;
X	struct direct **nlist;
X	int select();
X	char dirname[80],fname[80];
X
X	srandom(time(0));
X	strcpy(dirname,getenv("HOME"));
X	strcat(dirname,"/.screens");
X	if (chdir(dirname)) {
X	    fprintf(stderr,"mode: You must have a $HOME/.screens directory.\n");
X	    exit(1);
X	}
X	q = scandir(dirname,&nlist,select,NULL);
X	if (q < 1) {
X		fprintf(stderr,"mode: You must have at least one file in ");
X		fprintf(stderr,"your $HOME/.screens directory.\n");
X		exit(1);
X	}
X	filno = ((random()>>4)%q);
X	strcpy(fname,nlist[filno]->d_name);
X	if ((strind(fname,".g") == strlen(fname) - 2) && (!strncmp(getenv("TERM"),"z",1)))
X		graph = 1;
X	if (graph)
X		printf("\033[10m");
X	shofile(fname);
X	if (graph)
X		printf("\033[11m");
X	exit(0);
X}
X
Xint select(dirpt)
Xstruct direct *dirpt;
X{
X	struct stat s;
X
X	stat(dirpt->d_name,&s);
X	if ((s.st_mode & S_IFMT) == S_IFREG)
X		return(1);
X	else
X		return(0);
X}
X
Xint strind(str1,str2)
Xchar *str1,*str2;
X
X{
X	int c,res;
X
X	res = 0;
X	if (str1 != NULL)
X		for (c=0; c<=strlen(str1)-strlen(str2); c++)
X			if (!strncmp(&str1[c],str2,strlen(str2)))
X				return(c);
X}
X
Xnochar(string,c)
Xchar *string,c;
X
X{
X	char *p;
X
X	for (p=string;*p != c && *p != '\0'; ++p)
X		;
X
X	*p = '\0';
X
X	return;
X}
X
Xshofile(fname)
Xchar *fname;
X
X{
X	FILE *f;
X	int c;
X
X	f = fopen(fname,"r");
X	
X	while ((c=fgetc(f)) != EOF)
X		putchar(c);
X}
X
SHAR_EOF
chmod 0640 screens.c || echo "restore of screens.c fails"
sed 's/^X//' << 'SHAR_EOF' > screens.sh &&
SHAR_EOF
chmod 0640 screens.sh || echo "restore of screens.sh fails"
exit 0