C Programming

Readlink C Function

The C programming language’s readlink() function is used to insert the information of the symbolic link, which is referring to some path, into the buffer. Reading the content of a symbolic link is referred to as “reading a link,” or “readlink”. The remaining contents of the buffer are left undetermined if the symbolic link’s length of bytes is less than buffer_size. In this article, we will discuss some examples to illustrate the working of the readlink C function in the Kali Linux system. The syntax of the readlink function is given as follows:

The readlink() function is found in the header file <unistd.h>. This header file must be included above the main function in the code file. The buffer does not contain a NULL character after the result.

#include <unistd.h>
  • ssize_t readlink(const char *restrict link_pathname, char *restrict buffer,
      • size_t buffer_size);

The symbolic link’s value is shortened to fit in the buffer if it is too short to hold it in its entirety (buffer_size). The initial buffer size bytes must be added to the buffer in case the buffer argument is not big enough to hold the link content. If the buffer size is greater than SIZE MAX, the outcome is implementation-defined. The number of bytes loaded in the buffer will be returned if readlink() is successfully executed. Otherwise, it must return -1 and set errno to identify the exact error and leave the buffer unmodified. Here is the list of descriptions of errors along with the code:

  • EINVAL: There is no symbolic link in the named file.
  • EIO: Reading from the file system resulted in an input or output error.
  • EACCES: An element of the path prefix does not have search permission.
  • ELOOP: In symbolic linkages, there is a loop. When resolving the path argument, this error is raised if more than POSIX SYMLOOP symbolic links are found.
  • ENOENT: There is no such file as the named link.

Let’s evaluate the readlink() function in some examples. Create a C programming file in the Linux Terminal using the VIM editor. You can use Nano or any other editor to create the file.

All the header files are included in the code below before the main function. In the first line of the main block, the structure “stat” is declared. After that, a pointer of the character type is declared, which is later used in the readlink() function. We are using ssize_t instead of an integer data type. It is a signed type. Their maximum values are bound to whether the operating system is 32-bit or 64-bit. We have declared two variables of this data type: buffer_size and nbytes (number of bytes).

In the next line of code, we checked the number of arguments supplied from the command line to the main function. If this is not equal to 2, then the program exits from the main function using the exit function, which also displays an error message. The memory location referenced by buffer is where the status information for a given file is stored after being obtained by the lstat() function. If the file name is the symbolic link, then this function returns the details about the symbolic link itself. The information returned by the lstat function is in the form of a stat structure, which is referenced by a buffer.

The status data for a certain file is collected by the lstat() function and placed in the memory address pointed to by the buffer. This method delivers information on the symbolic link itself if the file name is the symbolic link. The data that the lstat function returns is in the form of a stat structure. We have increased the buffer size by one.

In some cases, the reported size is zero, returned by the symbolic link. In that situation, set the buffer size to the PATH_MAX value. Set the buffer using memory allocation, which uses the buffer_size. Check if the buffer is NULL, then display an error message. Call the readlink function and pass the path of the reading link, buffer, and buffer_size to this function. The function returns how many bytes were returned in the path. If the number of bytes is -1, then display the error, else display the point location of the linked file. Then, free the buffer.

Compile the C file using the GCC compiler. The readlink.out file contains the outcome of the file.

In the first execution, it passes the directory name, which is not the symbolic link, so it gives an error that there is no such file or directory.

Below we passed the root directory, which is an invalid argument.

Now that we have passed the path, the readlink function returns the name of the symbolic link to which it is pointing.

Let’s use a C file to construct a file and a symbolic link to it. We have created a file name and a symbolic link in the main function. Create the file with the S IWUSER switch using the create function. A new hard link can be made to an existing file using the C library’s link() method. To build a soft link, use the symlink() method. It won’t be overwritten if the link file or path already exists. When successful, the functions link() and symlink() both return 0.

Utilize the GCC compiler to compile the code, then save the results in the readlink1.out file.

Execute the code. Here is the output:


This is about the use of the readlink() function of C within some of the examples implemented on the Kali Linux system. We have elaborated on its usage, its syntax, and the errors it contains within some of the examples to be more specific and clearer.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.