C Programming

lstat() Function in C

“lstat() function is basically one kind of system call that is related to our operating system. To know the detail about the lstat() function, first, we have to know what is system calls.

System calls give an environment to the service mode that is available in the operating system. Our program can execute in two modes. One of them is user mode, and another one is kernel mode. If a program is executed in kernel mode, then the program has the right to access the memory, hardware, and other resources directly.

The system calls are the components of the operating system that have the right to use all the system files.”

What is lstat() Function?

lstat() function will control all the system status and returns the information about a special link called a symbolic link. stat() & fstat() function gives the information about the particular file, but the lstat() function refers to a link which points the particular file.

Syntax

The syntax of lstat() function is –
int lstat(const char *path, struct stat *buf);

Here inside the lstat() function, we will pass two parameters as an argument.

The first parameter of the function is a path that gives information about its identification and the probable or actual source of the file.

Another parameter is buff which gives information about the address to the stat structure. This stat structure holds all the updated information about the particular file, which is pointed by the *buf pointer.

The Structure of lstat() Function

The system called lstat() function returns a structure called stat structure. The data members of that stat structure are:

  1. st_mode: the file permissions and file type information.
  2. st_ino: Information about the inode.
  3. st_dev: the device name.
  4. st_uid: getting the identification of the source file.
  5. st_gid: getting the group of identification of the source file
  6. st_size: the size of the file.
  7. st_atime: mentioning the last time of the used file.
  8. st_ctime: mentioning the time of changing the metadata of the file. Example: file name change.
  9. st_mtime: mentioning the time to change the content of the file.
  10. st_nlink: mentioning the number of the directory entry.
  11. st_blocks: counting the number of used blocks (512 bytes).

Macros

The lstat() function has stast struction, which contains several types of macros. These macros help the lstat() function to recognize the type of files. The name of these macros is:

  1. S_ISBLK(): test for a block special file.
  2. S_ISCHR(): examine if the file is a character device file.
  3. S_ISDIR(): file type is a directory.
  4. S_ISFIFO(): inspection related to pipe in the system.
  5. S_ISREG(): examine the commonly used file.
  6. S_ISLINK(): examine the soft link.
  7. S_ISSOCK(): examine if the file is a socket.

Now we will see a programming example to demonstrate the lstat () system call function.

Programming Example-1

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<time.h>
int main()
{
    char fl[] = "t.c" , sln[] ="t.link" ;
    struct stat inf ;
    int fd;
    if ((fd=creat(fl,S_IWUSR))<0)
        perror("creat() error");
    else
    {
        close(fd) ;
        if(link(fl,sln)!=0)
            perror("link() error");
        else
        {
            if(lstat(sln,&inf)!=0)
                perror("lstat() error");
            else
            {
                puts("We are getting below information from lstat() for a file:");
       printf(" File links :%d \n", (int) inf.st_nlink ) ;
                printf(" File uid :%d \n", (int) inf.st_uid ) ;
                printf(" File inode :%d \n", (int) inf.st_ino ) ;                
                printf(" File gid :%d \n", (int) inf.st_gid ) ;
   printf(" File dev id :%d \n", (int) inf.st_dev ) ;
                printf(" File mode :%08x \n", inf.st_mode ) ;
            }
            unlink(sln) ;
        }
        unlink(fl) ;
    }
    return 0 ;
}

Output

Explanation

Here we will see some output using the lstat () function about the status of a particular file that is created by the programmer. In this program, we have created a regular file and also created its symbolic link. It will show the information about i-node, dev id, mode, no of links, uid, and gid. lstat () function provides all the detailed information of a created file and its corresponding soft link.

Programming Example-2

Let us see another detailed implementation of the lstat () function.

#include <sys/types.h>
#include<time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    struct stat a;

   if (argc != 2) {
        fprintf(stderr, "Please enter file name as 1st argument: %s \n", argv[0]);
        exit(EXIT_FAILURE);
    }

   if (lstat(argv[1], &a) == -1) {
        perror("some error for lstat");
        exit(EXIT_FAILURE);
    }


   printf("So the input File type is =>   ");

  if ((a.st_mode & S_IFMT) == S_IFDIR)
        printf ("Directory\n");
  else if ((a.st_mode & S_IFMT) == S_IFIFO)
        printf ("FIFO/PIPE\n");
  else if ((a.st_mode & S_IFMT) == S_IFCHR)
        printf ("Character Device\n");
  else if ((a.st_mode & S_IFMT) == S_IFBLK)
        printf ("Block Device\n");
  else if ((a.st_mode & S_IFMT) == S_IFREG)
        printf ("Regular\n");
  else if ((a.st_mode & S_IFMT) == S_IFSOCK)
        printf ("Socket\n");
  else if ((a.st_mode & S_IFMT) == S_IFLNK)
        printf ("Symbolic Link\n");
  else
        printf("Unknown\n");



   printf("No. of link list:%ld\n", (long) a.st_nlink);
   printf("Ownership: UID=%ld   GID=%ld\n",
            (long) a.st_uid, (long) a.st_gid);

   printf("inode no %ld\n", (long) a.st_ino);
   printf("Mode: %lo (octal)\n",
            (unsigned long) a.st_mode);


   printf("Last change time is:%s", ctime(&a.st_ctime));
   printf("Last access time is: %s", ctime(&a.st_atime));
   printf("Last changing the content of the file time is: %s", ctime(&a.st_mtime));

   printf("Preferred length of the blocks of giving I/O: %ld bytes\n",
            (long) a.st_blksize);
   printf("The length of the file is: %lld bytes\n",
            (long long) a.st_size);
   printf("Counting the no. of used  blocks %lld\n",
            (long long) a.st_blocks);

   exit(EXIT_SUCCESS);
}

Output

Explanation

Here we will show every prospect of the lstat () system call function.

At the first output, we will enter a file name without its path information. As an output, we will get usage of the program. The program needs an input file name/path as 1st argument.

In the next output, we will show the information about the block device file. Here 5+0 records in and 5+0 records out, and the input file type is a block device.

In the next output, we will create a file named “papan_char_device_file” which type is a character device. lstat () function will help it to give all the information related to this character device.

After that, we will make a directory named “habibpur” which file type is a directory. lstat () will create all the information about this file.

Next, we will create another file named “papan_fifo_file” whose file type is FIFO/PIPE. We will get the lstat function provided information for the file as output.

After that, we will make a symbolic link named “puja.txt” of a file named “avishek.txt” and get the lstat function provided information for the file as output.

Next, we will check the file type of “avishek.txt,” which is a regular file [already created in the previous step].

And last, we will create another file named” nadia_socket_file.sock”, which file type is a socket, and get all the detailed information about this file.

With the help of the lstat() function, we will recognize all the files.

Conclusion

Here we will have mentioned every aspect of the lstat () function system call. Seeing all the characteristics and implementation type of the lstat() function, we can clearly understand that it is a very important system call function for getting all the attributes of a particular file that we have created in our system, and the system will respond according to the instruction of lstat () function command.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com