umount: getopt return value is int, not char
[minix.git] / commands / loadramdisk / loadramdisk.c
blob60fcfeca82bc620566d7a79eca5ebb7ae587a792
1 /*
2 loadramdisk.c
4 Copy a device or file specified as argument to /dev/ram
5 */
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/ioctl.h>
15 #define RAM "/dev/ram"
17 char buf[10240];
19 static unsigned long size_device(int fd);
21 int main(int argc, char *argv[])
23 unsigned long off, size;
24 int r, s, fd, ramfd;
25 char *src;
27 if (argc != 2)
29 fprintf(stderr, "Usage: loadramdisk <file>\n");
30 exit(1);
32 src= argv[1];
33 fd= open(src, O_RDONLY);
34 if (fd < 0)
36 fprintf(stderr, "Unable to open '%s': %s\n",
37 src, strerror(errno));
38 exit(1);
41 /* Get the size of the device */
42 errno= 0;
43 size= size_device(fd);
44 if (errno != 0)
46 fprintf(stderr, "Lseek(end) failed on '%s': %s\n",
47 src, strerror(errno));
48 exit(1);
50 if (lseek(fd, 0, SEEK_SET) != 0)
52 fprintf(stderr, "Lseek(0) failed on '%s': %s\n",
53 src, strerror(errno));
54 exit(1);
57 ramfd= open(RAM, O_RDWR);
58 if (ramfd < 0)
60 fprintf(stderr, "Unable to open '%s': %s\n",
61 RAM, strerror(errno));
62 exit(1);
64 r= ioctl(ramfd, MIOCRAMSIZE, &size);
65 if (r != 0)
67 fprintf(stderr, "MIOCRAMSIZE %lu failed on '%s': %s\n",
68 size, RAM, strerror(errno));
69 exit(1);
72 off= 0;
73 while (off < size)
75 r= read(fd, buf, sizeof(buf));
76 if (r <= 0)
78 fprintf(stderr, "error reading '%s': %s\n",
79 src, r == 0 ? "unexpected EOF" :
80 strerror(errno));
81 exit(1);
83 s= write(ramfd, buf, r);
84 if (s != r)
86 fprintf(stderr, "error writing to '%s': %s\n",
87 s >= 0 ? "short write" : strerror(errno));
88 exit(1);
90 off += r;
92 exit(0);
95 static unsigned long size_device(int fd)
97 char b;
98 int r;
99 unsigned long low, mid, high;
101 /* Try to find the size of a device using binary search */
102 low= 0;
103 high= -1;
105 while (mid= low+(high-low)/2, mid > low)
107 if (lseek(fd, mid, SEEK_SET) != mid)
109 fprintf(stderr, "lseek to %lu failed: %s\n",
110 mid, strerror(errno));
111 exit(1);
113 r= read(fd, &b, 1);
114 if (r < 0)
116 fprintf(stderr, "read failed at position %lu: %s\n",
117 mid, strerror(errno));
118 exit(1);
120 if (r > 0)
121 low= mid;
122 else
123 high= mid;
125 return high;