C Programming

C: Basename Function Usage

While dealing with the files in Linux, you need to manipulate their paths. There are certain functions of the C programming language that can operate on the file paths; however, as far as the scope of this article is concerned, we will talk in detail about the usage of the Basename function of the C programming language.

The Purpose of the Basename Function in C:

The purpose of using the Basename function in the C programming language is simply to extract the last component of the provided path. For example, providing the path “/abc/def/ghi” to the Basename function will return “ghi” as the output.

Usage of the Basename Function in C:

For getting clarity regarding the usage of the Basename function in the C programming language, you will have to go through all of the following examples:

Example # 1:

In this example, we want to extract the last component of a three-leveled path of a Linux system. For that, we implemented the C program shown in the image below:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, in our “main()” function, we declared a character type pointer and assigned to it the three-leveled path whose last component we wanted to extract. Then, we declared another character type pointer and equalized it to the value of the Basename function to which we had assigned the pointer containing the three-leveled path. Finally, we printed the value of the latter variable on the terminal, followed by the “return 0” statement.

For compiling this C code, we made use of the following command:

$ gcc basename.c –o basename

Then, for executing the very same code, we used the command given below:

$ ./basename

The last component of our provided three-leveled path is shown in the following image:

Example # 2:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, we declared a character type pointer in our “main()” function and assigned the two-leveled path whose last component we wanted to extract. Then, we declared another character type pointer and equalized it to the value of the Basename function to which we had assigned the pointer containing the two-leveled path. Finally, we printed the value of the latter variable on the terminal, followed by the “return 0” statement.

When we compiled and executed this C code in the same manner as of our first example, we got the last component of our provided two-leveled path as shown in the following image:

Example # 3:

In this example, we want to extract the last component of a one-leveled path of a Linux system. For that, we implemented the C program shown in the image below:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, we declared a character type pointer in our “main()” function and assigned the one-leveled path whose last component we wanted to extract. Then, we declared another character type pointer and equalized it to the value of the Basename function to which we had assigned the pointer containing the one-leveled path. Finally, we printed the value of the latter variable on the terminal, followed by the “return 0” statement.

When we compiled and executed this C code in the same manner as of our first example, we got the last component of our provided one-leveled path as shown in the following image:

Example # 4:

In this example, we want to see the output of the Basename function to which a path containing only a “/” forward slash is assigned. For that, we implemented the C program shown in the image below:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, in our “main()” function, we declared a character type pointer and assigned to it the path containing only a “/.” Then, we declared another character type pointer and equalized it to the value of the Basename function to which we had assigned the pointer containing the desired path. Finally, we printed the value of the latter variable on the terminal, followed by the “return 0” statement.

When we compiled and executed this C code in the same manner as our first example, we got a “/” as the output as shown in the following image:

Example # 5:

In this example, we want to see the output of the Basename function to which a path containing only a “ ” null string is assigned. For that, we implemented the C program shown in the image below:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, we declared a character type pointer in our “main()” function and assigned the path containing only a null string. Then, we declared another character type pointer and equalized it to the value of the Basename function to which we had assigned the pointer containing the desired path. Finally, we printed the value of the latter variable on the terminal, followed by the “return 0” statement.

When we compiled and executed this C code in the same manner as our first example, we got a null string as the output, as shown in the following image:

Example # 6:

This example is slightly different from the rest of the five examples that we have just gone through. In this example, we want to provide the path whose last component is found at the runtime. For that, we implemented the C program shown in the image below:

For using the Basename function conveniently, we included the “libgen.h” header file along with the regular “stdio.h” and “stdlib.h” header files. After that, in our “main()” function, we have two parameters, “argc” and “argv,” for accepting the command line arguments. Then, we simply have a “for” loop that iterates over “argc” for reading the command line arguments. Finally, we printed the value of the last component of the provided path on the terminal by using the Basename function followed by the “return 0” statement.

For executing this C code, we had to provide the path along with the name of our executable file as shown in the following image:

Upon execution, we got the last component of our provided path as shown in the following image:

Conclusion:

This article was meant to demonstrate the usage of the Basename function in the C programming language. For that, we first explained to you the exact purpose of using this function. This was followed by six different examples of C scripts that used the Basename function. The goal of all of these examples was simply to educate you about the behavior of the Basename function in different scenarios.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.058620929718018