12 typedef unsigned int u_int32_t;
21 int main(int argc, char **argv)
31 while ((c = getopt(argc, argv, "o:")) != EOF) {
35 outfile = strdup(optarg);
45 if (argv[c][0]=='-') {
46 if (argv[c][1]=='o') {
47 if ( ((c+1)<argc) && (argv[c+1]) ) {
48 outfile = strdup(argv[c+1]);
61 if (outfile == NULL) {
62 printf("You must specify output filename with -o\n");
66 printf("You must specify some files to pack, duh\n");
71 /* prepare to pack things - get sizes and calculate offsets */
72 printf("%u files to pack\n", num_entries);
73 datafile = calloc(num_entries, sizeof(DirEntry));
75 /* skip past the directory structure */
76 offset = 4 + (num_entries * 20);
78 for (i = 0; i < num_entries; i++) {
80 if (stat(argv[i], &dummy)) {
81 fprintf(stderr, "%s is not accessible: ", argv[i]);
85 if (strlen(argv[i]) > 12) {
86 fprintf(stderr, "filename %s is longer than 12 chars\n", argv[i]);
89 strncpy(datafile[i].filename, argv[i], 12);
90 datafile[i].size = dummy.st_size;
91 /* num_entries * (12 + 8) */
92 datafile[i].offset = offset;
93 offset += datafile[i].size;
96 /* here, we checked that all files are ok, and ready to roll the packfile */
97 fd = open(outfile, O_RDWR | O_CREAT | O_BINARY, 0644);
99 perror("opening packfile");
102 printf("Opened %s\n", outfile);
104 /* write number of entries in this data file */
105 write(fd, &num_entries, 4);
107 /* write the directory structure */
108 for (i = 0; i < num_entries; i++) {
109 write(fd, &datafile[i], 20);
112 /* write in the actual files */
113 for (i = 0; i < num_entries; i++) {
117 printf("adding %s ", argv[i]);
119 infd = open(argv[i], O_RDONLY | O_BINARY);
121 perror("opening file");
124 buf = malloc(datafile[i].size + 16);
125 read(infd, buf, datafile[i].size);
127 write(fd, buf, datafile[i].size);