logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

checksum-transcriber.c (2324B)


  1. /*
  2. * SPDX-FileCopyrightText: 2022 fosslinux <fosslinux@aussies.space>
  3. *
  4. * SPDX-License-Identifier: GPL-3.0-or-later
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <bootstrappable.h>
  10. #include <unistd.h>
  11. #define MAX_STRING 4096
  12. #define MAX_TOKENS 3
  13. char *get_distfiles(char **envp) {
  14. char *envvar = "DISTFILES=";
  15. int i = 0;
  16. while (envp[i] != NULL && strncmp(envp[i], envvar, strlen(envvar)) != 0) i += 1;
  17. // Now we have distfiles= - get just the part we want.
  18. require(envp[i] != NULL, "Unable to find distfiles environment variable");
  19. return envp[i] + strlen(envvar);
  20. }
  21. int main(int argc, char **argv, char **envp) {
  22. // Random file things
  23. require(argc == 2, "Usage: checksum-transcriber FILENAME");
  24. char *input = argv[1];
  25. FILE *in = fopen(input, "r");
  26. require(in != NULL, "File does not exist");
  27. char *output = calloc(MAX_STRING, sizeof(char));
  28. require(strcpy(output, input) != NULL, "Failed copying string");
  29. require(strcat(output, ".SHA256SUM") != NULL, "Failed concating string");
  30. FILE *out = fopen(output, "w+");
  31. require(out != NULL, "Failed opening output file");
  32. char *orig_line;
  33. char *line = calloc(MAX_STRING, sizeof(char));
  34. require(line != NULL, "Failed allocating string");
  35. char **tokens;
  36. char *new_line;
  37. char *checksum;
  38. char *filename;
  39. int i;
  40. fgets(line, MAX_STRING, in);
  41. while (strlen(line) != 0) {
  42. // Split each line into tokens
  43. orig_line = line;
  44. tokens = calloc(MAX_TOKENS, sizeof(char*));
  45. i = 0;
  46. while (i < MAX_TOKENS) {
  47. tokens[i] = line;
  48. new_line = strchr(line, ' ');
  49. // Occurs when there are only two tokens
  50. if (new_line == NULL) break;
  51. line = new_line;
  52. line[0] = '\0';
  53. line += 1;
  54. i += 1;
  55. }
  56. line = strchr(line, '\n');
  57. line[0] = '\0';
  58. // Get checksum and filename
  59. checksum = tokens[1];
  60. if (tokens[2] != NULL) {
  61. filename = tokens[2];
  62. } else {
  63. filename = strrchr(tokens[0], '/');
  64. filename += 1;
  65. }
  66. // Put it all together
  67. fputs(checksum, out);
  68. fputs(" ", out);
  69. fputs(get_distfiles(envp), out);
  70. fputc('/', out);
  71. fputs(filename, out);
  72. fputc('\n', out);
  73. // Cleanup
  74. i = 0;
  75. free(orig_line);
  76. free(tokens);
  77. line = calloc(MAX_STRING, sizeof(char));
  78. require(line != NULL, "Failed allocating string");
  79. fgets(line, MAX_STRING, in);
  80. }
  81. // Clean up
  82. fclose(in);
  83. fclose(out);
  84. }