4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 #pragma ident "%Z%%M% %I% %E% SMI"
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
39 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 #define BLOCK_SIZE 512 /* bytes */
43 #define MEGABYTE (KILOBYTE * KILOBYTE)
44 #define GIGABYTE (KILOBYTE * MEGABYTE)
46 #define FILE_MODE (S_ISVTX + S_IRUSR + S_IWUSR)
48 static void usage(void);
51 main(int argc
, char **argv
)
61 int verbose
= 0; /* option variable */
62 int nobytes
= 0; /* option variable */
68 while (argv
[1] && argv
[1][0] == '-') {
88 len
= strlen(argv
[1]);
89 if (len
&& isalpha(argv
[1][len
-1])) {
90 switch (argv
[1][len
-1]) {
108 (void) fprintf(stderr
,
109 gettext("unknown size %s\n"), argv
[1]);
113 for (i
= 0; i
<= (len
-2); i
++) {
114 if (!isdigit(argv
[1][i
])) {
115 (void) fprintf(stderr
,
116 gettext("unknown size %s\n"), argv
[1]);
120 argv
[1][len
-1] = '\0';
122 size
= ((off_t
)atoll(argv
[1]) * (off_t
)mult
);
131 (void) fprintf(stdout
, gettext("%s %lld bytes\n"),
132 argv
[1], (offset_t
)size
);
133 fd
= open(argv
[1], O_CREAT
|O_TRUNC
|O_RDWR
, FILE_MODE
);
136 (void) fprintf(stderr
,
137 gettext("Could not open %s: %s\n"),
138 argv
[1], strerror(saverr
));
144 if (lseek(fd
, (off_t
)size
-1, SEEK_SET
) < 0) {
146 (void) fprintf(stderr
, gettext(
147 "Could not seek to offset %ld in %s: %s\n"),
148 (ulong_t
)size
-1, argv
[1], strerror(saverr
));
154 } else if (write(fd
, "", 1) != 1) {
156 (void) fprintf(stderr
, gettext(
157 "Could not set length of %s: %s\n"),
158 argv
[1], strerror(saverr
));
170 if (lseek(fd
, (off_t
)0, SEEK_SET
) < 0) {
172 (void) fprintf(stderr
, gettext(
173 "Could not seek to beginning of %s: %s\n"),
174 argv
[1], strerror(saverr
));
181 if (fstat64(fd
, &st
) < 0) {
183 (void) fprintf(stderr
, gettext(
184 "Could not fstat64 %s: %s\n"),
185 argv
[1], strerror(saverr
));
192 if (bufsz
!= st
.st_blksize
) {
195 bufsz
= (size_t)st
.st_blksize
;
196 buf
= calloc(bufsz
, 1);
198 (void) fprintf(stderr
, gettext(
199 "Could not allocate buffer of"
200 " size %d\n"), (int)bufsz
);
209 while (written
< size
) {
211 size_t bytes
= (size_t)MIN(bufsz
, size
-written
);
213 if ((result
= write(fd
, buf
, bytes
)) !=
219 (void) fprintf(stderr
, gettext(
220 "%s: initialized %lu of %lu bytes: %s\n"),
221 argv
[1], (ulong_t
)written
,
231 * A write(2) call in the above loop failed so
232 * close out this file and go on (error was
233 * already incremented when the write(2) failed).
235 if (written
< size
) {
244 (void) fprintf(stderr
, gettext(
245 "Error encountered when closing %s: %s\n"),
246 argv
[1], strerror(saverr
));
254 * Only set the modes (including the sticky bit) if we
255 * had no problems. It is not an error for the chmod(2)
256 * to fail, but do issue a warning.
258 if (chmod(argv
[1], FILE_MODE
) < 0)
259 (void) fprintf(stderr
, gettext(
260 "warning: couldn't set mode to %#o\n"), FILE_MODE
);
270 (void) fprintf(stderr
, gettext(
271 "Usage: mkfile [-nv] <size>[g|k|b|m] <name1> [<name2>] ...\n"));