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.

69 lines
1.7KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. /* Unpacks blkfs into its source form.
  5. *
  6. * If numerical "upto" is specified, we stop unpacking when reaching this
  7. * blkno.
  8. */
  9. void usage()
  10. {
  11. fprintf(stderr, "Usage: blkunpack [upto] < blkfs > blk.fs\n");
  12. }
  13. int main(int argc, char *argv[])
  14. {
  15. char buf[1024];
  16. int blkid = 0;
  17. int upto = 0;
  18. if (argc > 2) {
  19. usage();
  20. return 1;
  21. }
  22. if (argc == 2) {
  23. upto = strtol(argv[1], NULL, 10);
  24. }
  25. while (fread(buf, 1024, 1, stdin) == 1) {
  26. int linecnt = 0 ;
  27. for (int i=1023; i>=0; i--) {
  28. if (buf[i]) {
  29. linecnt = (i / 64) + 1;
  30. break;
  31. }
  32. }
  33. if (linecnt) {
  34. // not an empty block
  35. printf("( ----- %03d )\n", blkid);
  36. for (int i=0; i<linecnt; i++) {
  37. char *line = &buf[i*64];
  38. // line length is *not* strlen()! it's the
  39. // position of the first non-null, starting
  40. // from the right. Then, we normalize nulls
  41. // to space.
  42. int j;
  43. for (j=63; j>=0; j--) {
  44. if (line[j] != '\0') {
  45. break;
  46. }
  47. }
  48. int len = j+1;
  49. if (len) {
  50. for (; j>=0; j--) {
  51. if (line[j] == '\0') {
  52. line[j] = ' ';
  53. }
  54. }
  55. fwrite(line, len, 1, stdout);
  56. }
  57. fputc('\n', stdout);
  58. }
  59. }
  60. blkid++;
  61. if (blkid == upto) {
  62. break;
  63. }
  64. }
  65. return 0;
  66. }