Applied change 1505 of the NewOS repository: added the -p option to makeflop

to be able to pad to the floppy block size.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@285 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2002-07-17 19:42:44 +00:00
parent 19111f404e
commit 16ba498087
+51 -5
View File
@@ -2,7 +2,10 @@
** Copyright 2001, Travis Geiselbrecht. All rights reserved. ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License. ** Distributed under the terms of the NewOS License.
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@@ -11,25 +14,53 @@
# define O_BINARY 0 # define O_BINARY 0
#endif #endif
void usage(char **argv)
void
usage(char *progName)
{ {
printf("usage: %s bootblock payload outfile\n", argv[0]); printf("usage: %s [-p #padding] bootblock payload outfile\n", progName);
} }
int main(int argc, char *argv[])
int
main(int argc, char *argv[])
{ {
struct stat st; struct stat st;
int err;
unsigned int blocks; unsigned int blocks;
unsigned char bootsector[1024]; unsigned char bootsector[1024];
unsigned char buf[512]; unsigned char buf[512];
size_t read_size; size_t read_size;
size_t written_bytes;
int padding = 0;
int infd; int infd;
int outfd; int outfd;
char opt;
int err;
char *progName = argv[0];
if (strrchr(progName,'/'))
progName = strrchr(progName,'/') + 1;
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
padding = atoi(optarg);
if (padding < 0) {
usage(progName);
return -1;
}
break;
default:
usage(progName);
return -1;
}
}
argc -= optind - 1;
argv += optind - 1;
if (argc < 4) { if (argc < 4) {
printf("insufficient args\n"); printf("insufficient args\n");
usage(argv); usage(progName);
return -1; return -1;
} }
@@ -65,6 +96,7 @@ int main(int argc, char *argv[])
bootsector[3] = (blocks & 0xff00) >> 8; bootsector[3] = (blocks & 0xff00) >> 8;
write(outfd, bootsector, sizeof(bootsector)); write(outfd, bootsector, sizeof(bootsector));
written_bytes = sizeof(bootsector);
infd = open(argv[2], O_BINARY|O_RDONLY); infd = open(argv[2], O_BINARY|O_RDONLY);
if (infd < 0) { if (infd < 0) {
@@ -74,6 +106,20 @@ int main(int argc, char *argv[])
while ((read_size = read(infd, buf, sizeof(buf))) > 0) { while ((read_size = read(infd, buf, sizeof(buf))) > 0) {
write(outfd, buf, read_size); write(outfd, buf, read_size);
written_bytes += read_size;
}
if (padding) {
if (written_bytes % padding) {
size_t towrite = padding - written_bytes % padding;
unsigned char *buf = malloc(towrite);
memset(buf, 0, towrite);
write(outfd, buf, towrite);
written_bytes += towrite;
printf("output file padded to %ld\n", (unsigned long)written_bytes);
}
} }
close(outfd); close(outfd);