- if (maxlen > TRX_MAX_LEN) {
- fprintf(stderr, "WARNING: maxlen exceeds default maximum! Beware of overwriting nvram!\n");
- }
-
- if (!(buf = malloc(maxlen))) {
- fprintf(stderr, "malloc failed\n");
- return EXIT_FAILURE;
- }
-
- p = (struct trx_header *) buf;
-
- p->magic = STORE32_LE(TRX_MAGIC);
- cur_len = sizeof(struct trx_header);
- p->flag_version = STORE32_LE((TRX_VERSION << 16));
-
- i = 0;
-
- while (optind < argc) {
- p->offsets[i++] = STORE32_LE(cur_len);
-
- if (!(in = fopen(argv[optind], "r"))) {
- fprintf(stderr, "can not open \"%s\" for reading\n", argv[optind]);
- usage();
- }
-
- n = fread(buf + cur_len, 1, maxlen - cur_len, in);
- if (!feof(in)) {
- fprintf(stderr, "fread failure or file \"%s\" too large\n",
- argv[optind]);
- fclose(in);
- return EXIT_FAILURE;
- }
-
- fclose(in);
-
- ++optind;
-
- if (optind < argc) {
-#undef ROUND
-#define ROUND 4
- if (n & (ROUND-1)) {
- memset(buf + cur_len + n, 0, ROUND - (n & (ROUND-1)));
- n += ROUND - (n & (ROUND-1));
- }
- }
-
- cur_len += n;
- }
-