[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.18

1.3       kstailey    1: /*
1.18    ! kstailey    2:  * $OpenBSD: readlink.c,v 1.17 1998/08/24 14:43:16 kstailey 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.9       kstailey   30: #include <limits.h>
1.10      niklas     31: #include <errno.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:
                     37: int
                     38: main(argc, argv)
1.10      niklas     39:        int argc;
                     40:        char **argv;
1.1       kstailey   41: {
                     42:        char buf[PATH_MAX];
1.10      niklas     43:        int n, ch, nflag = 0, fflag = 0;
1.8       kstailey   44:        extern int optind;
1.1       kstailey   45:
1.10      niklas     46:        while ((ch = getopt(argc, argv, "fn")) != -1)
1.8       kstailey   47:                switch (ch) {
1.10      niklas     48:                case 'f':
                     49:                        fflag = 1;
                     50:                        break;
1.8       kstailey   51:                case 'n':
                     52:                        nflag = 1;
                     53:                        break;
                     54:                default:
                     55:                        (void)fprintf(stderr,
1.10      niklas     56:                            "usage: readlink [-n] [-f] symlink\n");
1.8       kstailey   57:                        exit(1);
                     58:                }
                     59:        argc -= optind;
                     60:        argv += optind;
                     61:
                     62:        if (argc != 1) {
1.10      niklas     63:                fprintf(stderr, "usage: readlink [-n] [-f] symlink\n");
1.5       deraadt    64:                exit(1);
                     65:        }
1.1       kstailey   66:
1.10      niklas     67:        n = strlen(argv[0]);
1.15      mickey     68:        if (n > PATH_MAX - 1) {
                     69:                fprintf(stderr,
                     70:                        "readlink: filename longer than PATH_MAX-1 (%d)\n",
                     71:                        PATH_MAX - 1);
                     72:                exit(1);
                     73:        }
1.10      niklas     74:
1.18    ! kstailey   75:        if (fflag)
1.12      niklas     76:                realpath(argv[0], buf);
1.18    ! kstailey   77:        else {
        !            78:                if ((n = readlink(argv[0], buf, sizeof buf-1)) < 0)
        !            79:                        exit(1);
        !            80:                buf[n] = '\0';
1.17      kstailey   81:        }
1.4       grr        82:
                     83:        printf("%s", buf);
1.8       kstailey   84:        if (!nflag)
                     85:                putchar('\n');
1.1       kstailey   86:        exit(0);
                     87: }