Anda di halaman 1dari 15

1.

CP command implementation
(i) with single source single destination
(ii) with multiple source single destination

Solution

(i)
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc,char** argv)
{
int fd,fd1;
char buf[2];
if(argc!=3)
{
printf("Usage is: %s source destination\n",argv[0]);
}
else
{
if(strcmp(argv[1],argv[2])==0)
{
printf("%s : \'%s\' and \'%s\' are the same files\n",argv[0],argv[1],argv[2]);
exit(0);
}
if((fd=open(argv[1],O_RDONLY,0))==-1)
{
perror("open");
exit(0);
}
if((fd1=open(argv[2],O_WRONLY|O_CREAT,777))==-1)
{
perror("open");
exit(0);
}
memset(buf,0,1);
while(read(fd,buf,1)!=0)
{
write(fd1,buf,1);
memset(buf,0,1);
}
printf("Copy Successfull\n");
close(fd);
close(fd1);
}
return(0);
}

Output:
(ii)
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<libgen.h>
int main(int argc,char** argv)
{
int fd,fd1,i;
char buf[2],name[1000];
char* filename;
char* ts2;
if(argc<3)
{
printf("Usage is %s source_files destination\n",argv[0]);
}
else
{
if((fd1=open(argv[argc-1],O_WRONLY|O_CREAT|O_APPEND,777))==-1)
{
perror("open");
exit(0);
}
for(i=1;i<argc-1;i++)
{
if((fd=open(argv[i],O_RDONLY,0))==-1)
{
perror("open");
exit(0);
}

memset(buf,0,1);
while(read(fd,buf,1)!=0)
{
write(fd1,buf,1);
memset(buf,0,1);
}
printf("Copy successfull\n");
close(fd);
}
close(fd1);
}
return(0);
}

Output:
2) To create a fixed size records with following operation (file operations)
(a) append, edit (based on primary key)
(b) display
(c)search

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
struct record
{
char name[20];
char USN[11];
};
void edit(int fd);
void display(int fd);
void search(int fd);
int main()
{
int choice;
int fd=open("file",O_RDWR|O_CREAT,0777);
if(fd==-1)
{
perror("open");
exit(0);
}
while(1)
{
printf("Enter\n1:append,edit(based on USN)\n2:display\n3:search\n");
scanf("%d",&choice);
switch(choice)
{
case 1:edit(fd);
break;
case 2:display(fd);
break;
case 3:search(fd);
break;
default:exit(0);
}
}
}
void edit(int fd)
{
struct record *d,dt;
d=&dt;
char name[20];
char USN[11];
int flag1=1,flag2=1;
int records=1;
int bytes=0;
lseek(fd,0,SEEK_SET);
printf("Enter the USN\n");
scanf("%s",USN);
while(flag1==1 && flag2==1)
{
if(read(fd,d,sizeof(struct record))>0)
{
if(strcmp(USN,d->USN)==0)
{
printf("USN : %s already exist\n",USN);
printf("Enter the name to edit else enter exit\n");
scanf("%s",name);
if(name!="exit")
{
strcpy(d->name,name);
bytes=(records-1)*sizeof(struct record);
if(lseek(fd,bytes,SEEK_SET)!=-1)
{
write(fd,d,sizeof(struct record));
flag1=0;
}
else
perror("lseek");
}
else
{
break;
}
}
records++;
}
else
{
flag2=0;
}
}
if(flag2==0)
{
printf("Enter the name\n");
scanf("%s",name);
strcpy(d->name,name);
strcpy(d->USN,USN);
lseek(fd, 0, SEEK_END);
write(fd,d,sizeof(struct record));
}
}
void display(int fd)
{
lseek(fd,0,SEEK_SET);
struct record *d,dt;
d=&dt;
int count=1;
while(read(fd,d,sizeof(struct record))>0)
{
printf("Record %d\n",count);
printf("\tUSN = %s\n",d->USN);
printf("\tname = %s\n",d->name);
count++;
}
}
void search(int fd)
{
struct record *d,dt;
d=&dt;
char USN[11];
int flag1=1;
lseek(fd,0,SEEK_SET);
printf("Enter the USN\n");
scanf("%s",USN);
while(flag1==1)
{
if(read(fd,d,sizeof(struct record))>0)
{
if(strcmp(USN,d->USN)==0)
{
printf("Record Found\n");
printf("USN : %s\n",d->USN);
printf("name = %s\n",d->name);
flag1=0;
}
}
else
{
printf("Record Not Found\n");
flag1=0;
}
}
}

Output:
3) Write a program to print file size or minor number and major number based on filetypes, for all
the files passed on the command line.

