Search
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // for close()
void make_toks(char *s, char *tok[])
int i = 0;
char *p;
p = strtok(s, " ");
while (p != NULL)
tok[i++] = p;
p = strtok(NULL, " ");
tok[i] = NULL;
void search(const char *fn, char op, const char *pattern)
{
int fh, count = 0, i = 0, j = 0;
char buff[255], c, *p;
fh = open(fn, O_RDONLY);
if (fh == -1)
perror("File not found");
return;
switch (op)
case 'f': // Find first occurrence of pattern
while (read(fh, &c, 1) > 0)
buff[j++] = c;
if (c == '\n')
buff[j] = '\0';
j = 0;
i++;
if (strstr(buff, pattern))
printf("%d: %s", i, buff);
break;
}
break;
case 'c': // Count occurrences of pattern
while (read(fh, &c, 1) > 0)
buff[j++] = c;
if (c == '\n')
buff[j] = '\0';
j = 0;
p = buff;
while ((p = strstr(p, pattern)) != NULL)
count++;
p++;
printf("Total occurrences = %d\n", count);
break;
case 'a': // Print all occurrences of pattern
while (read(fh, &c, 1) > 0)
buff[j++] = c;
if (c == '\n')
buff[j] = '\0';
j = 0;
i++;
if (strstr(buff, pattern))
printf("%d: %s", i, buff);
break;
default:
printf("Invalid option\n");
break;
close(fh);
int main()
char buff[80], *args[10];
int pid;
while (1)
printf("myshell$ ");
fflush(stdout); // flush output buffer
fgets(buff, sizeof(buff), stdin);
buff[strcspn(buff, "\n")] = 0; // safer newline removal
make_toks(buff, args);
if (strcmp(args[0], "search") == 0)
search(args[3], args[1][0], args[2]);
else
pid = fork();
if (pid > 0)
wait(NULL); // wait for child process
else if (pid == 0)
execvp(args[0], args);
perror("execvp failed");
exit(EXIT_FAILURE);
else
perror("fork failed");
return 0;
}
List
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h> // for fork() and wait()
void make_toks(char *s, char *tok[])
int i = 0;
char *p;
p = strtok(s, " ");
while (p != NULL)
tok[i++] = p;
p = strtok(NULL, " ");
tok[i] = NULL;
}
void list(const char *dn, char op)
DIR *dp;
struct dirent *entry;
int dc = 0, fc = 0;
dp = opendir(dn);
if (dp == NULL)
perror("opendir");
return;
switch (op)
case 'f':
while ((entry = readdir(dp)) != NULL)
if (entry->d_type == DT_REG)
printf("%s\n", entry->d_name);
break;
case 'n':
while ((entry = readdir(dp)) != NULL)
if (entry->d_type == DT_DIR) dc++;
if (entry->d_type == DT_REG) fc++;
printf("%d Dir(s)\t%d File(s)\n", dc, fc);
break;
case 'i':
while ((entry = readdir(dp)) != NULL)
if (entry->d_type == DT_REG)
printf("%s\t%d\n", entry->d_name, entry->d_fileno);
break;
default:
printf("Invalid option\n");
break;
closedir(dp);
int main()
char buff[80], *args[10];
int pid;
while (1)
printf("myshell$ ");
fflush(stdout); // changed from fflush(stdin) to fflush(stdout) for output buffer
fgets(buff, sizeof(buff), stdin);
buff[strcspn(buff, "\n")] = 0; // safer newline removal
make_toks(buff, args);
if (strcmp(args[0], "list") == 0)
list(args[2], args[1][0]);
else
pid = fork();
if (pid > 0)
wait(NULL); // wait for the child process
else if (pid == 0)
execvp(args[0], args);
perror("execvp"); // Print error if execvp fails
exit(EXIT_FAILURE); // Exit child process if execvp fails
else
perror("fork"); // Print error if fork fails
return 0;
}
Count
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // For fork(), execvp(), read(), close()
#include <sys/wait.h> // For wait()
void make_toks(char *s, char *tok[])
int i = 0;
char *p;
p = strtok(s, " ");
while (p != NULL)
tok[i++] = p;
p = strtok(NULL, " ");
tok[i] = NULL;
void count(char *fn, char op)
{
int fh, cc = 0, wc = 0, lc = 0;
char c;
int in_word = 0;
fh = open(fn, O_RDONLY);
if (fh == -1)
printf("File %s not found.\n", fn);
return;
while (read(fh, &c, 1) > 0)
cc++;
if (c == ' ' || c == '\n' || c == '\t')
if (in_word)
wc++;
in_word = 0;
if (c == '\n')
lc++;
else
{
in_word = 1;
// If the last character is not whitespace, count the last word.
if (in_word)
wc++;
close(fh);
switch (op)
case 'c':
printf("No. of characters: %d\n", cc);
break;
case 'w':
printf("No. of words: %d\n", wc);
break;
case 'l':
printf("No. of lines: %d\n", lc);
break;
default:
printf("Invalid option. Use 'c' for characters, 'w' for words, or 'l' for lines.\n");
break;
int main()
{
char buff[80], *args[10];
int pid;
while (1)
printf("myshell$ ");
fgets(buff, 80, stdin); // Get user input
buff[strlen(buff) - 1] = '\0'; // Remove the newline character at the end
make_toks(buff, args); // Tokenize the input
// If the input is 'count', handle it internally
if (strcmp(args[0], "count") == 0)
// Ensure correct number of arguments for 'count'
if (args[1] == NULL || args[2] == NULL)
printf("Usage: count [option] [filename]\n");
else
count(args[2], args[1][0]);
else
pid = fork(); // Create a child process
if (pid > 0)
wait(NULL); // Parent waits for the child process
else if (pid == 0)
// Execute external command in the child process
if (execvp(args[0], args) == -1)
printf("Bad command.\n");
exit(1); // Exit with error in the child process
else
printf("Fork failed.\n");
return 0;
Typeline
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // For fork(), execvp(), read(), close()
#include <sys/wait.h> // For wait()
void make_toks(char *s, char *tok[])
int i = 0;
char *p;
p = strtok(s, " ");
while (p != NULL)
tok[i++] = p;
p = strtok(NULL, " ");
tok[i] = NULL;
void typeline(char *fn, char *op)
int fh, i, j, n;
char c;
fh = open(fn, O_RDONLY);
if (fh == -1)
printf("File %s not found.\n", fn);
return;
// If option is "a", print the entire file
if (strcmp(op, "a") == 0)
while (read(fh, &c, 1) > 0)
printf("%c", c);
close(fh);
return;
// Convert the option to an integer
n = atoi(op);
if (n > 0)
// Print the first 'n' lines of the file
i = 0;
while (read(fh, &c, 1) > 0)
printf("%c", c);
if (c == '\n')
i++;
if (i == n)
break;
}
else if (n < 0)
// Count total lines first
i = 0;
while (read(fh, &c, 1) > 0)
if (c == '\n')
i++;
// Reset file pointer to the beginning
lseek(fh, 0, SEEK_SET);
// Skip lines until reaching the point to start printing
j = 0;
while (read(fh, &c, 1) > 0)
if (c == '\n')
j++;
if (j == i + n)
break;
// Print the rest of the file
while (read(fh, &c, 1) > 0)
{
printf("%c", c);
close(fh);
int main()
char buff[80], *args[10];
int pid;
while (1)
printf("myshell$ ");
fgets(buff, 80, stdin); // Get user input
buff[strlen(buff) - 1] = '\0'; // Remove the newline character at the end
make_toks(buff, args); // Tokenize the input
// If the input is 'typeline', handle it internally
if (strcmp(args[0], "typeline") == 0)
// Ensure correct number of arguments
if (args[1] == NULL || args[2] == NULL)
printf("Usage: typeline [option] [filename]\n");
}
else
typeline(args[2], args[1]);
else
pid = fork(); // Create a child process
if (pid > 0)
wait(NULL); // Parent waits for the child process
else if (pid == 0)
// Execute external command in the child process
if (execvp(args[0], args) == -1)
printf("Bad command.\n");
exit(1); // Exit with error in the child process
else
printf("Fork failed.\n");
}
return 0;