void CVariable_Container::allocate_space(int _dims,...){ std::vector<int> lengths(_dims); va_list args; va_start(args, _dims); for (int i = 0; i < _dims; i++) lengths[i] = va_arg(args, int); va_end(args); allocate_space(lengths); }
void write_file(void) { FILE *fp; int slot; int block, i, fsize, fbufsize; struct stat st; char *fbuf; if (!quiet) printf("Writing file %s\n", ufilename); if (stat(ufilename, &st) != 0) err(1, "stat %s", ufilename); if (st.st_size == 0) errx(1, "%s: file is empty", vfilename); if (!quiet) printf("File %s has %lld bytes\n", ufilename, st.st_size); slot = -1; for (i = 0; i < SGI_SIZE_VOLDIR; ++i) { if (volhdr->voldir[i].name[0] == '\0' && slot < 0) slot = i; if (strcmp(vfilename, volhdr->voldir[i].name) == 0) { slot = i; break; } } if (slot == -1) errx(1, "no more directory entries available"); if (betoh32(volhdr->voldir[slot].block) > 0) { if (!quiet) printf("File %s exists, removing old file\n", vfilename); volhdr->voldir[slot].name[0] = 0; volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0; } /* XXX assumes volume header starts at 0? */ block = allocate_space((int)st.st_size); if (block < 0) errx(1, "no more space available"); /* * Make sure the name in the volume header is max. 8 chars, * NOT including NUL. */ if (strlen(vfilename) > sizeof(volhdr->voldir[slot].name)) warnx("%s: filename is too long and will be truncated", vfilename); strncpy(volhdr->voldir[slot].name, vfilename, sizeof(volhdr->voldir[slot].name)); volhdr->voldir[slot].block = htobe32(block); volhdr->voldir[slot].bytes = htobe32(st.st_size); write_volhdr(); /* Write the file itself. */ if (lseek(fd, block * DEV_BSIZE, SEEK_SET) == -1) err(1, "lseek write"); fbufsize = volhdr->dp.dp_secbytes; if ((fbuf = malloc(fbufsize)) == NULL) err(1, "failed to allocate buffer"); i = st.st_size; fp = fopen(ufilename, "r"); while (i > 0) { bzero(fbuf, fbufsize); fsize = i > fbufsize ? fbufsize : i; if (fread(fbuf, 1, fsize, fp) != fsize) err(1, "reading file from disk"); if (write(fd, fbuf, fbufsize) != fbufsize) err(1, "writing file to SGI volume header"); i -= fsize; } fclose(fp); free(fbuf); }
void write_file(void) { FILE *fp; int slot; size_t namelen; int block, i; struct stat st; char fbuf[512]; if (!opt_q) printf("Writing file %s\n", ufilename); if (stat(ufilename, &st) < 0) { perror("stat"); exit(1); } if (!opt_q) printf("File %s has %lld bytes\n", ufilename, st.st_size); slot = -1; for (i = 0; i < SGI_BOOT_BLOCK_MAXVOLDIRS; ++i) { if (volhdr->voldir[i].name[0] == '\0' && slot < 0) slot = i; if (names_match(i, vfilename)) { slot = i; break; } } if (slot == -1) { printf("No directory space for file %s\n", vfilename); exit(1); } /* -w can overwrite, -a won't overwrite */ if (be32toh(volhdr->voldir[slot].block) > 0) { if (!opt_q) printf("File %s exists, removing old file\n", vfilename); volhdr->voldir[slot].name[0] = 0; volhdr->voldir[slot].block = volhdr->voldir[slot].bytes = 0; } if (st.st_size == 0) { printf("bad file size\n"); exit(1); } /* XXX assumes volume header starts at 0? */ block = allocate_space((int)st.st_size); if (block < 0) { printf("No space for file\n"); exit(1); } /* * Make sure the name in the volume header is max. 8 chars, * NOT including NUL. */ namelen = strlen(vfilename); if (namelen > sizeof(volhdr->voldir[slot].name)) { printf("Warning: '%s' is too long for volume header, ", vfilename); namelen = sizeof(volhdr->voldir[slot].name); printf("truncating to '%.8s'\n", vfilename); } /* Populate it w/ NULs */ memset(volhdr->voldir[slot].name, 0, sizeof(volhdr->voldir[slot].name)); /* Then copy the name */ memcpy(volhdr->voldir[slot].name, vfilename, namelen); volhdr->voldir[slot].block = htobe32(block); volhdr->voldir[slot].bytes = htobe32(st.st_size); write_volhdr(); /* write the file itself */ i = lseek(fd, block * 512, SEEK_SET); if (i < 0) { perror("lseek write"); exit(1); } i = st.st_size; fp = fopen(ufilename, "r"); while (i > 0) { fread(fbuf, 1, i > 512 ? 512 : i, fp); if (write(fd, fbuf, 512) != 512) { perror("write file"); exit(1); } i -= i > 512 ? 512 : i; } fclose(fp); }