Injecting a shell "*" into a C program

My teacher asked me to make a program using execlp that will do the same as:
scp * mylogin@mycomputer:/home/mylogin/myfolder

I tried:
execlp("scp", "scp", "*", "mylogin@mycomputer:/home/mylogin/myfolder", 0)

I found out this doesn't work as it is a wrapper that converts * to filename.

Is there a way I could do this, I really don't know how I could get all the filenames in the execlp call?

Sorry if I don't understand, it's just hard for me to explain :( but I'm really looking for any advice on how "*" works, that would be great :)


source to share

3 answers

You can use glob(3)

glob to match. Then allocate an array where you will put the matching filenames and other arguments in scp. You don't want to use execlp()

, but execvp()

in this case, since you now have an array instead of a list of arguments.

However, the assignment itself is pretty silly: if you want to use the shell expansion extension, use system()

one that executes the given command in the shell (with all its advantages and disadvantages).



As far as I know, the C standard library does not provide functions for scanning a directory. The dirent.h that may be available on your platform contains the opendir () function and its associated elements. You can investigate this.

Other answers have mentioned glob () from glob.h, but that is also platform dependent.



You can use a function glob(3)

to expand wildcards.



All Articles