File: readlink.c

package info (click to toggle)
klibc 2.0.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,248 kB
  • sloc: ansic: 47,887; asm: 2,394; perl: 758; makefile: 193; sh: 183
file content (36 lines) | stat: -rw-r--r-- 529 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

char *progname;

static __noreturn usage(void)
{
	fprintf(stderr, "Usage: %s link\n", progname);
	exit(1);
}

int main(int argc, char *argv[])
{
	char *name, *link_name = NULL;
	size_t max_siz = 128;

	progname = *argv++;

	name = *argv++;
	if (!name)
		usage();

	link_name = malloc(max_siz);
	if (!link_name) {
		perror("malloc");
		exit(1);
	}

	if (readlink(name, link_name, max_siz) == -1)
		exit(1);
	printf("%s\n", link_name);

	exit(0);
}