Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.6KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. void usage()
  8. {
  9. fprintf(stderr, "Usage: blkpack dirname\n");
  10. }
  11. int main(int argc, char *argv[])
  12. {
  13. DIR *dp;
  14. struct dirent *ep;
  15. char *buf = NULL;
  16. int blkcnt = 0;
  17. if (argc != 2) {
  18. usage();
  19. return 1;
  20. }
  21. dp = opendir(argv[1]);
  22. if (dp == NULL) {
  23. fprintf(stderr, "Couldn't open directory.\n");
  24. return 1;
  25. }
  26. while ((ep = readdir(dp))) {
  27. if ((strcmp(ep->d_name, ".") == 0) || strcmp(ep->d_name, "..") == 0) {
  28. continue;
  29. }
  30. if (ep->d_type != DT_REG) {
  31. continue;
  32. }
  33. int blkid = atoi(ep->d_name);
  34. if (blkid >= blkcnt) {
  35. int newcnt = blkid+1;
  36. buf = realloc(buf, newcnt*1024);
  37. bzero(buf+(blkcnt*1024), (newcnt-blkcnt)*1024);
  38. blkcnt = newcnt;
  39. }
  40. char fullpath[0x200];
  41. strcpy(fullpath, argv[1]);
  42. strcat(fullpath, "/");
  43. strcat(fullpath, ep->d_name);
  44. FILE *fp = fopen(fullpath, "r");
  45. char *line = NULL;
  46. size_t n = 0;
  47. for (int i=0; i<16; i++) {
  48. ssize_t cnt = getline(&line, &n, fp);
  49. if (cnt < 0) break;
  50. if (cnt > 65) {
  51. fprintf(stderr, "Line %d too long in blk %s\n", i+1, ep->d_name);
  52. }
  53. strncpy(buf+(blkid*1024)+(i*64), line, cnt-1);
  54. }
  55. free(line);
  56. }
  57. fwrite(buf, 1024, blkcnt, stdout);
  58. free(buf);
  59. closedir(dp);
  60. return 0;
  61. }