You are on page 1of 10

ii) Read a directory name III)Check whether the given directory is available iv) Open the directory v)Display

all the files in that directory and also display whether it is a regular file or directory vi)Delete all the files in the directory specified vii)Close the directory Viii) Delete the directory after removing all the files in it.

PROGRAM - K2.C #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/time.h> #include<stdlib.h> main(int argc,char *argv[]) { DIR *dp; struct dirent *dirp; dp=opendir(argv[1]); if(dp==NULL) { printf("directory does not exist"); exit(1);

} chdir(argv[1]); while((dirp=readdir(dp))!=NULL) { if(strcmp(dirp->d_name,".")!=0||strcmp(dirp->d_name,"..")!=0) unlink(dirp->d_name); } closedir(dp); chdir(".."); rmdir(argv[1]); } OUTPUT [mit18@localhost ~]$ mkdir trail [mit18@localhost ~]$ cd trail [mit18@localhost trail]$ cat > y Welcome [mit18@localhost trail]$ls Y [mit18@localhost trail]$cd [mit18@localhost ~]$ls trail k1.c k2.c

[mit18@localhost ~]$cc k2.c [mit18@localhost ~]$./a.out trail [mit18@localhost ~]$ls k1.c k2.c

PROGRAM K3.C #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #include<sys/types.h> #include<sys/time.h> #include<stdlib.h> main(int argc,char *argv[]) { DIR *dp; struct dirent *dirp; struct stat sbuf; char *ptr; dp=opendir(argv[1]); if(chdir(argv[1])) { printf("change directory is not done");

exit(0); }while((dirp=readdir(dp))!=NULL) { printf("%d",dirp->d_ino); printf("\n%s",dirp->d_name); lstat(dirp->d_name,&sbuf); if(S_ISREG(sbuf.st_mode)) printf("regular"); else if(S_ISDIR(sbuf.st_mode)) printf("directory"); else printf("neither"); ptr=ctime(&sbuf.st_mtime); printf("%s",ptr); } closedir(dp); } OUTPUT [mit18@localhost ~]$ mkdir trail [mit18@localhost ~]$ cd trail [mit18@localhost trail]$ cat > y

Welcome [mit18@localhost trail]$mkdir k [mit18@localhost trail]$cd k [mit18@localhost k]$cd [mit18@localhost ~]$cd trail [mit18@localhost trail]$ls Y k

[mit18@localhost trail]$cd [mit18@localhost ~]$ls a.c trail k1.c

[mit18@localhost ~]$cc k3.c [mit18@localhost ~]$./a.out trail 24234390-k3.c-regular 24215864-..-directory 24234388-y-regular 24234387-k-directory 24234386-.-directory Ex.No.02 AIM To write a C program using open, read & write I/O System calls. PROCEDURE

I/O System Calls open, read and write

i) ii) iii)

Create a C file using nano editor Declare a variable of character type named TEXT and store some text inside it Declare another variable of character type named ERR1 and store the error messag ethat is to be displayed.

iv) v)

Pass file name using command line argument using system calls If the file can not opened then display the message present in ERR1 else open the file, write inside it and close the file.

vi) vii) viii) ix)

Open the same file,move the pointer to end and write text again. Move the cursor backward by two steps and write buf text Close the file Read the file till the end and print it in the console

PROGRAM-K4.C #include<stdio.h> #include<string.h> #include<unistd.h> #include<fcntl.h> #include<sys/stat.h> # define MAXBUF BUFSIZ # define FMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) main(int argc,char *argv[]) { int fd,nread; char buf[MAXBUF]; char TEXT[]="this is line 1 \n"; char ERR1[]="cannot open the file\n";

fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,FMODE); if(fd==-1) { write(STDERR_FILENO,ERR1,strlen(ERR1)); exit(1); } write(fd,TEXT,strlen(TEXT)); close(fd); fd=open(argv[1],O_RDWR); lseek(fd,0,SEEK_END); write(fd,TEXT,strlen(TEXT)); lseek(fd,-3,SEEK_CUR); buf[0]='2'; write(fd,buf,1); lseek(fd,0,SEEK_SET); while(nread=read(fd,buf,MAXBUF)!=0) write(STDOUT_FILENO,buf,strlen(buf)); }

OUTPUT [mit18@localhost ~]$cat > test

Welcome Hello [mit18@localhost ~]$cc k4.c [mit18@localhost ~]$./a.out test This is line 1 This is line 2 [mit18@localhost ~]$cat test This is line 1 This is line 2 Ex.No.3

SIMULATE UNIX COMMANDS- IS

AND GREP
AIM To write a c program to simulate ls and grep UNIX commands PROCEDURE
i) ii)

Create a C file using nano editor Get the name of the directory from the user using command line arguments

iii) iv) v)

Open the directory using system call and return the value to dp Read the directory till the end Print the names of the files in directory using d_name which is in struct dirent

vi) vii)

Close the directory Get the pattern name and file name from the user using command line

arguments
viii) ix) x)

Find the pattern in the ile using the command strstr function If the pattern is found display the full text Repeat the process till the end of the file

PROGRAM-K5.C #include<stdio.h> #include<dirent.h> int main(int argc,char *argv[]) { DIR *dp; struct dirent *dirp; dp=opendir(argv[1]); while((dirp=readdir(dp))!=NULL) { printf("%s\n",dirp->d_name); } closedir(dp); } OUTPUT [mit18@localhost ~]$mkdir test [mit18@localhost ~]$cd test [mit18@localhost test]$cat > j1

Welcome [mit18@localhost test]$cat > j2 Hello [mit18@localhost test]$cd [mit18@localhost ~]$cc k5.c [mit18@localhost ~]$./a.out test j1 j2

You might also like