diff --git a/bin/cp/cp.c b/bin/cp/cp.c --- a/bin/cp/cp.c +++ b/bin/cp/cp.c @@ -296,6 +296,11 @@ sep = strchr(to.base, '\0'); sep[0] = '/'; sep[1] = '\0'; + } else { + /* + * We will create the destination directory imminently. + */ + to.dir = -1; } if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) @@ -633,8 +638,9 @@ } if (errno) err(1, "fts_read"); - fts_close(ftsp); - close(to.dir); + (void)fts_close(ftsp); + if (to.dir != AT_FDCWD && to.dir >= 0) + (void)close(to.dir); free(recpath); return (rval); }