/*
 * readlink: print a symbolic link
 *
 * PUBLIC DOMAIN
 */
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>


char *__progname;

main(int argc, char **argv)
{
    char *bfr;
    int bfrsize=1024;
    int st;

    __progname = basename(argv[0]);

    if (argc != 2) {
	fprintf(stderr, "usage: %s file\n", __progname);
	exit(1);
    }

    while (1) {
	if ((bfr = malloc(bfrsize)) == 0 || (st = readlink(argv[1], bfr, bfrsize)) < 0) {
	    if (errno == EINVAL) {
		write(1, argv[1], strlen(argv[1]));
		write(1, "\n", 1);
		exit(0);
	    }
	    perror(__progname);
	    exit(1);
	}
	if (st < bfrsize) {
	    write(1, bfr, bfrsize);
	    write(1, "\n", 1);
	    exit(0);
	}
	free(bfr);
	bfrsize *= 2;
    }
}
