[BACK]Return to readlink.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / readlink

Annotation of src/usr.bin/readlink/readlink.c, Revision 1.26

1.3       kstailey    1: /*
1.26    ! deraadt     2:  * $OpenBSD: readlink.c,v 1.25 2009/05/01 10:36:48 chl 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.25      chl        30: #include <err.h>
                     31: #include <errno.h>
1.9       kstailey   32: #include <limits.h>
1.1       kstailey   33: #include <stdio.h>
1.12      niklas     34: #include <stdlib.h>
1.10      niklas     35: #include <string.h>
1.1       kstailey   36: #include <unistd.h>
                     37:
1.24      sobrado    38: static void    usage(void);
                     39:
1.1       kstailey   40: int
1.19      deraadt    41: main(int argc, char *argv[])
1.1       kstailey   42: {
                     43:        char buf[PATH_MAX];
1.22      deraadt    44:        int n, ch, nflag = 0, fflag = 0;
                     45:        extern int optind;
1.26    ! deraadt    46:
        !            47:        if (tame("stdio rpath", NULL) == -1)
        !            48:                err(1, "tame");
1.1       kstailey   49:
1.10      niklas     50:        while ((ch = getopt(argc, argv, "fn")) != -1)
1.8       kstailey   51:                switch (ch) {
1.10      niklas     52:                case 'f':
                     53:                        fflag = 1;
                     54:                        break;
1.8       kstailey   55:                case 'n':
                     56:                        nflag = 1;
                     57:                        break;
                     58:                default:
1.24      sobrado    59:                        usage();
1.8       kstailey   60:                }
                     61:        argc -= optind;
                     62:        argv += optind;
                     63:
1.24      sobrado    64:        if (argc != 1)
                     65:                usage();
1.22      deraadt    66:
                     67:        n = strlen(argv[0]);
                     68:        if (n > PATH_MAX - 1) {
                     69:                fprintf(stderr,
1.24      sobrado    70:                    "readlink: filename longer than PATH_MAX-1 (%d)\n",
                     71:                    PATH_MAX - 1);
1.22      deraadt    72:                exit(1);
                     73:        }
1.10      niklas     74:
1.23      otto       75:        if (fflag) {
                     76:                if (realpath(argv[0], buf) == NULL)
                     77:                        err(1, "%s", argv[0]);
                     78:        } else {
1.18      kstailey   79:                if ((n = readlink(argv[0], buf, sizeof buf-1)) < 0)
1.22      deraadt    80:                        exit(1);
1.18      kstailey   81:                buf[n] = '\0';
1.17      kstailey   82:        }
1.4       grr        83:
                     84:        printf("%s", buf);
1.8       kstailey   85:        if (!nflag)
                     86:                putchar('\n');
1.1       kstailey   87:        exit(0);
1.24      sobrado    88: }
                     89:
                     90: static void
                     91: usage(void)
                     92: {
                     93:        (void)fprintf(stderr, "usage: readlink [-fn] file\n");
                     94:        exit(1);
1.1       kstailey   95: }