Fetching the Search Path

The search path is a list of directories in which the operating system scans for programs. Its purpose is to allow quick access to common programs and system utilities without the necessity of typing a full pathname or changing to the right directory to start an application.

Back in the text mode days, the search path was extremely helpful. It remains so today in the terminal window: Directories full of common programs are listed on the path. When you type a command, the operating system searches each directory on the path for a matching program file.

Various methods exist to display the search path, but you can write a quick utility in C that displays each directory on a line by itself. To do so, you must examine the system’s environment and look for the PATH variable. I demonstrated such a program back in 2013’s blog post on the environment:

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

int main()
{
    char *searchpath;

    searchpath = getenv("PATH");
    printf("The search path is '%s'\n",searchpath);
    return(0);
}

With just a few modifications, you can dump out the directory list by scanning through the string returned (searchpath) and replacing the path separator character with a newline.

In Windows, the path separator character is ;, the semicolon.

For Unix, the path separator character is :, the colon.

Here’s the code:

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

#define SEPARATOR ';'
#define SEPARATOR ':'

int main()
{
    char *searchpath,*s;

    searchpath = getenv("PATH");
    s = searchpath;

    while(*s)
    {
        if( *s == SEPARATOR)
            putchar('\n');
        else
            putchar(*s);
        s++;
    }
    putchar('\n');

    return(0);
}

Comment out Line 4 or 5, depending on the OS.

Character pointer s is initialized to the base of the string returned at Line 12.

The while loop at Line 14 steps through the string one character at a time. If the character matches the SEPARATOR, a newline is output. Otherwise, the character found is output.

Line 20 increments variable s to keep moving through the string.

Here’s sample output from my Mac:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Users/dang/bin

In Windows, the list is much longer. It’s been tradition in Windows (since the days of MS-DOS) to stack directories in the path for each program installed. Some applications are getting better at avoiding this noisome tradition, but you still find plenty that insist upon putting their own directories on the path.

In my Mac’s path, you see the directory /Users/dang/bin. Into this directory I place all my command line utilities, most of which are written in C but with a few in Bash shell script and some in Perl. Having this personal bin directory allows me to use my utilities in any directory when I’m working in a terminal window — which is quite often. I find the C language quite practical for writing such short, useful command line utilities, as did Unix titans Ken Thompson, Brian Kernighan, and Dennis Ritchie. I hope you do as well.

2 thoughts on “Fetching the Search Path

  1. Another suggestion for a future post, or maybe exercise – a function which takes a string and a separator character and returns some sort of data structure with the various strings.

    I agree with you about Windows applications adding themselves to the path, cluttering it up unnecessarily. If you need to edit the path manually the text box is tiny and it’s really fiddly to do so. I use quite a lot of Linux command line programs so it’s pretty much essential for these, but I can’t see the point for Windows desktop applications.

    Also, how about a post on editing the path programatically? I assume this is possible in C.

    Are we going to see any of your “short, useful command line utilities”? (The C ones, not the Perl ones :‑/ )

  2. Thank you for the suggestions, Chris. I’ll put them on the list.

    The problem with setting the path in C (or in any program) is that the program’s environment is local. Once the program exits, any changes to the environment are lost. This limitation extends even to the chdir() function, which changes directories only when the code runs. To modify the path (or any part of the environment) directly, you must edit the shell’s configuration file and then restart the shell.

Leave a Reply