| 1 | /* `ln' program to create links between files.
|
|---|
| 2 | Copyright (C) 86, 89, 90, 91, 1995-2005 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 2, or (at your option)
|
|---|
| 7 | 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, write to the Free Software Foundation,
|
|---|
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|---|
| 17 | |
|---|
| 18 |
|
|---|
| 19 | /* Written by Mike Parker and David MacKenzie. */
|
|---|
| 20 |
|
|---|
| 21 | #include <config.h>
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <sys/types.h>
|
|---|
| 24 | #include <getopt.h>
|
|---|
| 25 |
|
|---|
| 26 | #include "system.h"
|
|---|
| 27 | #include "same.h"
|
|---|
| 28 | #include "backupfile.h"
|
|---|
| 29 | #include "dirname.h"
|
|---|
| 30 | #include "error.h"
|
|---|
| 31 | #include "quote.h"
|
|---|
| 32 | #include "yesno.h"
|
|---|
| 33 |
|
|---|
| 34 | /* The official name of this program (e.g., no `g' prefix). */
|
|---|
| 35 | #define PROGRAM_NAME "ln"
|
|---|
| 36 |
|
|---|
| 37 | #define AUTHORS "Mike Parker", "David MacKenzie"
|
|---|
| 38 |
|
|---|
| 39 | #ifndef ENABLE_HARD_LINK_TO_SYMLINK_WARNING
|
|---|
| 40 | # define ENABLE_HARD_LINK_TO_SYMLINK_WARNING 0
|
|---|
| 41 | #endif
|
|---|
| 42 |
|
|---|
| 43 | /* In being careful not even to try to make hard links to directories,
|
|---|
| 44 | we have to know whether link(2) follows symlinks. If it does, then
|
|---|
| 45 | we have to *stat* the `source' to see if the resulting link would be
|
|---|
| 46 | to a directory. Otherwise, we have to use *lstat* so that we allow
|
|---|
| 47 | users to make hard links to symlinks-that-point-to-directories. */
|
|---|
| 48 |
|
|---|
| 49 | #if LINK_FOLLOWS_SYMLINKS
|
|---|
| 50 | # define STAT_LIKE_LINK(File, Stat_buf) \
|
|---|
| 51 | stat (File, Stat_buf)
|
|---|
| 52 | #else
|
|---|
| 53 | # define STAT_LIKE_LINK(File, Stat_buf) \
|
|---|
| 54 | lstat (File, Stat_buf)
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | /* Construct a string NEW_DEST by concatenating DEST, a slash, and
|
|---|
| 58 | basename (SOURCE) in alloca'd memory. Don't modify DEST or SOURCE.
|
|---|
| 59 | Omit unnecessary slashes in the boundary between DEST and SOURCE in
|
|---|
| 60 | the result; they can cause harm if "/" and "//" denote different
|
|---|
| 61 | directories. */
|
|---|
| 62 |
|
|---|
| 63 | #define FILE_BASENAME_CONCAT(new_dest, dest, source) \
|
|---|
| 64 | do \
|
|---|
| 65 | { \
|
|---|
| 66 | const char *source_base; \
|
|---|
| 67 | char *tmp_source; \
|
|---|
| 68 | size_t buf_len = strlen (source) + 1; \
|
|---|
| 69 | size_t dest_len = strlen (dest); \
|
|---|
| 70 | \
|
|---|
| 71 | tmp_source = alloca (buf_len); \
|
|---|
| 72 | memcpy (tmp_source, (source), buf_len); \
|
|---|
| 73 | strip_trailing_slashes (tmp_source); \
|
|---|
| 74 | source_base = base_name (tmp_source); \
|
|---|
| 75 | source_base += (source_base[0] == '/'); \
|
|---|
| 76 | dest_len -= (dest_len != 0 && (dest)[dest_len - 1] == '/'); \
|
|---|
| 77 | (new_dest) = alloca (dest_len + 1 + strlen (source_base) + 1); \
|
|---|
| 78 | memcpy (new_dest, dest, dest_len); \
|
|---|
| 79 | (new_dest)[dest_len] = '/'; \
|
|---|
| 80 | strcpy ((new_dest) + dest_len + 1, source_base); \
|
|---|
| 81 | } \
|
|---|
| 82 | while (0)
|
|---|
| 83 |
|
|---|
| 84 | /* The name by which the program was run, for error messages. */
|
|---|
| 85 | char *program_name;
|
|---|
| 86 |
|
|---|
| 87 | /* FIXME: document */
|
|---|
| 88 | static enum backup_type backup_type;
|
|---|
| 89 |
|
|---|
| 90 | /* A pointer to the function used to make links. This will point to either
|
|---|
| 91 | `link' or `symlink'. */
|
|---|
| 92 | static int (*linkfunc) ();
|
|---|
| 93 |
|
|---|
| 94 | /* If true, make symbolic links; otherwise, make hard links. */
|
|---|
| 95 | static bool symbolic_link;
|
|---|
| 96 |
|
|---|
| 97 | /* If true, ask the user before removing existing files. */
|
|---|
| 98 | static bool interactive;
|
|---|
| 99 |
|
|---|
| 100 | /* If true, remove existing files unconditionally. */
|
|---|
| 101 | static bool remove_existing_files;
|
|---|
| 102 |
|
|---|
| 103 | /* If true, list each file as it is moved. */
|
|---|
| 104 | static bool verbose;
|
|---|
| 105 |
|
|---|
| 106 | /* If true, allow the superuser to *attempt* to make hard links
|
|---|
| 107 | to directories. However, it appears that this option is not useful
|
|---|
| 108 | in practice, since even the superuser is prohibited from hard-linking
|
|---|
| 109 | directories on most (all?) existing systems. */
|
|---|
| 110 | static bool hard_dir_link;
|
|---|
| 111 |
|
|---|
| 112 | /* If nonzero, and the specified destination is a symbolic link to a
|
|---|
| 113 | directory, treat it just as if it were a directory. Otherwise, the
|
|---|
| 114 | command `ln --force --no-dereference file symlink-to-dir' deletes
|
|---|
| 115 | symlink-to-dir before creating the new link. */
|
|---|
| 116 | static bool dereference_dest_dir_symlinks = true;
|
|---|
| 117 |
|
|---|
| 118 | static struct option const long_options[] =
|
|---|
| 119 | {
|
|---|
| 120 | {"backup", optional_argument, NULL, 'b'},
|
|---|
| 121 | {"directory", no_argument, NULL, 'F'},
|
|---|
| 122 | {"no-dereference", no_argument, NULL, 'n'},
|
|---|
| 123 | {"no-target-directory", no_argument, NULL, 'T'},
|
|---|
| 124 | {"force", no_argument, NULL, 'f'},
|
|---|
| 125 | {"interactive", no_argument, NULL, 'i'},
|
|---|
| 126 | {"suffix", required_argument, NULL, 'S'},
|
|---|
| 127 | {"target-directory", required_argument, NULL, 't'},
|
|---|
| 128 | {"symbolic", no_argument, NULL, 's'},
|
|---|
| 129 | {"verbose", no_argument, NULL, 'v'},
|
|---|
| 130 | {GETOPT_HELP_OPTION_DECL},
|
|---|
| 131 | {GETOPT_VERSION_OPTION_DECL},
|
|---|
| 132 | {NULL, 0, NULL, 0}
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | /* FILE is the last operand of this command. Return true if FILE is a
|
|---|
| 136 | directory. But report an error there is a problem accessing FILE,
|
|---|
| 137 | or if FILE does not exist but would have to refer to an existing
|
|---|
| 138 | directory if it referred to anything at all. */
|
|---|
| 139 |
|
|---|
| 140 | static bool
|
|---|
| 141 | target_directory_operand (char const *file)
|
|---|
| 142 | {
|
|---|
| 143 | char const *b = base_name (file);
|
|---|
| 144 | size_t blen = strlen (b);
|
|---|
| 145 | bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
|
|---|
| 146 | struct stat st;
|
|---|
| 147 | int err = ((dereference_dest_dir_symlinks ? stat : lstat) (file, &st) == 0
|
|---|
| 148 | ? 0 : errno);
|
|---|
| 149 | bool is_a_dir = !err && S_ISDIR (st.st_mode);
|
|---|
| 150 | if (err && err != ENOENT)
|
|---|
| 151 | error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
|
|---|
| 152 | if (is_a_dir < looks_like_a_dir)
|
|---|
| 153 | error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
|
|---|
| 154 | return is_a_dir;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /* Make a link DEST to the (usually) existing file SOURCE.
|
|---|
| 158 | Symbolic links to nonexistent files are allowed.
|
|---|
| 159 | If DEST_IS_DIR, put the link to SOURCE in the DEST directory.
|
|---|
| 160 | Return true if successful. */
|
|---|
| 161 |
|
|---|
| 162 | static bool
|
|---|
| 163 | do_link (const char *source, const char *dest, bool dest_is_dir)
|
|---|
| 164 | {
|
|---|
| 165 | struct stat source_stats;
|
|---|
| 166 | struct stat dest_stats;
|
|---|
| 167 | char *dest_backup = NULL;
|
|---|
| 168 | bool lstat_ok = false;
|
|---|
| 169 |
|
|---|
| 170 | /* Use stat here instead of lstat.
|
|---|
| 171 | On SVR4, link does not follow symlinks, so this check disallows
|
|---|
| 172 | making hard links to symlinks that point to directories. Big deal.
|
|---|
| 173 | On other systems, link follows symlinks, so this check is right. */
|
|---|
| 174 | if (!symbolic_link)
|
|---|
| 175 | {
|
|---|
| 176 | if (STAT_LIKE_LINK (source, &source_stats) != 0)
|
|---|
| 177 | {
|
|---|
| 178 | error (0, errno, _("accessing %s"), quote (source));
|
|---|
| 179 | return false;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if (ENABLE_HARD_LINK_TO_SYMLINK_WARNING
|
|---|
| 183 | && S_ISLNK (source_stats.st_mode))
|
|---|
| 184 | {
|
|---|
| 185 | error (0, 0, _("%s: warning: making a hard link to a symbolic link\
|
|---|
| 186 | is not portable"),
|
|---|
| 187 | quote (source));
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
|
|---|
| 191 | {
|
|---|
| 192 | error (0, 0, _("%s: hard link not allowed for directory"),
|
|---|
| 193 | quote (source));
|
|---|
| 194 | return false;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if (dest_is_dir)
|
|---|
| 199 | {
|
|---|
| 200 | /* Treat DEST as a directory; build the full filename. */
|
|---|
| 201 | char *new_dest;
|
|---|
| 202 | FILE_BASENAME_CONCAT (new_dest, dest, source);
|
|---|
| 203 | dest = new_dest;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | if (remove_existing_files || interactive || backup_type != no_backups)
|
|---|
| 207 | {
|
|---|
| 208 | lstat_ok = (lstat (dest, &dest_stats) == 0);
|
|---|
| 209 | if (!lstat_ok && errno != ENOENT)
|
|---|
| 210 | {
|
|---|
| 211 | error (0, errno, _("accessing %s"), quote (dest));
|
|---|
| 212 | return false;
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /* If --force (-f) has been specified without --backup, then before
|
|---|
| 217 | making a link ln must remove the destination file if it exists.
|
|---|
| 218 | (with --backup, it just renames any existing destination file)
|
|---|
| 219 | But if the source and destination are the same, don't remove
|
|---|
| 220 | anything and fail right here. */
|
|---|
| 221 | if (remove_existing_files
|
|---|
| 222 | && lstat_ok
|
|---|
| 223 | /* Allow `ln -sf --backup k k' to succeed in creating the
|
|---|
| 224 | self-referential symlink, but don't allow the hard-linking
|
|---|
| 225 | equivalent: `ln -f k k' (with or without --backup) to get
|
|---|
| 226 | beyond this point, because the error message you'd get is
|
|---|
| 227 | misleading. */
|
|---|
| 228 | && (backup_type == no_backups || !symbolic_link)
|
|---|
| 229 | && (!symbolic_link || stat (source, &source_stats) == 0)
|
|---|
| 230 | && SAME_INODE (source_stats, dest_stats)
|
|---|
| 231 | /* The following detects whether removing DEST will also remove
|
|---|
| 232 | SOURCE. If the file has only one link then both are surely
|
|---|
| 233 | the same link. Otherwise check whether they point to the same
|
|---|
| 234 | name in the same directory. */
|
|---|
| 235 | && (source_stats.st_nlink == 1 || same_name (source, dest)))
|
|---|
| 236 | {
|
|---|
| 237 | error (0, 0, _("%s and %s are the same file"),
|
|---|
| 238 | quote_n (0, source), quote_n (1, dest));
|
|---|
| 239 | return false;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | if (lstat_ok)
|
|---|
| 243 | {
|
|---|
| 244 | if (S_ISDIR (dest_stats.st_mode))
|
|---|
| 245 | {
|
|---|
| 246 | error (0, 0, _("%s: cannot overwrite directory"), quote (dest));
|
|---|
| 247 | return false;
|
|---|
| 248 | }
|
|---|
| 249 | if (interactive)
|
|---|
| 250 | {
|
|---|
| 251 | fprintf (stderr, _("%s: replace %s? "), program_name, quote (dest));
|
|---|
| 252 | if (!yesno ())
|
|---|
| 253 | return true;
|
|---|
| 254 | remove_existing_files = true;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (backup_type != no_backups)
|
|---|
| 258 | {
|
|---|
| 259 | char *tmp_backup = find_backup_file_name (dest, backup_type);
|
|---|
| 260 | size_t buf_len = strlen (tmp_backup) + 1;
|
|---|
| 261 | dest_backup = alloca (buf_len);
|
|---|
| 262 | memcpy (dest_backup, tmp_backup, buf_len);
|
|---|
| 263 | free (tmp_backup);
|
|---|
| 264 | if (rename (dest, dest_backup))
|
|---|
| 265 | {
|
|---|
| 266 | if (errno != ENOENT)
|
|---|
| 267 | {
|
|---|
| 268 | error (0, errno, _("cannot backup %s"), quote (dest));
|
|---|
| 269 | return false;
|
|---|
| 270 | }
|
|---|
| 271 | else
|
|---|
| 272 | dest_backup = NULL;
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | if (verbose)
|
|---|
| 278 | {
|
|---|
| 279 | printf ((symbolic_link
|
|---|
| 280 | ? _("create symbolic link %s to %s")
|
|---|
| 281 | : _("create hard link %s to %s")),
|
|---|
| 282 | quote_n (0, dest), quote_n (1, source));
|
|---|
| 283 | if (dest_backup)
|
|---|
| 284 | printf (_(" (backup: %s)"), quote (dest_backup));
|
|---|
| 285 | putchar ('\n');
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | if ((*linkfunc) (source, dest) == 0)
|
|---|
| 289 | return true;
|
|---|
| 290 |
|
|---|
| 291 | /* If the attempt to create a link failed and we are removing or
|
|---|
| 292 | backing up destinations, unlink the destination and try again.
|
|---|
| 293 |
|
|---|
| 294 | POSIX 1003.1-2004 requires that ln -f A B must unlink B even on
|
|---|
| 295 | failure (e.g., when A does not exist). This is counterintuitive,
|
|---|
| 296 | and we submitted a defect report
|
|---|
| 297 | <http://www.opengroup.org/sophocles/show_mail.tpl?source=L&listname=austin-review-l&id=1795>
|
|---|
| 298 | (2004-06-24). If the committee does not fix the standard we'll
|
|---|
| 299 | have to change the behavior of ln -f, at least if POSIXLY_CORRECT
|
|---|
| 300 | is set. In the meantime ln -f A B will not unlink B unless the
|
|---|
| 301 | attempt to link A to B failed because B already existed.
|
|---|
| 302 |
|
|---|
| 303 | Try to unlink DEST even if we may have backed it up successfully.
|
|---|
| 304 | In some unusual cases (when DEST and DEST_BACKUP are hard-links
|
|---|
| 305 | that refer to the same file), rename succeeds and DEST remains.
|
|---|
| 306 | If we didn't remove DEST in that case, the subsequent LINKFUNC
|
|---|
| 307 | call would fail. */
|
|---|
| 308 |
|
|---|
| 309 | if (errno == EEXIST && (remove_existing_files || dest_backup))
|
|---|
| 310 | {
|
|---|
| 311 | if (unlink (dest) != 0)
|
|---|
| 312 | {
|
|---|
| 313 | error (0, errno, _("cannot remove %s"), quote (dest));
|
|---|
| 314 | return false;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | if (linkfunc (source, dest) == 0)
|
|---|
| 318 | return true;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | error (0, errno,
|
|---|
| 322 | (symbolic_link
|
|---|
| 323 | ? _("creating symbolic link %s to %s")
|
|---|
| 324 | : _("creating hard link %s to %s")),
|
|---|
| 325 | quote_n (0, dest), quote_n (1, source));
|
|---|
| 326 |
|
|---|
| 327 | if (dest_backup)
|
|---|
| 328 | {
|
|---|
| 329 | if (rename (dest_backup, dest))
|
|---|
| 330 | error (0, errno, _("cannot un-backup %s"), quote (dest));
|
|---|
| 331 | }
|
|---|
| 332 | return false;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | void
|
|---|
| 336 | usage (int status)
|
|---|
| 337 | {
|
|---|
| 338 | if (status != EXIT_SUCCESS)
|
|---|
| 339 | fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
|---|
| 340 | program_name);
|
|---|
| 341 | else
|
|---|
| 342 | {
|
|---|
| 343 | printf (_("\
|
|---|
| 344 | Usage: %s [OPTION]... [-T] TARGET LINK_NAME (1st form)\n\
|
|---|
| 345 | or: %s [OPTION]... TARGET (2nd form)\n\
|
|---|
| 346 | or: %s [OPTION]... TARGET... DIRECTORY (3rd form)\n\
|
|---|
| 347 | or: %s [OPTION]... -t DIRECTORY TARGET... (4th form)\n\
|
|---|
| 348 | "),
|
|---|
| 349 | program_name, program_name, program_name, program_name);
|
|---|
| 350 | fputs (_("\
|
|---|
| 351 | In the 1st form, create a link to TARGET with the name LINK_NAME.\n\
|
|---|
| 352 | In the 2nd form, create a link to TARGET in the current directory.\n\
|
|---|
| 353 | In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.\n\
|
|---|
| 354 | Create hard links by default, symbolic links with --symbolic.\n\
|
|---|
| 355 | When creating hard links, each TARGET must exist.\n\
|
|---|
| 356 | \n\
|
|---|
| 357 | "), stdout);
|
|---|
| 358 | fputs (_("\
|
|---|
| 359 | Mandatory arguments to long options are mandatory for short options too.\n\
|
|---|
| 360 | "), stdout);
|
|---|
| 361 | fputs (_("\
|
|---|
| 362 | --backup[=CONTROL] make a backup of each existing destination file\n\
|
|---|
| 363 | -b like --backup but does not accept an argument\n\
|
|---|
| 364 | -d, -F, --directory allow the superuser to attempt to hard link\n\
|
|---|
| 365 | directories (note: will probably fail due to\n\
|
|---|
| 366 | system restrictions, even for the superuser)\n\
|
|---|
| 367 | -f, --force remove existing destination files\n\
|
|---|
| 368 | "), stdout);
|
|---|
| 369 | fputs (_("\
|
|---|
| 370 | -n, --no-dereference treat destination that is a symlink to a\n\
|
|---|
| 371 | directory as if it were a normal file\n\
|
|---|
| 372 | -i, --interactive prompt whether to remove destinations\n\
|
|---|
| 373 | -s, --symbolic make symbolic links instead of hard links\n\
|
|---|
| 374 | "), stdout);
|
|---|
| 375 | fputs (_("\
|
|---|
| 376 | -S, --suffix=SUFFIX override the usual backup suffix\n\
|
|---|
| 377 | -t, --target-directory=DIRECTORY specify the DIRECTORY in which to create\n\
|
|---|
| 378 | the links\n\
|
|---|
| 379 | -T, --no-target-directory treat LINK_NAME as a normal file\n\
|
|---|
| 380 | -v, --verbose print name of each file before linking\n\
|
|---|
| 381 | "), stdout);
|
|---|
| 382 | fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|---|
| 383 | fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
|---|
| 384 | fputs (_("\
|
|---|
| 385 | \n\
|
|---|
| 386 | The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
|
|---|
| 387 | The version control method may be selected via the --backup option or through\n\
|
|---|
| 388 | the VERSION_CONTROL environment variable. Here are the values:\n\
|
|---|
| 389 | \n\
|
|---|
| 390 | "), stdout);
|
|---|
| 391 | fputs (_("\
|
|---|
| 392 | none, off never make backups (even if --backup is given)\n\
|
|---|
| 393 | numbered, t make numbered backups\n\
|
|---|
| 394 | existing, nil numbered if numbered backups exist, simple otherwise\n\
|
|---|
| 395 | simple, never always make simple backups\n\
|
|---|
| 396 | "), stdout);
|
|---|
| 397 | printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
|---|
| 398 | }
|
|---|
| 399 | exit (status);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | int
|
|---|
| 403 | main (int argc, char **argv)
|
|---|
| 404 | {
|
|---|
| 405 | int c;
|
|---|
| 406 | bool ok;
|
|---|
| 407 | bool make_backups = false;
|
|---|
| 408 | char *backup_suffix_string;
|
|---|
| 409 | char *version_control_string = NULL;
|
|---|
| 410 | char const *target_directory = NULL;
|
|---|
| 411 | bool no_target_directory = false;
|
|---|
| 412 | int n_files;
|
|---|
| 413 | char **file;
|
|---|
| 414 |
|
|---|
| 415 | initialize_main (&argc, &argv);
|
|---|
| 416 | program_name = argv[0];
|
|---|
| 417 | setlocale (LC_ALL, "");
|
|---|
| 418 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 419 | textdomain (PACKAGE);
|
|---|
| 420 |
|
|---|
| 421 | atexit (close_stdout);
|
|---|
| 422 |
|
|---|
| 423 | /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
|
|---|
| 424 | we'll actually use backup_suffix_string. */
|
|---|
| 425 | backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
|
|---|
| 426 |
|
|---|
| 427 | symbolic_link = remove_existing_files = interactive = verbose
|
|---|
| 428 | = hard_dir_link = false;
|
|---|
| 429 |
|
|---|
| 430 | while ((c = getopt_long (argc, argv, "bdfinst:vFS:T", long_options, NULL))
|
|---|
| 431 | != -1)
|
|---|
| 432 | {
|
|---|
| 433 | switch (c)
|
|---|
| 434 | {
|
|---|
| 435 | case 'b':
|
|---|
| 436 | make_backups = true;
|
|---|
| 437 | if (optarg)
|
|---|
| 438 | version_control_string = optarg;
|
|---|
| 439 | break;
|
|---|
| 440 | case 'd':
|
|---|
| 441 | case 'F':
|
|---|
| 442 | hard_dir_link = true;
|
|---|
| 443 | break;
|
|---|
| 444 | case 'f':
|
|---|
| 445 | remove_existing_files = true;
|
|---|
| 446 | interactive = false;
|
|---|
| 447 | break;
|
|---|
| 448 | case 'i':
|
|---|
| 449 | remove_existing_files = false;
|
|---|
| 450 | interactive = true;
|
|---|
| 451 | break;
|
|---|
| 452 | case 'n':
|
|---|
| 453 | dereference_dest_dir_symlinks = false;
|
|---|
| 454 | break;
|
|---|
| 455 | case 's':
|
|---|
| 456 | #ifdef S_ISLNK
|
|---|
| 457 | symbolic_link = true;
|
|---|
| 458 | #else
|
|---|
| 459 | error (EXIT_FAILURE, 0,
|
|---|
| 460 | _("symbolic links are not supported on this system"));
|
|---|
| 461 | #endif
|
|---|
| 462 | break;
|
|---|
| 463 | case 't':
|
|---|
| 464 | if (target_directory)
|
|---|
| 465 | error (EXIT_FAILURE, 0, _("multiple target directories specified"));
|
|---|
| 466 | else
|
|---|
| 467 | {
|
|---|
| 468 | struct stat st;
|
|---|
| 469 | if (stat (optarg, &st) != 0)
|
|---|
| 470 | error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
|
|---|
| 471 | if (! S_ISDIR (st.st_mode))
|
|---|
| 472 | error (EXIT_FAILURE, 0, _("target %s is not a directory"),
|
|---|
| 473 | quote (optarg));
|
|---|
| 474 | }
|
|---|
| 475 | target_directory = optarg;
|
|---|
| 476 | break;
|
|---|
| 477 | case 'T':
|
|---|
| 478 | no_target_directory = true;
|
|---|
| 479 | break;
|
|---|
| 480 | case 'v':
|
|---|
| 481 | verbose = true;
|
|---|
| 482 | break;
|
|---|
| 483 | case 'S':
|
|---|
| 484 | make_backups = true;
|
|---|
| 485 | backup_suffix_string = optarg;
|
|---|
| 486 | break;
|
|---|
| 487 | case_GETOPT_HELP_CHAR;
|
|---|
| 488 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
|
|---|
| 489 | default:
|
|---|
| 490 | usage (EXIT_FAILURE);
|
|---|
| 491 | break;
|
|---|
| 492 | }
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | n_files = argc - optind;
|
|---|
| 496 | file = argv + optind;
|
|---|
| 497 |
|
|---|
| 498 | if (n_files <= 0)
|
|---|
| 499 | {
|
|---|
| 500 | error (0, 0, _("missing file operand"));
|
|---|
| 501 | usage (EXIT_FAILURE);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | if (no_target_directory)
|
|---|
| 505 | {
|
|---|
| 506 | if (target_directory)
|
|---|
| 507 | error (EXIT_FAILURE, 0,
|
|---|
| 508 | _("Cannot combine --target-directory "
|
|---|
| 509 | "and --no-target-directory"));
|
|---|
| 510 | if (n_files != 2)
|
|---|
| 511 | {
|
|---|
| 512 | if (n_files < 2)
|
|---|
| 513 | error (0, 0,
|
|---|
| 514 | _("missing destination file operand after %s"),
|
|---|
| 515 | quote (file[0]));
|
|---|
| 516 | else
|
|---|
| 517 | error (0, 0, _("extra operand %s"), quote (file[2]));
|
|---|
| 518 | usage (EXIT_FAILURE);
|
|---|
| 519 | }
|
|---|
| 520 | }
|
|---|
| 521 | else if (!target_directory)
|
|---|
| 522 | {
|
|---|
| 523 | if (n_files < 2)
|
|---|
| 524 | target_directory = ".";
|
|---|
| 525 | else if (2 <= n_files && target_directory_operand (file[n_files - 1]))
|
|---|
| 526 | target_directory = file[--n_files];
|
|---|
| 527 | else if (2 < n_files)
|
|---|
| 528 | error (EXIT_FAILURE, 0, _("target %s is not a directory"),
|
|---|
| 529 | quote (file[n_files - 1]));
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | if (symbolic_link)
|
|---|
| 533 | linkfunc = symlink;
|
|---|
| 534 | else
|
|---|
| 535 | linkfunc = link;
|
|---|
| 536 |
|
|---|
| 537 | if (backup_suffix_string)
|
|---|
| 538 | simple_backup_suffix = xstrdup (backup_suffix_string);
|
|---|
| 539 |
|
|---|
| 540 | backup_type = (make_backups
|
|---|
| 541 | ? xget_version (_("backup type"), version_control_string)
|
|---|
| 542 | : no_backups);
|
|---|
| 543 |
|
|---|
| 544 | if (target_directory)
|
|---|
| 545 | {
|
|---|
| 546 | int i;
|
|---|
| 547 | ok = true;
|
|---|
| 548 | for (i = 0; i < n_files; ++i)
|
|---|
| 549 | ok &= do_link (file[i], target_directory, true);
|
|---|
| 550 | }
|
|---|
| 551 | else
|
|---|
| 552 | ok = do_link (file[0], file[1], false);
|
|---|
| 553 |
|
|---|
| 554 | exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
|
|---|
| 555 | }
|
|---|