0% found this document useful (0 votes)
30 views2 pages

Progm 2

This C program uses the stat() system call and file mode properties to determine and print the file type of the given filepath. It takes a filepath as a command line argument, calls stat() to get the file status, and uses the file mode bits to switch between the different file types like regular file, directory, device, and print the result. The code is compiled with gcc and executed, passing filepaths of different types, which correctly prints the file type in each case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Progm 2

This C program uses the stat() system call and file mode properties to determine and print the file type of the given filepath. It takes a filepath as a command line argument, calls stat() to get the file status, and uses the file mode bits to switch between the different file types like regular file, directory, device, and print the result. The code is compiled with gcc and executed, passing filepaths of different types, which correctly prints the file type in each case.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM CODE

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc, char *argv[])
{
struct stat sb;
if(argc !=2){
fprintf(stderr,"Usage: %s filename \n",argv[0]);
exit(EXIT_FAILURE);
}
printf("File : %s\n",argv[1]);
if(stat(argv[1],&sb)==-1){
perror("stat");
exit(EXIT_FAILURE);
}
printf("File Type: ");
switch(sb.st_mode & 0x0f000){
case S_IFBLK : printf("Block Device\n");
case S_IFCHR : printf("Character Device\n");
case S_IFDIR : printf("Directory\n");
case S_IFIFO : printf("FIFO/pipe \n");
case S_IFLNK : printf("Symlink\n");
case S_IFREG : printf("Regular File\n");
case S_IFSOCK: printf("Socket \n");
default: printf("Unknown.. \n");
}
}

EXECUTION STEPS
gcc filetype.c
./a.out filepath

break;
break;
break;
break;
break;
break;
break;

OUTPUT
./a.out createfifo.c
File : createfifo.c
File Type: Regular File
./a.out /dev/tty
File : /dev/tty
File Type: Character Device
./a.out /dev/cdrw
File : /dev/cdrw
File Type: Block Device
./a.out Original/
File : Original/
File Type: Directory
./a.out Myfifo
File : Myfifo
File Type: FIFO/pipe

You might also like