19f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright/*
29f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * Copyright (c) 2013, The Android Open Source Project
39f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * All rights reserved.
49f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *
59f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * Redistribution and use in source and binary forms, with or without
69f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * modification, are permitted provided that the following conditions
79f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * are met:
89f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *  * Redistributions of source code must retain the above copyright
99f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    notice, this list of conditions and the following disclaimer.
109f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *  * Redistributions in binary form must reproduce the above copyright
119f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    notice, this list of conditions and the following disclaimer in
129f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    the documentation and/or other materials provided with the
139f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    distribution.
149f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *  * Neither the name of Google, Inc. nor the names of its contributors
159f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    may be used to endorse or promote products derived from this
169f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *    software without specific prior written permission.
179f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright *
189f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
219f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
229f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
239f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
249f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
259f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
269f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
279f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
289f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright * SUCH DAMAGE.
309f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright */
319f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
329f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright#include <limits.h>
339f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright#include <stdio.h>
349f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright#include <stdlib.h>
359f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright#include <unistd.h>
369f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
371e1d29133f12031bd432b6818143c309f18de417Michael Wrightstatic int skip_newline, quiet_errors, canonicalize;
381e1d29133f12031bd432b6818143c309f18de417Michael Wright
399f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wrightstatic void usage(char* name) {
401e1d29133f12031bd432b6818143c309f18de417Michael Wright    fprintf(stderr, "Usage: %s [OPTION]... FILE\n", name);
419f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright}
429f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
439f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wrightint readlink_main(int argc, char* argv[]) {
441e1d29133f12031bd432b6818143c309f18de417Michael Wright    int c;
451e1d29133f12031bd432b6818143c309f18de417Michael Wright    while ((c = getopt(argc, argv, "nfqs")) != -1) {
461e1d29133f12031bd432b6818143c309f18de417Michael Wright        switch (c) {
471e1d29133f12031bd432b6818143c309f18de417Michael Wright        case 'n':
481e1d29133f12031bd432b6818143c309f18de417Michael Wright            skip_newline = 1;
491e1d29133f12031bd432b6818143c309f18de417Michael Wright            break;
501e1d29133f12031bd432b6818143c309f18de417Michael Wright        case 'f':
511e1d29133f12031bd432b6818143c309f18de417Michael Wright            canonicalize = 1;
521e1d29133f12031bd432b6818143c309f18de417Michael Wright            break;
531e1d29133f12031bd432b6818143c309f18de417Michael Wright        case 'q':
541e1d29133f12031bd432b6818143c309f18de417Michael Wright        case 's':
551e1d29133f12031bd432b6818143c309f18de417Michael Wright            quiet_errors = 1;
561e1d29133f12031bd432b6818143c309f18de417Michael Wright            break;
571e1d29133f12031bd432b6818143c309f18de417Michael Wright        case '?':
581e1d29133f12031bd432b6818143c309f18de417Michael Wright        default:
591e1d29133f12031bd432b6818143c309f18de417Michael Wright            usage(argv[0]);
601e1d29133f12031bd432b6818143c309f18de417Michael Wright            return EXIT_FAILURE;
611e1d29133f12031bd432b6818143c309f18de417Michael Wright        }
621e1d29133f12031bd432b6818143c309f18de417Michael Wright    }
631e1d29133f12031bd432b6818143c309f18de417Michael Wright    int index = optind;
641e1d29133f12031bd432b6818143c309f18de417Michael Wright    if (argc - index != 1) {
659f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright        usage(argv[0]);
669f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright        return EXIT_FAILURE;
679f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright    }
689f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
699f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright    char name[PATH_MAX+1];
701e1d29133f12031bd432b6818143c309f18de417Michael Wright    if (canonicalize) {
711e1d29133f12031bd432b6818143c309f18de417Michael Wright        if(!realpath(argv[optind], name)) {
721e1d29133f12031bd432b6818143c309f18de417Michael Wright            if (!quiet_errors) {
731e1d29133f12031bd432b6818143c309f18de417Michael Wright                perror("readlink");
741e1d29133f12031bd432b6818143c309f18de417Michael Wright            }
751e1d29133f12031bd432b6818143c309f18de417Michael Wright            return EXIT_FAILURE;
761e1d29133f12031bd432b6818143c309f18de417Michael Wright        }
771e1d29133f12031bd432b6818143c309f18de417Michael Wright    } else {
781e1d29133f12031bd432b6818143c309f18de417Michael Wright        ssize_t len = readlink(argv[1], name, PATH_MAX);
799f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
801e1d29133f12031bd432b6818143c309f18de417Michael Wright        if (len < 0) {
811e1d29133f12031bd432b6818143c309f18de417Michael Wright            if (!quiet_errors) {
821e1d29133f12031bd432b6818143c309f18de417Michael Wright                perror("readlink");
831e1d29133f12031bd432b6818143c309f18de417Michael Wright            }
841e1d29133f12031bd432b6818143c309f18de417Michael Wright            return EXIT_FAILURE;
851e1d29133f12031bd432b6818143c309f18de417Michael Wright        }
861e1d29133f12031bd432b6818143c309f18de417Michael Wright        name[len] = '\0';
879f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright    }
889f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
891e1d29133f12031bd432b6818143c309f18de417Michael Wright    fputs(name, stdout);
901e1d29133f12031bd432b6818143c309f18de417Michael Wright    if (!skip_newline) {
911e1d29133f12031bd432b6818143c309f18de417Michael Wright        fputs("\n", stdout);
921e1d29133f12031bd432b6818143c309f18de417Michael Wright    }
939f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright
949f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright    return EXIT_SUCCESS;
959f50abdee00a9571393ce4a589080b4d6129aaf3Michael Wright}
96