Skip to main content

Assignment No-2

 //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:

πŸ‘‡ 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <wait.h>
  7. char comm[100],*ptr;
  8. char *args[10];
  9. int tot;

  10. int count(char *option, char *fname);
  11. void getcomm();
  12. void sep_arg();
  13. void take_action();
  14. void main()
  15. {
  16. do
  17. {
  18. printf("\nCodingLab");
  19. getcomm();
  20. sep_arg();
  21. take_action();
  22. } while(1);
  23. }

  24. void sep_arg()
  25. {
  26. int i, j;
  27. char *token;
  28. i = j = tot = 0;
  29. token = strtok(comm," ");
  30. while(token != NULL)
  31. {
  32. args[tot] = (char*)malloc(20);
  33. strcpy(args[tot++],token);
  34. token=strtok(NULL," ");
  35. }
  36. return;
  37. }

  38. void getcomm()
  39. {
  40. int len;
  41. ptr = fgets(comm,80,stdin);
  42. len = strlen(comm);
  43. comm[len-1] = "\0";
  44. return;
  45. }

  46. void take_action()
  47. {
  48. char str[100];
  49. pid_t pid;
  50. int status;

  51. ptr = comm;
  52. if(ptr == NULL)
  53. exit(0);
  54. if(strlen(comm) <= 1)
  55. return;
  56. if(strcmp(args[0],"count") == 0)
  57. count(args[1],args[2]);
  58. else
  59. if(pid = fork() < 0)
  60. {
  61. printf("\nUnable to create process");
  62. }
  63. else
  64. if(pid == 0)//child
  65. {
  66. execvp(args[0],args);
  67. strcpy(str,"./");
  68. strcat(str,args[0]);
  69. execvp(str,args);
  70. printf("\n%s: Command Not Found ");
  71. exit(0);
  72. }
  73. else
  74. waitpid(pid,&status,0);
  75. }

  76. int count(char *option, char *fname)
  77. {
  78. int ccnt,wcnt,lcnt;
  79. int ch;
  80. FILE *fp;
  81. int i;
  82. fp = fopen(fname,"r");
  83. if(fp==NULL)
  84. {
  85. printf("\nUnable to open file");
  86. exit(0);
  87. }
  88. ccnt=wcnt=lcnt=0;
  89. ch = fgetc(fp);
  90. while(ch != EOF)
  91. {
  92. ccnt++;
  93. if(ch == '' || ch == '\t')
  94. {
  95. wcnt++;
  96. }
  97. else
  98. if(ch == '\n')
  99. {
  100. lcnt++;
  101. wcnt++;
  102. }
  103. ch = fgetc(fp);
  104. }
  105. fclose(fp);
  106. if(strcmp(option,"c") == 0)
  107. {
  108. printf("\nTotal chars in the file:\t%d",ccnt);
  109. }
  110. else
  111. if(strcmp(option,"w") == 0)
  112. {
  113. printf("\nTotal words in the file:\t%d",wcnt);
  114. }
  115. else
  116. if(strcmp(option,"l") == 0)
  117. {
  118. printf("\nTotal lines in the file:\t%d",lcnt);
  119. }
  120. else
  121. printf("\nInvalid Option");
  122. }

