I'm trying to figure out what words does the -interactive shell of cp accepts as input.

For your convenience, here's code that sets up files for experimentation.

touch example_file{1..3}
mkdir example_dir
cp example_file? example_dir
cp -i example_file? example_dir

The shell then asks interactively for each file whether it should be overwritten. It seems to accept all sorts of random input.

cp: overwrite 'example_dir/example_file1'? q
cp: overwrite 'example_dir/example_file2'? w
cp: overwrite 'example_dir/example_file3'? e

I tried looking into the source code of cp, but I don't know C and searching for overwrite is of no help.

As far as I can tell it accepts some words as confirmation for overwriting, and everything else is taken as a no. The problem is even words like ys seem to be accepted as yes, so I don't know what works and what doesn't.

I'd like to know how exactly does this work and to have some proof of it by means of documentation or intelligible snippets of source code.

  • In the interest of making this not too localized, I can accept unintelligible source code as well, but it will be hard for me to accept an answer if I can't be convinced of what the code is doing. – Git Gud Mar 4 at 12:47

The POSIX standard only specifies that the response need to be "affirmative" for the copying to be carried out when -i is in effect.

For GNU cp, the actual input at that point is handled by a function called yesno(). This function is defined in the lib/yesno.c file in the gnulib source distribution, and looks like this:

yesno (void)
  bool yes;

  char *response = NULL;
  size_t response_size = 0;
  ssize_t response_len = getline (&response, &response_size, stdin);

  if (response_len <= 0)
    yes = false;
      /* Remove EOL if present as that's not part of the matched response,
         and not matched by $ for example.  */
      if (response[response_len - 1] == '\n')
        response[response_len - 1] = '\0';
      yes = (0 < rpmatch (response));

  free (response);
  /* Test against "^[yY]", hardcoded to avoid requiring getline,
     regex, and rpmatch.  */
  int c = getchar ();
  yes = (c == 'y' || c == 'Y');
  while (c != '\n' && c != EOF)
    c = getchar ();

  return yes;

If NLS ("National Language Support") is not used, you can see that the only reply that the function returns true for is a response that starts with an upper or lower case Y character. Any additional or other input is discarded.

If NLS is used, the rpmatch() function is called to determine whether the response was affirmative or not. The purpose of the rpmatch() NLS library function is to determine whether a given string is affirmative or not (with support for internationalisation).

On BSD systems, the corresponding function is found in src/bin/cp/utils.c:

 * If the file exists and we're interactive, verify with the user.
        int ch, checkch;

        if (iflag) {
                (void)fprintf(stderr, "overwrite %s? ", to.p_path);
                checkch = ch = getchar();
                while (ch != '\n' && ch != EOF)
                        ch = getchar();
                if (checkch != 'y' && checkch != 'Y')
                        return (0);
        return 1;

This is essentially the same as the non-NLS code path in the GNU code.

  • Thanks a lot. I was unable to find the source other than on opencoverage.net. Any chance you can link to it on git.savannah.gnu.org ? As I understand it (and correct me if I'm wrong), this is the closest to the truth that we can have (other than looking into what's inside one's machine), so a link there would be very much appreciated. – Git Gud Mar 4 at 13:08
  • 1
  • @JeffSchaller Thank you very much. I suggested an edit to the answer in order to include this. – Git Gud Mar 4 at 13:29

If you look at the source code and search for interactive you can see that it sets x.interactive = I_ASK_USER;, so you can search in this and other files where I_ASK_USER is checked.

In copy.c you will find this symbol near a function call overwrite_ok, and this function contains a call to a function yesno.

The rest is left as an exercise for the reader. ;-)

  • I honestly appreciate the exercise, and even chuckled. Alas, I was unable to find the source code and for this reason I'm preferring Kusalananda's answer a little more. Thank you very much. – Git Gud Mar 4 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.