Wild About Wildcards

Wildcards were highly useful during the glory days of text mode operating systems. They still exist: ? represents a single character in a filename and * represents a group of characters. Using wildcards to manipulate files is a staple of computer file management, perhaps a lost art in the era of graphical operating systems, but still relevant. The C language is also still relevant, so how does it deal with wildcards in a filename?

Here’s a quick program to discover how wildcards are interpreted in C code:

2021_05_29-Lesson.c

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

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

    if( argc > 1 )
    {
        filename = argv[1];
    }
    else
    {
        fprintf(stderr,"Filename argument required\n");
        exit(1);
    }
    
    printf("The filename is %s\n",filename);

    return(0);
}

The code relies upon a second command line argument, which is tested for at Line 8. If found, the argument’s text is conveniently saved in the filename pointer. Otherwise, the program bails with a reminder message, output at Line 14.

The second command line argument is output at Line 18. No file is opened; I merely want to see how the program interprets a wildcard as input. Here’s what I typed at the prompt:

$ ./a.out *.c

The program name is a.out (prefixed by the current directory shortcut, ./). The filename argument uses the wildcard *.c to represent all C source code files in the current directory. Here is how the output looked on one of my computers:

The filename is 0424a.c

I’m not sure what I was expecting. I had a faint guess that the output may just be *.c, which is what I typed. But no, the program, or most likely the operating system, translated the *.c wildcard into the name of the first matching file in the directory. For me, the file is named 0424a.c.

To try and force the issue, I placed the argument in double quotes:

$ ./a.out "*.c"

The output on my Linux systems as well as the Mac showed *.c, literally interpreting the input string. But on Windows, the output continued to be 0424a.c, just as before.

Hmmm . . .

My goal is to understand how a program interprets wildcard input. Is it up to the coder to translate *.c into every matching C source code file in the current directory? Does the operating system do the heavy lifting? Obviously many programs, specifically command line utilities, understand and properly interpret wildcard input. But how exactly does it work when you’re coding?

What I discovered is that to understand wildcards, you must know the term glob. Next week’s Lesson covers the topic of glob.

2 thoughts on “Wild About Wildcards

Leave a Reply