100% found this document useful (2 votes)
5K views5 pages

ExNo.3 Simulate UNIX Commands Like CP, LS, Grep, Etc

The document contains code snippets to simulate the grep, ls, and cp commands in C programming language. The grep code takes a file name and pattern as input and prints all lines containing the pattern. The ls code takes a directory name as input and prints all files in that directory. The cp code takes two file names as arguments and copies the contents of the first file to the second file.

Uploaded by

ASD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
5K views5 pages

ExNo.3 Simulate UNIX Commands Like CP, LS, Grep, Etc

The document contains code snippets to simulate the grep, ls, and cp commands in C programming language. The grep code takes a file name and pattern as input and prints all lines containing the pattern. The ls code takes a directory name as input and prints all files in that directory. The cp code takes two file names as arguments and copies the contents of the first file to the second file.

Uploaded by

ASD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SIMULATE grep COMMAND USING C

#include<stdio.h>

#include<string.h>

void main()

char fn[10],pat[10],temp[200];

FILE *fp;

printf("Enter file name\n");

scanf("%s",fn);

printf("Enter pattern to be searched\n");

scanf("%s",pat);

fp=fopen(fn,"r");

while(!feof(fp))

fgets(temp,1000,fp);

if(strstr(temp,pat))

printf("%s",temp);

fclose(fp);

}
Output:
pugazh@Pugazh-PC:/mnt/e/ospgm$ gcc grep.c

pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out

Enter file name

grep.c

Enter pattern to be searched

printf

printf("Enter file name\n");

printf("Enter pattern to be searched\n");

printf("%s",temp);

pugazh@Pugazh-PC:/mnt/e/ospgm$
SIMULATE ls COMMAND USING C

#include<stdio.h>

#include<dirent.h>

main()

char dirname[10];

DIR*p;

struct dirent *d;

printf("Enter directory name\n");

scanf("%s",dirname);

p=opendir(dirname);

if(p==NULL)

perror("Cannot find directory");

exit(-1);

while(d=readdir(p))

printf("%s",d->d_name);

}
OUTPUT:

pugazh@Pugazh-PC:/mnt/e/ospgm$ gcc ls.c

pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out

Enter directory name

Hello

Cannot find directory: No such file or directory

pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out

Enter directory name

sam

HillCipher.javaMD5.javaObjectSigningExample.javaplayfair.javaRailFence2.javaRSA.jav
aSHA1.javaVigenereCipher.java

pugazh@Pugazh-PC:/mnt/e/ospgm$
SIMULATE cp COMMAND USING C
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <fcntl.h>

main(int argc, char **argv)

char buffer[1024];

int files[2];

ssize_t count;

/* Check for insufficient parameters */

if (argc < 3)

return -1;

files[0] = open(argv[1], O_RDONLY);

if (files[0] == -1)

return -1;

files[1] = open(argv[2], O_WRONLY | O_CREAT | S_IRUSR | S_IWUSR);

if (files[1] == -1)

close(files[0]);

return -1;

while ((count = read(files[0], buffer, sizeof(buffer))) != 0)

write(files[1], buffer, count);

OUTPUT:

pugazh@Pugazh-PC:/mnt/e/ospgm$ ./a.out cp.c cp1.c

You might also like