k9core

Coreutils for *nix operating systems [fork]
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

ln.c (846B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{

	if(argc == 1) {
		printf("usage: ln [-s] destination link\n");
		return 1;
	}
	int opts, fd, fflag;
	while((opts = getopt(argc, argv, "sf")) != -1) {
		if(opts == 'f')
			fflag = 1;
		switch(opts) {
			case 's':
				if(fflag == 1 && (access(argv[3], F_OK) != 1)) {
					if(remove(argv[3]) == -1)
						rmdir(argv[3]);
				}
				int symstat = symlink(argv[2], argv[3]);
				if(symstat == -1) {
					fprintf(stderr, "ln: %s: %s", argv[3], strerror(errno));
					return 1;
				}
				break;
			case '?':
				printf("-%c: Argument not found", optopt);
				break;
			default:
				fd = link(argv[1], argv[2]);
				if(fd == -1) {
					fprintf(stderr, "ln: %s: %s\n", argv[2], strerror(errno));
					return 1;
				}
				return 0;
		}
	}
}