1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1982-2007 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: readlink (AT&T Research) 2008-06-08 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?readlink - read the contents of a symbolic link]"
1N/A"[+DESCRIPTION?\breadlink\b returns the contents of the symbolic "
1N/A "link referred to by the path argument. Unless the \b-f\b "
1N/A "option is specified an error will be returned when the path "
1N/A "is not a symbolic link.]"
1N/A"[f:canonicalize?The returned value will be an absolute pathname that names "
1N/A "the same file, whose resolution does not involve \".\", \"..\", or "
1N/A "symbolic links, otherwise only the exact (relative) value will be returned.]"
1N/A"[n:no-newline?Supress newline at the end.]"
1N/A"[v:verbose?Verbose - print errors.]"
1N/A"[+SEE ALSO?\bbasename\b(1),\bdirname\b(2),\breadlink\b(2),\breadpath\n(2)]"
1N/A;
1N/A
1N/A#include <cmd.h>
1N/A
1N/Aint
1N/Ab_readlink(int argc, char** argv, void* context)
1N/A{
1N/A register char* s;
1N/A register int i;
1N/A register char* m;
1N/A register char* x;
1N/A int canonicalize = 0,
1N/A nonewline = 0,
1N/A verbose = 0;
1N/A char buf[PATH_MAX+2];
1N/A int len = 0;
1N/A char *filename,
1N/A *resolvedname = NULL;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'f':
1N/A canonicalize = opt_info.num;
1N/A continue;
1N/A case 'n':
1N/A nonewline = opt_info.num;
1N/A continue;
1N/A case 'v':
1N/A verbose = opt_info.num;
1N/A continue;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s", opt_info.arg);
1N/A continue;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A continue;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A argc -= opt_info.index;
1N/A if(error_info.errors || argc != 1)
1N/A error(ERROR_usage(2),"%s", optusage(NiL));
1N/A filename = argv[0];
1N/A
1N/A if (canonicalize)
1N/A {
1N/A len = resolvepath(filename, buf, sizeof(buf)-2);
1N/A }
1N/A else
1N/A {
1N/A len = readlink(filename, buf, sizeof(buf)-2);
1N/A }
1N/A
1N/A if (len != -1)
1N/A resolvedname = buf;
1N/A
1N/A if (!resolvedname)
1N/A {
1N/A if (verbose)
1N/A error(ERROR_system(1),"%s: readlink failed", filename);
1N/A else
1N/A return 1;
1N/A }
1N/A
1N/A if (!nonewline)
1N/A resolvedname[len++] = '\n';
1N/A
1N/A sfwrite(sfstdout, resolvedname, len);
1N/A
1N/A return 0;
1N/A}