CS8461- OPERATING SYSTEMS LABORATORY-Write C programs to simulate UNIX commands like cp, ls, grep, etc.
Write C programs to simulate UNIX commands like cp, ls, grep, etc.
x
Before seeing the program you have to learn about what is the meaning of the following commands.
cp- copy- it is used to copying contents from one file to another file.
Syntax:
cp Source Destination
ls-list- its used to list all file names in the directory.
Syntax:
ls
grep-The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern.
Syntax:
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames. -i : Ignores, case for matching -l : Displays list of a filenames only. -n : Display the matched lines and their line numbers.
Below code is included code of both cp & ls command.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#define DATA_SIZE 1000
void createf()
{ char data[DATA_SIZE];
char n[100];
FILE * fPtr;
int i;
printf("create 2 files \nfile1: with data \nfile2: without data for copying\n");
for ( i=0;i<2;i++){
printf("enter a file name:");
gets(n);
fPtr = fopen(n,"w");
if(fPtr == NULL)
{ printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
fputs(data, fPtr);
fclose(fPtr);
printf("File created and saved successfully. ?? \n");
}
}
void copyfun(){
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if (source == NULL)
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if (target == NULL)
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
}
void lsandgrep(){
char fn[10],pat[10],temp[200];
FILE *fp;
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(0);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
int main(){
createf();
copyfun();
lsandgrep();
}
output:
create 2 files
file1: with data
file2: without data for copying
enter a file name:a.txt
Enter contents to store in file :
one two three
File created and saved successfully. ??
enter a file name:b.txt
Enter contents to store in file :
four
File created and saved successfully. ??
Enter name of file to copy
a.txt
Enter name of target file
b.txt
File copied successfully.
Enter directory name
PROJECT
.
..
COUNT_LINE.c
COUNT_LINE.txt
fsearch.c
fsearch.exe
LINE.c
LINE.exe
LINE.txt
tn.txt
Below code is included code of Grep command n- pattern
Program:
#include <stdio.h> #include <stdlib.h> #include<dirent.h> #include<conio.h> #define DATA_SIZE 1000 #define BUFFER_SIZE 1000 void countch(){ FILE *fptr; char path[100]; char word[50]; int wCount; printf("Enter file name: "); scanf("%s", path); printf("Enter word to search in file: "); scanf("%s", word); fptr = fopen(path, "r"); if (fptr == NULL) { printf("Unable to open file.\n"); printf("Please check you have read/write previleges.\n"); exit(EXIT_FAILURE); } Search_in_File(fptr, word); printf("%d",wCount); fclose(fptr); } int Search_in_File(char *fname, char *str) { FILE *fp; int line_num = 1; int find_result = 0; char temp[512]; if((fopen(fname, "r")) != NULL) { return(-1); } while(fgets(temp, 512, fp) != NULL) { if((strstr(temp,str)) != NULL) { line_num=line_num+0; printf("\n%s\n", temp); find_result++; } line_num++; } if(find_result == 0) { printf("\nSorry, couldn't find a match.\n"); } if(fp) { fclose(fp); } return(0); } int main(){ countch(); getch(); return 0; }Program:
Below code is included code of Grep command c - pattern
#include<stdio.h>
#define MAX_FILE_NAME 100
#include<conio.h>
int main()
{
FILE *fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
count = count + 1;
fclose(fp);
printf("The file %s has %d lines\n ", filename, count);
getch();
return 0;}
Enter file name: tn.txt
The file tn.txt has 4 lines
program:
Below code is included code of Grep command h- pattern.
#include <stdio.h>
#include <stdlib.h>
#include<dirent.h>
#include<conio.h>
#define DATA_SIZE 1000
#define BUFFER_SIZE 1000
void countch(){
FILE *fptr;
char path[100];
char word[50];
int wCount;
printf("Enter file name: ");
scanf("%s", path);
printf("Enter word to search in file: ");
scanf("%s", word);
fptr = fopen(path, "r");
if (fptr == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write previleges.\n");
exit(EXIT_FAILURE);
}
Search_in_File(fptr,word);
printf("%d",wCount);
fclose(fptr);
}
int Search_in_File(char *fname, char *str)
{
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
if((fopen(fname, "r")) != NULL) {
return(-1);
}
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp,str)) != NULL) {
line_num=line_num+0;
// printf("\n%s\n", temp);
if(temp!=str){
printf("\n%s\n", temp);
}
find_result++;
}
line_num++;
}
if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}
if(fp) {
fclose(fp);
}
return(0);
}
int main(){
countch();
getch();
return 0;
}
program:
Below code is included code of Grep command i- pattern.
#include <stdio.h>
#include <stdlib.h>
#include<dirent.h>
#include<conio.h>
#define DATA_SIZE 1000
#define BUFFER_SIZE 1000
void countch(){
FILE *fptr;
char path[100];
char word[50];
int wCount;
printf("Enter file name: ");
scanf("%s", path);
printf("Enter word to search in file: ");
scanf("%s", word);
fptr = fopen(path, "r");
if (fptr == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write previleges.\n");
exit(EXIT_FAILURE);
}
Search_in_File(fptr,tolower(word));
printf("%d",wCount);
fclose(fptr);
}
int Search_in_File(char *fname, char *str)
{
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
if((fopen(fname, "r")) != NULL) {
return(-1);
}
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp,str)) != NULL) {
line_num=line_num+0;
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}
if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
}
if(fp) {
fclose(fp);
}
return(0);
}
int main(){
countch();
getch();
return 0;
}
program:
Below code is included code of Grep command l- pattern.
#include <stdio.h>
#include <stdlib.h>
#include<dirent.h>
void l(){
char fn[10],pat[10],temp[200];
FILE *fp;
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(0);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
void main(){
l();
}
Output:
Enter directory name
PROJECT
.
..
COUNT_LINE.c
COUNT_LINE.txt
fsearch.c
fsearch.exe
LINE.c
LINE.exe
LINE.txt
tn.txt
Comments
Post a Comment