0% found this document useful (0 votes)
15 views

lab1-file system calls_student

Uploaded by

nanip9120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

lab1-file system calls_student

Uploaded by

nanip9120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 1-File Management System Calls

Lab1 Objective:
Lab1 is intended to provide the way to use the most common system calls in order to make input-
output operations on files, as well as operations to handle files and directories in Linux

List of programs:
1. Write a C Program to illustrate File Management System calls.

2. Write a C Program to illustrate stat System call.

3. Write a C program performs the simulation of the ls command in Linux

4. Write a C program that searches the current directory for each of the arguments
supplied on the command line.

5. Using a similar approach as covered in the above experiment, implement in C the following LINUX
commands using System calls : cat and mv

6. Write a C program which reads contents from file1 and perform square root for the respected
integers, write it into another file
1)AIM: To write a program in C to illustrate the use of create and open system calls using command
line arguments.

Description:

Main can actually accept two arguments: one argument is number of command line arguments, and
the other argument is a full list of all of the command line arguments.

The full declaration of main looks like this:

int main ( int argc, char *argv[] )

The integer, argc is the argument count. It is the number of arguments passed into the program
from the command line, including the name of the program. The array of character pointers is the
listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not
available. After that, every element number less than argc is a command line argument. You can use
each argv element just like a string, or use argv as a two dimensional array. argv[argc] is a null
pointer.

PROGRAM :

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
main(int argc,char *argv[])
{
int fid1,fid2,i;
char buff[500];
if(argc!=3)
{
printf("error");
exit(1);
}
fid1=open(argv[1],O_RDONLY);
i=read(fid1,buff,500);
creat(argv[2],S_IRWXU);
fid2=open(argv[2],O_RDWR);
write(fid2,buff,i);
close(fid1);
close(fid2);
}
2. Write a C program to printing file flags for specified descriptor.

Aim:

To write a program in C to print file flags for specified descriptor.

PROGRAM DESCRIPTION:

The stat function returns information about the specified file.

int stat(const char *file_name, struct stat *buf);

This function retrieves the status of the file pointed to by file_name and fills in buf.

It returns a stat structure, which contains the following fields:

struct stat
{
dev_tst_dev; /* device */
ino_tst_ino; /* inode */
mode_tst_mode; /* protection */
nlink_tst_nlink; /* number of hard links */
uid_tst_uid; /* user ID of owner */
gid_tst_gid; /* group ID of owner */
dev_tst_rdev; /* device type (if inode device) */
off_tst_size; /* total size, in bytes */
unsigned long st_blksize; /* blocksize for filesystem I/O */
unsigned long st_blocks; /* number of blocks allocated */
time_tst_atime; /* time of last access */
time_tst_mtime; /* time of last modification */
time_tst_ctime; /* time of last change */
};
Program:
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
if(argc != 2)
{
printf(“wrong number of arguments”);
exit(1);
}
else
{
Struct stat str;
Stat(argv[1],&str);
printf(“\n mode is %lu \n”,str.st_mode);
printf(“device type is %lu \n”,str.st_dev);
printf(“mode no. is %lu \n”,str.st_ino);
printf(“no.of links is %lu \n”,str.st_nlink);
printf(“user id is %lu \n”,str.st_uid);
printf(“group id is %lu \n”,str.st_gid);
printf(“device name is %lu \n”,str.st_rdev);
printf(“block size is %lu \n”,str.st_blksize);
printf(“no. of blocks is %lu \n”,str.st_blocks);
}
return (0);

}
3. Write a C program which simulate ls command using command line arguments.
Aim:

To write a C program which simulate ls command in linux using command line arguments and file
management system calls.

Description:

The dirent structure is defined as follows:

struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
unsigned short d_reclen; /* Length of this record */
unsigned char d_type; /* Type of file; not supported
by all filesystem types */
char d_name[256]; /* Null-terminated filename */
};
Opendir:

#include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);


Description:

The opendir() function opens a directory stream corresponding to the directory


name, and returns a pointer to the directory stream. The stream is positioned at the first
entry in the directory.
Readdir:
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
Description:
The readdir() function returns a pointer to a dirent structure representing the next directory
entry in the directory stream pointed to by dirp. It returns NULL on reaching the end of the
directory stream or if an error occurred.
Program:
#include<stdio.h>
#include<dirent.h>
#include<errno.h>
#include<fcntl.h>
main(int argc,char *argv[])
{
struct dirent *direntp;
DIR *dirp;
if(argc != 2)
{ printf("Usage: %s directory name\n",argv[0]); return 1;
}
if((dirp=opendir(argv[1]))==NULL)
{ perror("Failed to open directry\n");
return 1;
}
while((direntp=readdir(dirp))!= NULL)
printf("%s\n",direntp->d_name);
while((closedir(dirp)==-1) && (errno==EINTR));
return 0;
}
4.Write a C program that searches the current directory for each of the arguments
supplied on the command line.

Aim:

To write a C program that searches the current directory for each of the arguments supplied
on the command line.

Program:

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
static void lookup(const char *arg)
{
DIR *dirp;
struct dirent *dp;
if ((dirp = opendir(".")) == NULL)
{
perror("couldn't open '.'");
return;
}
do {
Write the incomplete code here

}
while (dp != NULL);
if (errno != 0)
perror("error reading directory");
else
(void) printf("failed to find %s\n", arg);
(void) closedir(dirp);
return;
}
int main(int argc, char *argv[])
{
int i;
for (i = 1; i<argc; i++)
lookup(argv[i]);
return (0);
}

You might also like