DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

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

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T m

⟦d95e28a41⟧ TextFile

    Length: 2870 (0xb36)
    Types: TextFile
    Names: »munge.c«

Derivation

└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki
    └─ ⟦this⟧ »EUUGD11/euug-87hel/sec1/news/misc/search/munge.c« 

TextFile

/* munge [file]

   Header munger, copies a news article but puts the header in a
   somewhat standard format, removing unnecessary headers, and
   adding others.  This could do much, much more...
*/

#include "lib/defs.h"

#include <sys/types.h>
#include <sys/stat.h>

/* Getopt globals: */
extern int optind;
extern char *optarg;

main(argc, argv)
	int argc;
	char **argv;
{
	int c;
	int status= 0;
	
	while ((c= getopt(argc, argv, "k:")) != EOF) {
		switch (c) {
		case 'k':
			addkeyarg(optarg);
			break;
		default:
			fprintf(stderr, "usage: %s [-k key=value] [file]\n",
				argv[0]);
			exit(2);
		}
	}
	if (optind >= argc)
		status= fprocess(stdin, "-");
	else if (optind < argc-1) {
		fprintf(stderr, "%s: only one file allowed\n", argv[0]);
		exit(2);
	}
	else if (strcmp(argv[optind], "-") == 0)
		status= fprocess(stdin, "-");
	else {
		FILE *fp= fopen(argv[optind], "r");
		if (fp == NULL) {
			perror(argv[optind]);
			exit(1);
		}
		status= fprocess(fp, argv[optind]);
		(void) fclose(fp);
	}
	exit(status);
}

/* Process one file, given as a FILE pointer. */

int maycopy(); /* Forward */

int
fprocess(fp, name)
	FILE *fp;
	char *name;
{
	if (scanheaders(fp, name, maycopy) != 0)
		return 1;
	addnew(fp, name);
	printf("\n");
	copyrest(fp, stdout);
	return 0;
}

/* List of headers to be ignored. */

char *ignore[]= {
	"Relay-Version",
	"Posting-Version",
	"Path",
	"Message-ID",
	"Article-I.D.",
	"Distribution",
	"Lines",
	"Apparently-To",
	"Approved",
	"Xref",
	"NF-ID",
	"NF-From",
	/* These dates should really be processesed specially: */
	"Date",
	"Posted",
	"Date-Received",
	NULL
};

/* Copy a header line, or not.
   It may be ignored, printed, or transformed and printed. */

/*ARGSUSED*/
bool
maycopy(key, value, file, first)
	char *key;
	char *value;
	char *file;
	bool first;
{
	char **pp;
	
	for (pp= ignore; *pp != NULL; ++pp) {
		if (cistrcmp(*pp, key) == 0)
			return;
	}
	if (first)
		printf("%s: ", key);
	else
		printf("\t");
	printf("%s\n", trim(value));
	return FALSE;
}

/* Add key=value pairs from the command line. */

addkeyarg(s)
	char *s;
{
	char *p;
	
	for (p= s; *p != EOS; ++p) {
		if (*p == ':' || *p == '=') {
			*p++= EOS;
			break;
		}
	}
	printf("%s: %s\n", s, trim(p));
}

/* Calculate a file's size, return -1 if unknown. */

long
calcsize(fp, name)
	FILE *fp;
	char *name;
{
	struct stat s;
	
	if (fstat(fileno(fp), &s) < 0)
		return -1;
	else
		return s.st_size;
}

/* Make up some useful headers.
   Eventually this will also ask the user for keywords. */

addnew(fp, name)
	FILE *fp;
	char *name;
{
	long size;
	long now;
	
	time(&now);
	printf("Date-Saved: %ld (%s)\n", now, trim(ctime(&now)));
	
	if ((size= calcsize(fp, name)) >= 0) {
		size -= ftell(fp);
		/* Don't do this from a pipe --
		   pipes seem to be recognizable because size == 1. */
		if (size > 1)
			printf("Size: %ld bytes\n", size);
	}
}