Mirror of CollapseOS
25개 이상의 토픽을 선택하실 수 없습니다. 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 <string.h>
  5. #include <sys/stat.h>
  6. void usage()
  7. {
  8. fprintf(stderr, "Usage: blkunpack dirname\n");
  9. }
  10. int main(int argc, char *argv[])
  11. {
  12. char buf[1024];
  13. int blkid = 0;
  14. if (argc != 2) {
  15. usage();
  16. return 1;
  17. }
  18. while (fread(buf, 1024, 1, stdin) == 1) {
  19. char fullpath[0x200];
  20. sprintf(fullpath, "%s/%03d", argv[1], blkid);
  21. int linecnt = 0 ;
  22. for (int i=1023; i>=0; i--) {
  23. if (buf[i]) {
  24. linecnt = (i / 64) + 1;
  25. break;
  26. }
  27. }
  28. if (linecnt) {
  29. // not an empty block
  30. FILE *fp = fopen(fullpath, "w");
  31. for (int i=0; i<linecnt; i++) {
  32. char *line = &buf[i*64];
  33. // line length is *not* strlen()! it's the
  34. // position of the first non-null, starting
  35. // from the right. Then, we normalize nulls
  36. // to space.
  37. int j;
  38. for (j=63; j>=0; j--) {
  39. if (line[j] != '\0') {
  40. break;
  41. }
  42. }
  43. int len = j+1;
  44. if (len) {
  45. for (; j>=0; j--) {
  46. if (line[j] == '\0') {
  47. line[j] = ' ';
  48. }
  49. }
  50. fwrite(line, len, 1, fp);
  51. }
  52. fputc('\n', fp);
  53. }
  54. fclose(fp);
  55. } else {
  56. // empty block, delete
  57. unlink(fullpath);
  58. }
  59. blkid++;
  60. }
  61. return 0;
  62. }