#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include <sys/sysmacros.h>
int main(int argc,char** argv)
{
struct stat st;
int fd,i;
if(argc<2)
{
printf("Enter the aruguments\n");
exit(0);
}
for(i=1;i<argc;i++)
{
if(lstat(argv[i],&st)!=-1)
{
}
else if(stat(argv[i],&st)==-1)
{
perror("stat");
exit(0);
}
if(S_ISREG(st.st_mode))
{
printf("\"%s\" is REGULAR FILE\n",argv[i]);
printf("Size = %ld bytes\n\n",st.st_size);
}
else if(S_ISDIR(st.st_mode))
{
printf("\"%s\" is DIRECTORY\n",argv[i]);
printf("Size = %ld bytes\n\n",st.st_size);
}
else if(S_ISFIFO(st.st_mode))
{
printf("\"%s\" is FIFO FILE\n",argv[i]);
printf("Size = %ld bytes\n\n",st.st_size);
}
else if(S_ISLNK(st.st_mode))
{
printf("\"%s\" is LINK FILE\n",argv[i]);
printf("Size = %ld bytes\n\n",st.st_size);
}
else if(S_ISSOCK(st.st_mode))
{
printf("\"%s\" is SOCKET FILE\n",argv[i]);
printf("Size = %ld bytes\n\n",st.st_size);
}
else if(S_ISCHR(st.st_mode))
{
printf("\"%s\" is CHARACTER DEVICE FILE\n",argv[i]);
printf("Major Number = %d\n",major(st.st_rdev));
printf("Minor Number = %d\n\n",minor(st.st_rdev));
}
else if(S_ISBLK(st.st_mode))
{
printf("\"%s\" is BLOCK DEVICE FILE\n",argv[i]);
printf("Major Number = %d\n",major(st.st_rdev));
printf("Minor Number = %d\n\n",minor(st.st_rdev));
}
}
return(0);
}

Output:
4)Write a program to set a read-lock in a file from a give position till the end of the file.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int main(int argc,char **argv)
{
int fd,strt;
if(argc!=3)
{
printf("Usage : %s filename starting_position \n",argv[0]);
exit(0);
}
fd=open(argv[1],O_RDWR,0777);

struct stat st;


if((stat(argv[1],&st))==-1)
{
perror("stat");
exit(0);
}
struct flock fld;
strt=atoi(argv[2]);
fld.l_type=F_RDLCK;
fld.l_whence=SEEK_SET;
fld.l_start=strt;
fld.l_len=st.st_size-strt;
fld.l_pid=getpid();
if((fcntl(fd,F_SETLK,&fld))==-1)
{
perror("fcntl");
exit(0);
}
else
{
printf("Read lock successful.\n");
}
close(fd);
return 0;
}

Output:
5)Implement ls command using directory API’s red and display the records.

#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include<string.h>
int main(int argc, char** argv)
{
char *curr_dir = NULL;
DIR *dp = NULL;
int i,flag=0;
struct dirent *dptr = NULL;
unsigned int count = 0;
curr_dir = getenv("PWD");
if(curr_dir == NULL)
{
printf("\n ERROR : Could not get the working directory\n");
return -1;
}
dp = opendir((const char*)curr_dir);
if(dp == NULL)
{
printf("\n ERROR : Could not open the working directory\n");
return -1;
}
if(argc==1)
{
for(count = 0; (dptr = readdir(dp)) != NULL ; count++)
{
printf("%s\t",dptr->d_name);
}
rewinddir(dp);
printf("\n");
}
else
{
for(i=1;i<argc;i++)
{
flag=0;
for(count = 0; (dptr = readdir(dp)) != NULL ; count++)
{
if(strcmp(dptr->d_name,argv[i])==0)
{
printf("%s\t",dptr->d_name);
flag=1;
}
}
if(flag==0)
{
printf("%s not found\n",argv[i]);
flag=1;
}
rewinddir(dp);
}
printf("\n");
}
return 0;
}

Output:
6)Print Inode number, filetype and permission set for all the files passed on the command line.

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
void permission(struct stat a);
int main(int argc,char** argv)
{
struct stat st,k;
int fd,i;
if(argc<2)
{
printf("Enter the aruguments\n");
exit(0);
}
for(i=1;i<argc;i++)
{
if(lstat(argv[i],&st)!=-1)
{
}
else if(stat(argv[i],&st)==-1)
{
perror("stat");
exit(0);
}
if(S_ISREG(st.st_mode))
{
printf("\"%s\" is REGULAR FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISDIR(st.st_mode))
{
printf("\"%s\" is DIRECTORY\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISFIFO(st.st_mode))
{
printf("\"%s\" is FIFO FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISLNK(st.st_mode))
{
printf("\"%s\" is LINK FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISSOCK(st.st_mode))
{
printf("\"%s\" is SOCKET FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISCHR(st.st_mode))
{
printf("\"%s\" is CHARACTER DEVICE FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
else if(S_ISBLK(st.st_mode))
{
printf("\"%s\" is BLOCK DEVICE FILE\n",argv[i]);
printf("Inode Number = %ld\n",st.st_ino);
permission(st);
}
}
return(0);
}
void permission(struct stat a)
{
printf("Permission set = ");
char x[10]="rwxrwxrwx";
int j=1,t=0,i,ji;
int p=a.st_mode&(~S_IFMT);
j=j<<8;
for(i=0;i<9;i++)
{
ji=j&p;
if(ji>0)
{
printf("%c",x[t]);
t++;
}
else
{
printf("-");
t++;
}
j=j>>1;
}
printf("\n\n");
}

Output:

Anda mungkin juga menyukai