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
237c478bd9Sstevel@tonic-gate /*
24*6db6c2a6Sperrin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b))
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #define BLOCK_SIZE 512 /* bytes */
417c478bd9Sstevel@tonic-gate #define KILOBYTE 1024
427c478bd9Sstevel@tonic-gate #define MEGABYTE (KILOBYTE * KILOBYTE)
437c478bd9Sstevel@tonic-gate #define GIGABYTE (KILOBYTE * MEGABYTE)
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #define FILE_MODE (S_ISVTX + S_IRUSR + S_IWUSR)
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate static void usage(void);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)507c478bd9Sstevel@tonic-gate main(int argc, char **argv)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate char *opts;
537c478bd9Sstevel@tonic-gate off_t size;
547c478bd9Sstevel@tonic-gate size_t len;
557c478bd9Sstevel@tonic-gate size_t mult = 1;
56*6db6c2a6Sperrin char *buf = NULL;
57*6db6c2a6Sperrin size_t bufsz = 0;
587c478bd9Sstevel@tonic-gate int errors = 0;
597c478bd9Sstevel@tonic-gate int i;
607c478bd9Sstevel@tonic-gate int verbose = 0; /* option variable */
617c478bd9Sstevel@tonic-gate int nobytes = 0; /* option variable */
627c478bd9Sstevel@tonic-gate int saverr;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if (argc == 1)
657c478bd9Sstevel@tonic-gate usage();
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate while (argv[1] && argv[1][0] == '-') {
687c478bd9Sstevel@tonic-gate opts = &argv[1][0];
697c478bd9Sstevel@tonic-gate while (*(++opts)) {
707c478bd9Sstevel@tonic-gate switch (*opts) {
717c478bd9Sstevel@tonic-gate case 'v':
727c478bd9Sstevel@tonic-gate verbose++;
737c478bd9Sstevel@tonic-gate break;
747c478bd9Sstevel@tonic-gate case 'n':
757c478bd9Sstevel@tonic-gate nobytes++;
767c478bd9Sstevel@tonic-gate break;
777c478bd9Sstevel@tonic-gate default:
787c478bd9Sstevel@tonic-gate usage();
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate argc--;
827c478bd9Sstevel@tonic-gate argv++;
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate if (argc < 3)
857c478bd9Sstevel@tonic-gate usage();
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate len = strlen(argv[1]);
887c478bd9Sstevel@tonic-gate if (len && isalpha(argv[1][len-1])) {
897c478bd9Sstevel@tonic-gate switch (argv[1][len-1]) {
907c478bd9Sstevel@tonic-gate case 'k':
917c478bd9Sstevel@tonic-gate case 'K':
927c478bd9Sstevel@tonic-gate mult = KILOBYTE;
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate case 'b':
957c478bd9Sstevel@tonic-gate case 'B':
967c478bd9Sstevel@tonic-gate mult = BLOCK_SIZE;
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate case 'm':
997c478bd9Sstevel@tonic-gate case 'M':
1007c478bd9Sstevel@tonic-gate mult = MEGABYTE;
1017c478bd9Sstevel@tonic-gate break;
1027c478bd9Sstevel@tonic-gate case 'g':
1037c478bd9Sstevel@tonic-gate case 'G':
1047c478bd9Sstevel@tonic-gate mult = GIGABYTE;
1057c478bd9Sstevel@tonic-gate break;
1067c478bd9Sstevel@tonic-gate default:
107*6db6c2a6Sperrin (void) fprintf(stderr,
108*6db6c2a6Sperrin gettext("unknown size %s\n"), argv[1]);
1097c478bd9Sstevel@tonic-gate usage();
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate for (i = 0; i <= (len-2); i++) {
1137c478bd9Sstevel@tonic-gate if (!isdigit(argv[1][i])) {
114*6db6c2a6Sperrin (void) fprintf(stderr,
115*6db6c2a6Sperrin gettext("unknown size %s\n"), argv[1]);
1167c478bd9Sstevel@tonic-gate usage();
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate argv[1][len-1] = '\0';
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate size = ((off_t)atoll(argv[1]) * (off_t)mult);
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate argv++;
1247c478bd9Sstevel@tonic-gate argc--;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate while (argc > 1) {
1277c478bd9Sstevel@tonic-gate int fd;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate if (verbose)
130*6db6c2a6Sperrin (void) fprintf(stdout, gettext("%s %lld bytes\n"),
131*6db6c2a6Sperrin argv[1], (offset_t)size);
1327c478bd9Sstevel@tonic-gate fd = open(argv[1], O_CREAT|O_TRUNC|O_RDWR, FILE_MODE);
1337c478bd9Sstevel@tonic-gate if (fd < 0) {
1347c478bd9Sstevel@tonic-gate saverr = errno;
1357c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1367c478bd9Sstevel@tonic-gate gettext("Could not open %s: %s\n"),
1377c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1387c478bd9Sstevel@tonic-gate errors++;
1397c478bd9Sstevel@tonic-gate argv++;
1407c478bd9Sstevel@tonic-gate argc--;
1417c478bd9Sstevel@tonic-gate continue;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate if (lseek(fd, (off_t)size-1, SEEK_SET) < 0) {
1447c478bd9Sstevel@tonic-gate saverr = errno;
1457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1467c478bd9Sstevel@tonic-gate "Could not seek to offset %ld in %s: %s\n"),
1477c478bd9Sstevel@tonic-gate (ulong_t)size-1, argv[1], strerror(saverr));
1487c478bd9Sstevel@tonic-gate (void) close(fd);
1497c478bd9Sstevel@tonic-gate errors++;
1507c478bd9Sstevel@tonic-gate argv++;
1517c478bd9Sstevel@tonic-gate argc--;
1527c478bd9Sstevel@tonic-gate continue;
1537c478bd9Sstevel@tonic-gate } else if (write(fd, "", 1) != 1) {
1547c478bd9Sstevel@tonic-gate saverr = errno;
1557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1567c478bd9Sstevel@tonic-gate "Could not set length of %s: %s\n"),
1577c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1587c478bd9Sstevel@tonic-gate (void) close(fd);
1597c478bd9Sstevel@tonic-gate errors++;
1607c478bd9Sstevel@tonic-gate argv++;
1617c478bd9Sstevel@tonic-gate argc--;
1627c478bd9Sstevel@tonic-gate continue;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (!nobytes) {
1667c478bd9Sstevel@tonic-gate off_t written = 0;
167*6db6c2a6Sperrin struct stat64 st;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate if (lseek(fd, (off_t)0, SEEK_SET) < 0) {
1707c478bd9Sstevel@tonic-gate saverr = errno;
1717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1727c478bd9Sstevel@tonic-gate "Could not seek to beginning of %s: %s\n"),
1737c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
1747c478bd9Sstevel@tonic-gate (void) close(fd);
1757c478bd9Sstevel@tonic-gate errors++;
1767c478bd9Sstevel@tonic-gate argv++;
1777c478bd9Sstevel@tonic-gate argc--;
1787c478bd9Sstevel@tonic-gate continue;
1797c478bd9Sstevel@tonic-gate }
180*6db6c2a6Sperrin if (fstat64(fd, &st) < 0) {
181*6db6c2a6Sperrin saverr = errno;
182*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
183*6db6c2a6Sperrin "Could not fstat64 %s: %s\n"),
184*6db6c2a6Sperrin argv[1], strerror(saverr));
185*6db6c2a6Sperrin (void) close(fd);
186*6db6c2a6Sperrin errors++;
187*6db6c2a6Sperrin argv++;
188*6db6c2a6Sperrin argc--;
189*6db6c2a6Sperrin continue;
190*6db6c2a6Sperrin }
191*6db6c2a6Sperrin if (bufsz != st.st_blksize) {
192*6db6c2a6Sperrin if (buf)
193*6db6c2a6Sperrin free(buf);
194*6db6c2a6Sperrin bufsz = (size_t)st.st_blksize;
195*6db6c2a6Sperrin buf = calloc(bufsz, 1);
196*6db6c2a6Sperrin if (buf == NULL) {
197*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
198*6db6c2a6Sperrin "Could not allocate buffer of"
199*6db6c2a6Sperrin " size %d\n"), (int)bufsz);
200*6db6c2a6Sperrin (void) close(fd);
201*6db6c2a6Sperrin bufsz = 0;
202*6db6c2a6Sperrin errors++;
203*6db6c2a6Sperrin argv++;
204*6db6c2a6Sperrin argc--;
205*6db6c2a6Sperrin continue;
206*6db6c2a6Sperrin }
207*6db6c2a6Sperrin }
2087c478bd9Sstevel@tonic-gate while (written < size) {
2097c478bd9Sstevel@tonic-gate ssize_t result;
210*6db6c2a6Sperrin size_t bytes = (size_t)MIN(bufsz, size-written);
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if ((result = write(fd, buf, bytes)) !=
2137c478bd9Sstevel@tonic-gate (ssize_t)bytes) {
2147c478bd9Sstevel@tonic-gate saverr = errno;
2157c478bd9Sstevel@tonic-gate if (result < 0)
2167c478bd9Sstevel@tonic-gate result = 0;
2177c478bd9Sstevel@tonic-gate written += result;
2187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2197c478bd9Sstevel@tonic-gate "%s: initialized %lu of %lu bytes: %s\n"),
2207c478bd9Sstevel@tonic-gate argv[1], (ulong_t)written,
2217c478bd9Sstevel@tonic-gate (ulong_t)size,
2227c478bd9Sstevel@tonic-gate strerror(saverr));
2237c478bd9Sstevel@tonic-gate errors++;
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate written += bytes;
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate * A write(2) call in the above loop failed so
2317c478bd9Sstevel@tonic-gate * close out this file and go on (error was
2327c478bd9Sstevel@tonic-gate * already incremented when the write(2) failed).
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate if (written < size) {
2357c478bd9Sstevel@tonic-gate (void) close(fd);
2367c478bd9Sstevel@tonic-gate argv++;
2377c478bd9Sstevel@tonic-gate argc--;
2387c478bd9Sstevel@tonic-gate continue;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate if (close(fd) < 0) {
2427c478bd9Sstevel@tonic-gate saverr = errno;
2437c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2447c478bd9Sstevel@tonic-gate "Error encountered when closing %s: %s\n"),
2457c478bd9Sstevel@tonic-gate argv[1], strerror(saverr));
2467c478bd9Sstevel@tonic-gate errors++;
2477c478bd9Sstevel@tonic-gate argv++;
2487c478bd9Sstevel@tonic-gate argc--;
2497c478bd9Sstevel@tonic-gate continue;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Only set the modes (including the sticky bit) if we
2547c478bd9Sstevel@tonic-gate * had no problems. It is not an error for the chmod(2)
2557c478bd9Sstevel@tonic-gate * to fail, but do issue a warning.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate if (chmod(argv[1], FILE_MODE) < 0)
258*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
259*6db6c2a6Sperrin "warning: couldn't set mode to %#o\n"), FILE_MODE);
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate argv++;
2627c478bd9Sstevel@tonic-gate argc--;
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate return (errors);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
usage()2677c478bd9Sstevel@tonic-gate static void usage()
2687c478bd9Sstevel@tonic-gate {
269*6db6c2a6Sperrin (void) fprintf(stderr, gettext(
270*6db6c2a6Sperrin "Usage: mkfile [-nv] <size>[g|k|b|m] <name1> [<name2>] ...\n"));
2717c478bd9Sstevel@tonic-gate exit(1);
2727c478bd9Sstevel@tonic-gate /* NOTREACHED */
2737c478bd9Sstevel@tonic-gate }
274