add: grep
This commit is contained in:
parent
61889f25be
commit
f981262907
@ -10,6 +10,7 @@
|
|||||||
#define TYPE_DIRECTORY 3
|
#define TYPE_DIRECTORY 3
|
||||||
#define TYPE_SYMBOLIC_LINK 4
|
#define TYPE_SYMBOLIC_LINK 4
|
||||||
|
|
||||||
|
#define COLOR_RED "\e[0;31m"
|
||||||
#define COLOR_BLUE "\x1b[34m"
|
#define COLOR_BLUE "\x1b[34m"
|
||||||
#define COLOR_GREEN "\x1b[32m"
|
#define COLOR_GREEN "\x1b[32m"
|
||||||
#define ESCAPE_COLOR "\x1b[0m"
|
#define ESCAPE_COLOR "\x1b[0m"
|
||||||
|
|||||||
69
src/disk.c
69
src/disk.c
@ -525,11 +525,10 @@ char *read_in_file(disk *d, int file_index) {
|
|||||||
int do_cat(disk *disk, char *path_to_file) {
|
int do_cat(disk *disk, char *path_to_file) {
|
||||||
|
|
||||||
if (path_to_file == NULL) {
|
if (path_to_file == NULL) {
|
||||||
char buffer[4096];
|
char buffer[1024];
|
||||||
ssize_t bytes_read;
|
|
||||||
|
|
||||||
while ((bytes_read = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
|
while (fgets(buffer, 1024, stdin)) {
|
||||||
write(STDOUT_FILENO, buffer, bytes_read);
|
dprintf(STDOUT_FILENO, "%s", buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,6 +556,68 @@ int do_cat(disk *disk, char *path_to_file) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int *find_all_occ_of_pattern(char *pattern, char *txt) {
|
||||||
|
int nbr_occ = 0;
|
||||||
|
int *idx_occs = NULL;
|
||||||
|
int pat_len = strlen(pattern);
|
||||||
|
int txt_len = strlen(txt);
|
||||||
|
if (pat_len == 0)
|
||||||
|
return NULL;
|
||||||
|
for (int i = 0; i <= txt_len - pat_len; i++) {
|
||||||
|
if (strncmp(&txt[i], pattern, pat_len) == 0) {
|
||||||
|
nbr_occ++;
|
||||||
|
idx_occs = realloc(idx_occs, (nbr_occ + 1) * sizeof(int));
|
||||||
|
if (idx_occs == NULL)
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
idx_occs[nbr_occ - 1] = i;
|
||||||
|
idx_occs[nbr_occ] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return idx_occs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int do_grep(char *pattern) {
|
||||||
|
char buffer[1024];
|
||||||
|
if (pattern == NULL) {
|
||||||
|
dprintf(STDERR_FILENO, "grep: pattern missing\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ssize_t n;
|
||||||
|
while ((n = read(STDIN_FILENO, buffer, sizeof(buffer) - 1)) > 0) {
|
||||||
|
buffer[n] = '\0';
|
||||||
|
|
||||||
|
int *occs = find_all_occ_of_pattern(pattern, buffer);
|
||||||
|
|
||||||
|
if (occs == NULL || occs[0] == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nbr_occ = 0;
|
||||||
|
for (nbr_occ = 0; occs[nbr_occ] != -1; nbr_occ++);
|
||||||
|
|
||||||
|
int new_size = n + (nbr_occ * (strlen(COLOR_RED) + strlen(ESCAPE_COLOR)));
|
||||||
|
char *colored_line = malloc(new_size + 1);
|
||||||
|
|
||||||
|
int last_pos = 0;
|
||||||
|
colored_line[0] = '\0';
|
||||||
|
|
||||||
|
for (int i = 0; occs[i] != -1; i++) {
|
||||||
|
int pos = occs[i];
|
||||||
|
strncat(colored_line, buffer + last_pos, pos - last_pos);
|
||||||
|
strcat(colored_line, COLOR_RED);
|
||||||
|
strncat(colored_line, buffer + pos, strlen(pattern));
|
||||||
|
strcat(colored_line, ESCAPE_COLOR);
|
||||||
|
|
||||||
|
last_pos = pos + strlen(pattern);
|
||||||
|
}
|
||||||
|
strcat(colored_line, buffer + last_pos);
|
||||||
|
write(STDOUT_FILENO, colored_line, strlen(colored_line));
|
||||||
|
free(occs);
|
||||||
|
free(colored_line);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Créer un fichier "disk" de taille MAX_BYTES_PER_BLOC * MAX_BLOCS,
|
* @brief Créer un fichier "disk" de taille MAX_BYTES_PER_BLOC * MAX_BLOCS,
|
||||||
* renvoi ce disque
|
* renvoi ce disque
|
||||||
|
|||||||
@ -21,5 +21,6 @@ int do_df(disk *d);
|
|||||||
int do_rm(disk *disk, char *filepath);
|
int do_rm(disk *disk, char *filepath);
|
||||||
int do_rmdir(disk *disk, char *filepath);
|
int do_rmdir(disk *disk, char *filepath);
|
||||||
int do_cat(disk *disk, char* path_to_file);
|
int do_cat(disk *disk, char* path_to_file);
|
||||||
|
int do_grep(char* arg);
|
||||||
int find_dir_inode_by_name(char *name, int dir_index, disk *d);
|
int find_dir_inode_by_name(char *name, int dir_index, disk *d);
|
||||||
void persist_on_disk(disk* d);
|
void persist_on_disk(disk* d);
|
||||||
@ -6,9 +6,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
const char *cmd_list[] = {"ls", "touch", "df", "rm", "rmdir",
|
|
||||||
"cat", "echo", "cd", 0};
|
|
||||||
|
|
||||||
int do_cd(disk *d, char *path) {
|
int do_cd(disk *d, char *path) {
|
||||||
char full_path[1024] = {0};
|
char full_path[1024] = {0};
|
||||||
|
|
||||||
@ -106,6 +103,8 @@ int exec_cmd_job(disk *d, char **args) {
|
|||||||
persist_on_disk(d);
|
persist_on_disk(d);
|
||||||
} else if (strcmp("cat", args[0]) == 0) {
|
} else if (strcmp("cat", args[0]) == 0) {
|
||||||
do_cat(d, args[1]);
|
do_cat(d, args[1]);
|
||||||
|
} else if(strcmp("grep", args[0]) == 0) {
|
||||||
|
do_grep(args[1]);
|
||||||
} else {
|
} else {
|
||||||
dprintf(STDERR_FILENO, "%s: command does not exist\n", args[0]);
|
dprintf(STDERR_FILENO, "%s: command does not exist\n", args[0]);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user