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