Annotation of src/usr.bin/readlink/readlink.c, Revision 1.21
1.3 kstailey 1: /*
1.21 ! ray 2: * $OpenBSD: readlink.c,v 1.20 2006/05/08 22:33:04 ray Exp $
1.3 kstailey 3: *
4: * Copyright (c) 1997
5: * Kenneth Stailey (hereinafter referred to as the author)
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
1.20 ray 30: #include <err.h>
1.9 kstailey 31: #include <limits.h>
1.1 kstailey 32: #include <stdio.h>
1.12 niklas 33: #include <stdlib.h>
1.10 niklas 34: #include <string.h>
1.1 kstailey 35: #include <unistd.h>
36:
1.20 ray 37: __dead void usage(void);
38:
1.1 kstailey 39: int
1.19 deraadt 40: main(int argc, char *argv[])
1.1 kstailey 41: {
1.20 ray 42: int ch, n, fflag = 0, nflag = 0;
1.1 kstailey 43: char buf[PATH_MAX];
44:
1.10 niklas 45: while ((ch = getopt(argc, argv, "fn")) != -1)
1.8 kstailey 46: switch (ch) {
1.10 niklas 47: case 'f':
48: fflag = 1;
49: break;
1.8 kstailey 50: case 'n':
51: nflag = 1;
52: break;
53: default:
1.20 ray 54: usage();
1.8 kstailey 55: }
56: argc -= optind;
57: argv += optind;
58:
1.20 ray 59: if (argc != 1)
60: usage();
1.10 niklas 61:
1.20 ray 62: if (strlen(argv[0]) > PATH_MAX - 1)
63: errx(1, "filename longer than PATH_MAX-1 (%d)",
64: PATH_MAX - 1);
65:
66: if (fflag) {
67: if (realpath(argv[0], buf) == NULL)
68: err(1, "%s", argv[0]);
69: } else {
1.18 kstailey 70: if ((n = readlink(argv[0], buf, sizeof buf-1)) < 0)
1.20 ray 71: err(1, "%s", argv[0]);
1.18 kstailey 72: buf[n] = '\0';
1.17 kstailey 73: }
1.4 grr 74:
75: printf("%s", buf);
1.8 kstailey 76: if (!nflag)
77: putchar('\n');
1.1 kstailey 78: exit(0);
1.20 ray 79: }
80:
81: void
82: usage(void)
83: {
84: extern char *__progname;
85:
1.21 ! ray 86: fprintf(stderr, "usage: %s [-fn] symlink\n", __progname);
1.20 ray 87: exit(1);
1.1 kstailey 88: }