1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1982-2007 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
23 static const char usage
[] =
24 "[-?\n@(#)$Id: readlink (AT&T Research) 2008-06-08 $\n]"
26 "[+NAME?readlink - read the contents of a symbolic link]"
27 "[+DESCRIPTION?\breadlink\b returns the contents of the symbolic "
28 "link referred to by the path argument. Unless the \b-f\b "
29 "option is specified an error will be returned when the path "
30 "is not a symbolic link.]"
31 "[f:canonicalize?The returned value will be an absolute pathname that names "
32 "the same file, whose resolution does not involve \".\", \"..\", or "
33 "symbolic links, otherwise only the exact (relative) value will be returned.]"
34 "[n:no-newline?Supress newline at the end.]"
35 "[v:verbose?Verbose - print errors.]"
36 "[+SEE ALSO?\bbasename\b(1),\bdirname\b(2),\breadlink\b(2),\breadpath\n(2)]"
42 b_readlink(int argc
, char** argv
, void* context
)
56 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, 0);
59 switch (optget(argv
, usage
))
62 canonicalize
= opt_info
.num
;
65 nonewline
= opt_info
.num
;
68 verbose
= opt_info
.num
;
71 error(ERROR_usage(2), "%s", opt_info
.arg
);
74 error(2, "%s", opt_info
.arg
);
79 argv
+= opt_info
.index
;
80 argc
-= opt_info
.index
;
81 if(error_info
.errors
|| argc
!= 1)
82 error(ERROR_usage(2),"%s", optusage(NiL
));
87 len
= resolvepath(filename
, buf
, sizeof(buf
)-2);
91 len
= readlink(filename
, buf
, sizeof(buf
)-2);
100 error(ERROR_system(1),"%s: readlink failed", filename
);
106 resolvedname
[len
++] = '\n';
108 sfwrite(sfstdout
, resolvedname
, len
);