Implementation of System Calls
Implementation of System Calls
Implementation of System Calls
1.exec, exit,wait-command
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
int pid;
pid = fork();
exit(1) ;
wait (NULL) ;
exit (0) ;
}
2)stat command
#include<stdio.h>
#include<sys/stat.h>
main()
int fd;
char pathl[10];
struct stat*nfile;
scanf("%s",pathl);
stat(pathl,nfile);
printf("Program inodeno:%d\n",nfile->st_ino);
printf("Protection :%d\n",nfile->st_mode);
printf("User id :%d\n",nfile->st_uid);
printf("Group id :%d\n",nfile->st_gid);
printf("Device driverno:%d\n",nfile->st_dev);
return 0;
}
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
struct dirent *dptr;
DIR *dname;
if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n");
exit(-1);
}
if((dname = opendir(argv[1])) == NULL)
{
perror(argv[1]);
exit(-1);
}
while(dptr=readdir(dname))
printf("%s\n", dptr->d_name);
closedir(dname);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1024
void usage()
{
printf("usage:\t. /a.out filename word\n ");
}
int main(int argc, char *argv[])
{
FILE *fp;
char fline[max];
char *newline;
int count=0;
int occurrences=0;
if(argc!=3)
{
usage();
exit(1);
}
if(!(fp=fopen(argv[1],"r")))
{
printf("grep: couldnot open file : %s\n",argv[1]);
exit(1);
}
while(fgets(fline,max,fp)!=NULL)
{
count++;
if(newline=strchr(fline, '\n'))
*newline='\0';
if(strstr(fline,argv[2])!=NULL)
{
printf("%s %d %s\n", argv[1],count, fline);
occurrences++;
}
}
}
5.SIMULATION OF CAT
UNIX COMMANDS
#include<stdio.h>
#include<stdlib.h>
void printfile(FILE *f)
{
char str[80];
while(fgets(str,80,f))
printf("%s",str);
}
int main(int argc,char *argv[])
{
char str[80];
FILE *f;
if(argc<2)
printfile(stdin);
else
{
int i;
for(i=1;i<argc;i++)
{
if(!(f=fopen(argv[i],"rt")))
perror(argv[i]);
else
printfile(f);
fclose(f);
}
}
}