#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COMMAND_LENGTH 100
#define MAX_ARGUMENTS 10
void executeCommand(char* command) {
int i = 0;
char* args[MAX_ARGUMENTS];
char* token = strtok(command, " \t\n");
while (token != NULL) {
args[i++] = token;
token = strtok(NULL, " \t\n");
}
args[i] = NULL;
if (strcmp(args[0], "typeline") == 0) {
if (args[1] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
return;
}
FILE* file = fopen(args[2], "r");
if (file == NULL) {
printf("Error: Failed to open the file '%s'.\n", args[2]);
return;
}
char line[256];
int lineCount = 0;
if (strcmp(args[1], "+") == 0) {
if (args[2] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
fclose(file);
return;
}
int numLines = atoi(args[2]);
while (fgets(line, sizeof(line), file) != NULL && lineCount < numLines) {
printf("%s", line);
lineCount++;
}
} else if (strcmp(args[1], "-") == 0) {
if (args[2] == NULL) {
printf("Error: Insufficient arguments for typeline command.\n");
fclose(file);
return;
}
int numLines = atoi(args[2]);
int totalLines = 0;
while (fgets(line, sizeof(line), file) != NULL) {
totalLines++;
}
fseek(file, 0, SEEK_SET);
int startLine = totalLines - numLines;
lineCount = 0;
while (fgets(line, sizeof(line), file) != NULL && lineCount < totalLines) {
if (lineCount >= startLine) {
printf("%s", line);
}
lineCount++;
}
} else if (strcmp(args[1], "a") == 0) {
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
} else {
printf("Error: Invalid typeline command.\n");
}
fclose(file);
} else {
pid_t pid = fork();
if (pid < 0) {
printf("Error: Failed to fork.\n");
return;
} else if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
printf("Error: Failed to execute the command '%s'.\n", args[0]);
}
exit(0);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
if (fgets(command, sizeof(command), stdin) == NULL) {
printf("\n");
break;
}
executeCommand(command);
}
return 0;
}
count
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_COMMAND_LENGTH 100
void executeCommand(char *command) {
// Tokenize the command
char *token = strtok(command, " \t\n");
// Check for special commands
if (strcmp(token, "count") == 0) {
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("Missing file name for 'count' command\n");
return;
}
FILE *file = fopen(token, "r");
if (file == NULL) {
printf("File not found: %s\n", token);
return;
}
char ch;
int charCount = 0, wordCount = 0, lineCount = 0;
int insideWord = 0;
while ((ch = fgetc(file)) != EOF) {
charCount++;
if (ch == ' ' || ch == '\t' || ch == '\n') {
insideWord = 0;
} else if (insideWord == 0) {
insideWord = 1;
wordCount++;
}
if (ch == '\n') {
lineCount++;
}
}
fclose(file);
token = strtok(NULL, " \t\n");
if (token != NULL) {
if (strcmp(token, "c") == 0) {
printf("Character count: %d\n", charCount);
} else if (strcmp(token, "w") == 0) {
printf("Word count: %d\n", wordCount);
} else if (strcmp(token, "l") == 0) {
printf("Line count: %d\n", lineCount);
} else {
printf("Invalid count option: %s\n", token);
}
} else {
printf("Missing count option for 'count' command\n");
}
} else {
pid_t pid = fork();
if (pid == -1) {
printf("Fork failed\n");
return;
} else if (pid == 0) {
// Child process
execlp(command, command, NULL);
printf("Command not found: %s\n", command);
exit(1);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
fgets(command, MAX_COMMAND_LENGTH, stdin);
// Remove newline character
command[strcspn(command, "\n")] = '\0';
if (strcmp(command, "exit") == 0) {
break;
}
executeCommand(command);
}
return 0;
}
list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAX_COMMAND_LENGTH 100
void executeCommand(char *command) {
// Tokenize the command
char *token = strtok(command, " \t\n");
// Check for special commands
if (token != NULL && strcmp(token, "list") == 0) {
token = strtok(NULL, " \t\n");
if (token == NULL) {
printf("Missing option for 'list' command\n");
return;
}
if (strcmp(token, "f") == 0) {
// Print name of all files in the directory
DIR *dir;
struct dirent *entry;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
} else if (strcmp(token, "n") == 0) {
// Print number of all entries in the directory
DIR *dir;
struct dirent *entry;
int count = 0;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
count++;
}
closedir(dir);
printf("Number of entries: %d\n", count);
} else if (strcmp(token, "i") == 0) {
// Print name and inode of all files in the directory
DIR *dir;
struct dirent *entry;
struct stat fileStat;
dir = opendir(".");
if (dir == NULL) {
printf("Unable to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
if (stat(entry->d_name, &fileStat) == 0) {
printf("Name: %s, Inode: %ld\n", entry->d_name, fileStat.st_ino);
}
}
}
closedir(dir);
} else {
printf("Invalid option for 'list' command\n");
}
} else {
pid_t pid = fork();
if (pid == -1) {
printf("Fork failed\n");
return;
} else if (pid == 0) {
// Child process
execlp(command, command, NULL);
printf("Command not found: %s\n", command);
exit(1);
} else {
// Parent process
wait(NULL);
}
}
}
int main() {
char command[MAX_COMMAND_LENGTH];
while (1) {
printf("NewShell$ ");
fgets(command, MAX_COMMAND_LENGTH, stdin);
// Remove newline character
command[strcspn(command, "\n")] = '\0';
if (strcmp(command, "exit") == 0) {
break;
}
executeCommand(command);
}
return 0;
}

0 Comments