16 int main(int argc, char **argv)
25 while ((c = getopt(argc, argv, "o:")) != EOF) {
29 outfile = strdup(optarg);
37 if (outfile == NULL) {
38 printf("You must specify output filename with -o\n");
42 printf("You must specify some files to pack, duh\n");
47 /* prepare to pack things - get sizes and calculate offsets */
48 printf("%u files to pack\n", num_entries);
49 datafile = calloc(num_entries, sizeof(DirEntry));
51 /* skip past the directory structure */
52 offset = 4 + (num_entries * 20);
54 for (i = 0; i < num_entries; i++) {
56 if (stat(argv[i], &dummy)) {
57 fprintf(stderr, "%s is not accessible: ", argv[i]);
61 if (strlen(argv[i]) > 12) {
62 fprintf(stderr, "filename %s is longer than 12 chars\n", argv[i]);
65 strncpy(datafile[i].filename, argv[i], 12);
66 datafile[i].size = dummy.st_size;
67 /* num_entries * (12 + 8) */
68 datafile[i].offset = offset;
69 offset += datafile[i].size;
72 /* here, we checked that all files are ok, and ready to roll the packfile */
73 fd = open(outfile, O_RDWR | O_CREAT, 0644);
75 perror("opening packfile");
78 printf("Opened %s\n", outfile);
80 /* write number of entries in this data file */
81 write(fd, &num_entries, 4);
83 /* write the directory structure */
84 for (i = 0; i < num_entries; i++) {
85 write(fd, &datafile[i], 20);
88 /* write in the actual files */
89 for (i = 0; i < num_entries; i++) {
93 printf("adding %s ", argv[i]);
95 infd = open(argv[i], O_RDONLY);
97 perror("opening file");
100 buf = malloc(datafile[i].size + 16);
101 read(infd, buf, datafile[i].size);
103 write(fd, buf, datafile[i].size);