-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslip25_2.c
More file actions
66 lines (66 loc) · 1.67 KB
/
slip25_2.c
File metadata and controls
66 lines (66 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_LINE 1024
#define MAX_TOKENS 64
void tokenize(char *line, char *tokens[]) {
int i = 0;
char *token = strtok(line, " \t\n");
while (token != NULL && i < MAX_TOKENS) {
tokens[i++] = token;
token = strtok(NULL, " \t\n");
}
tokens[i] = NULL;
}
void execute_command(char *tokens[]) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) { // Child process
execvp(tokens[0], tokens);
perror("execvp");
exit(1);
} else { // Parent process
wait(NULL);
}
}
void search_command(char *filename, char *pattern) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("fopen");
return;
}
char line[MAX_LINE];
while (fgets(line, MAX_LINE, fp) != NULL) {
if (strstr(line, pattern) != NULL) {
printf("%s", line);
break;
}
}
fclose(fp);
}
int main() {
char line[MAX_LINE];
char *tokens[MAX_TOKENS];
while (1) {
printf("myshell$ ");
fflush(stdout);
fgets(line, MAX_LINE, stdin);
tokenize(line, tokens);
if (strcmp(tokens[0], "search") == 0) {
if (tokens[1] != NULL && tokens[2] != NULL) {
search_command(tokens[1], tokens[2]);
} else {
printf("Usage: search <filename> <pattern>\n");
}
} else {
execute_command(tokens);
}
}
return 0;
}