| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/readlink.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /* readlink -- display value of a symbolic link. | - | ||||||
| 2 | Copyright (C) 2002-2018 Free Software Foundation, Inc. | - | ||||||
| 3 | - | |||||||
| 4 | This program is free software: you can redistribute it and/or modify | - | ||||||
| 5 | it under the terms of the GNU General Public License as published by | - | ||||||
| 6 | the Free Software Foundation, either version 3 of the License, or | - | ||||||
| 7 | (at your option) any later version. | - | ||||||
| 8 | - | |||||||
| 9 | This program is distributed in the hope that it will be useful, | - | ||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||
| 12 | GNU General Public License for more details. | - | ||||||
| 13 | - | |||||||
| 14 | You should have received a copy of the GNU General Public License | - | ||||||
| 15 | along with this program. If not, see <https://www.gnu.org/licenses/>. */ | - | ||||||
| 16 | - | |||||||
| 17 | /* Written by Dmitry V. Levin */ | - | ||||||
| 18 | - | |||||||
| 19 | #include <config.h> | - | ||||||
| 20 | #include <stdio.h> | - | ||||||
| 21 | #include <getopt.h> | - | ||||||
| 22 | #include <sys/types.h> | - | ||||||
| 23 | - | |||||||
| 24 | #include "system.h" | - | ||||||
| 25 | #include "canonicalize.h" | - | ||||||
| 26 | #include "error.h" | - | ||||||
| 27 | #include "areadlink.h" | - | ||||||
| 28 | - | |||||||
| 29 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||
| 30 | #define PROGRAM_NAME "readlink" | - | ||||||
| 31 | - | |||||||
| 32 | #define AUTHORS proper_name ("Dmitry V. Levin") | - | ||||||
| 33 | - | |||||||
| 34 | /* If true, do not output the trailing newline. */ | - | ||||||
| 35 | static bool no_newline; | - | ||||||
| 36 | - | |||||||
| 37 | /* If true, report error messages. */ | - | ||||||
| 38 | static bool verbose; | - | ||||||
| 39 | - | |||||||
| 40 | static struct option const longopts[] = | - | ||||||
| 41 | { | - | ||||||
| 42 | {"canonicalize", no_argument, NULL, 'f'}, | - | ||||||
| 43 | {"canonicalize-existing", no_argument, NULL, 'e'}, | - | ||||||
| 44 | {"canonicalize-missing", no_argument, NULL, 'm'}, | - | ||||||
| 45 | {"no-newline", no_argument, NULL, 'n'}, | - | ||||||
| 46 | {"quiet", no_argument, NULL, 'q'}, | - | ||||||
| 47 | {"silent", no_argument, NULL, 's'}, | - | ||||||
| 48 | {"verbose", no_argument, NULL, 'v'}, | - | ||||||
| 49 | {"zero", no_argument, NULL, 'z'}, | - | ||||||
| 50 | {GETOPT_HELP_OPTION_DECL}, | - | ||||||
| 51 | {GETOPT_VERSION_OPTION_DECL}, | - | ||||||
| 52 | {NULL, 0, NULL, 0} | - | ||||||
| 53 | }; | - | ||||||
| 54 | - | |||||||
| 55 | void | - | ||||||
| 56 | usage (int status) | - | ||||||
| 57 | { | - | ||||||
| 58 | if (status != EXIT_SUCCESS)
| 3-19 | ||||||
| 59 | emit_try_help (); executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||
| 60 | else | - | ||||||
| 61 | { | - | ||||||
| 62 | printf (_("Usage: %s [OPTION]... FILE...\n"), program_name); | - | ||||||
| 63 | fputs (_("Print value of a symbolic link or canonical file name\n\n"), | - | ||||||
| 64 | stdout); | - | ||||||
| 65 | fputs (_("\ | - | ||||||
| 66 | -f, --canonicalize canonicalize by following every symlink in\n\ | - | ||||||
| 67 | every component of the given name recursively;\ | - | ||||||
| 68 | \n\ | - | ||||||
| 69 | all but the last component must exist\n\ | - | ||||||
| 70 | -e, --canonicalize-existing canonicalize by following every symlink in\n\ | - | ||||||
| 71 | every component of the given name recursively,\ | - | ||||||
| 72 | \n\ | - | ||||||
| 73 | all components must exist\n\ | - | ||||||
| 74 | "), stdout); | - | ||||||
| 75 | fputs (_("\ | - | ||||||
| 76 | -m, --canonicalize-missing canonicalize by following every symlink in\n\ | - | ||||||
| 77 | every component of the given name recursively,\ | - | ||||||
| 78 | \n\ | - | ||||||
| 79 | without requirements on components existence\n\ | - | ||||||
| 80 | -n, --no-newline do not output the trailing delimiter\n\ | - | ||||||
| 81 | -q, --quiet\n\ | - | ||||||
| 82 | -s, --silent suppress most error messages (on by default)\n\ | - | ||||||
| 83 | -v, --verbose report error messages\n\ | - | ||||||
| 84 | -z, --zero end each output line with NUL, not newline\n\ | - | ||||||
| 85 | "), stdout); | - | ||||||
| 86 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||
| 87 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||
| 88 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||
| 89 | } executed 19 times by 1 test: end of blockExecuted by:
| 19 | ||||||
| 90 | exit (status); executed 22 times by 1 test: exit (status);Executed by:
| 22 | ||||||
| 91 | } | - | ||||||
| 92 | - | |||||||
| 93 | int | - | ||||||
| 94 | main (int argc, char **argv) | - | ||||||
| 95 | { | - | ||||||
| 96 | /* If not -1, use this method to canonicalize. */ | - | ||||||
| 97 | int can_mode = -1; | - | ||||||
| 98 | int status = EXIT_SUCCESS; | - | ||||||
| 99 | int optc; | - | ||||||
| 100 | bool use_nuls = false; | - | ||||||
| 101 | - | |||||||
| 102 | initialize_main (&argc, &argv); | - | ||||||
| 103 | set_program_name (argv[0]); | - | ||||||
| 104 | setlocale (LC_ALL, ""); | - | ||||||
| 105 | bindtextdomain (PACKAGE, LOCALEDIR); | - | ||||||
| 106 | textdomain (PACKAGE); | - | ||||||
| 107 | - | |||||||
| 108 | atexit (close_stdout); | - | ||||||
| 109 | - | |||||||
| 110 | while ((optc = getopt_long (argc, argv, "efmnqsvz", longopts, NULL)) != -1)
| 216-256 | ||||||
| 111 | { | - | ||||||
| 112 | switch (optc) | - | ||||||
| 113 | { | - | ||||||
| 114 | case 'e': executed 63 times by 1 test: case 'e':Executed by:
| 63 | ||||||
| 115 | can_mode = CAN_EXISTING; | - | ||||||
| 116 | break; executed 63 times by 1 test: break;Executed by:
| 63 | ||||||
| 117 | case 'f': executed 73 times by 1 test: case 'f':Executed by:
| 73 | ||||||
| 118 | can_mode = CAN_ALL_BUT_LAST; | - | ||||||
| 119 | break; executed 73 times by 1 test: break;Executed by:
| 73 | ||||||
| 120 | case 'm': executed 67 times by 1 test: case 'm':Executed by:
| 67 | ||||||
| 121 | can_mode = CAN_MISSING; | - | ||||||
| 122 | break; executed 67 times by 1 test: break;Executed by:
| 67 | ||||||
| 123 | case 'n': executed 5 times by 1 test: case 'n':Executed by:
| 5 | ||||||
| 124 | no_newline = true; | - | ||||||
| 125 | break; executed 5 times by 1 test: break;Executed by:
| 5 | ||||||
| 126 | case 'q': executed 2 times by 1 test: case 'q':Executed by:
| 2 | ||||||
| 127 | case 's': executed 2 times by 1 test: case 's':Executed by:
| 2 | ||||||
| 128 | verbose = false; | - | ||||||
| 129 | break; executed 4 times by 1 test: break;Executed by:
| 4 | ||||||
| 130 | case 'v': executed 6 times by 1 test: case 'v':Executed by:
| 6 | ||||||
| 131 | verbose = true; | - | ||||||
| 132 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||
| 133 | case 'z': executed 6 times by 1 test: case 'z':Executed by:
| 6 | ||||||
| 134 | use_nuls = true; | - | ||||||
| 135 | break; executed 6 times by 1 test: break;Executed by:
| 6 | ||||||
| 136 | case_GETOPT_HELP_CHAR; never executed: break;executed 19 times by 1 test: case GETOPT_HELP_CHAR:Executed by:
| 0-19 | ||||||
| 137 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); executed 10 times by 1 test: exit ( 0 );Executed by:
never executed: break;executed 10 times by 1 test: case GETOPT_VERSION_CHAR:Executed by:
| 0-10 | ||||||
| 138 | default: executed 3 times by 1 test: default:Executed by:
| 3 | ||||||
| 139 | usage (EXIT_FAILURE); | - | ||||||
| 140 | } never executed: end of block | 0 | ||||||
| 141 | } | - | ||||||
| 142 | - | |||||||
| 143 | if (optind >= argc)
| 0-216 | ||||||
| 144 | { | - | ||||||
| 145 | error (0, 0, _("missing operand")); | - | ||||||
| 146 | usage (EXIT_FAILURE); | - | ||||||
| 147 | } never executed: end of block | 0 | ||||||
| 148 | - | |||||||
| 149 | if (argc - optind > 1)
| 7-209 | ||||||
| 150 | { | - | ||||||
| 151 | if (no_newline)
| 2-5 | ||||||
| 152 | error (0, 0, _("ignoring --no-newline with multiple arguments")); executed 2 times by 1 test: error (0, 0, dcgettext (((void *)0), "ignoring --no-newline with multiple arguments" , 5) );Executed by:
| 2 | ||||||
| 153 | no_newline = false; | - | ||||||
| 154 | } executed 7 times by 1 test: end of blockExecuted by:
| 7 | ||||||
| 155 | - | |||||||
| 156 | for (; optind < argc; ++optind)
| 216-224 | ||||||
| 157 | { | - | ||||||
| 158 | const char *fname = argv[optind]; | - | ||||||
| 159 | char *value = (can_mode != -1
| 23-201 | ||||||
| 160 | ? canonicalize_filename_mode (fname, can_mode) | - | ||||||
| 161 | : areadlink_with_size (fname, 63)); | - | ||||||
| 162 | if (value)
| 70-154 | ||||||
| 163 | { | - | ||||||
| 164 | fputs (value, stdout); | - | ||||||
| 165 | if (! no_newline)
| 1-153 | ||||||
| 166 | putchar (use_nuls ? '\0' : '\n'); executed 153 times by 1 test: putchar_unlocked (use_nuls ? '\0' : '\n');Executed by:
| 153 | ||||||
| 167 | free (value); | - | ||||||
| 168 | } executed 154 times by 1 test: end of blockExecuted by:
| 154 | ||||||
| 169 | else | - | ||||||
| 170 | { | - | ||||||
| 171 | status = EXIT_FAILURE; | - | ||||||
| 172 | if (verbose)
| 2-68 | ||||||
| 173 | error (0, errno, "%s", quotef (fname)); executed 2 times by 1 test: error (0, (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, fname));Executed by:
| 2 | ||||||
| 174 | } executed 70 times by 1 test: end of blockExecuted by:
| 70 | ||||||
| 175 | } | - | ||||||
| 176 | - | |||||||
| 177 | return status; executed 216 times by 1 test: return status;Executed by:
| 216 | ||||||
| 178 | } | - | ||||||
| Source code | Switch to Preprocessed file |