#include #include #include #include #include #include #define DEFAULT_PADSIZE 0x4000 #define PADCHAR 0 unsigned long padsize = DEFAULT_PADSIZE; int verbose; void usage(char *prog, int rc) { char *p; if((p = strrchr(prog, '/'))) prog = p + 1; printf("usage: %s [-p -hv] [infile [outfile]]\n\n" "where: -p is the padsize (default %d)\n" " -h gives this help\n" " -v ups the verbosity\n" , prog, DEFAULT_PADSIZE); exit(rc); } int main(int argc, char *argv[]) { int c; int in, out; unsigned long size = 0, pad; int n, w; char buf[4096]; while((c = getopt(argc, argv, "hp:v")) != EOF) switch(c) { case 'h': usage(argv[0], 0); case 'p': padsize = strtol(optarg, 0, 0); break; case 'v': ++verbose; break; default: usage(argv[0], 2); } if(optind < argc) { char *fname = argv[optind++]; if((in = open(fname, O_RDONLY)) < 0) { perror(fname); exit(1); } } else in = 0; if(optind < argc) { char *fname = argv[optind++]; if((out = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { perror(fname); exit(1); } } else out = 1; while((n = read(in, buf, sizeof(buf))) > 0) { if((w = write(out, buf, n)) != n) { printf("ERROR: short write %d of %d\n", w, n); exit(1); } size += n; } close(in); if(size % padsize) { pad = padsize - size % padsize; if(verbose) printf("File size 0x%lx pad 0x%lx to 0x%lx\n", size, pad, size + pad); memset(buf, PADCHAR, sizeof(buf)); while(pad > 0) { n = pad > sizeof(buf) ? sizeof(buf) : pad; if((w = write(out, buf, n)) != n) { printf("ERROR: short write %d of %d\n", w, n); exit(1); } pad -= n; } } else if(verbose) printf("Already aligned\n"); close(out); return 0; } /* * Local Variables: * compile-command: "gcc -O3 -Wall padit.c -o padit" * End: */