diff -Naur a/doc/coreutils.texi b/doc/coreutils.texi --- a/doc/coreutils.texi 2014-07-14 02:09:52.000000000 +0400 +++ b/doc/coreutils.texi 2014-07-20 20:19:41.460200000 +0400 @@ -7838,6 +7838,14 @@ @table @samp +@itemx --append-exe +@opindex --append-exe +@cindex appending exe on cygwin +Cygwin only: Cygwin normally performs @samp{.exe} magic, where a +command line argument typed without an .exe extension transparently +refers to the existing file with an extension. Specifying this option +will make the .exe show if cygwin magic was involved. + @item -b @itemx --escape @itemx --quoting-style=escape @@ -11799,6 +11807,14 @@ by each symbolic link argument. Without it, @command{stat} acts on any symbolic link argument directly. +@itemx --append-exe +@opindex --append-exe +@cindex appending exe on cygwin +Cygwin only: Cygwin normally performs .exe magic, where a command line +argument typed without an .exe extension transparently refers to the +existing file with an extension. Specifying this option will make +the .exe show if cygwin magic was involved. + @item -f @itemx --file-system @opindex -f diff -Naur a/lib/cygwin.c b/lib/cygwin.c --- a/lib/cygwin.c 1970-01-01 03:00:00.000000000 +0300 +++ b/lib/cygwin.c 2014-07-20 20:19:41.491400000 +0400 @@ -0,0 +1,73 @@ +/* cygwin.c - helper functions unique to Cygwin + + Copyright (C) 2005, 2006, 2008, 2011 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Written by Eric Blake. */ + +#include + +#include "cygwin.h" + +#include +#include +#include +#include + +/* Return -1 if PATH not found, 0 if PATH spelled correctly, and 1 if PATH + had ".exe" automatically appended by cygwin. Don't change errno. */ + +int +cygwin_spelling (char const *path) +{ + int saved_errno = errno; + int result = 0; /* Start with assumption that PATH is okay. */ + int len = strlen (path); + + if (! path || ! *path || len > PATH_MAX) + /* PATH will cause EINVAL or ENAMETOOLONG, treat it as non-existing. */ + return -1; + if (path[len - 1] == '/') + /* Don't change spelling if there is a trailing `/'. */ + return 0; + if (readlink (path, NULL, 0) < 0) + { /* PATH is not a symlink. */ + if (errno == EINVAL) + { /* PATH exists. */ + char *path_exact = malloca (len + 5); /* adding ".exe" and NUL. */ + struct stat st1; + struct stat st2; + strcat (stpcpy (path_exact, path), ".exe"); + /* Avoid cost of double stat, if possible */ + if (access (path_exact, F_OK) == 0 + && lstat (path_exact, &st1) == 0 + && lstat (path, &st2) == 0 + && st1.st_ino == st2.st_ino) + /* So does PATH.exe, with same inode, so append .exe. */ + result = 1; + freea (path_exact); + } + else + /* PATH does not exist. */ + result = -1; + } + else + { /* PATH is a symlink. Don't append. */ + } + + errno = saved_errno; + return result; +} diff -Naur a/lib/cygwin.h b/lib/cygwin.h --- a/lib/cygwin.h 1970-01-01 03:00:00.000000000 +0300 +++ b/lib/cygwin.h 2014-07-20 20:19:41.491400000 +0400 @@ -0,0 +1,38 @@ +/* cygwin.h - helper functions unique to Cygwin + + Copyright (C) 2005, 2006, 2008, 2010, 2011 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + Written by Eric Blake. */ + +#ifndef CYGWIN_H +# define CYGWIN_H 1 + +#include "malloca.h" + +int cygwin_spelling (char const *); + +/* Append ".exe" to char *__NAME_ORIG, where __NAME is either NULL or + between __NAME_ORIG and the nul terminator. Both params will be + evaluated more than once and assigned the new value. The user must + later call freea(__NAME). */ +#define CYGWIN_APPEND_EXE(__name, __name_orig) \ + __name_orig = __name = \ + strcat (strcpy (malloca (strchr (__name ? __name : __name_orig, '\0') \ + - (__name_orig) + 5), \ + __name_orig), ".exe") + +#endif /* CYGWIN_H */ diff -Naur a/lib/file-has-acl.c b/lib/file-has-acl.c --- a/lib/file-has-acl.c 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/file-has-acl.c 2014-07-20 20:19:41.507000000 +0400 @@ -625,15 +625,12 @@ ; else { - /* Don't use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin + /* Use MIN_ACL_ENTRIES: It's set to 4 on Cygwin, but Cygwin returns only 3 entries for files with no ACL. But this is safe: If there are more than 4 entries, there cannot be only the "user::", "group::", "other:", and "mask:" entries. */ - if (count > 4) - { - free (malloced); - return 1; - } + free (malloced); + return (count > 4); if (acl_nontrivial (count, entries)) { diff -Naur a/lib/hash-pjw.c b/lib/hash-pjw.c --- a/lib/hash-pjw.c 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/hash-pjw.c 2014-07-20 20:19:41.507000000 +0400 @@ -19,6 +19,7 @@ #include "hash-pjw.h" +#include #include #define SIZE_BITS (sizeof (size_t) * CHAR_BIT) @@ -38,3 +39,16 @@ return h % tablesize; } + +/* Likewise, but case-insensitive. */ +size_t +hash_pjw_case (const void *x, size_t tablesize) +{ + const unsigned char *s; + size_t h = 0; + + for (s = x; *s; s++) + h = tolower (*s) + ((h << 9) | (h >> (SIZE_BITS - 9))); + + return h % tablesize; +} diff -Naur a/lib/hash-pjw.h b/lib/hash-pjw.h --- a/lib/hash-pjw.h 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/hash-pjw.h 2014-07-20 20:19:41.507000000 +0400 @@ -21,3 +21,4 @@ The result is platform dependent: it depends on the size of the 'size_t' type and on the signedness of the 'char' type. */ extern size_t hash_pjw (void const *x, size_t tablesize) _GL_ATTRIBUTE_PURE; +extern size_t hash_pjw_case (void const *x, size_t tablesize) _GL_ATTRIBUTE_PURE; diff -Naur a/lib/hash-triple.c b/lib/hash-triple.c --- a/lib/hash-triple.c 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/hash-triple.c 2014-07-20 20:19:41.507000000 +0400 @@ -34,7 +34,14 @@ triple_hash (void const *x, size_t table_size) { struct F_triple const *p = x; +#if ! __CYGWIN__ size_t tmp = hash_pjw (p->name, table_size); +#else /* __CYGWIN__ */ + /* Hash case-insensitively, to force collisions on names that differ + by case, so that copy.c can then account for case-insensitive + renames. */ + size_t tmp = hash_pjw_case (p->name, table_size); +#endif /* __CYGWIN__ */ /* Ignoring the device number here should be fine. */ return (tmp ^ p->st_ino) % table_size; diff -Naur a/lib/root-dev-ino.c b/lib/root-dev-ino.c --- a/lib/root-dev-ino.c 2014-07-11 15:00:07.000000000 +0400 +++ b/lib/root-dev-ino.c 2014-07-20 20:19:41.507000000 +0400 @@ -25,13 +25,17 @@ /* Call lstat to get the device and inode numbers for '/'. Upon failure, return NULL. Otherwise, set the members of *ROOT_D_I accordingly and return ROOT_D_I. */ -struct dev_ino * -get_root_dev_ino (struct dev_ino *root_d_i) +struct root_dev_ino * +get_root_dev_ino (struct root_dev_ino *root_d_i) { struct stat statbuf; if (lstat ("/", &statbuf)) return NULL; - root_d_i->st_ino = statbuf.st_ino; - root_d_i->st_dev = statbuf.st_dev; + root_d_i->single_slash.st_ino = statbuf.st_ino; + root_d_i->single_slash.st_dev = statbuf.st_dev; + if (lstat ("//", &statbuf)) + return NULL; + root_d_i->double_slash.st_ino = statbuf.st_ino; + root_d_i->double_slash.st_dev = statbuf.st_dev; return root_d_i; } diff -Naur a/lib/root-dev-ino.h b/lib/root-dev-ino.h --- a/lib/root-dev-ino.h 2014-07-11 15:00:07.000000000 +0400 +++ b/lib/root-dev-ino.h 2014-07-20 20:19:41.522600000 +0400 @@ -21,19 +21,26 @@ # include "dev-ino.h" # include "same-inode.h" -struct dev_ino * -get_root_dev_ino (struct dev_ino *root_d_i); +struct root_dev_ino +{ + struct dev_ino single_slash; + struct dev_ino double_slash; +}; + +struct root_dev_ino * +get_root_dev_ino (struct root_dev_ino *root_d_i); /* These macros are common to the programs that support the --preserve-root and --no-preserve-root options. */ # define ROOT_DEV_INO_CHECK(Root_dev_ino, Dir_statbuf) \ - (Root_dev_ino && SAME_INODE (*Dir_statbuf, *Root_dev_ino)) + (Root_dev_ino && (SAME_INODE (*Dir_statbuf, (Root_dev_ino)->single_slash) \ + || SAME_INODE (*Dir_statbuf, (Root_dev_ino)->double_slash))) # define ROOT_DEV_INO_WARN(Dirname) \ do \ { \ - if (STREQ (Dirname, "/")) \ + if (STREQ (Dirname, "/") || STREQ (Dirname, "//")) \ error (0, 0, _("it is dangerous to operate recursively on %s"), \ quote (Dirname)); \ else \ diff -Naur a/lib/same.c b/lib/same.c --- a/lib/same.c 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/same.c 2014-07-20 20:19:41.522600000 +0400 @@ -40,6 +40,12 @@ #include "error.h" #include "same-inode.h" +#if __CYGWIN__ +# include +# include "memcasecmp.h" +# include "xalloc.h" +#endif + #ifndef MIN # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif @@ -58,6 +64,30 @@ bool identical_basenames = (source_baselen == dest_baselen && memcmp (source_basename, dest_basename, dest_baselen) == 0); +#if __CYGWIN__ + /* Some, but not all, files are case-insensitive (depending on mount + options, CYGWIN=case settings, and virtual file systems). Do + some sleuthing to decide whether case-insensitivity matters. */ + if (! identical_basenames && source_baselen == dest_baselen) + { + ssize_t wsrclen = cygwin_conv_path (CCP_POSIX_TO_WIN_W | CCP_RELATIVE, + source_basename, NULL, 0); + ssize_t wdstlen = cygwin_conv_path (CCP_POSIX_TO_WIN_W | CCP_RELATIVE, + dest_basename, NULL, 0); + char *wsrc = xcharalloc (wsrclen); + char *wdst = xcharalloc (wdstlen); + if (cygwin_conv_path (CCP_POSIX_TO_WIN_W | CCP_RELATIVE, source_basename, + wsrc, wsrclen)) + error (EXIT_FAILURE, errno, "unable to convert path name %s", source); + if (cygwin_conv_path (CCP_POSIX_TO_WIN_W | CCP_RELATIVE, dest_basename, + wdst, wdstlen)) + error (EXIT_FAILURE, errno, "unable to convert path name %s", dest); + if (wsrclen == wdstlen && memcasecmp (wsrc, wdst, wsrclen) == 0) + identical_basenames = true; + free (wsrc); + free (wdst); + } +#endif /* __CYGWIN__ */ bool compare_dirs = identical_basenames; bool same = false; diff -Naur a/lib/xfreopen.c b/lib/xfreopen.c --- a/lib/xfreopen.c 2014-05-29 16:05:50.000000000 +0400 +++ b/lib/xfreopen.c 2014-07-20 20:19:41.522600000 +0400 @@ -18,6 +18,8 @@ #include "xfreopen.h" #include +#include + #include "error.h" #include "exitfail.h" #include "quote.h" @@ -26,9 +28,17 @@ #include "gettext.h" #define _(msgid) gettext (msgid) +#define STREQ(s1, s2) (strcmp (s1, s2) == 0) + void xfreopen (char const *filename, char const *mode, FILE *fp) { + if (!filename && STREQ (mode, "wb")) + { + int flag = fcntl (fileno (fp), F_GETFL); + if (0 <= flag && (flag & O_APPEND)) + mode = "ab"; + } if (!freopen (filename, mode, fp)) { char const *f = (filename ? filename diff -Naur a/src/chcon.c b/src/chcon.c --- a/src/chcon.c 2014-07-14 02:09:52.000000000 +0400 +++ b/src/chcon.c 2014-07-20 20:19:41.522600000 +0400 @@ -48,7 +48,7 @@ /* Pointer to the device and inode numbers of '/', when --recursive. Otherwise NULL. */ -static struct dev_ino *root_dev_ino; +static struct root_dev_ino *root_dev_ino; /* The name of the context file is being given. */ static char const *specified_context; @@ -569,7 +569,7 @@ if (recurse && preserve_root) { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), diff -Naur a/src/chgrp.c b/src/chgrp.c --- a/src/chgrp.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/chgrp.c 2014-07-20 20:19:41.522600000 +0400 @@ -299,7 +299,7 @@ if (chopt.recurse && preserve_root) { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (chopt.root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), diff -Naur a/src/chmod.c b/src/chmod.c --- a/src/chmod.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/chmod.c 2014-07-20 20:19:41.538200000 +0400 @@ -81,7 +81,7 @@ /* Pointer to the device and inode numbers of '/', when --recursive. Otherwise NULL. */ -static struct dev_ino *root_dev_ino; +static struct root_dev_ino *root_dev_ino; /* For long options that have no equivalent short option, use a non-character as a pseudo short option, starting with CHAR_MAX + 1. */ @@ -552,7 +552,7 @@ if (recurse && preserve_root) { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), diff -Naur a/src/chown.c b/src/chown.c --- a/src/chown.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/chown.c 2014-07-20 20:19:41.538200000 +0400 @@ -312,7 +312,7 @@ if (chopt.recurse && preserve_root) { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (chopt.root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), diff -Naur a/src/chown-core.h b/src/chown-core.h --- a/src/chown-core.h 2014-07-11 15:00:07.000000000 +0400 +++ b/src/chown-core.h 2014-07-20 20:19:41.538200000 +0400 @@ -50,7 +50,7 @@ /* Pointer to the device and inode numbers of '/', when --recursive. Need not be freed. Otherwise NULL. */ - struct dev_ino *root_dev_ino; + struct root_dev_ino *root_dev_ino; /* This corresponds to the --dereference (opposite of -h) option. */ bool affect_symlink_referent; diff -Naur a/src/cksum.c b/src/cksum.c --- a/src/cksum.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/cksum.c 2014-07-20 20:19:41.538200000 +0400 @@ -301,6 +301,9 @@ have_read_stdin = false; + if (O_BINARY) + xfreopen (NULL, "wb", stdout); + if (optind == argc) ok = cksum ("-", false); else diff -Naur a/src/copy.c b/src/copy.c --- a/src/copy.c 2014-07-14 02:09:52.000000000 +0400 +++ b/src/copy.c 2014-07-20 20:19:41.538200000 +0400 @@ -70,6 +70,10 @@ # include "verror.h" #endif +#if __CYGWIN__ +# include "cygwin.h" +#endif + #ifndef HAVE_FCHOWN # define HAVE_FCHOWN false # define fchown(fd, uid, gid) (-1) @@ -1313,7 +1317,11 @@ static bool same_file_ok (char const *src_name, struct stat const *src_sb, char const *dst_name, struct stat const *dst_sb, - const struct cp_options *x, bool *return_now, bool *unlink_src) + const struct cp_options *x, bool *return_now, bool *unlink_src +#if __CYGWIN__ + , bool *case_change +#endif /* __CYGWIN__ */ + ) { const struct stat *src_sb_link; const struct stat *dst_sb_link; @@ -1461,6 +1469,18 @@ if (S_ISLNK (dst_sb_link->st_mode)) return true; +#if __CYGWIN__ + /* If the files have the same name, but differ in case, then let + rename() change the case. */ + if (same_link && x->move_mode && same_name (src_name, dst_name) + && memcmp (last_component (src_name), last_component (dst_name), + base_len (src_name)) != 0) + { + *case_change = true; + return true; + } +#endif /* __CYGWIN__ */ + if (same_link && 1 < dst_sb_link->st_nlink && ! same_name (src_name, dst_name)) @@ -1813,10 +1833,20 @@ && ! (x->move_mode || x->symbolic_link || x->hard_link || x->backup_type != no_backups || x->unlink_dest_before_opening)); - if ((use_stat - ? stat (dst_name, &dst_sb) - : lstat (dst_name, &dst_sb)) - != 0) + int res = (use_stat + ? stat (dst_name, &dst_sb) + : lstat (dst_name, &dst_sb)); +#if __CYGWIN__ + /* stat("a") succeeds even if it was really "a.exe". */ + if (! res && cygwin_spelling (dst_name) != 0) + { + /* Only DST_NAME.exe exists, but we want the non-existant + DST_NAME. */ + res = -1; + errno = ENOENT; + } +#endif /* __CYGWIN__ */ + if (res != 0) { if (errno != ENOENT) { @@ -1833,10 +1863,17 @@ that it is stat'able or lstat'able. */ bool return_now; bool unlink_src; +#if __CYGWIN__ + bool case_change = false; +#endif /* __CYGWIN__ */ have_dst_lstat = !use_stat; if (! same_file_ok (src_name, &src_sb, dst_name, &dst_sb, - x, &return_now, &unlink_src)) + x, &return_now, &unlink_src +#if __CYGWIN__ + , &case_change +#endif /* __CYGWIN__ */ + )) { error (0, 0, _("%s and %s are the same file"), quote_n (0, src_name), quote_n (1, dst_name)); @@ -1895,6 +1932,9 @@ cp and mv treat -i and -f differently. */ if (x->move_mode) { +#if __CYGWIN__ + if (!case_change) +#endif /* __CYGWIN__ */ if (abandon_move (x, dst_name, &dst_sb) || (unlink_src && unlink (src_name) == 0)) { @@ -2058,7 +2098,11 @@ /* Never unlink dst_name when in move mode. */ && ! x->move_mode && (x->unlink_dest_before_opening - || (x->preserve_links && 1 < dst_sb.st_nlink) + || (x->preserve_links && 1 < dst_sb.st_nlink +#if __CYGWIN__ + && !case_change +#endif /* __CYGWIN__ */ + ) || (x->dereference == DEREF_NEVER && ! S_ISREG (src_sb.st_mode)) )) @@ -2816,6 +2860,21 @@ { assert (valid_options (options)); +#if __CYGWIN__ + /* .exe magic - if src exists with an implicit .exe suffix, but dst + does not exist and was also specified without a suffix, then + append .exe to dst. */ + int cygwin = cygwin_spelling (src_name); + char *p; + if (cygwin == 1 + && ((p = strchr (dst_name, '\0') - 4) <= dst_name + || strcasecmp (p, ".exe") != 0)) + { + cygwin = 2; + CYGWIN_APPEND_EXE (p, dst_name); + } +#endif /* __CYGWIN__ */ + /* Record the file names: they're used in case of error, when copying a directory into itself. I don't like to make these tools do *any* extra work in the common case when that work is solely to handle @@ -2827,10 +2886,15 @@ top_level_dst_name = dst_name; bool first_dir_created_per_command_line_arg = false; - return copy_internal (src_name, dst_name, nonexistent_dst, NULL, NULL, + bool result = copy_internal (src_name, dst_name, nonexistent_dst, NULL, NULL, options, true, &first_dir_created_per_command_line_arg, copy_into_self, rename_succeeded); +#if __CYGWIN__ + if (cygwin == 2) + freea ((char *) dst_name); +#endif /* __CYGWIN__ */ + return result; } /* Set *X to the default options for a value of type struct cp_options. */ diff -Naur a/src/dd.c b/src/dd.c --- a/src/dd.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/dd.c 2014-07-20 20:19:41.569400000 +0400 @@ -37,6 +37,10 @@ #include "xstrtol.h" #include "xtime.h" +#if __CYGWIN__ +# include +#endif + /* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "dd" @@ -1862,6 +1866,13 @@ static void set_fd_flags (int fd, int add_flags, char const *name) { +#if __CYGWIN__ + /* Cygwin does not allow fcntl to set the mode. */ + int mode_flags = add_flags & (O_BINARY | O_TEXT); + add_flags &= ~(O_BINARY | O_TEXT); + if (mode_flags && setmode (fd, mode_flags) == -1) + error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name)); +#endif /* __CYGWIN__ */ /* Ignore file creation flags that are no-ops on file descriptors. */ add_flags &= ~ (O_NOCTTY | O_NOFOLLOW); @@ -2242,6 +2253,8 @@ } else { + if ((input_flags & (O_BINARY | O_TEXT)) == 0) + input_flags |= O_BINARY; if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0) error (EXIT_FAILURE, errno, _("failed to open %s"), quote (input_file)); } @@ -2264,6 +2277,8 @@ | (conversions_mask & C_NOCREAT ? 0 : O_CREAT) | (conversions_mask & C_EXCL ? O_EXCL : 0) | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC)); + if ((opts & (O_BINARY | O_TEXT)) == 0) + opts |= O_BINARY; /* Open the output file with *read* access only if we might need to read to satisfy a 'seek=' request. If we can't read diff -Naur a/src/dircolors.c b/src/dircolors.c --- a/src/dircolors.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/dircolors.c 2014-07-20 20:19:41.585000000 +0400 @@ -494,8 +494,12 @@ } else { + /* tcsh treats LS_COLORS as a magic shell variable for its + builtin ls-F, but does not recognize all the categories + that coreutils ls does. Therefore, silence stderr to + avoid messages like "Unknown colorls variable `su'.". */ prefix = "setenv LS_COLORS '"; - suffix = "'\n"; + suffix = "' >&/dev/null\n"; } fputs (prefix, stdout); fwrite (s, 1, len, stdout); diff -Naur a/src/install.c b/src/install.c --- a/src/install.c 2014-07-14 02:09:52.000000000 +0400 +++ b/src/install.c 2014-07-20 20:19:41.585000000 +0400 @@ -44,6 +44,10 @@ #include "utimens.h" #include "xstrtol.h" +#if __CYGWIN__ +# include "cygwin.h" +#endif + /* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "install" @@ -531,6 +535,16 @@ error (0, errno, _("fork system call failed")); break; case 0: /* Child. */ +#if __CYGWIN__ + { + /* Check for .exe here, since strip doesn't. */ + char *p; + if (((p = strchr (name, '\0') - 4) <= name + || strcasecmp (p, ".exe") != 0) + && cygwin_spelling (name) > 0) + CYGWIN_APPEND_EXE (p, name); + } +#endif /* __CYGWIN__ */ execlp (strip_program, strip_program, name, NULL); error (EXIT_FAILURE, errno, _("cannot run %s"), strip_program); break; diff -Naur a/src/ls.c b/src/ls.c --- a/src/ls.c 2014-07-14 02:09:52.000000000 +0400 +++ b/src/ls.c 2014-07-20 20:19:41.585000000 +0400 @@ -117,6 +117,10 @@ # include #endif +#if __CYGWIN__ +# include "cygwin.h" +#endif + #define PROGRAM_NAME (ls_mode == LS_LS ? "ls" \ : (ls_mode == LS_MULTI_COL \ ? "dir" : "vdir")) @@ -747,6 +751,11 @@ N_("%b %e %H:%M") }; +#if __CYGWIN__ +/* Whether .exe should be appended to command-line args as needed. */ +static bool append_exe = false; +#endif /* __CYGWIN__ */ + /* The set of signals that are caught. */ static sigset_t caught_signals; @@ -782,6 +791,9 @@ enum { AUTHOR_OPTION = CHAR_MAX + 1, +#if __CYGWIN__ + APPEND_EXE_OPTION, +#endif /* __CYGWIN__ */ BLOCK_SIZE_OPTION, COLOR_OPTION, DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION, @@ -843,6 +855,9 @@ {"block-size", required_argument, NULL, BLOCK_SIZE_OPTION}, {"context", no_argument, 0, 'Z'}, {"author", no_argument, NULL, AUTHOR_OPTION}, +#if __CYGWIN__ + {"append-exe", no_argument, NULL, APPEND_EXE_OPTION}, +#endif /* __CYGWIN__ */ {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0} @@ -1954,6 +1969,12 @@ print_scontext = true; break; +#if __CYGWIN__ + case APPEND_EXE_OPTION: + append_exe = true; + break; +#endif /* __CYGWIN__ */ + case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); @@ -2924,6 +2945,12 @@ uintmax_t blocks = 0; struct fileinfo *f; +#if __CYGWIN__ + char *name_alt = NULL; + if (command_line_arg && append_exe && cygwin_spelling (name) == 1) + CYGWIN_APPEND_EXE (name_alt, name); +#endif /* __CYGWIN__ */ + /* An inode value prior to gobble_file necessarily came from readdir, which is not used for command line arguments. */ assert (! command_line_arg || inode == NOT_AN_INODE_NUMBER); @@ -3035,11 +3062,19 @@ file_failure (command_line_arg, _("cannot access %s"), absolute_name); if (command_line_arg) - return 0; + { +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ + return 0; + } f->name = xstrdup (name); cwd_n_used++; +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ return 0; } @@ -3223,6 +3258,9 @@ f->name = xstrdup (name); cwd_n_used++; +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ return blocks; } @@ -4923,6 +4961,11 @@ -Z, --context print any security context of each file\n\ -1 list one file per line\n\ "), stdout); +#if __CYGWIN__ + fputs (_("\ + --append-exe append .exe if cygwin magic was needed\n\ +"), stdout); +#endif /* __CYGWIN__ */ fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); emit_size_note (); diff -Naur a/src/md5sum.c b/src/md5sum.c --- a/src/md5sum.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/md5sum.c 2014-07-20 20:19:41.600600000 +0400 @@ -813,6 +813,9 @@ if (optind == argc) argv[argc++] = bad_cast ("-"); + if (O_BINARY) + xfreopen (NULL, "wb", stdout); + for (; optind < argc; ++optind) { char *file = argv[optind]; diff -Naur a/src/mv.c b/src/mv.c --- a/src/mv.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/mv.c 2014-07-20 20:19:41.600600000 +0400 @@ -92,7 +92,7 @@ x->require_restore_cwd = true; { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; x->root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (x->root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), @@ -467,6 +467,16 @@ else if (!target_directory) { assert (2 <= n_files); +#if __CYGWIN__ + struct stat s1, s2; + if (2 == n_files + && lstat (file[0], &s1) == 0 && lstat (file[1], &s2) == 0 + && s1.st_ino == s2.st_ino) + { + /* Allow 'mv foo Foo' to change case of the directory foo. */ + } + else +#endif /* __CYGWIN__ */ if (target_directory_operand (file[n_files - 1])) target_directory = file[--n_files]; else if (2 < n_files) diff -Naur a/src/pwd.c b/src/pwd.c --- a/src/pwd.c 2014-07-14 02:09:52.000000000 +0400 +++ b/src/pwd.c 2014-07-20 20:19:41.600600000 +0400 @@ -268,8 +268,8 @@ robust_getcwd (struct file_name *file_name) { size_t height = 1; - struct dev_ino dev_ino_buf; - struct dev_ino *root_dev_ino = get_root_dev_ino (&dev_ino_buf); + struct root_dev_ino dev_ino_buf; + struct root_dev_ino *root_dev_ino = get_root_dev_ino (&dev_ino_buf); struct stat dot_sb; if (root_dev_ino == NULL) @@ -282,7 +282,7 @@ while (1) { /* If we've reached the root, we're done. */ - if (SAME_INODE (dot_sb, *root_dev_ino)) + if (ROOT_DEV_INO_CHECK (root_dev_ino, &dot_sb)) break; find_dir_entry (&dot_sb, file_name, height++); @@ -291,6 +291,9 @@ /* See if a leading slash is needed; file_name_prepend adds one. */ if (file_name->start[0] == '\0') file_name_prepend (file_name, "", 0); + /* If we aren't in `/', we must be in `//'. */ + if (! SAME_INODE (root_dev_ino->single_slash, dot_sb)) + file_name_prepend (file_name, "", 0); } diff -Naur a/src/remove.h b/src/remove.h --- a/src/remove.h 2014-07-11 15:00:07.000000000 +0400 +++ b/src/remove.h 2014-07-20 20:19:41.616200000 +0400 @@ -54,7 +54,7 @@ /* Pointer to the device and inode numbers of '/', when --recursive and preserving '/'. Otherwise NULL. */ - struct dev_ino *root_dev_ino; + struct root_dev_ino *root_dev_ino; /* If nonzero, stdin is a tty. */ bool stdin_tty; diff -Naur a/src/rm.c b/src/rm.c --- a/src/rm.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/rm.c 2014-07-20 20:19:41.616200000 +0400 @@ -325,7 +325,7 @@ if (x.recursive && preserve_root) { - static struct dev_ino dev_ino_buf; + static struct root_dev_ino dev_ino_buf; x.root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (x.root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), diff -Naur a/src/stat.c b/src/stat.c --- a/src/stat.c 2014-07-11 15:00:07.000000000 +0400 +++ b/src/stat.c 2014-07-20 20:19:41.616200000 +0400 @@ -73,6 +73,13 @@ #include "find-mount-point.h" #include "xvasprintf.h" +#if __CYGWIN__ +# include "cygwin.h" +/* Whether .exe should be appended to command-line args as needed. */ +static bool append_exe = false; +# define APPEND_EXE_OPTION 10000 +#endif + #if USE_STATVFS # define STRUCT_STATVFS struct statvfs # define STRUCT_STATXFS_F_FSID_IS_INTEGER STRUCT_STATVFS_F_FSID_IS_INTEGER @@ -189,6 +196,9 @@ {"format", required_argument, NULL, 'c'}, {"printf", required_argument, NULL, PRINTF_OPTION}, {"terse", no_argument, NULL, 't'}, +#if __CYGWIN__ + {"append-exe", no_argument, NULL, APPEND_EXE_OPTION}, +#endif /* __CYGWIN__ */ {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0} @@ -1264,14 +1274,26 @@ return false; } +#if __CYGWIN__ + char *name_alt = NULL; + if (append_exe && cygwin_spelling (filename) == 1) + CYGWIN_APPEND_EXE (name_alt, filename); +#endif /* __CYGWIN__ */ + if (STATFS (filename, &statfsbuf) != 0) { error (0, errno, _("cannot read file system information for %s"), quote (filename)); +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ return false; } bool fail = print_it (format, -1, filename, print_statfs, &statfsbuf); +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ return ! fail; } @@ -1282,6 +1304,7 @@ { int fd = STREQ (filename, "-") ? 0 : -1; struct stat statbuf; + char *name_alt = NULL; if (0 <= fd) { @@ -1294,18 +1317,29 @@ /* We can't use the shorter (follow_links?stat:lstat) (filename, &statbug) since stat might be a function-like macro. */ - else if ((follow_links - ? stat (filename, &statbuf) - : lstat (filename, &statbuf)) != 0) + else { - error (0, errno, _("cannot stat %s"), quote (filename)); - return false; + if ((follow_links + ? stat (filename, &statbuf) + : lstat (filename, &statbuf)) != 0) + { + error (0, errno, _("cannot stat %s"), quote (filename)); + return false; + } + +#if __CYGWIN__ + if (append_exe && cygwin_spelling (filename) == 1) + CYGWIN_APPEND_EXE (name_alt, filename); +#endif /* __CYGWIN__ */ } if (S_ISBLK (statbuf.st_mode) || S_ISCHR (statbuf.st_mode)) format = format2; bool fail = print_it (format, fd, filename, print_stat, &statbuf); +#if __CYGWIN__ + freea (name_alt); +#endif /* __CYGWIN__ */ return ! fail; } @@ -1427,6 +1461,11 @@ if you want a newline, include \\n in FORMAT\n\ -t, --terse print the information in terse form\n\ "), stdout); +#if __CYGWIN__ + fputs (_("\ + --append-exe append .exe if cygwin magic was needed\n\ +"), stdout); +#endif /* __CYGWIN__ */ fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); @@ -1547,6 +1586,12 @@ terse = true; break; +#if __CYGWIN__ + case APPEND_EXE_OPTION: + append_exe = true; + break; +#endif /* __CYGWIN__ */ + case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);