fixed grep

This commit is contained in:
Your Name 2025-02-04 22:07:33 +03:00
parent c4f4bf3ca3
commit ec265a4c23
9 changed files with 148 additions and 146 deletions

1
TODO
View File

@ -26,6 +26,7 @@ getty
insmod
rmmod
lsmod
grep
find
FIX:

View File

@ -8,11 +8,6 @@
#define MUTILS_PATH "/bin/mutils"
#endif
#ifdef _SHRED_C
/* source of random (shred) */
#define RAND_SOURCE "/dev/urandom"
#endif
#ifdef _HEAD_C
/* format for printf (head) */
#define HEAD_FMT "==> %s <==\n"

30
libmu/replace.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
char *mu_replace(const char *prog_name, const char *src, const char *str, const char *fmt) {
size_t src_len = strlen(src);
size_t str_len = strlen(str);
size_t fmt_len = strlen(fmt);
char *buf = malloc(src_len - fmt_len + str_len + 1);
if (buf == NULL) {
if (prog_name)
fprintf(stderr, "%s: malloc failed: %s\n", prog_name, strerror(errno));
return NULL;
}
strcpy(buf, src);
/* REPLACE */
char *ptr = strstr(buf, fmt);
if (ptr == NULL)
return NULL;
memmove(ptr + str_len, ptr + fmt_len, strlen(ptr + fmt_len) + 1);
strncpy(ptr, str, str_len);
return buf;
}

6
libmu/replace.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _MU_REPLACE_H
#define _MU_REPLACE_H
char *mu_replace(const char *prog_name, const char *src, const char *str, const char *fmt);
#endif

View File

@ -175,9 +175,11 @@ static int grep(FILE *fp, const char *file) {
char match = 0;
size_t i = 0;
for (; i < ptrns_size; i++) {
if (F_flag && cmp(buf, ptrns[i]->str)) {
match = 1;
break;
if (F_flag) {
if (cmp(buf, ptrns[i]->str)) {
match = 1;
break;
}
}
else if (regexec(&ptrns[i]->reg, buf, 1, &m, 0) == 0) {

View File

@ -230,6 +230,9 @@ static int col_print(struct d_node **node, const size_t files, const struct wins
if (s_flag)
maxlen += 10;
if (F_flag)
maxlen++;
size_t ncols = w.ws_col / maxlen;
size_t nrows = files;
if (ncols > 1) {

89
src/sh.c Normal file
View File

@ -0,0 +1,89 @@
/*#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "replace.h"
enum {
NONE,
QUOTE
};
typedef {
char **argv;
size_t argc;
int stdin;
int stdout;
int stderr;
};
char **parse(char **str, size_t *size) {
size_t count = 1;
char *p = *str;
while (*p) {
if (*p == ' ')
count++;
p++;
}
char **tok = malloc((count + 1) * sizeof(char *));
if (tok == NULL) {
fprintf(stderr, "sh: malloc: %s\n", strerror(errno));
return NULL;
}
*size = 0;
p = strtok(*str, " \t\n\r\a");
while (p != NULL) {
tok[*size] = strdup(p);
(*size)++;
p = strtok(NULL, " \t\n\r\a");
}
tok[*size] = NULL;
return tok;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "l")) != -1) {
switch (opt) {
case 'l':
break;
default:
puts("sh [l]\n\t-l Login");
break;
}
}
int ret = 1;
char *string = NULL;
size_t n = 0;
ssize_t bytes = 0;
while ((bytes = getline(&string, &n, stdin)) > 0) {
size_t size = 0;
char **tok = parse(&string, &size);
for (size_t i = 0; i < size; i++)
puts(tok[i]);
clear_parser(tok);
}
ret = 0;
CLOSE:
if (string)
free(string);
return ret;
}
*/

View File

@ -1,113 +0,0 @@
#define _SHRED_C
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "config.h"
static char f_flag;
static char u_flag;
static char z_flag;
static int n_loops = 3;
static int shred(int rand_fd, int fd) {
/* Get size */
off_t size = lseek(fd, 0, SEEK_END);
if (size <= 0)
return 1;
void *buf = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
fprintf(stderr, "shred: mmap: %s\n", strerror(errno));
return 1;
}
for (char n = 0; n < n_loops; n++) {
if (read(rand_fd, buf, size) == 0)
fprintf(stderr, "shred: %s", strerror(errno));
fsync(fd);
}
if (z_flag)
memset(buf, 0, size);
if (munmap(buf, size)) {
fprintf(stderr, "shred: munmap: %s\n", strerror(errno));
return 1;
}
return 0;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "fuzn:")) != -1) {
switch (opt) {
case 'f':
f_flag = 1;
break;
case 'u':
u_flag = 1;
break;
case 'z':
z_flag = 1;
break;
case 'n':
n_loops = atoi(optarg);
break;
default:
puts("shred [fuzn] [file1 file2...]\n\t-n N Overwrite N times, default 3\n\t-z Final overwrite with zeros\n\t-u Remove file\n\t-f Chmod to ensure writability");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
fprintf(stderr, "shred: missing operand\n");
return 1;
}
int rand_fd = open(RAND_SOURCE, O_RDONLY);
if (rand_fd < 0) {
fprintf(stderr, "shred: %s: %s\n", RAND_SOURCE, strerror(errno));
return 1;
}
for (int i = 0; i < argc; i++) {
int fd = open(argv[i], O_RDWR);
if (fd < 0) {
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
continue;
}
shred(rand_fd, fd);
fsync(fd);
close(fd);
if (f_flag) {
if (chmod(argv[i], 0) < 0)
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
}
if (u_flag) {
if (unlink(argv[i]) < 0)
fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno));
}
}
close(rand_fd);
return 0;
}

View File

@ -8,6 +8,7 @@
#include <limits.h>
#include <ctype.h>
#include "parse_escape.h"
#include "replace.h"
#ifndef ARG_MAX
#define ARG_MAX 10000
@ -48,29 +49,6 @@ static void clear_cmd(void) {
args = 0;
}
static int Iflag_push(const char *str, char **arg) {
size_t arg_len = strlen(*arg);
size_t str_len = strlen(str);
size_t Iflag_len = strlen(I_flag);
char *buf = malloc(arg_len - Iflag_len + str_len + 1);
if (buf == NULL) {
fprintf(stderr, "xargs: malloc failed\n");
return 1;
}
strcpy(buf, *arg);
/* Replace */
char *ptr = strstr(buf, I_flag);
memmove(ptr + str_len, ptr + Iflag_len, strlen(ptr + Iflag_len) + 1);
strncpy(ptr, str, str_len);
free(*arg);
*arg = buf;
return 0;
}
static int add_arg(const char *str, size_t chars, int flag) {
if (args >= ARG_MAX)
return ERROR;
@ -83,10 +61,21 @@ static int add_arg(const char *str, size_t chars, int flag) {
if (!flag && I_flag) {
for (int i = 0; i < args; i++)
if (strstr(cmd[i], I_flag))
if (Iflag_push(str, &cmd[i]))
if (strstr(cmd[i], I_flag)) {
char *ptr = NULL;
CONTINUE:
/* Ptr will be freed in the clear_cmd() */
ptr = mu_replace("xargs", cmd[i], str, I_flag);
free(cmd[i]);
if (ptr == NULL)
return ERROR;
cmd[i] = ptr;
if (strstr(cmd[i], I_flag))
goto CONTINUE;
}
return I_FLAG;
}