view netdiff/renpart/rename.c @ 138:77acb816727b

netdiff: donl-rename-parts put together
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 07 Sep 2020 04:02:14 +0000
parents
children
line wrap: on
line source

#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include "struct.h"

extern struct part_rename *part_rename_list;

static char *infname;
static FILE *inf;
static char linebuf[80];
static int lineno;

static void
rename_entry(old, new)
	char *old, *new;
{
	struct part_rename *rp, **rpp;
	char *dp;

	for (rpp = &part_rename_list; rp = *rpp; rpp = &rp->next) {
		if (!strcmp(rp->old, old)) {
			fprintf(stderr, "%s line %d: old name %s given twice\n",
				infname, lineno, old);
			exit(1);
		}
	}
	rp = malloc(sizeof(struct part_rename) + strlen(old) + strlen(new) + 2);
	dp = (char *)(rp + 1);
	rp->old = dp;
	strcpy(dp, old);
	dp += strlen(old) + 1;
	rp->new = dp;
	strcpy(dp, new);
	rp->next = 0;
	*rpp = rp;
}

static void
process_line()
{
	char *cp, *old, *new;

	for (cp = linebuf; isspace(*cp); cp++)
		;
	if (*cp == '\0' || *cp == '#')
		return;
	for (old = cp; *cp && !isspace(*cp); cp++)
		;
	if (*cp)
		*cp++ = '\0';
	while (isspace(*cp))
		cp++;
	if (*cp == '\0' || *cp == '#') {
		fprintf(stderr, "%s line %d: too few fields\n",
			infname, lineno);
		exit(1);
	}
	for (new = cp; *cp && !isspace(*cp); cp++)
		;
	if (*cp)
		*cp++ = '\0';
	while (isspace(*cp))
		cp++;
	if (*cp != '\0' && *cp != '#') {
		fprintf(stderr, "%s line %d: too many fields\n",
			infname, lineno);
		exit(1);
	}
	rename_entry(old, new);
}

process_rename_file(input_filename)
	char *input_filename;
{
	infname = input_filename;
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	while (fgets(linebuf, sizeof linebuf, inf)) {
		lineno++;
		process_line();
	}
	fclose(inf);
	return(0);
}