//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:
πŸ‘‡
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <wait.h>
  7. #include <dirent.h>
  8. //Programs for create own shell
  9. char comm[100],*ptr;
  10. char *args[10];
  11. int tot;

  12. //function declear 
  13. void list(char *option, char *dirname);
  14. void getcomm();
  15. void sep_arg();
  16. void take_action();

  17. void main()//main fuction
  18. {
  19. do
  20. {
  21. printf("\nCodingLab$");//enter which name you want to give shell
  22. getcomm();
  23. sep_arg();
  24. take_action();
  25. }while(1);
  26. }

  27. void sep_arg()
  28. {
  29. int i, j;
  30. char *token;
  31. i = j = tot =0;
  32. token = strtok(comm," ");
  33. while(token != NULL)
  34. {
  35. args[tot] = (char*)malloc(20);
  36. strcpy(args[tot++],token);
  37. token = strtok(NULL, " ");
  38. }
  39. for(i=0; i<tot; i++)
  40. {
  41. printf("%s",args[i]);
  42. }
  43. return;
  44. }
  45. void getcomm()
  46. {
  47. int len;
  48. ptr = fgets(comm, 80,stdin);
  49. len = strlen(comm);
  50. comm[len-1] = '\0';
  51. return;
  52. }

  53. void take_action()
  54. {
  55. char  str[100];
  56. pid_t  pid;
  57. int status;

  58. ptr = comm;
  59. if(ptr == NULL)
  60. exit(0);
  61. if( strlen(comm) <= 1)
  62. return;
  63. if(strcmp(args[0],"list") == 0)
  64. list(args[1],args[2]);
  65. else
  66. if((pid = fork() < 0))
  67. {
  68. printf("\n Unable to create process");
  69. }
  70. else
  71. if(pid == 0)//child
  72. {
  73. execvp(args[0],args);
  74. strcpy(str,"./");
  75. strcat(str,args[0]);
  76. execvp(str,args);
  77. printf("\n%s: Command not found", comm);
  78. exit(0);
  79. }
  80. else
  81. waitpid(pid,&status,0);
  82. }

  83. void list(char *option, char *dirname)
  84. {
  85. DIR *dp;
  86. struct dirent *dent;
  87. int cnt;
  88. if(strcmp(option, "-f")==0)
  89. {
  90. //list all  dir entries
  91. dp = opendir(dirname);
  92. if(dp == NULL)
  93. {
  94. printf("\nUnable to open directory\n");
  95. exit(0);
  96. }
  97. dent = readdir(dp);
  98. while(dent != NULL)
  99. {
  100. printf("\n%s", dent->d_name);
  101. dent = readdir(dp);
  102. }
  103. printf("\n");
  104. }
  105. else
  106. if(strcmp(option, "-n")==0)
  107. {
  108. //list number all dir entries
  109. cnt = 0;
  110. dp = opendir(dirname);
  111. if(dp == NULL)
  112. {
  113. printf("\nUnable to open dir");
  114. exit(0);
  115. }
  116. dent = readdir(dp);
  117. while(dent != NULL)
  118. {
  119. cnt++;
  120. dent = readdir(dp);
  121. }
  122. printf("\nTotal dir entries :\t %d\n",cnt);
  123. closedir(dp);
  124. }
  125. else
  126. if(strcmp(option, "-i")==0)
  127. {
  128. //list all dir entries with inode
  129. dp = opendir(dirname);
  130. if(dp == NULL)
  131. {
  132. printf("\nUnable to open dir");
  133. exit(0);
  134. }
  135. dent = readdir(dp);
  136. while(dent != NULL)
  137. {
  138. printf("\n%s %ld",dent->d_name,dent->d_ino);
  139. dent = readdir(dp);
  140. }
  141. printf("\n");
  142. closedir(dp);
  143. }
  144. else
  145. printf("\n>>Invalid Option<<\n");
  146. }


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:
πŸ‘‡
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <wait.h>
  7. //Programs for create own shell
  8. char comm[100],*ptr;
  9. char *args[10];
  10. int tot;

  11. //function declear 
  12. void typeline(char *option, char *fname);
  13. void getcomm();
  14. void sep_arg();
  15. void take_action();

  16. void main()//main fuction
  17. {
  18. do
  19. {
  20. printf("\nCodingLab$");//enter which name you want to give shell
  21. getcomm();
  22. sep_arg();
  23. take_action();
  24. }while(1);
  25. }

  26. void sep_arg()
  27. {
  28. int i, j;
  29. char *token;
  30. i = j = tot =0;
  31. token = strtok(comm," ");
  32. while(token != NULL)
  33. {
  34. args[tot] = (char*)malloc(20);
  35. strcpy(args[tot++],token);
  36. token = strtok(NULL, " ");
  37. }
  38. for(i=0; i<tot; i++)
  39. {
  40. printf("%s",args[i]);
  41. }
  42. return;
  43. }
  44. void getcomm()
  45. {
  46. int len;
  47. ptr = fgets(comm, 80,stdin);
  48. len = strlen(comm);
  49. comm[len-1] = '\0';
  50. return;
  51. }

  52. void take_action()
  53. {
  54. char  str[100];
  55. pid_t  pid;
  56. int status;

  57. ptr = comm;
  58. if(ptr == NULL)
  59. exit(0);
  60. if( strlen(comm) <= 1)
  61. return;
  62. if(strcmp(args[0],"typeline") == 0)
  63. typeline(args[1],args[2]);
  64. else
  65. if((pid = fork() < 0))
  66. {
  67. printf("\n Unable to create process");
  68. }
  69. else
  70. if(pid == 0)//child
  71. {
  72. execvp(args[0],args);
  73. strcpy(str,"./");
  74. strcat(str,args[0]);
  75. execvp(str,args);
  76. printf("\n%s: Command not found", comm);
  77. exit(0);
  78. }
  79. else
  80. waitpid(pid,&status,0);
  81. }

  82. void typeline(char *option, char *fname)
  83. {
  84. FILE *fp;
  85. char str[80];
  86. int tot_lines,ch,n,cnt;
  87. //count the lines file
  88. fp = fopen(fname, "r");
  89. if(fp == NULL)
  90. {
  91. printf("\nUnable to Open file");
  92. return;
  93. }
  94. tot_lines = 0;
  95. while(!feof(fp))
  96. {
  97. fgets(str,80,fp);
  98. tot_lines++;
  99. }
  100. fclose(fp);
  101. if(strcmp(option, "a")==0)
  102. {
  103. fp=fopen(fname,"r");
  104. while(!feof(fp))
  105. {
  106. fgets(str,80,fp);
  107. if(feof(fp))
  108. break;
  109. printf("%s",str);
  110. }
  111. fclose(fp);
  112. }
  113. else
  114. if(option[0]== '+')//print first n line
  115. {
  116. n = atol(option+1);
  117. if(n > tot_lines)
  118. {
  119. printf("\ninvalid number of lines");
  120. return;
  121. }
  122. fp=fopen(fname,"r");
  123. cnt = 0;
  124. while(cnt < n)
  125. {
  126. fgets(str,80,fp); if(feof(fp)) break;
  127. printf("%s",str);
  128. cnt++;
  129. }
  130. fclose(fp);
  131. }
  132. else
  133. if(option[0]== '-')//print last n lines
  134. {
  135. n = atoi(option+1);
  136. if(n > tot_lines)
  137. {
  138. printf("\ninvalid number of lines");
  139. return;
  140. }
  141. fp = fopen(fname,"r");
  142. cnt = 0;
  143. while(cnt < tot_lines - n)
  144. {
  145. fgets(str,80,fp); if(feof(fp)) break;
  146. cnt++;
  147. }
  148. while(!feof(fp))
  149. {
  150. fgets(str,80,fp); if(feof(fp)) break;
  151. printf("%s",str);
  152. cnt++;
  153. }
  154. fclose(fp);
  155. }
  156. else
  157. printf("\nInvalid options");
  158. }

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:
πŸ‘‡

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <wait.h>
  7. //Programs for create own shell
  8. char comm[100],*ptr;
  9. char *args[10];
  10. int tot;

  11. //function declear 
  12. void search(char *option, char *pattern, char *fname);
  13. void getcomm();
  14. void sep_arg();
  15. void take_action();

  16. void main()//main fuction
  17. {
  18. do
  19. {
  20. printf("\nCodingLab$");//enter which name you want to give shell
  21. getcomm();
  22. sep_arg();
  23. take_action();
  24. }while(1);
  25. }

  26. void sep_arg()
  27. {
  28. int i, j;
  29. char *token;
  30. i = j = tot =0;
  31. args[tot] = (char*)malloc(sizeof(char)*20);
  32. for(i=0; comm[i] != '\0'; i++)
  33. {
  34. if(comm[i] == ' ')
  35. {
  36. args[tot][j] = '\0';
  37. tot++;
  38. args[tot] = (char*)malloc(sizeof(char)*20);
  39. j=0;
  40. }
  41. else
  42. {
  43. args[tot][j] = comm[i];
  44. j++;
  45. }
  46. }//for
  47. args[tot][j] = '\0';
  48. return;
  49. }
  50. void getcomm()
  51. {
  52. int len;
  53. ptr = fgets(comm, 80,stdin);
  54. len = strlen(comm);
  55. comm[len-1] = '\0';
  56. return;
  57. }

  58. void take_action()
  59. {
  60. char  str[100];
  61. pid_t  pid;
  62. int status;

  63. ptr = comm;
  64. if(ptr == NULL)
  65. exit(0);
  66. if( strlen(comm) <= 1)
  67. return;
  68. if(strcmp(args[0],"search") == 0)
  69. search(args[1],args[2],args[3]);
  70. else
  71. if((pid = fork() < 0))
  72. {
  73. printf("\n Unable to create process");
  74. }
  75. else
  76. if(pid == 0)//child
  77. {
  78. execvp(args[0],args);
  79. strcpy(str,"./");
  80. strcat(str,args[0]);
  81. execvp(str,args);
  82. printf("\n%s: Command not found", comm);
  83. exit(0);
  84. }
  85. else
  86. waitpid(pid,&status,0);
  87. }

  88. void search(char *option, char *pattern, char *fname)
  89. {
  90. char buff[100], *ptr;
  91. int cnt;
  92. FILE *fp;
  93. int i;
  94. fp = fopen(fname, "r");
  95. if(fp == NULL)
  96. {
  97. printf("\nUnable to open file");
  98. exit(0);
  99. }
  100. switch(option[0])
  101. {
  102. case 'a' :
  103. while(!feof(fp))
  104. {
  105. buff[0] = '\0';
  106. fgets(buff,80,fp);
  107. ptr = strstr(buff,pattern);
  108. if(ptr != NULL)
  109. printf("%s", buff);
  110. }
  111. fclose(fp);
  112. break;
  113. case 'c' :
  114. cnt = 0;
  115. buff[0] = '\0';
  116. fgets(buff,80,fp);
  117. ptr = strstr(buff,pattern);
  118. while(ptr != NULL)
  119. {
  120. cnt++;
  121. ptr = ptr + strlen(pattern);
  122. ptr = strstr(ptr, pattern);
  123. }
  124. fclose(fp);
  125. printf("\nThe search %s occurs %d times ",pattern ,cnt);
  126. break;
  127. case 'f' :
  128. while(!feof(fp))
  129. {
  130. buff[0] = '\0';
  131. fgets(buff,80,fp);
  132. ptr = strstr(buff,pattern);
  133. if(ptr != NULL)
  134. {
  135. printf("%s", buff);
  136. break;
  137. }
  138. }
  139. fclose(fp);
  140. break;
  141. }
  142. return;
  143. }
//
/*Output

*/

Comments