]> git.saurik.com Git - apple/system_cmds.git/blame - mkfile.tproj/mkfile.c
system_cmds-597.1.1.tar.gz
[apple/system_cmds.git] / mkfile.tproj / mkfile.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
2fc1e207
A
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
1815bff5
A
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2fc1e207
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
1815bff5
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved
26 *
27 * HISTORY
28 * 29-Aug-97 Daniel Wade (danielw) at Apple
29 * Created.
30 *
31 */
32
33
34
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43#include <ctype.h>
34d340d7 44#include <err.h>
1815bff5
A
45
46#define BF_SZ 512 /* Size of write chunks */
47
48extern void usage(char *, char *);
49extern void create_file(char *, quad_t, int, int);
50extern void err_rm(char *, char *);
51
52int
53main (argc, argv)
54 int argc;
55 char **argv;
56{
57 char *b_num, *prog_name;
58 char *options = "nv";
59 char c;
ef8ad44b
A
60 off_t multiplier = 1;
61 off_t file_size;
1815bff5
A
62 int len;
63 int empty = 0;
64 int verbose = 0;
ef8ad44b 65 char* endptr = NULL;
1815bff5
A
66
67 prog_name = argv[0]; /* Get program name */
68 if (1 == argc)
69 usage(prog_name, options);
70
71 /* Get options */
72 opterr=1;
73
74 while ((c=getopt(argc, argv, options)) != EOF)
75 switch (c) {
76 case 'v': /* Turn on verbose setting */
77 verbose = 1;
78 break;
79 case 'n': /* Create an empty file */
80 empty = 1;
81 break;
82 default:
83 usage(prog_name, options);
84 break;
85 }
86
87 /* Stop getting options
88 */
89 argv += optind;
90 if (*argv == NULL) /* Is there a size given? */
91 usage(prog_name, options);
92
93 b_num = *argv++; /* Size of file and byte multiplier */
94 len = strlen(b_num) - 1;
95
96 if (!isdigit(b_num[len])) {
97 switch(b_num[len]) { /* Figure out multiplier */
98 case 'B':
99 case 'b':
100 multiplier = 512;
101 break;
102 case 'K':
103 case 'k':
104 multiplier = 1024;
105 break;
106 case 'M':
107 case 'm':
108 multiplier = 1024 * 1024;
109 break;
110 case 'G':
111 case 'g':
112 multiplier = 1024 * 1024 * 1024;
113 break;
114 default:
115 usage(prog_name, options);
116 }
117 }
118
119 if (*argv == NULL) /* Was a file name given? */
120 usage(prog_name, options);
121
ef8ad44b
A
122 if ((file_size = strtoll(b_num, &endptr, 10)) == 0 &&
123 (*endptr != 0 && endptr != &b_num[len])) {
1815bff5 124 err(1, "Bad file size!");
ef8ad44b 125 }
1815bff5
A
126
127 while ( *argv != NULL ) { /* Create file for each file_name */
128 create_file(*argv, file_size*multiplier, empty, verbose);
129 argv++;
130 }
131
132 return (0);
133
134}
135
136
137/* Create a file and make it empty (lseek) or zero'd */
138
139void
140create_file(file_name, size, empty, verbose)
141 char *file_name;
142 quad_t size;
143 int empty;
144 int verbose;
145{
146 char buff[BF_SZ];
147 int fd, bytes_written = BF_SZ;
148 quad_t i;
34d340d7 149 mode_t mode = S_IRUSR | S_IWUSR;
1815bff5 150
34d340d7
A
151 /* If superuser, then set sticky bit */
152 if (!geteuid()) mode |= S_ISVTX;
153
154 if ((fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
1815bff5
A
155 err(1, NULL);
156
157
158 if (empty) { /* Create an empty file */
159 lseek(fd, (off_t)size-1, SEEK_SET);
160 if ( 1 != write(fd, "\0", 1))
161 err_rm(file_name, "Write Error");
162 }
163 else {
164 bzero(buff, BF_SZ);
165
166 /*
167 * First loop: write BF_SZ chunks until you have
168 * less then BF_SZ bytes to write.
169 * Second loop: write the remaining bytes.
170 * ERRORS in the write process will cause the
171 * file to be removed before the error is
172 * reported.
173 */
174 for (i = size; i > BF_SZ; i -= bytes_written) {
175 bytes_written = write (fd, buff, BF_SZ);
176 if ( bytes_written == -1 )
177 err_rm (file_name, "Write Error");
178 }
179 for (; i > 0; i -= bytes_written) {
180 bytes_written = write (fd, buff, i);
181 if ( bytes_written == -1 )
182 err_rm (file_name, "Write Error");
183 }
184 }
185
34d340d7
A
186 if (fchmod(fd, mode)) /* Change permissions */
187 err_rm(file_name, NULL);
1815bff5 188
34d340d7
A
189 if ((close(fd)) == -1)
190 err_rm(file_name, NULL);
1815bff5
A
191
192 if (verbose)
193 (void)fprintf(stderr, "%s %qd bytes\n", file_name, size);
194
195}
196
197/* On error remove the file */
198
199void
200err_rm(filename, msg)
201 char *filename;
202 char *msg;
203{
204 unlink(filename);
205 err(1, "(%s removed) %s", filename, msg);
206}
207
208
209/* Print usage string */
210void
211usage (prog_name, options)
212 char *prog_name;
213 char *options;
214{
215 (void)fprintf(stderr,
216 "usage: %s [-%s] size[b|k|m|g] filename ...\n", prog_name, options);
217 exit(1);
218
219}