morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

readlink.c (853B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <unistd.h>
      3 #include <errno.h>
      4 #include <limits.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 #include "util.h"
     10 
     11 static void
     12 usage(void)
     13 {
     14 	eprintf("usage: %s [-efmn] file\n", argv0);
     15 }
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20 	char buf[PATH_MAX];
     21 	int nflag = 0;
     22 	int fflag = 0;
     23 	ssize_t n;
     24 
     25 	ARGBEGIN {
     26 	case 'e':
     27 	case 'm':
     28 		eprintf("not implemented\n");
     29 	case 'f':
     30 		fflag = 1;
     31 		break;
     32 	case 'n':
     33 		nflag = 1;
     34 		break;
     35 	default:
     36 		usage();
     37 	} ARGEND;
     38 
     39 	if (argc != 1)
     40 		usage();
     41 
     42 	if (strlen(argv[0]) > PATH_MAX - 1)
     43 		eprintf("path too long\n");
     44 
     45 	if (fflag) {
     46 		if (!realpath(argv[0], buf))
     47 			exit(1);
     48 	} else {
     49 		if ((n = readlink(argv[0], buf, sizeof(buf) - 1)) < 0)
     50 			exit(1);
     51 		buf[n] = '\0';
     52 	}
     53 
     54 	printf("%s", buf);
     55 	if (!nflag)
     56 		putchar('\n');
     57 
     58 	return 0;
     59 }