Subject: Make /usr/bin/from also look for rfc822 header fields
To: None <current-users@NetBSD.ORG>
From: Jarle Greipsland <jarle@idt.unit.no>
List: current-users
Date: 01/07/1995 19:21:45
Hi, I've just made some minor modifications to /usr/bin/from (diffs below).
I got tired of only seeing the envelope From field when what I really want
to see is the real sender and the subject field.

I added a -h option to make /usr/bin/from search and print From: and
Subject: header fields from the mailbox/file instead of the default
envelope From field.

I tried to get by with as little work as possible, so there is probably
much room for improvement (e.g. look for a possibly present Sender:
field).......  If you don't like it, or there is a vastly superior way of
displaying the From: and Subject: fields,/dev/null is probably a nice place
to archive this message :-)

					-jarle
----
"You are in a twisty maze of Sendmail rules, all obscure..."
				-- Dwight E. Cass (dec@nrtc.northrop.com)
================================================================
*** from.c.orig	Fri Jan  6 17:54:37 1995
--- from.c	Sat Jan  7 19:08:10 1995
***************
*** 48,51 ****
--- 48,52 ----
  #include <stdio.h>
  #include <stdlib.h>
+ #include <string.h>
  #include <unistd.h>
  #include <paths.h>
***************
*** 58,75 ****
  	extern int optind;
  	struct passwd *pwd;
! 	int ch, newline;
  	char *file, *sender, *p;
  #if MAXPATHLEN > BUFSIZ
! 	char buf[MAXPATHLEN];
  #else
! 	char buf[BUFSIZ];
  #endif
  
  	file = sender = NULL;
! 	while ((ch = getopt(argc, argv, "f:s:")) != EOF)
  		switch((char)ch) {
  		case 'f':
  			file = optarg;
  			break;
  		case 's':
  			sender = optarg;
--- 59,81 ----
  	extern int optind;
  	struct passwd *pwd;
! 	int ch, newline, inheader, useheader;
  	char *file, *sender, *p;
  #if MAXPATHLEN > BUFSIZ
! 	char buf[MAXPATHLEN], from[MAXPATHLEN], subject[MAXPATHLEN];
  #else
! 	char buf[BUFSIZ], from[BUFSIZ], subject[BUFSIZ];
  #endif
  
  	file = sender = NULL;
! 	useheader = inheader = 0;
! 	from[0] = subject[0] = '\0';
! 	while ((ch = getopt(argc, argv, "f:hs:")) != EOF)
  		switch((char)ch) {
  		case 'f':
  			file = optarg;
  			break;
+ 		case 'h':
+ 			useheader = 1;
+ 			break;
  		case 's':
  			sender = optarg;
***************
*** 120,128 ****
  		if (*buf == '\n') {
  			newline = 1;
  			continue;
  		}
! 		if (newline && !strncmp(buf, "From ", 5) &&
! 		    (!sender || match(buf + 5, sender)))
! 			printf("%s", buf);
  		newline = 0;
  	}
--- 126,159 ----
  		if (*buf == '\n') {
  			newline = 1;
+ 			/* If done with header and we have a match */
+ 			if (inheader && from[0]) {
+ 				if (p = strchr(from, '\n'))
+ 					*p = '\0';
+ 				if (p = strchr(subject, '\n'))
+ 					*p = '\0';
+ 				if (useheader)
+ 					snprintf(from, 79, "%s  : %s",
+ 						 from, subject);
+ 				printf("%s\n", from);
+ 			}
+ 			inheader = 0;
+ 			from[0] = subject[0] = '\0';
  			continue;
  		}
! 		if (newline && !strncmp(buf, "From ", 5)) {
! 			inheader = 1;
! 			if (!sender || match(buf + 5, sender))
! 				strncpy(from, buf, sizeof(from));
! 		}
! 
! 		if (useheader && inheader && !strncmp(buf, "From: ", 6)) {
! 			from[0] = '\0';
! 			if (!sender || match(buf+6, sender))
! 				strncpy(from, buf+6, sizeof(from));
! 		}
! 		
! 		if (useheader && inheader && !strncmp(buf, "Subject: ", 9))
! 			strncpy(subject, buf+9, sizeof(subject));
! 		
  		newline = 0;
  	}
*** from.1.orig	Fri Jan  6 18:18:27 1995
--- from.1	Fri Jan  6 18:44:58 1995
***************
*** 41,44 ****
--- 41,45 ----
  .Sh SYNOPSIS
  .Nm from
+ .Op Fl h
  .Op Fl s Ar sender
  .Op Fl f Ar file
***************
*** 51,54 ****
--- 52,58 ----
  Options:
  .Bl -tag -width Fl
+ .It Fl h
+ Print the RFC822 ``From:'' and ``Subject:'' header fields instead of the
+ envelope ``From'' address.
  .It Fl f Ar file 
  The supplied file