17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate /*
25*6db6c2a6Sperrin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b))
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #define BLOCK_SIZE 512 /* bytes */
427c478bd9Sstevel@tonic-gate #define KILOBYTE 1024
437c478bd9Sstevel@tonic-gate #define MEGABYTE (KILOBYTE * KILOBYTE)
447c478bd9Sstevel@tonic-gate #define GIGABYTE (KILOBYTE * MEGABYTE)
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define FILE_MODE (S_ISVTX + S_IRUSR + S_IWUSR)
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static void usage(void);
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)517c478bd9Sstevel@tonic-gate main(int argc, char **argv)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate char *opts;
547c478bd9Sstevel@tonic-gate off_t size;
557c478bd9Sstevel@tonic-gate size_t len;
567c478bd9Sstevel@tonic-gate size_t mult = 1;
57*6db6c2a6Sperrin char *buf = NULL;
58*6db6c2a6Sperrin size_t bufsz = 0;
597c478bd9Sstevel@tonic-gate int errors = 0;
607c478bd9Sstevel@tonic-gate int i;
617c478bd9Sstevel@tonic-gate int verbose = 0; /* option variable */
627c478bd9Sstevel@tonic-gate int nobytes = 0; /* option variable */
637c478bd9Sstevel@tonic-gate int saverr;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate if (argc == 1)
667c478bd9Sstevel@tonic-gate usage();
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate while (argv[1] && argv[1][0] == '-') {
697c478bd9Sstevel@tonic-gate opts = &argv[1][0];
707c478bd9Sstevel@tonic-gate while (*(++opts)) {
717c478bd9Sstevel@tonic-gate switch (*opts) {
727c478bd9Sstevel@tonic-gate case 'v':
737c478bd9Sstevel@tonic-gate verbose++;
747c478bd9Sstevel@tonic-gate break;
757c478bd9Sstevel@tonic-gate case 'n':
767c478bd9Sstevel@tonic-gate nobytes++;
777c478bd9Sstevel@tonic-gate break;
787c478bd9Sstevel@tonic-gate default:
797c478bd9Sstevel@tonic-gate usage();
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate argc--;
837c478bd9Sstevel@tonic-gate argv++;
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate if (argc < 3)
867c478bd9Sstevel@tonic-gate usage();
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate len = strlen(argv[1]);
897c478bd9Sstevel@tonic-gate if (len && isalpha(argv[1][len-1])) {
907c478bd9Sstevel@tonic-gate switch (argv[1][len-1]) {
917c478bd9Sstevel@tonic-gate case 'k':
927c478bd9Sstevel@tonic-gate case 'K':
937c478bd9Sstevel@tonic-gate mult = KILOBYTE;
947c478bd9Sstevel@tonic-gate break;
957c478bd9Sstevel@tonic-gate case 'b':
967c478bd9Sstevel@tonic-gate case 'B':
977c478bd9Sstevel@tonic-gate mult = BLOCK_SIZE;
987c478bd9Sstevel@tonic-gate break;
997c478bd9Sstevel@tonic-gate case 'm':
1007c478bd9Sstevel@tonic-gate case 'M':
1017c478bd9Sstevel@tonic-gate mult = MEGABYTE;
1027c478bd9Sstevel@tonic-gate break;
1037c478bd9Sstevel@tonic-gate case 'g':
1047c478bd9Sstevel@tonic-gate case 'G':
1057c478bd9Sstevel@tonic-gate mult = GIGABYTE;
1067c478bd9Sstevel@tonic-gate break;
1077c478bd9Sstevel@tonic-gate default:
108*6db6c2a6Sperrin (void) fprintf(stderr,
109*6db6c2a6Sperrin gettext("unknown size %s\n"), argv[1]);
1107c478bd9Sstevel@tonic-gate usage();
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate for (i = 0; i <= (len-2); i++) {
1147c478bd9Sstevel@tonic-gate if (!isdigit(argv[1][i])) {
115*6db6c2a6Sperrin (void) fprintf(stderr,
116*6db6c2a6Sperrin gettext("unknown size %s\n"), argv[1]);
1177c478bd9Sstevel@tonic-gate usage();
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate argv[1][len-1] = '\0';
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate size = ((off_t)atoll(argv[1]) * (off_t)mult);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate argv++;
1257c478bd9Sstevel@tonic-gate argc--;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate while (argc > 1) {
1287c478bd9Sstevel@tonic-gate int fd;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (verbose)
131*6db6c2a6Sperrin (void) fprintf(stdout, gettext("%s %lld bytes\n"),
132*6db6c2a6Sperrin argv[1], (offset_t)size);
1337c478bd9Sstevel@tonic-gate fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR, FILE_MODE);
1347c478bd9Sstevel@tonic-gate if (fd < 0) {
1357c478bd9Sstevel@tonic-gate saverr = errno;
1367c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1377c478bd9Sstevel@tonic-gate gettext("Could not open %s: %s\n"),
1387c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1397c478bd9Sstevel@tonic-gate errors++;
1407c478bd9Sstevel@tonic-gate argv++;
1417c478bd9Sstevel@tonic-gate argc--;
1427c478bd9Sstevel@tonic-gate continue;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate if (lseek(fd, (off_t)size-1, SEEK_SET) < 0) {
1457c478bd9Sstevel@tonic-gate saverr = errno;
1467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1477c478bd9Sstevel@tonic-gate "Could not seek to offset %ld in %s: %s\n"),
1487c478bd9Sstevel@tonic-gate (ulong_t)size-1, argv[1], strerror(saverr));
1497c478bd9Sstevel@tonic-gate (void) close(fd);
1507c478bd9Sstevel@tonic-gate errors++;
1517c478bd9Sstevel@tonic-gate argv++;
1527c478bd9Sstevel@tonic-gate argc--;
1537c478bd9Sstevel@tonic-gate continue;
1547c478bd9Sstevel@tonic-gate } else if (write(fd, "", 1) != 1) {
1557c478bd9Sstevel@tonic-gate saverr = errno;
1567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1577c478bd9Sstevel@tonic-gate "Could not set length of %s: %s\n"),
1587c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1597c478bd9Sstevel@tonic-gate (void) close(fd);
1607c478bd9Sstevel@tonic-gate errors++;
1617c478bd9Sstevel@tonic-gate argv++;
1627c478bd9Sstevel@tonic-gate argc--;
1637c478bd9Sstevel@tonic-gate continue;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate if (!nobytes) {
1677c478bd9Sstevel@tonic-gate off_t written = 0;
168*6db6c2a6Sperrin struct stat64 st;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
1717c478bd9Sstevel@tonic-gate saverr = errno;
1727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1737c478bd9Sstevel@tonic-gate "Could not seek to beginning of %s: %s\n"),
1747c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1757c478bd9Sstevel@tonic-gate (void) close(fd);
1767c478bd9Sstevel@tonic-gate errors++;
1777c478bd9Sstevel@tonic-gate argv++;
1787c478bd9Sstevel@tonic-gate argc--;
1797c478bd9Sstevel@tonic-gate continue;
1807c478bd9Sstevel@tonic-gate }
181*6db6c2a6Sperrin if (fstat64(fd, &st) < 0) {
182*6db6c2a6Sperrin saverr = errno;
183*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
184*6db6c2a6Sperrin "Could not fstat64 %s: %s\n"),
185*6db6c2a6Sperrin argv[1], strerror(saverr));
186*6db6c2a6Sperrin (void) close(fd);
187*6db6c2a6Sperrin errors++;
188*6db6c2a6Sperrin argv++;
189*6db6c2a6Sperrin argc--;
190*6db6c2a6Sperrin continue;
191*6db6c2a6Sperrin }
192*6db6c2a6Sperrin if (bufsz != st.st_blksize) {
193*6db6c2a6Sperrin if (buf)
194*6db6c2a6Sperrin free(buf);
195*6db6c2a6Sperrin bufsz = (size_t)st.st_blksize;
196*6db6c2a6Sperrin buf = calloc(bufsz, 1);
197*6db6c2a6Sperrin if (buf == NULL) {
198*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
199*6db6c2a6Sperrin "Could not allocate buffer of"
200*6db6c2a6Sperrin " size %d\n"), (int)bufsz);
201*6db6c2a6Sperrin (void) close(fd);
202*6db6c2a6Sperrin bufsz = 0;
203*6db6c2a6Sperrin errors++;
204*6db6c2a6Sperrin argv++;
205*6db6c2a6Sperrin argc--;
206*6db6c2a6Sperrin continue;
207*6db6c2a6Sperrin }
208*6db6c2a6Sperrin }
2097c478bd9Sstevel@tonic-gate while (written < size) {
2107c478bd9Sstevel@tonic-gate ssize_t result;
211*6db6c2a6Sperrin size_t bytes = (size_t)MIN(bufsz, size-written);
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate if ((result = write(fd, buf, bytes)) !=
2147c478bd9Sstevel@tonic-gate (ssize_t)bytes) {
2157c478bd9Sstevel@tonic-gate saverr = errno;
2167c478bd9Sstevel@tonic-gate if (result < 0)
2177c478bd9Sstevel@tonic-gate result = 0;
2187c478bd9Sstevel@tonic-gate written += result;
2197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2207c478bd9Sstevel@tonic-gate "%s: initialized %lu of %lu bytes: %s\n"),
2217c478bd9Sstevel@tonic-gate argv[1], (ulong_t)written,
2227c478bd9Sstevel@tonic-gate (ulong_t)size,
2237c478bd9Sstevel@tonic-gate strerror(saverr));
2247c478bd9Sstevel@tonic-gate errors++;
2257c478bd9Sstevel@tonic-gate break;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate written += bytes;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * A write(2) call in the above loop failed so
2327c478bd9Sstevel@tonic-gate * close out this file and go on (error was
2337c478bd9Sstevel@tonic-gate * already incremented when the write(2) failed).
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate if (written < size) {
2367c478bd9Sstevel@tonic-gate (void) close(fd);
2377c478bd9Sstevel@tonic-gate argv++;
2387c478bd9Sstevel@tonic-gate argc--;
2397c478bd9Sstevel@tonic-gate continue;
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate if (close(fd) < 0) {
2437c478bd9Sstevel@tonic-gate saverr = errno;
2447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2457c478bd9Sstevel@tonic-gate "Error encountered when closing %s: %s\n"),
2467c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
2477c478bd9Sstevel@tonic-gate errors++;
2487c478bd9Sstevel@tonic-gate argv++;
2497c478bd9Sstevel@tonic-gate argc--;
2507c478bd9Sstevel@tonic-gate continue;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * Only set the modes (including the sticky bit) if we
2557c478bd9Sstevel@tonic-gate * had no problems. It is not an error for the chmod(2)
2567c478bd9Sstevel@tonic-gate * to fail, but do issue a warning.
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate if (chmod(argv[1], FILE_MODE) < 0)
259*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
260*6db6c2a6Sperrin "warning: couldn't set mode to %#o\n"), FILE_MODE);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate argv++;
2637c478bd9Sstevel@tonic-gate argc--;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate return (errors);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
usage()2687c478bd9Sstevel@tonic-gate static void usage()
2697c478bd9Sstevel@tonic-gate {
270*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
271*6db6c2a6Sperrin "Usage: mkfile [-nv] <size>[g|k|b|m] <name1> [<name2>] ...\n"));
2727c478bd9Sstevel@tonic-gate exit(1);
2737c478bd9Sstevel@tonic-gate /* NOTREACHED */
2747c478bd9Sstevel@tonic-gate }
275