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 https://opensource.org/licenses/CDDL-1.0.
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]
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
37 #include <sys/stdtypes.h>
38 #include <sys/sysmacros.h>
40 #define BLOCKSIZE 512 /* bytes */
42 #define MEGABYTE (KILOBYTE * KILOBYTE)
43 #define GIGABYTE (KILOBYTE * MEGABYTE)
45 #define FILE_MODE (S_ISVTX + S_IRUSR + S_IWUSR)
47 static __attribute__((noreturn
)) void
50 (void) fprintf(stderr
, gettext(
51 "Usage: mkfile [-nv] <size>[g|k|b|m] <name1> [<name2>] ...\n"));
56 main(int argc
, char **argv
)
66 int verbose
= 0; /* option variable */
67 int nobytes
= 0; /* option variable */
73 while (argv
[1] && argv
[1][0] == '-') {
93 len
= strlen(argv
[1]);
94 if (len
&& isalpha(argv
[1][len
-1])) {
95 switch (argv
[1][len
-1]) {
113 (void) fprintf(stderr
,
114 gettext("unknown size %s\n"), argv
[1]);
118 for (i
= 0; i
<= (len
-2); i
++) {
119 if (!isdigit(argv
[1][i
])) {
120 (void) fprintf(stderr
,
121 gettext("unknown size %s\n"), argv
[1]);
125 argv
[1][len
-1] = '\0';
127 size
= ((off_t
)atoll(argv
[1]) * (off_t
)mult
);
136 (void) fprintf(stdout
, gettext("%s %lld bytes\n"),
137 argv
[1], (offset_t
)size
);
138 fd
= open(argv
[1], O_CREAT
|O_TRUNC
|O_RDWR
, FILE_MODE
);
141 (void) fprintf(stderr
,
142 gettext("Could not open %s: %s\n"),
143 argv
[1], strerror(saverr
));
148 } else if (fchown(fd
, getuid(), getgid()) < 0) {
150 (void) fprintf(stderr
, gettext(
151 "Could not set owner/group of %s: %s\n"),
152 argv
[1], strerror(saverr
));
158 } else if (lseek(fd
, (off_t
)size
-1, SEEK_SET
) < 0) {
160 (void) fprintf(stderr
, gettext(
161 "Could not seek to offset %ld in %s: %s\n"),
162 (unsigned long)size
-1, argv
[1], strerror(saverr
));
168 } else if (write(fd
, "", 1) != 1) {
170 (void) fprintf(stderr
, gettext(
171 "Could not set length of %s: %s\n"),
172 argv
[1], strerror(saverr
));
184 if (lseek(fd
, (off_t
)0, SEEK_SET
) < 0) {
186 (void) fprintf(stderr
, gettext(
187 "Could not seek to beginning of %s: %s\n"),
188 argv
[1], strerror(saverr
));
195 if (fstat64(fd
, &st
) < 0) {
197 (void) fprintf(stderr
, gettext(
198 "Could not fstat64 %s: %s\n"),
199 argv
[1], strerror(saverr
));
206 if (bufsz
!= st
.st_blksize
) {
209 bufsz
= (size_t)st
.st_blksize
;
210 buf
= calloc(1, bufsz
);
212 (void) fprintf(stderr
, gettext(
213 "Could not allocate buffer of"
214 " size %d\n"), (int)bufsz
);
223 while (written
< size
) {
225 size_t bytes
= (size_t)MIN(bufsz
, size
-written
);
227 if ((result
= write(fd
, buf
, bytes
)) !=
233 (void) fprintf(stderr
, gettext(
234 "%s: initialized %lu of %lu bytes: %s\n"),
235 argv
[1], (unsigned long)written
,
245 * A write(2) call in the above loop failed so
246 * close out this file and go on (error was
247 * already incremented when the write(2) failed).
249 if (written
< size
) {
258 (void) fprintf(stderr
, gettext(
259 "Error encountered when closing %s: %s\n"),
260 argv
[1], strerror(saverr
));
268 * Only set the modes (including the sticky bit) if we
269 * had no problems. It is not an error for the chmod(2)
270 * to fail, but do issue a warning.
272 if (chmod(argv
[1], FILE_MODE
) < 0)
273 (void) fprintf(stderr
, gettext(
274 "warning: couldn't set mode to %#o\n"), FILE_MODE
);