This commit is contained in:
Your Name 2024-03-26 08:36:15 +03:00
parent 2a914caec3
commit 1552e3f553
4 changed files with 47 additions and 27 deletions

View File

@ -68,15 +68,22 @@ struct d_node *stat_file(char *filename, int lfile) {
}
struct d_node **list(const char *path, size_t *nfiles, int *ret) {
struct stat sb;
if (mu_get_stat("ls", path, &sb))
return NULL;
struct d_node **dir = malloc((sb.st_nlink + 2) * sizeof(struct d_node));
if (dir == NULL) {
fprintf(stderr, "ls: malloc failed\n");
exit(1);
}
DIR *dp = opendir(path);
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
return NULL;
}
struct d_node **dir, *cur, *dr = NULL;
size_t files = 0;
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
if (ep->d_name[0] == '.' && !a_flag)
@ -86,38 +93,17 @@ struct d_node **list(const char *path, size_t *nfiles, int *ret) {
if (full_path == NULL)
continue;
cur = stat_file(full_path, 0);
if (cur == NULL) {
dir[*nfiles] = stat_file(full_path, 0);
if (dir[*nfiles] == NULL) {
*ret = 1;
free(full_path);
continue;
}
cur->next = dr;
dr = cur;
files++;
(*nfiles)++;
}
closedir(dp);
if (dr == NULL)
return NULL;
*nfiles = files;
dir = malloc((files + 1) * sizeof(struct d_node *));
if (dir == NULL) {
fprintf(stderr, "ls: malloc failed\n");
exit(1);
}
for (size_t i = 0; ; i++) {
dir[i] = dr;
dr = dr->next;
if (dr == NULL)
break;
}
return dir;
}

3
src/findutils/grep/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
project_dir=$(pwd)
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC

30
src/findutils/grep/grep.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <regex.h>
typedef struct {
char *pattern;
regex_t rgex;
} PATTERN;
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "e:")) != -1) {
switch (opt) {
case 'e':
addpattern(optarg);
break;
default:
printf("grep [e] [PATTERN | -e PATTERN]\n");
return 0;
}
}
argv += optind;
argc -= optind;
return 0;
}

View File

@ -1,3 +1,4 @@
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>