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
)
76 unsigned long size
, mul
;
82 if (argc
!= 3) usage();
86 while ((unsigned) (*cp
- '0') < 10) {
87 unsigned d
= *cp
++ - '0';
88 if (size
<= (ULONG_MAX
-9) / 10) {
94 if (cp
== argv
[1]) usage();
99 case 'g': mul
*= 1024;
101 case 'm': mul
*= 1024;
103 case 'k': mul
*= 1024;
108 if (size
<= ULONG_MAX
/ mul
) {
115 if (size
> 1024L*1024*1024) {
116 printf("mkfile: A file size over 1G is a bit too much\n");
120 /* Open existing file, or create a new file. */
122 if ((fd
= open(file
, O_WRONLY
)) < 0) {
124 fd
= creat(file
, 0666);
128 printf("mkfile: Can't open %s: %s\n", file
, strerror(errno
));
132 /* How big is the file now? */
133 if ((offset
= lseek(fd
, 0, SEEK_END
)) == -1) {
134 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
138 if (offset
== 0 && size
== 0) exit(0); /* Huh? */
140 /* Write the first bit if the file is zero length. This is necessary
141 * to circumvent a DOS bug by extending a new file by lseek. We also
142 * want to make sure there are zeros in the first sector.
145 if (write(fd
, buf
, sizeof(buf
)) == -1) {
146 printf("mkfile: Can't write to %s: %s\n",
147 file
, strerror(errno
));
152 /* Seek to the required size and write 0 bytes to extend/truncate the
155 if (lseek(fd
, size
, SEEK_SET
) == -1) {
156 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
159 if (write(fd
, buf
, 0) == -1) {
160 printf("mkfile: Can't write to %s: %s\n",
161 file
, strerror(errno
));
165 /* Did the file become the required size? */
166 if ((offset
= lseek(fd
, 0, SEEK_END
)) == -1) {
167 printf("mkfile: Can't seek in %s: %s\n", file
, strerror(errno
));
170 if (offset
!= size
) {
171 printf("mkfile: Failed to extend %s. Disk full?\n", file
);
178 * $PchId: mkfile.c,v 1.4 2000/08/13 22:06:40 philip Exp $