//Assignment No-2
Set A:
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It accepts the command, tokenize the command line and execute it by creating the child process. Also implement the additional command ‘count’ as
⇛ myshell$ count c filename: It will display the number of characters in given file
⇛ myshell$ count w filename: It will display the number of words in given file
⇛ myshell$ count l filename: It will display the number of lines in given file.
CODE:
π
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <wait.h>
- char comm[100],*ptr;
- char *args[10];
- int tot;
- int count(char *option, char *fname);
- void getcomm();
- void sep_arg();
- void take_action();
- void main()
- {
- do
- {
- printf("\nCodingLab");
- getcomm();
- sep_arg();
- take_action();
- } while(1);
- }
- void sep_arg()
- {
- int i, j;
- char *token;
- i = j = tot = 0;
- token = strtok(comm," ");
- while(token != NULL)
- {
- args[tot] = (char*)malloc(20);
- strcpy(args[tot++],token);
- token=strtok(NULL," ");
- }
- return;
- }
- void getcomm()
- {
- int len;
- ptr = fgets(comm,80,stdin);
- len = strlen(comm);
- comm[len-1] = "\0";
- return;
- }
- void take_action()
- {
- char str[100];
- pid_t pid;
- int status;
- ptr = comm;
- if(ptr == NULL)
- exit(0);
- if(strlen(comm) <= 1)
- return;
- if(strcmp(args[0],"count") == 0)
- count(args[1],args[2]);
- else
- if(pid = fork() < 0)
- {
- printf("\nUnable to create process");
- }
- else
- if(pid == 0)//child
- {
- execvp(args[0],args);
- strcpy(str,"./");
- strcat(str,args[0]);
- execvp(str,args);
- printf("\n%s: Command Not Found ");
- exit(0);
- }
- else
- waitpid(pid,&status,0);
- }
- int count(char *option, char *fname)
- {
- int ccnt,wcnt,lcnt;
- int ch;
- FILE *fp;
- int i;
- fp = fopen(fname,"r");
- if(fp==NULL)
- {
- printf("\nUnable to open file");
- exit(0);
- }
- ccnt=wcnt=lcnt=0;
- ch = fgetc(fp);
- while(ch != EOF)
- {
- ccnt++;
- if(ch == '' || ch == '\t')
- {
- wcnt++;
- }
- else
- if(ch == '\n')
- {
- lcnt++;
- wcnt++;
- }
- ch = fgetc(fp);
- }
- fclose(fp);
- if(strcmp(option,"c") == 0)
- {
- printf("\nTotal chars in the file:\t%d",ccnt);
- }
- else
- if(strcmp(option,"w") == 0)
- {
- printf("\nTotal words in the file:\t%d",wcnt);
- }
- else
- if(strcmp(option,"l") == 0)
- {
- printf("\nTotal lines in the file:\t%d",lcnt);
- }
- else
- printf("\nInvalid Option");
- }
//Execute
/*Output
*/
Set B:
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘list’ as
⇛ myshell$ list f dirname: It will display filenames in a given directory.
⇛ myshell$ list n dirname: It will count the number of entries in a given directory.
⇛ myshell$ list i dirname: It will display filenames and their inode number for the files in a given
directory.
CODE:
π
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <wait.h>
- #include <dirent.h>
- //Programs for create own shell
- char comm[100],*ptr;
- char *args[10];
- int tot;
- //function declear
- void list(char *option, char *dirname);
- void getcomm();
- void sep_arg();
- void take_action();
- void main()//main fuction
- {
- do
- {
- printf("\nCodingLab$");//enter which name you want to give shell
- getcomm();
- sep_arg();
- take_action();
- }while(1);
- }
- void sep_arg()
- {
- int i, j;
- char *token;
- i = j = tot =0;
- token = strtok(comm," ");
- while(token != NULL)
- {
- args[tot] = (char*)malloc(20);
- strcpy(args[tot++],token);
- token = strtok(NULL, " ");
- }
- for(i=0; i<tot; i++)
- {
- printf("%s",args[i]);
- }
- return;
- }
- void getcomm()
- {
- int len;
- ptr = fgets(comm, 80,stdin);
- len = strlen(comm);
- comm[len-1] = '\0';
- return;
- }
- void take_action()
- {
- char str[100];
- pid_t pid;
- int status;
- ptr = comm;
- if(ptr == NULL)
- exit(0);
- if( strlen(comm) <= 1)
- return;
- if(strcmp(args[0],"list") == 0)
- list(args[1],args[2]);
- else
- if((pid = fork() < 0))
- {
- printf("\n Unable to create process");
- }
- else
- if(pid == 0)//child
- {
- execvp(args[0],args);
- strcpy(str,"./");
- strcat(str,args[0]);
- execvp(str,args);
- printf("\n%s: Command not found", comm);
- exit(0);
- }
- else
- waitpid(pid,&status,0);
- }
- void list(char *option, char *dirname)
- {
- DIR *dp;
- struct dirent *dent;
- int cnt;
- if(strcmp(option, "-f")==0)
- {
- //list all dir entries
- dp = opendir(dirname);
- if(dp == NULL)
- {
- printf("\nUnable to open directory\n");
- exit(0);
- }
- dent = readdir(dp);
- while(dent != NULL)
- {
- printf("\n%s", dent->d_name);
- dent = readdir(dp);
- }
- printf("\n");
- }
- else
- if(strcmp(option, "-n")==0)
- {
- //list number all dir entries
- cnt = 0;
- dp = opendir(dirname);
- if(dp == NULL)
- {
- printf("\nUnable to open dir");
- exit(0);
- }
- dent = readdir(dp);
- while(dent != NULL)
- {
- cnt++;
- dent = readdir(dp);
- }
- printf("\nTotal dir entries :\t %d\n",cnt);
- closedir(dp);
- }
- else
- if(strcmp(option, "-i")==0)
- {
- //list all dir entries with inode
- dp = opendir(dirname);
- if(dp == NULL)
- {
- printf("\nUnable to open dir");
- exit(0);
- }
- dent = readdir(dp);
- while(dent != NULL)
- {
- printf("\n%s %ld",dent->d_name,dent->d_ino);
- dent = readdir(dp);
- }
- printf("\n");
- closedir(dp);
- }
- else
- printf("\n>>Invalid Option<<\n");
- }
Set C:
1)
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘typeline’ as
⇛myshell$ typeline n filename: It will display first n lines of the file.
⇛myshell$ typeline -n filename: It will display last n lines of the file.
⇛myshell$ typeline a filename: It will display all the lines of the file.
CODE:
π
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <wait.h>
- //Programs for create own shell
- char comm[100],*ptr;
- char *args[10];
- int tot;
- //function declear
- void typeline(char *option, char *fname);
- void getcomm();
- void sep_arg();
- void take_action();
- void main()//main fuction
- {
- do
- {
- printf("\nCodingLab$");//enter which name you want to give shell
- getcomm();
- sep_arg();
- take_action();
- }while(1);
- }
- void sep_arg()
- {
- int i, j;
- char *token;
- i = j = tot =0;
- token = strtok(comm," ");
- while(token != NULL)
- {
- args[tot] = (char*)malloc(20);
- strcpy(args[tot++],token);
- token = strtok(NULL, " ");
- }
- for(i=0; i<tot; i++)
- {
- printf("%s",args[i]);
- }
- return;
- }
- void getcomm()
- {
- int len;
- ptr = fgets(comm, 80,stdin);
- len = strlen(comm);
- comm[len-1] = '\0';
- return;
- }
- void take_action()
- {
- char str[100];
- pid_t pid;
- int status;
- ptr = comm;
- if(ptr == NULL)
- exit(0);
- if( strlen(comm) <= 1)
- return;
- if(strcmp(args[0],"typeline") == 0)
- typeline(args[1],args[2]);
- else
- if((pid = fork() < 0))
- {
- printf("\n Unable to create process");
- }
- else
- if(pid == 0)//child
- {
- execvp(args[0],args);
- strcpy(str,"./");
- strcat(str,args[0]);
- execvp(str,args);
- printf("\n%s: Command not found", comm);
- exit(0);
- }
- else
- waitpid(pid,&status,0);
- }
- void typeline(char *option, char *fname)
- {
- FILE *fp;
- char str[80];
- int tot_lines,ch,n,cnt;
- //count the lines file
- fp = fopen(fname, "r");
- if(fp == NULL)
- {
- printf("\nUnable to Open file");
- return;
- }
- tot_lines = 0;
- while(!feof(fp))
- {
- fgets(str,80,fp);
- tot_lines++;
- }
- fclose(fp);
- if(strcmp(option, "a")==0)
- {
- fp=fopen(fname,"r");
- while(!feof(fp))
- {
- fgets(str,80,fp);
- if(feof(fp))
- break;
- printf("%s",str);
- }
- fclose(fp);
- }
- else
- if(option[0]== '+')//print first n line
- {
- n = atol(option+1);
- if(n > tot_lines)
- {
- printf("\ninvalid number of lines");
- return;
- }
- fp=fopen(fname,"r");
- cnt = 0;
- while(cnt < n)
- {
- fgets(str,80,fp); if(feof(fp)) break;
- printf("%s",str);
- cnt++;
- }
- fclose(fp);
- }
- else
- if(option[0]== '-')//print last n lines
- {
- n = atoi(option+1);
- if(n > tot_lines)
- {
- printf("\ninvalid number of lines");
- return;
- }
- fp = fopen(fname,"r");
- cnt = 0;
- while(cnt < tot_lines - n)
- {
- fgets(str,80,fp); if(feof(fp)) break;
- cnt++;
- }
- while(!feof(fp))
- {
- fgets(str,80,fp); if(feof(fp)) break;
- printf("%s",str);
- cnt++;
- }
- fclose(fp);
- }
- else
- printf("\nInvalid options");
- }
2)
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘search’ as
⇛myshell$ search f filename pattern : It will search the first occurrence of pattern in the given
file.
⇛myshell$ search a filename pattern : It will search all the occurrence of pattern in the given file.
⇛myshell$ search c filename pattern : It will count the number of occurrence of pattern in the
given file.
CODE:
π
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <wait.h>
- //Programs for create own shell
- char comm[100],*ptr;
- char *args[10];
- int tot;
- //function declear
- void search(char *option, char *pattern, char *fname);
- void getcomm();
- void sep_arg();
- void take_action();
- void main()//main fuction
- {
- do
- {
- printf("\nCodingLab$");//enter which name you want to give shell
- getcomm();
- sep_arg();
- take_action();
- }while(1);
- }
- void sep_arg()
- {
- int i, j;
- char *token;
- i = j = tot =0;
- args[tot] = (char*)malloc(sizeof(char)*20);
- for(i=0; comm[i] != '\0'; i++)
- {
- if(comm[i] == ' ')
- {
- args[tot][j] = '\0';
- tot++;
- args[tot] = (char*)malloc(sizeof(char)*20);
- j=0;
- }
- else
- {
- args[tot][j] = comm[i];
- j++;
- }
- }//for
- args[tot][j] = '\0';
- return;
- }
- void getcomm()
- {
- int len;
- ptr = fgets(comm, 80,stdin);
- len = strlen(comm);
- comm[len-1] = '\0';
- return;
- }
- void take_action()
- {
- char str[100];
- pid_t pid;
- int status;
- ptr = comm;
- if(ptr == NULL)
- exit(0);
- if( strlen(comm) <= 1)
- return;
- if(strcmp(args[0],"search") == 0)
- search(args[1],args[2],args[3]);
- else
- if((pid = fork() < 0))
- {
- printf("\n Unable to create process");
- }
- else
- if(pid == 0)//child
- {
- execvp(args[0],args);
- strcpy(str,"./");
- strcat(str,args[0]);
- execvp(str,args);
- printf("\n%s: Command not found", comm);
- exit(0);
- }
- else
- waitpid(pid,&status,0);
- }
- void search(char *option, char *pattern, char *fname)
- {
- char buff[100], *ptr;
- int cnt;
- FILE *fp;
- int i;
- fp = fopen(fname, "r");
- if(fp == NULL)
- {
- printf("\nUnable to open file");
- exit(0);
- }
- switch(option[0])
- {
- case 'a' :
- while(!feof(fp))
- {
- buff[0] = '\0';
- fgets(buff,80,fp);
- ptr = strstr(buff,pattern);
- if(ptr != NULL)
- printf("%s", buff);
- }
- fclose(fp);
- break;
- case 'c' :
- cnt = 0;
- buff[0] = '\0';
- fgets(buff,80,fp);
- ptr = strstr(buff,pattern);
- while(ptr != NULL)
- {
- cnt++;
- ptr = ptr + strlen(pattern);
- ptr = strstr(ptr, pattern);
- }
- fclose(fp);
- printf("\nThe search %s occurs %d times ",pattern ,cnt);
- break;
- case 'f' :
- while(!feof(fp))
- {
- buff[0] = '\0';
- fgets(buff,80,fp);
- ptr = strstr(buff,pattern);
- if(ptr != NULL)
- {
- printf("%s", buff);
- break;
- }
- }
- fclose(fp);
- break;
- }
- return;
- }
//
/*Output
*/
Comments
Post a Comment