logo

bootstrap-initrd

Linux initrd generator to bootstrap a POSIX-ish system from a reasonably small binary seed git clone https://hacktivis.me/git/make-initrd.git

ln-stub.c (865B)


  1. #define _POSIX_C_SOURCE 200809L
  2. #include <stdio.h> // fprintf
  3. #include <unistd.h> // getopt, symlink, link
  4. #include <string.h> // strerror
  5. #include <errno.h>
  6. #include <stdbool.h>
  7. int
  8. main(int argc, char *argv[])
  9. {
  10. bool opt_s = false;
  11. int c = -1;
  12. while((c = getopt(argc, argv, ":s")) != -1)
  13. {
  14. switch(c)
  15. {
  16. case 's':
  17. opt_s = true;
  18. break;
  19. case '?':
  20. fprintf(stderr, "ln: Unknown option '-%c'\n", optopt);
  21. break;
  22. }
  23. }
  24. argc -= optind;
  25. argv += optind;
  26. if(argc != 2)
  27. {
  28. fprintf(stderr, "Usage: ln [-s] src dest\n");
  29. return 1;
  30. }
  31. if(opt_s)
  32. {
  33. if(symlink(argv[0], argv[1]) < 0)
  34. {
  35. fprintf(stderr, "ln: Failed creating symlink: %s\n", strerror(errno));
  36. return 1;
  37. }
  38. }
  39. else
  40. {
  41. if(link(argv[0], argv[1]) < 0)
  42. {
  43. fprintf(stderr, "ln: Failed creating (hard) link: %s\n", strerror(errno));
  44. return 1;
  45. }
  46. }
  47. return 0;
  48. }