0

Though there are multiple files under path "/home/abhijit/Documents/LINUX/editor/" , but I am not getting desired output in "temp" (which should copied all files from editor folder) folder instead output is coming error as : "cp: cannot stat ‘/home/abhijit/Documents/LINUX/editor/*’: No such file or directory".

#include <stdio.h>
#include <unistd.h>

int main( void )
{
    char* argv1[10] = { "cp", "-rf", "/home/abhijit/Documents/LINUX/editor/*", "/home/abhijit/Documents/LINUX/temp", 0 };
    execvp( argv1[0], argv1 );
}

But if i say as below , it works fine.

 char* argv1[MAX_ARGS] = { "cp", "-rf", "/home/abhijit/Documents/LINUX/editor/", "/home/abhijit/Documents/LINUX/temp", 0 };
jww
  • 97,681
  • 90
  • 411
  • 885
Abhijit Sahu
  • 315
  • 3
  • 9
  • 2
    If you need shell-globbing, you can use `system()` instead – Ctx Jul 04 '17 at 11:21
  • Also see [How to use execvp()](https://stackoverflow.com/q/27541910/608639), [Directory listing with wildcards in C](https://stackoverflow.com/q/38972164/608639) and [Regex for directory and file listing in C](https://stackoverflow.com/q/10464646/608639). – jww Jul 04 '17 at 11:29
  • @BhargavRao - I attempted to vote to undelete Abhijit's answer but I cannot due to your status. Please forgive me for challenging your decision, but I think Abhijit's answer should stand. It has the complete code available, and it credits's SomeProgrammerDude's answer. I think its better than SomeProgrammerDude answer because SPD's answer lacks the code. – jww Jul 04 '17 at 21:12
  • I strongly believe stack-overflow gives complete information as much as possible. BhargavRao - I am completely agree with you.The code which i have provided is the "SIMPLE USAGE of execvp() and glob()" to resolve the issue and complete credit of the ans goes to SomeProgrammerDude's inputs. As per SomeProgrammerDude's point fork and exec should be used , which i am not denying it. By the way i already made that comment. As per my opinion the code which i have provided "should not be deleted" as it convey the basic functionality of the same to achieve output. Please correct me if i am wrong. – Abhijit Sahu Jul 05 '17 at 06:03

1 Answers1

4

The asterisk pattern * is expanded by the shell and not by the cp command itself.

You could make it work if you run the cp command through a shell, either by invoking a shell with the -c option through exec or by using the system function instead of exec.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the quick response. Yes , through shell it's working fine as expected. But ,Sorry i am using busybox without shell. But how to achieve in execvp() system call itself?Is there any possibility to achieve it? – Abhijit Sahu Jul 04 '17 at 11:29
  • @AbhijitSahu If you want to use [*globbing*](https://en.wikipedia.org/wiki/Glob_(programming)) wildcards, you either have to use a shell, or use the [`glob`](http://man7.org/linux/man-pages/man3/glob.3.html) function and multiple `cp` commands (one for each match). – Some programmer dude Jul 04 '17 at 11:39
  • Thank you very much for the inputs - "Some programmer dude". I have updated the code at answer comment. Thank you again for the help. – Abhijit Sahu Jul 04 '17 at 12:58
  • @AbhijitSahu You do know what the [`exec`](http://man7.org/linux/man-pages/man3/exec.3.html) functions *do*? That they *replace* your program with the one you try to `exec`? The `exec` function only return on error. Perhaps you should also learn about [`fork`](http://man7.org/linux/man-pages/man2/fork.2.html) and [the fork-exec idiom](https://en.wikipedia.org/wiki/Fork%E2%80%93exec). – Some programmer dude Jul 04 '17 at 13:00
  • Yes , i know it very well. Thanks for the clarifications! Actually i am using fork() , sigchld_hdl handler with execvp() in my code-base. Instead of writing fork() with whole long prog here i had raised the questions with simple execvp() example - just to convey this wild character issue. Thanks. – Abhijit Sahu Jul 04 '17 at 13:09