Add a naive /bin/fgrep for testing pipes.

This commit is contained in:
Andreas Kling 2018-11-11 20:42:41 +01:00
parent d5d45d1088
commit 18e3ddf605
Notes: sideshowbarker 2024-07-19 16:11:03 +09:00
8 changed files with 56 additions and 8 deletions

View File

@ -20,7 +20,7 @@ ssize_t TTY::read(byte* buffer, size_t size)
if (nread == (ssize_t)m_buffer.size())
m_buffer.clear();
else {
dbgprintf("had %u, read %u\n", m_buffer.size(), nread);
kprintf("had %u, read %u\n", m_buffer.size(), nread);
ASSERT_NOT_REACHED();
}
return nread;

View File

@ -8,6 +8,7 @@ cp -v ../Userland/sh mnt/bin/sh
cp -v ../Userland/id mnt/bin/id
cp -v ../Userland/ps mnt/bin/ps
cp -v ../Userland/ls mnt/bin/ls
cp -v ../Userland/fgrep mnt/bin/fgrep
cp -v ../Userland/sleep mnt/bin/sleep
cp -v ../Userland/date mnt/bin/date
cp -v ../Userland/true mnt/bin/true

View File

@ -191,4 +191,23 @@ char* strsignal(int signum)
return const_cast<char*>(sys_siglist[signum]);
}
char* strstr(const char* haystack, const char* needle)
{
char nch;
char hch;
if ((nch = *needle++) != 0) {
size_t len = strlen(needle);
do {
do {
if ((hch = *haystack++) == 0)
return nullptr;
} while (hch != nch);
} while (strncmp(haystack, needle, len) != 0);
--haystack;
}
return const_cast<char*>(haystack);
}
}

View File

@ -17,6 +17,7 @@ void* memset(void*, int, size_t);
char* strcpy(char* dest, const char* src);
char* strncpy(char* dest, const char* src, size_t);
char* strchr(const char*, int c);
char* strstr(const char* haystack, const char* needle);
char* strrchr(const char*, int c);
char* strcat(char *dest, const char *src);
char* strncat(char *dest, const char *src, size_t);

View File

@ -23,13 +23,13 @@ int tgetent(char* bp, const char* name)
assert(false);
}
static HashMap<String, String>* caps = nullptr;
static HashMap<String, const char*>* caps = nullptr;
void ensure_caps()
{
if (caps)
return;
caps = new HashMap<String, String>;
caps = new HashMap<String, const char*>;
caps->set("DC", "\033[%p1%dP");
caps->set("IC", "\033[%p1%d@");
caps->set("ce", "\033[K");
@ -60,12 +60,13 @@ void ensure_caps()
char* tgetstr(char* id, char** area)
{
ensure_caps();
fprintf(stderr, "tgetstr: id='%s', area=%p\n", id, area);
fprintf(stderr, "tgetstr: id='%s', area=%p", id, area);
auto it = caps->find(id);
if (it != caps->end()) {
char* ret = *area;
strcpy(*area, (*it).value.characters());
*area += (*it).value.length() + 1;
const char* val = (*it).value;
strcpy(*area, val);
*area += strlen(val) + 1;
return ret;
}
assert(false);
@ -85,7 +86,7 @@ int tgetnum(char* id)
fprintf(stderr, "tgetnum: '%s'\n", id);
auto it = caps->find(id);
if (it != caps->end()) {
return atoi((*it).value.characters());
return atoi((*it).value);
}
assert(false);
}
@ -97,7 +98,7 @@ char* tgoto(const char* cap, int col, int row)
int tputs(const char* str, int affcnt, int (*putc)(int))
{
assert(false);
printf("%s", str);
}
}

1
Userland/.gitignore vendored
View File

@ -19,3 +19,4 @@ tty
ft
ft2
strsignal
fgrep

View File

@ -17,6 +17,7 @@ OBJS = \
ft.o \
ft2.o \
strsignal.o \
fgrep.o \
tty.o
APPS = \
@ -38,6 +39,7 @@ APPS = \
ft \
ft2 \
strsignal \
fgrep \
tty
ARCH_FLAGS =
@ -70,6 +72,9 @@ ps: ps.o
ls: ls.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
fgrep: fgrep.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
sleep: sleep.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a

20
Userland/fgrep.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if (argc < 2) {
printf("usage: fgrep <str>\n");
return 0;
}
for (;;) {
char buf[4096];
fgets(buf, sizeof(buf), stdin);
if (feof(stdin))
return 0;
if (strstr(buf, argv[1]))
write(1, buf, strlen(buf));
}
return 0;
}