]>
git.saurik.com Git - apple/system_cmds.git/blob - mkfile.tproj/mkfile.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved
28 * 29-Aug-97 Daniel Wade (danielw) at Apple
35 #include <sys/types.h>
46 #define BF_SZ 512 /* Size of write chunks */
48 extern void usage(char *, char *);
49 extern void create_file(char *, quad_t
, int, int);
50 extern void err_rm(char *, char *);
57 char *b_num
, *prog_name
;
67 prog_name
= argv
[0]; /* Get program name */
69 usage(prog_name
, options
);
74 while ((c
=getopt(argc
, argv
, options
)) != EOF
)
76 case 'v': /* Turn on verbose setting */
79 case 'n': /* Create an empty file */
83 usage(prog_name
, options
);
87 /* Stop getting options
90 if (*argv
== NULL
) /* Is there a size given? */
91 usage(prog_name
, options
);
93 b_num
= *argv
++; /* Size of file and byte multiplier */
94 len
= strlen(b_num
) - 1;
96 if (!isdigit(b_num
[len
])) {
97 switch(b_num
[len
]) { /* Figure out multiplier */
108 multiplier
= 1024 * 1024;
112 multiplier
= 1024 * 1024 * 1024;
115 usage(prog_name
, options
);
119 if (*argv
== NULL
) /* Was a file name given? */
120 usage(prog_name
, options
);
122 if ((file_size
= strtoll(b_num
, &endptr
, 10)) == 0 &&
123 (*endptr
!= 0 && endptr
!= &b_num
[len
])) {
124 err(1, "Bad file size!");
127 while ( *argv
!= NULL
) { /* Create file for each file_name */
128 create_file(*argv
, file_size
*multiplier
, empty
, verbose
);
137 /* Create a file and make it empty (lseek) or zero'd */
140 create_file(file_name
, size
, empty
, verbose
)
147 int fd
, bytes_written
= BF_SZ
;
149 mode_t mode
= S_IRUSR
| S_IWUSR
;
151 /* If superuser, then set sticky bit */
152 if (!geteuid()) mode
|= S_ISVTX
;
154 if ((fd
= open(file_name
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
)) == -1)
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");
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
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");
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");
186 if (fchmod(fd
, mode
)) /* Change permissions */
187 err_rm(file_name
, NULL
);
189 if ((close(fd
)) == -1)
190 err_rm(file_name
, NULL
);
193 (void)fprintf(stderr
, "%s %qd bytes\n", file_name
, size
);
197 /* On error remove the file */
200 err_rm(filename
, msg
)
205 err(1, "(%s removed) %s", filename
, msg
);
209 /* Print usage string */
211 usage (prog_name
, options
)
215 (void)fprintf(stderr
,
216 "usage: %s [-%s] size[b|k|m|g] filename ...\n", prog_name
, options
);