#include #include #include #include #include #include #include "dlx.h" #include "memory.h" /* subsumes file.h */ #define VERSION "$Id: dlxobj2binary.c,v 1.2 2004/12/03 15:32:05 obi Exp $" int main(int argc, char **argv) { int c, option_index, start = -1, length = -1; char *output = 0; int fd; printf("dlxobj2binary (" VERSION ", compiled " __DATE__ ")\n"); /* option parsing */ while(1) { static struct option long_options[] = { {"start", 1, 0, 0}, {"length", 1, 0, 0}, {"output", 1, 0, 0}, {0, 0, 0, 0} }; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; if (c > 0) { fprintf(stderr,"Cannot handle case in %s:%d\n", __FILE__, __LINE__ ); exit(666); } /* c == 0 */ switch(option_index) { case 0: /* start */ start = atoi(optarg); if( start < 0 || start&0x3 || start >= INIT_MEMORY_SIZE ) { fprintf(stderr,"The argument to --start has to be non-negative, divisible by four, and less than %d.\n", INIT_MEMORY_SIZE); exit(1); } break; case 1: /* start */ length = atoi(optarg); if( length <= 0 || length&0x3 ) { fprintf(stderr,"The argument to --length has to be non-negative, divisible by four.\n" ); exit(1); } break; case 2: /* start */ output = optarg; break; default: fprintf(stderr,"Cannot handle case in %s:%d\n", __FILE__, __LINE__ ); exit(666); } } if( start < 0 ) { fprintf(stderr,"?Must specify --start X argument.\n" ); exit(1); } if( length < 0 ) { fprintf(stderr,"?Must specify --length X argument.\n" ); exit(1); } if( !output ) { fprintf(stderr,"?Must specify --output OUTPUT argument.\n" ); exit(1); } if( start + length >= INIT_MEMORY_SIZE ) { fprintf(stderr,"Sum of start and length exceeds the memory size, %d bytes.\n", INIT_MEMORY_SIZE ); exit(1); } if(optind != argc - 1) { printf("?Missing file name\nUsage: %s --start X --length X FILE, file name missing\n", argv[0]); exit(1); } /* load the object file and dump the desired part... */ init_state(); init_memory(INIT_MEMORY_SIZE); load_file(argv[optind]); if( (fd=open(output,O_CREAT|O_WRONLY,0644))==-1 ) { perror("?Error on open()"); exit(1); } if( write(fd,state.memory+start,length)!=length ) { perror("?Error on write()"); exit(1); } if( close(fd)==-1 ) { perror("?Error on close()"); exit(1); } return 0; }