vm: don't force physical addresses to be nonzero.
[minix.git] / boot / mkfile.c
blobd9a130d544ea49fbc877283a39026c29461ed17f
1 /* mkfile 1.0 - create a file under DOS for use as a Minix "disk".
2 * Author: Kees J. Bot
3 * 9 May 1998
4 */
5 #define nil 0
6 #include <sys/types.h>
7 #include <string.h>
8 #include <limits.h>
10 /* Stuff normally found in <unistd.h>, <errno.h>, etc. */
11 extern int errno;
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, ...);
19 #define O_WRONLY 1
20 #define SEEK_SET 0
21 #define SEEK_END 2
23 /* Kernel printf requires a putk() function. */
24 void putk(int c)
26 char ch = c;
28 if (c == 0) return;
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");
37 exit(1);
40 char *strerror(int err)
41 /* Translate some DOS error numbers to text. */
43 static struct errlist {
44 int err;
45 char *what;
46 } errlist[] = {
47 { 0, "No error" },
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" },
57 struct errlist *ep;
58 static char unknown[]= "Error 65535";
59 unsigned e;
60 char *p;
62 for (ep= errlist; ep < errlist + sizeof(errlist)/sizeof(errlist[0]);
63 ep++) {
64 if (ep->err == err) return ep->what;
66 p= unknown + sizeof(unknown) - 1;
67 e= err;
68 do *--p= '0' + (e % 10); while ((e /= 10) > 0);
69 strcpy(unknown + 6, p);
70 return unknown;
73 int main(int argc, char **argv)
75 static char buf[512];
76 unsigned long size, mul;
77 off_t offset;
78 char *cp;
79 int fd;
80 char *file;
82 if (argc != 3) usage();
84 cp= argv[1];
85 size= 0;
86 while ((unsigned) (*cp - '0') < 10) {
87 unsigned d= *cp++ - '0';
88 if (size <= (ULONG_MAX-9) / 10) {
89 size= size * 10 + d;
90 } else {
91 size= ULONG_MAX;
94 if (cp == argv[1]) usage();
95 while (*cp != 0) {
96 mul = 1;
97 switch (*cp++) {
98 case 'G':
99 case 'g': mul *= 1024;
100 case 'M':
101 case 'm': mul *= 1024;
102 case 'K':
103 case 'k': mul *= 1024;
104 case 'B':
105 case 'b': break;
106 default: usage();
108 if (size <= ULONG_MAX / mul) {
109 size *= mul;
110 } else {
111 size= ULONG_MAX;
115 if (size > 1024L*1024*1024) {
116 printf("mkfile: A file size over 1G is a bit too much\n");
117 exit(1);
120 /* Open existing file, or create a new file. */
121 file= argv[2];
122 if ((fd= open(file, O_WRONLY)) < 0) {
123 if (errno == 2) {
124 fd= creat(file, 0666);
127 if (fd < 0) {
128 printf("mkfile: Can't open %s: %s\n", file, strerror(errno));
129 exit(1);
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));
135 exit(1);
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.
144 if (offset == 0) {
145 if (write(fd, buf, sizeof(buf)) == -1) {
146 printf("mkfile: Can't write to %s: %s\n",
147 file, strerror(errno));
148 exit(1);
152 /* Seek to the required size and write 0 bytes to extend/truncate the
153 * file to that size.
155 if (lseek(fd, size, SEEK_SET) == -1) {
156 printf("mkfile: Can't seek in %s: %s\n", file, strerror(errno));
157 exit(1);
159 if (write(fd, buf, 0) == -1) {
160 printf("mkfile: Can't write to %s: %s\n",
161 file, strerror(errno));
162 exit(1);
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));
168 exit(1);
170 if (offset != size) {
171 printf("mkfile: Failed to extend %s. Disk full?\n", file);
172 exit(1);
174 return 0;
178 * $PchId: mkfile.c,v 1.4 2000/08/13 22:06:40 philip Exp $