ln.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2015 Mike Gerow
  5. * 2018 K. Lange
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. static const char usage[] =
  12. "Usage: %s [-s] TARGET NAME\n"
  13. " -s: Create a symbolic link.\n"
  14. " -h: Print this help message and exit.\n";
  15. int main(int argc, char * argv[]) {
  16. int symlink_flag = 0;
  17. int c;
  18. while ((c = getopt(argc, argv, "sh")) != -1) {
  19. switch (c) {
  20. case 's':
  21. symlink_flag = 1;
  22. break;
  23. case 'h':
  24. fprintf(stdout, usage, argv[0]);
  25. exit(EXIT_SUCCESS);
  26. default:
  27. fprintf(stderr, usage, argv[0]);
  28. exit(EXIT_FAILURE);
  29. }
  30. }
  31. if (argc - optind < 2) {
  32. fprintf(stderr, usage, argv[0]);
  33. exit(EXIT_FAILURE);
  34. }
  35. char * target = argv[optind];
  36. char * name = argv[optind + 1];
  37. if (symlink_flag) {
  38. if(symlink(target, name) < 0) {
  39. fprintf(stderr, "%s: %s: %s\n", argv[0], name, strerror(errno));
  40. exit(EXIT_FAILURE);
  41. }
  42. exit(EXIT_SUCCESS);
  43. }
  44. #ifdef link
  45. if (link(target, name) < 0) {
  46. fprintf(stderr, "%s: %s: %s\n", argv[0], name, strerror(errno));
  47. exit(EXIT_FAILURE);
  48. }
  49. #else
  50. fprintf(stderr, "%s: %s: hard link not supported\n", argv[0], name);
  51. #endif
  52. exit(EXIT_SUCCESS);
  53. }