0

I am a rookie on C, and now I want to use C to complete a wildcard function. For example, I write a photo processing program named myphoto, and I want to use it like this: myphoto ./photos/*.png, and then myphoto will process all the png file in the dir one by one.

I would like to solve this problem as easily as possible, without the usage of regular expression, and I came up with a idea that maybe I could use the EXEC function to execute a command, but the EXEC function only returns int, not the char*.

So how can I solve this problem? thanks!

Eric Wong
  • 3
  • 6

1 Answers1

3

It is operating system specific. I'm giving a Posix and Linux point of view (on Windows it is different, and I don't know it).

Notice that if you are writing the program myprog.c compiled into myprog then running myprog photos/*.png the main function in myprog.c is getting an array of strings (declare int main(int argc, char**argv) then the array of arguments has argc strings in array argv ....). The expansion is done by the shell before starting your myprog binary executable. See execve(2)

On Linux and Posix systems: read glob(7), you may want to use glob(3) and/or fnmatch(3) and/or wordexp(3). These functions are useful mostly if some data (e.g. a line in a file) contains photos/*.jpeg and your program want to "glob" that. You don't need to "glob" the arguments of main, this has been done already by your shell.

Read Advanced Linux Programming

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Oh, I have just written a short program to try want you said, and now I found how powerful the shell is, thanks a lot! – Eric Wong Sep 25 '14 at 12:30