xref: /titanic_52/usr/src/cmd/mkfile/mkfile.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <ctype.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
33*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <libintl.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #define	WRITEBUF_SIZE	8192
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	BLOCK_SIZE	512		/* bytes */
44*7c478bd9Sstevel@tonic-gate #define	KILOBYTE	1024
45*7c478bd9Sstevel@tonic-gate #define	MEGABYTE	(KILOBYTE * KILOBYTE)
46*7c478bd9Sstevel@tonic-gate #define	GIGABYTE	(KILOBYTE * MEGABYTE)
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	FILE_MODE	(S_ISVTX + S_IRUSR + S_IWUSR)
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static void usage(void);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate char buf[WRITEBUF_SIZE];
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate int
55*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	char	*opts;
58*7c478bd9Sstevel@tonic-gate 	off_t	size;
59*7c478bd9Sstevel@tonic-gate 	size_t	len;
60*7c478bd9Sstevel@tonic-gate 	size_t	mult = 1;
61*7c478bd9Sstevel@tonic-gate 	int	errors = 0;
62*7c478bd9Sstevel@tonic-gate 	int	i;
63*7c478bd9Sstevel@tonic-gate 	int	verbose = 0;	/* option variable */
64*7c478bd9Sstevel@tonic-gate 	int	nobytes = 0;	/* option variable */
65*7c478bd9Sstevel@tonic-gate 	int	saverr;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (argc == 1)
68*7c478bd9Sstevel@tonic-gate 		usage();
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	while (argv[1] && argv[1][0] == '-') {
71*7c478bd9Sstevel@tonic-gate 		opts = &argv[1][0];
72*7c478bd9Sstevel@tonic-gate 		while (*(++opts)) {
73*7c478bd9Sstevel@tonic-gate 			switch (*opts) {
74*7c478bd9Sstevel@tonic-gate 			case 'v':
75*7c478bd9Sstevel@tonic-gate 				verbose++;
76*7c478bd9Sstevel@tonic-gate 				break;
77*7c478bd9Sstevel@tonic-gate 			case 'n':
78*7c478bd9Sstevel@tonic-gate 				nobytes++;
79*7c478bd9Sstevel@tonic-gate 				break;
80*7c478bd9Sstevel@tonic-gate 			default:
81*7c478bd9Sstevel@tonic-gate 				usage();
82*7c478bd9Sstevel@tonic-gate 			}
83*7c478bd9Sstevel@tonic-gate 		}
84*7c478bd9Sstevel@tonic-gate 		argc--;
85*7c478bd9Sstevel@tonic-gate 		argv++;
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	if (argc < 3)
88*7c478bd9Sstevel@tonic-gate 		usage();
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	len = strlen(argv[1]);
91*7c478bd9Sstevel@tonic-gate 	if (len && isalpha(argv[1][len-1])) {
92*7c478bd9Sstevel@tonic-gate 		switch (argv[1][len-1]) {
93*7c478bd9Sstevel@tonic-gate 		case 'k':
94*7c478bd9Sstevel@tonic-gate 		case 'K':
95*7c478bd9Sstevel@tonic-gate 			mult = KILOBYTE;
96*7c478bd9Sstevel@tonic-gate 			break;
97*7c478bd9Sstevel@tonic-gate 		case 'b':
98*7c478bd9Sstevel@tonic-gate 		case 'B':
99*7c478bd9Sstevel@tonic-gate 			mult = BLOCK_SIZE;
100*7c478bd9Sstevel@tonic-gate 			break;
101*7c478bd9Sstevel@tonic-gate 		case 'm':
102*7c478bd9Sstevel@tonic-gate 		case 'M':
103*7c478bd9Sstevel@tonic-gate 			mult = MEGABYTE;
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case 'g':
106*7c478bd9Sstevel@tonic-gate 		case 'G':
107*7c478bd9Sstevel@tonic-gate 			mult = GIGABYTE;
108*7c478bd9Sstevel@tonic-gate 			break;
109*7c478bd9Sstevel@tonic-gate 		default:
110*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown size %s\n", argv[1]);
111*7c478bd9Sstevel@tonic-gate 			usage();
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 		for (i = 0; i <= (len-2); i++) {
115*7c478bd9Sstevel@tonic-gate 			if (!isdigit(argv[1][i])) {
116*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "unknown size "
117*7c478bd9Sstevel@tonic-gate 					"%s\n", argv[1]);
118*7c478bd9Sstevel@tonic-gate 				usage();
119*7c478bd9Sstevel@tonic-gate 			}
120*7c478bd9Sstevel@tonic-gate 		}
121*7c478bd9Sstevel@tonic-gate 		argv[1][len-1] = '\0';
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 	size = ((off_t)atoll(argv[1]) * (off_t)mult);
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	argv++;
126*7c478bd9Sstevel@tonic-gate 	argc--;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	while (argc > 1) {
129*7c478bd9Sstevel@tonic-gate 		int fd;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 		if (verbose)
132*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s %lld bytes\n", argv[1],
133*7c478bd9Sstevel@tonic-gate 			    (offset_t)size);
134*7c478bd9Sstevel@tonic-gate 		fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR, FILE_MODE);
135*7c478bd9Sstevel@tonic-gate 		if (fd < 0) {
136*7c478bd9Sstevel@tonic-gate 			saverr = errno;
137*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
138*7c478bd9Sstevel@tonic-gate 			    gettext("Could not open %s: %s\n"),
139*7c478bd9Sstevel@tonic-gate 			    argv[1], strerror(saverr));
140*7c478bd9Sstevel@tonic-gate 			errors++;
141*7c478bd9Sstevel@tonic-gate 			argv++;
142*7c478bd9Sstevel@tonic-gate 			argc--;
143*7c478bd9Sstevel@tonic-gate 			continue;
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 		if (lseek(fd, (off_t)size-1, SEEK_SET) < 0) {
146*7c478bd9Sstevel@tonic-gate 			saverr = errno;
147*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
148*7c478bd9Sstevel@tonic-gate 			    "Could not seek to offset %ld in %s: %s\n"),
149*7c478bd9Sstevel@tonic-gate 			    (ulong_t)size-1, argv[1], strerror(saverr));
150*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
151*7c478bd9Sstevel@tonic-gate 			errors++;
152*7c478bd9Sstevel@tonic-gate 			argv++;
153*7c478bd9Sstevel@tonic-gate 			argc--;
154*7c478bd9Sstevel@tonic-gate 			continue;
155*7c478bd9Sstevel@tonic-gate 		} else if (write(fd, "", 1) != 1) {
156*7c478bd9Sstevel@tonic-gate 			saverr = errno;
157*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
158*7c478bd9Sstevel@tonic-gate 			    "Could not set length of %s: %s\n"),
159*7c478bd9Sstevel@tonic-gate 			    argv[1], strerror(saverr));
160*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
161*7c478bd9Sstevel@tonic-gate 			errors++;
162*7c478bd9Sstevel@tonic-gate 			argv++;
163*7c478bd9Sstevel@tonic-gate 			argc--;
164*7c478bd9Sstevel@tonic-gate 			continue;
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		if (!nobytes) {
168*7c478bd9Sstevel@tonic-gate 			off_t written = 0;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 			if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
171*7c478bd9Sstevel@tonic-gate 				saverr = errno;
172*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
173*7c478bd9Sstevel@tonic-gate 				    "Could not seek to beginning of %s: %s\n"),
174*7c478bd9Sstevel@tonic-gate 				    argv[1], strerror(saverr));
175*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
176*7c478bd9Sstevel@tonic-gate 				errors++;
177*7c478bd9Sstevel@tonic-gate 				argv++;
178*7c478bd9Sstevel@tonic-gate 				argc--;
179*7c478bd9Sstevel@tonic-gate 				continue;
180*7c478bd9Sstevel@tonic-gate 			}
181*7c478bd9Sstevel@tonic-gate 			while (written < size) {
182*7c478bd9Sstevel@tonic-gate 				ssize_t result;
183*7c478bd9Sstevel@tonic-gate 				size_t bytes = (size_t)MIN(sizeof (buf),
184*7c478bd9Sstevel@tonic-gate 					size-written);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 				if ((result = write(fd, buf, bytes)) !=
187*7c478bd9Sstevel@tonic-gate 				    (ssize_t)bytes) {
188*7c478bd9Sstevel@tonic-gate 					saverr = errno;
189*7c478bd9Sstevel@tonic-gate 					if (result < 0)
190*7c478bd9Sstevel@tonic-gate 					    result = 0;
191*7c478bd9Sstevel@tonic-gate 					written += result;
192*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
193*7c478bd9Sstevel@tonic-gate 			    "%s: initialized %lu of %lu bytes: %s\n"),
194*7c478bd9Sstevel@tonic-gate 					    argv[1], (ulong_t)written,
195*7c478bd9Sstevel@tonic-gate 					    (ulong_t)size,
196*7c478bd9Sstevel@tonic-gate 					    strerror(saverr));
197*7c478bd9Sstevel@tonic-gate 					errors++;
198*7c478bd9Sstevel@tonic-gate 					break;
199*7c478bd9Sstevel@tonic-gate 				}
200*7c478bd9Sstevel@tonic-gate 				written += bytes;
201*7c478bd9Sstevel@tonic-gate 			}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			/*
204*7c478bd9Sstevel@tonic-gate 			 * A write(2) call in the above loop failed so
205*7c478bd9Sstevel@tonic-gate 			 * close out this file and go on (error was
206*7c478bd9Sstevel@tonic-gate 			 * already incremented when the write(2) failed).
207*7c478bd9Sstevel@tonic-gate 			 */
208*7c478bd9Sstevel@tonic-gate 			if (written < size) {
209*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
210*7c478bd9Sstevel@tonic-gate 				argv++;
211*7c478bd9Sstevel@tonic-gate 				argc--;
212*7c478bd9Sstevel@tonic-gate 				continue;
213*7c478bd9Sstevel@tonic-gate 			}
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 		if (close(fd) < 0) {
216*7c478bd9Sstevel@tonic-gate 			saverr = errno;
217*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
218*7c478bd9Sstevel@tonic-gate 			    "Error encountered when closing %s: %s\n"),
219*7c478bd9Sstevel@tonic-gate 			    argv[1], strerror(saverr));
220*7c478bd9Sstevel@tonic-gate 			errors++;
221*7c478bd9Sstevel@tonic-gate 			argv++;
222*7c478bd9Sstevel@tonic-gate 			argc--;
223*7c478bd9Sstevel@tonic-gate 			continue;
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		/*
227*7c478bd9Sstevel@tonic-gate 		 * Only set the modes (including the sticky bit) if we
228*7c478bd9Sstevel@tonic-gate 		 * had no problems.  It is not an error for the chmod(2)
229*7c478bd9Sstevel@tonic-gate 		 * to fail, but do issue a warning.
230*7c478bd9Sstevel@tonic-gate 		 */
231*7c478bd9Sstevel@tonic-gate 		if (chmod(argv[1], FILE_MODE) < 0)
232*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
233*7c478bd9Sstevel@tonic-gate 			    "warning: couldn't set mode to %#o\n", FILE_MODE);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		argv++;
236*7c478bd9Sstevel@tonic-gate 		argc--;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 	return (errors);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate static void usage()
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
244*7c478bd9Sstevel@tonic-gate 		"Usage: mkfile [-nv] <size>[g|k|b|m] <name1> [<name2>] ...\n");
245*7c478bd9Sstevel@tonic-gate 	exit(1);
246*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
247*7c478bd9Sstevel@tonic-gate }
248