Program 1 and 2
Program 1 and 2
Write a program to implement commands ls( -l option), cp, rm and mv using UNIX file
APIs.
ls –l command
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main()
{
DIR *d;
struct dirent *de;
struct stat buf;
int i,j;
char P[10]="rwxrwxrwx",AP[10]=" ";
struct passwd *p;
struct group *g;
struct tm *t;
char time[26];
d=opendir(".");
readdir(d);
readdir(d);
while( (de=readdir(d))!=NULL)
{
stat(de->d_name,&buf);
// File Type
if(S_ISDIR(buf.st_mode))
printf("d");
else if(S_ISREG(buf.st_mode))
printf("-");
else if(S_ISCHR(buf.st_mode))is it a character device file
printf("c");
else if(S_ISBLK(buf.st_mode))
printf("b");
else if(S_ISLNK(buf.st_mode))
printf("l");
else if(S_ISFIFO(buf.st_mode))
printf("p");
else if(S_ISSOCK(buf.st_mode))
printf("s");
//File Permissions P-Full Permissions AP-Actual Permissions
for(i=0,j=(1<<8);i<9;i++,j>>=1)
AP[i]= (buf.st_mode & j ) ? P[i] : '-' ;
printf("%s",AP);
//No. of Hard Links
printf("%5d",buf.st_nlink);
//User Name
p=getpwuid(buf.st_uid);
printf(" %.8s",p->pw_name);
//Group Name
g=getgrgid(buf.st_gid);
printf(" %-8.8s",g->gr_name);
//File Size
printf(" %8d",buf.st_size);
//Date and Time of modification
t=localtime(&buf.st_mtime);
strftime(time,sizeof(time),"%b %d %H:%M",t);
printf(" %s",time);
//File Name
printf(" %s\n",de->d_name);
}
}
cp command
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
return (EXIT_SUCCESS);
}
mv command
rm command
output_fd = unlink(argv[1]);
if(output_fd == -1){
perror("unlink error");
return 3;
}
}
$./myls
$./myrm a.c
PROGRAM – 2
AHost Program
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<wait.h>
if(pid<0){
printf("Error in fork operation\n");
}
if(pid==0){
printf("PID for Child process: %d\nPID of its parent process: %d\n",getpid(),getppid());
execl("./binsearch",argv[1],NULL);
}
else{
printf("PID of parent process: %d\n",getpid());
wait(&retval);
if(WIFEXITED(retval)==1)
{
printf("Child terminated normally\n");
}
else{
printf("Child terminated abnormally\n");
exit(0);
}
}
return 0;
}
#include<stdio.h>
int main(void){
Output