]> git.saurik.com Git - apple/system_cmds.git/blob - mkfile.tproj/mkfile.c
system_cmds-336.23.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 * "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.
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,
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."
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>
44
45 #define BF_SZ 512 /* Size of write chunks */
46
47 extern void usage(char *, char *);
48 extern void create_file(char *, quad_t, int, int);
49 extern void err_rm(char *, char *);
50
51 int
52 main (argc, argv)
53 int argc;
54 char **argv;
55 {
56 char *b_num, *prog_name;
57 char *options = "nv";
58 char c;
59 quad_t multiplier = 1;
60 quad_t file_size;
61 int len;
62 int empty = 0;
63 int verbose = 0;
64
65 prog_name = argv[0]; /* Get program name */
66 if (1 == argc)
67 usage(prog_name, options);
68
69 /* Get options */
70 opterr=1;
71
72 while ((c=getopt(argc, argv, options)) != EOF)
73 switch (c) {
74 case 'v': /* Turn on verbose setting */
75 verbose = 1;
76 break;
77 case 'n': /* Create an empty file */
78 empty = 1;
79 break;
80 default:
81 usage(prog_name, options);
82 break;
83 }
84
85 /* Stop getting options
86 */
87 argv += optind;
88 if (*argv == NULL) /* Is there a size given? */
89 usage(prog_name, options);
90
91 b_num = *argv++; /* Size of file and byte multiplier */
92 len = strlen(b_num) - 1;
93
94 if (!isdigit(b_num[len])) {
95 switch(b_num[len]) { /* Figure out multiplier */
96 case 'B':
97 case 'b':
98 multiplier = 512;
99 break;
100 case 'K':
101 case 'k':
102 multiplier = 1024;
103 break;
104 case 'M':
105 case 'm':
106 multiplier = 1024 * 1024;
107 break;
108 case 'G':
109 case 'g':
110 multiplier = 1024 * 1024 * 1024;
111 break;
112 default:
113 usage(prog_name, options);
114 }
115 }
116
117 if (*argv == NULL) /* Was a file name given? */
118 usage(prog_name, options);
119
120 if ((file_size = strtoq(b_num, NULL, 10)) == 0 )
121 err(1, "Bad file size!");
122
123 while ( *argv != NULL ) { /* Create file for each file_name */
124 create_file(*argv, file_size*multiplier, empty, verbose);
125 argv++;
126 }
127
128 return (0);
129
130 }
131
132
133 /* Create a file and make it empty (lseek) or zero'd */
134
135 void
136 create_file(file_name, size, empty, verbose)
137 char *file_name;
138 quad_t size;
139 int empty;
140 int verbose;
141 {
142 char buff[BF_SZ];
143 int fd, bytes_written = BF_SZ;
144 quad_t i;
145 mode_t mode;
146
147 if ((fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC )) == -1)
148 err(1, NULL);
149
150
151 if (empty) { /* Create an empty file */
152 lseek(fd, (off_t)size-1, SEEK_SET);
153 if ( 1 != write(fd, "\0", 1))
154 err_rm(file_name, "Write Error");
155 }
156 else {
157 bzero(buff, BF_SZ);
158
159 /*
160 * First loop: write BF_SZ chunks until you have
161 * less then BF_SZ bytes to write.
162 * Second loop: write the remaining bytes.
163 * ERRORS in the write process will cause the
164 * file to be removed before the error is
165 * reported.
166 */
167 for (i = size; i > BF_SZ; i -= bytes_written) {
168 bytes_written = write (fd, buff, BF_SZ);
169 if ( bytes_written == -1 )
170 err_rm (file_name, "Write Error");
171 }
172 for (; i > 0; i -= bytes_written) {
173 bytes_written = write (fd, buff, i);
174 if ( bytes_written == -1 )
175 err_rm (file_name, "Write Error");
176 }
177 }
178
179
180 mode = S_IRUSR | S_IWUSR;
181 /* If superuser, then set sticky bit */
182 if (! geteuid())
183 mode |= S_ISVTX;
184
185 if (fchmod(fd, mode)) /* Change permissions */
186 err_rm(file_name, NULL);
187
188 if ((close(fd)) == -1)
189 err_rm(file_name, NULL);
190
191 if (verbose)
192 (void)fprintf(stderr, "%s %qd bytes\n", file_name, size);
193
194 }
195
196 /* On error remove the file */
197
198 void
199 err_rm(filename, msg)
200 char *filename;
201 char *msg;
202 {
203 unlink(filename);
204 err(1, "(%s removed) %s", filename, msg);
205 }
206
207
208 /* Print usage string */
209 void
210 usage (prog_name, options)
211 char *prog_name;
212 char *options;
213 {
214 (void)fprintf(stderr,
215 "usage: %s [-%s] size[b|k|m|g] filename ...\n", prog_name, options);
216 exit(1);
217
218 }