#define _GNU_SOURCE
#include "usr_hdr.h"
#include <getopt.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
static void die(const char *s)
{
error(EXIT_FAILURE, errno, s);
}
static void __attribute__ ((noreturn)) die_bad_usage()
{
fprintf(stderr, "Try `%s --help' for more information.\n", program_invocation_name);
exit(EXIT_FAILURE);
}
static void print_help()
{
printf("Usage: %s BIN_ARCHIVE LIB_ARCHIVE\n", program_invocation_name);
printf("Create archives header for XOS.\n");
printf("\n");
printf(" -h, --help display this help and exit\n");
printf(" -o, --output=FILE write output in FILE instead of standard output\n");
}
static void makeusrhdr(const char *bin_filename, const char *lib_filename, FILE *output)
{
usr_hdr_t usr_hdr;
unsigned long offset;
struct stat statbuf;
offset = sizeof (usr_hdr);
usr_hdr.bin_offset = offset;
if (stat(bin_filename, &statbuf) == -1)
error(EXIT_FAILURE, errno, "cannot stat `%s'", bin_filename);
offset += statbuf.st_size;
usr_hdr.lib_offset = offset;
fwrite(&usr_hdr, sizeof (usr_hdr_t), 1, output);
}
int main(int argc, char **argv)
{
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"output", required_argument, NULL, 'o'},
{NULL, 0, NULL, 0}
};
char *output_filename;
FILE *output;
int c;
output_filename = NULL;
while ((c = getopt_long(argc, argv, "ho:", longopts, NULL)) != -1)
switch (c) {
case 'h':
print_help();
return EXIT_SUCCESS;
case 'o':
if (output_filename)
free(output_filename);
if (!(output_filename = strdup(optarg)))
die("strdup");
break;
case '?':
die_bad_usage();
}
if (output_filename) {
if (!(output = fopen(output_filename, "w+")))
die(output_filename);
free(output_filename);
}
else
output = stdout;
argc -= optind - 1;
argv += optind - 1;
if (argc != 3)
die_bad_usage();
makeusrhdr(argv[1], argv[2], output);
if (output != stdout)
fclose(output);
return EXIT_SUCCESS;
}