summaryrefslogtreecommitdiffabout
path: root/tests/genfile.c
blob: 6616709609c0fe5c07eb1756195b59b56c1c0dff (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* This file is part of Idest.
   Copyright (C) 2009-2011, 2017 Sergey Poznyakoff
 
   Idest is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Idest is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Idest.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif

static void
copyfile(FILE *file, const char *name)
{
	int c;
	FILE *in = fopen(name, "r");
	if (!in) { 
		perror(name);
		exit(1);
	}
	while ((c = fgetc(in)) != EOF)
		fputc(c, file);
	fclose(in);
}

int
main(int argc, char **argv)
{
	char *suf;
	unsigned int length, i, c;
	char *filename = NULL, *suffix = NULL, *prefix = NULL;
	FILE *fp;
	
	while ((c = getopt(argc, argv, "hf:p:s:")) != EOF) {
		switch (c) {
		case 'f':
			filename = optarg;
			break;

		case 'p':
			prefix = optarg;
			break;

		case 's':
			suffix = optarg;
			break;

		case 'h':
			fprintf(stdout,
				"usage: %s [-f FILE] [-p PREFIX]"
				" [-s SUFFIX] SIZE\n", argv[0]);
			exit(0);

		default:
			exit(1);
		}
	}

	if (optind + 1 != argc) {
		fprintf(stderr, "%s: invalid usage\n", argv[0]);
		exit(1);
	}

	length = strtoul(argv[optind], &suf, 10);
	if (suf[0] && suf[1]) {
		fprintf(stderr, "%s: unknown size suffix: %s\n", argv[0], suf);
		exit(1);
	}
	
	switch (*suf) {
	case 0:
		break;
	case 'g':
	case 'G':
		length *= 1024;
	case 'm':
	case 'M':
		length *= 1024;
	case 'k':
	case 'K':
		length *= 1024;
		break;
	default:
		fprintf(stderr, "%s: unknown size suffix: %s\n", argv[0], suf);
		exit(1);
	}

	if (filename) {
		fp = fopen(filename, "w");
		if (!fp) {
			perror(filename);
			exit(1);
		}
	} else
		fp = stdout;

	if (prefix)
		copyfile(fp, prefix);
	
	for (i = 0; i < length; i++)
		fputc (i & 255, fp);

	if (suffix)
		copyfile(fp, suffix);
	
	if (ferror(fp)) {
		fprintf(stderr, "%s: write error", argv[0]);
		exit(1);
	}
	fclose(fp);
	exit(0);
}
	

Return to:

Send suggestions and report system problems to the System administrator.