1 /* mkfile 1.0 - create a file under DOS for use as a Minix "disk".
10 /* Stuff normally found in <unistd.h>, <errno.h>, etc. */
12 int creat(const char *file
, int mode
);
13 int open(const char *file
, int oflag
);
14 off_t
lseek(int fd
, off_t offset
, int whence
);
15 ssize_t
write(int fd
, const char *buf
, size_t len
);
16 void exit(int status
);
17 int printf(const char *fmt
, ...);
23 /* Kernel printf requires a putk() function. */
29 if (c
== '\n') putk('\r');
30 (void) write(2, &ch
, 1);
33 static void usage(void)
35 printf("Usage: mkfile <size>[gmk] <file>\n"
36 "(Example sizes, all 50 meg: 52428800, 51200k, 50m)\n");
40 char *strerror(int err
)
41 /* Translate some DOS error numbers to text. */
43 static struct errlist
{
48 { 1, "Function number invalid" },
49 { 2, "File not found" },
50 { 3, "Path not found" },
51 { 4, "Too many open files" },
52 { 5, "Access denied" },
53 { 6, "Invalid handle" },
54 { 12, "Access code invalid" },
55 { 39, "Insufficient disk space" },
58 static char unknown
[]= "Error 65535";
62 for (ep
= errlist
; ep
< errlist
+ sizeof(errlist
)/sizeof(errlist
[0]);
64 if (ep
->err
== err
) return ep
->what
;
66 p
= unknown
+ sizeof(unknown
) - 1;
68 do *--p
= '0' + (e
% 10); while ((e
/= 10) > 0);
69 strcpy(unknown
+ 6, p
);
73 int main(int argc
, char **argv
)
77 unsigned long size
, mul
;
83 if (argc
!= 3) usage();
87 while ((unsigned) (*cp
- '0') < 10) {
88 unsigned d
= *cp
++ - '0';
89 if (size
<= (ULONG_MAX
-9) / 10) {
95 if (cp
== argv
[1]) usage();
100 case 'g': mul
*= 1024;
102 case 'm': mul
*= 1024;
104 case 'k': mul
*= 1024;
109 if (size
<= ULONG_MAX
/ mul
) {
116 if (size
> 1024L*1024*1024) {
117 printf("mkfile: A file size over 1G is a bit too much\n");
121 /* Open existing file, or create a new file. */
123 if ((fd
= open(file
, O_WRONLY
)) < 0) {
125 fd
= creat(file
, 0666);
129 printf("mkfile: Can't open %s: %s\n", file
, strerror(errno
));
133 /* How big is the file now? */
134 if ((offset
= lseek(fd
, 0, SEEK_END
)) == -1) {
135 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
139 if (offset
== 0 && size
== 0) exit(0); /* Huh? */
141 /* Write the first bit if the file is zero length. This is necessary
142 * to circumvent a DOS bug by extending a new file by lseek. We also
143 * want to make sure there are zeros in the first sector.
146 if (write(fd
, buf
, sizeof(buf
)) == -1) {
147 printf("mkfile: Can't write to %s: %s\n",
148 file
, strerror(errno
));
153 /* Seek to the required size and write 0 bytes to extend/truncate the
156 if (lseek(fd
, size
, SEEK_SET
) == -1) {
157 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
160 if (write(fd
, buf
, 0) == -1) {
161 printf("mkfile: Can't write to %s: %s\n",
162 file
, strerror(errno
));
166 /* Did the file become the required size? */
167 if ((offset
= lseek(fd
, 0, SEEK_END
)) == -1) {
168 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
171 if (offset
!= size
) {
172 printf("mkfile: Failed to extend %s. Disk full?\n", file
);
179 * $PchId: mkfile.c,v 1.4 2000/08/13 22:06:40 philip Exp $