aboutsummaryrefslogtreecommitdiff
path: root/re.c
blob: f86f3c271bf21f1a969cf992a909f0fed9af074d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <err.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	char *a, ans[10], file[30];
	FILE *fp;
	int i;

	if (argc-1 == 0) {
		fprintf(stderr, "usage: %s file [...]\n", argv[0]);
		return 1;
	}

	/* Ensure arguments do not contain newlines. */
	for (i = 1; i < argc; i++)
		for (a = argv[i]; *a; a++)
			if (*a == '\n') {
				*a = '*';
				fprintf(stderr,
					"%s contains newline (at *)\n",
					argv[i]);
				return 1;
			}

	sprintf(file, "/var/tmp/re.%d", getppid());

	if (!access(file, F_OK)) {
		do
			fprintf(stderr, "%s already exists; overwrite? ", file);
		while (!gets(ans) || !*ans);
		if (ans[0] != 'y') return 1;
	}

	if (!(fp = fopen(file, "w")))
		err(1, "fopen");

	for (i = 1; i < argc; i++) {
		fprintf(fp, "%s\n", argv[i]);
		printf("%s\n", argv[i]);
	}

	fclose(fp);
}