Mirror of CollapseOS
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8KB

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