lab1-file system calls_student
lab1-file system calls_student
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.
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 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:
PROGRAM DESCRIPTION:
This function retrieves the status of the file pointed to by file_name and fills in buf.
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:
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>
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);
}