ln.c 615 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. static int usage()
  6. {
  7. fprintf(stderr,"ln [-s] <target> <name>\n");
  8. return -1;
  9. }
  10. int main(int argc, char *argv[])
  11. {
  12. int symbolic = 0;
  13. int ret;
  14. if(argc < 2) return usage();
  15. if(!strcmp(argv[1],"-s")) {
  16. symbolic = 1;
  17. argc--;
  18. argv++;
  19. }
  20. if(argc < 3) return usage();
  21. if(symbolic) {
  22. ret = symlink(argv[1], argv[2]);
  23. } else {
  24. ret = link(argv[1], argv[2]);
  25. }
  26. if(ret < 0)
  27. fprintf(stderr, "link failed %s\n", strerror(errno));
  28. return ret;
  29. }