2020-04-13 22:05:03 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2020-06-08 20:09:45 -04:00
|
|
|
#include <errno.h>
|
2020-04-13 22:05:03 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-09-20 09:38:28 -04:00
|
|
|
static char *buf;
|
|
|
|
static int blkcnt;
|
|
|
|
|
2020-06-16 19:58:00 -04:00
|
|
|
static void usage()
|
2020-04-13 22:05:03 -04:00
|
|
|
{
|
2020-09-20 09:38:28 -04:00
|
|
|
fprintf(stderr, "Usage: blkpack dirname [dirname ...]\n");
|
2020-04-13 22:05:03 -04:00
|
|
|
}
|
|
|
|
|
2020-09-20 09:38:28 -04:00
|
|
|
static int spit(char *dirname)
|
2020-04-13 22:05:03 -04:00
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *ep;
|
2020-09-20 09:38:28 -04:00
|
|
|
|
|
|
|
dp = opendir(dirname);
|
2020-04-13 22:05:03 -04:00
|
|
|
if (dp == NULL) {
|
2020-09-20 09:38:28 -04:00
|
|
|
fprintf(stderr, "Couldn't open directory %s.\n", dirname);
|
2020-04-13 22:05:03 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while ((ep = readdir(dp))) {
|
|
|
|
if ((strcmp(ep->d_name, ".") == 0) || strcmp(ep->d_name, "..") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int blkid = atoi(ep->d_name);
|
|
|
|
if (blkid >= blkcnt) {
|
|
|
|
int newcnt = blkid+1;
|
|
|
|
buf = realloc(buf, newcnt*1024);
|
2020-06-16 19:58:23 -04:00
|
|
|
memset(buf+(blkcnt*1024), 0, (newcnt-blkcnt)*1024);
|
2020-04-13 22:05:03 -04:00
|
|
|
blkcnt = newcnt;
|
|
|
|
}
|
2020-09-20 09:38:28 -04:00
|
|
|
char *fullpath = malloc(strlen(dirname) + MAXNAMLEN + 2);
|
|
|
|
strcpy(fullpath, dirname);
|
2020-04-13 22:05:03 -04:00
|
|
|
strcat(fullpath, "/");
|
|
|
|
strcat(fullpath, ep->d_name);
|
|
|
|
FILE *fp = fopen(fullpath, "r");
|
2020-09-20 09:38:28 -04:00
|
|
|
free(fullpath);
|
2020-06-08 20:09:45 -04:00
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Could not open %s: %s\n", ep->d_name, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-13 22:05:03 -04:00
|
|
|
char *line = NULL;
|
|
|
|
size_t n = 0;
|
|
|
|
for (int i=0; i<16; i++) {
|
|
|
|
ssize_t cnt = getline(&line, &n, fp);
|
|
|
|
if (cnt < 0) break;
|
|
|
|
if (cnt > 65) {
|
|
|
|
fprintf(stderr, "Line %d too long in blk %s\n", i+1, ep->d_name);
|
|
|
|
}
|
|
|
|
strncpy(buf+(blkid*1024)+(i*64), line, cnt-1);
|
|
|
|
}
|
2020-06-04 10:39:59 -04:00
|
|
|
ssize_t cnt = getline(&line, &n, fp);
|
|
|
|
if (cnt > 0) {
|
|
|
|
fprintf(stderr, "blk %s has more than 16 lines\n", ep->d_name);
|
|
|
|
}
|
2020-04-13 22:05:03 -04:00
|
|
|
free(line);
|
2020-06-08 20:09:45 -04:00
|
|
|
fclose(fp);
|
2020-04-13 22:05:03 -04:00
|
|
|
}
|
2020-09-20 09:38:28 -04:00
|
|
|
closedir(dp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
buf = NULL;
|
|
|
|
blkcnt = 0;
|
|
|
|
for (int i=1; i<argc; i++) {
|
|
|
|
if (spit(argv[i]) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 22:05:03 -04:00
|
|
|
fwrite(buf, 1024, blkcnt, stdout);
|
|
|
|
free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|