Reading Wildcards from the Command Line

Back in May, I wondered how command line input could be processed when a wildcard is present (Lesson link). My research lead me to the glob() function, but you don’t use this function to process a command line wildcard argument. The reason is that these wildcards are handled by the shell; your code has no direct way to determine when a wildcard is present as a command line argument.

My glob Lesson helped present the globbing concept. It’s useful, but when it comes to processing a wildcard in a command line argument, things work differently — though the principles of glob remain in effect.

When your code attempts to read a wildcard argument, only the first matching file is returned. Why is that? And why isn’t the wildcard returned directly?

The answer deals with how the shell processes wildcard command line arguments: They are expanded from a single argument into multiple arguments, one for each matching filename.

For example, if you have 14 C source code files in a directory, and you have a command line with a single *.c wildcard as its argument, the operating system creates 15 command line arguments: the program name plus the 14 matching C source code files. The value of argc in the main() function reflects this condition:

2021_06_12-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int x;

    for( x=0; x<argc; x++ )
        printf("%d = %s\n",x,argv[x]);

    return(0);
}

In the above code, each command line argument is output. A for loop at Line 8 outputs each argument and its argv[] element number, starting with zero. Here is the output when only the program name is present:

$ ./a.out
0 = ./a.out

And here is the program’s output with a wildcard argument:

$ ./a.out *.c
0 = ./a.out
1 = 0424a.c
2 = 0424b.c
3 = 0529.c

Though only two arguments are specified, the operating system generates four argv[] elements: the program filename and the three matching C source code files.

It’s important to remember that if your code requires several command line arguments, you must properly parse them and not rely upon their discrete positions. I’ll cover a technique for processing command line switches in a future Lesson.

For next week’s Lesson, I present a function similar to glob(), which also uses wildcards.

Leave a Reply