Mirror of CollapseOS
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

62 Zeilen
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. char c = 0;
  22. for (int i=0; i<1024; i++) {
  23. c |= buf[i];
  24. }
  25. if (c) {
  26. // not an empty block
  27. FILE *fp = fopen(fullpath, "w");
  28. for (int i=0; i<16; i++) {
  29. char *line = &buf[i*64];
  30. // line length is *not* strlen()! it's the
  31. // position of the first non-null, starting
  32. // from the right. Then, we normalize nulls
  33. // to space.
  34. int j;
  35. for (j=63; j>=0; j--) {
  36. if (line[j] != '\0') {
  37. break;
  38. }
  39. }
  40. int len = j+1;
  41. if (len) {
  42. for (; j>=0; j--) {
  43. if (line[j] == '\0') {
  44. line[j] = ' ';
  45. }
  46. }
  47. fwrite(line, len, 1, fp);
  48. }
  49. fputc('\n', fp);
  50. }
  51. fclose(fp);
  52. } else {
  53. // empty block, delete
  54. unlink(fullpath);
  55. }
  56. blkid++;
  57. }
  58. return 0;
  59. }