mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
Add a naive /bin/fgrep for testing pipes.
This commit is contained in:
parent
d5d45d1088
commit
18e3ddf605
8 changed files with 56 additions and 8 deletions
|
@ -20,7 +20,7 @@ ssize_t TTY::read(byte* buffer, size_t size)
|
||||||
if (nread == (ssize_t)m_buffer.size())
|
if (nread == (ssize_t)m_buffer.size())
|
||||||
m_buffer.clear();
|
m_buffer.clear();
|
||||||
else {
|
else {
|
||||||
dbgprintf("had %u, read %u\n", m_buffer.size(), nread);
|
kprintf("had %u, read %u\n", m_buffer.size(), nread);
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
return nread;
|
return nread;
|
||||||
|
|
|
@ -8,6 +8,7 @@ cp -v ../Userland/sh mnt/bin/sh
|
||||||
cp -v ../Userland/id mnt/bin/id
|
cp -v ../Userland/id mnt/bin/id
|
||||||
cp -v ../Userland/ps mnt/bin/ps
|
cp -v ../Userland/ps mnt/bin/ps
|
||||||
cp -v ../Userland/ls mnt/bin/ls
|
cp -v ../Userland/ls mnt/bin/ls
|
||||||
|
cp -v ../Userland/fgrep mnt/bin/fgrep
|
||||||
cp -v ../Userland/sleep mnt/bin/sleep
|
cp -v ../Userland/sleep mnt/bin/sleep
|
||||||
cp -v ../Userland/date mnt/bin/date
|
cp -v ../Userland/date mnt/bin/date
|
||||||
cp -v ../Userland/true mnt/bin/true
|
cp -v ../Userland/true mnt/bin/true
|
||||||
|
|
|
@ -191,4 +191,23 @@ char* strsignal(int signum)
|
||||||
return const_cast<char*>(sys_siglist[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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ void* memset(void*, int, size_t);
|
||||||
char* strcpy(char* dest, const char* src);
|
char* strcpy(char* dest, const char* src);
|
||||||
char* strncpy(char* dest, const char* src, size_t);
|
char* strncpy(char* dest, const char* src, size_t);
|
||||||
char* strchr(const char*, int c);
|
char* strchr(const char*, int c);
|
||||||
|
char* strstr(const char* haystack, const char* needle);
|
||||||
char* strrchr(const char*, int c);
|
char* strrchr(const char*, int c);
|
||||||
char* strcat(char *dest, const char *src);
|
char* strcat(char *dest, const char *src);
|
||||||
char* strncat(char *dest, const char *src, size_t);
|
char* strncat(char *dest, const char *src, size_t);
|
||||||
|
|
|
@ -23,13 +23,13 @@ int tgetent(char* bp, const char* name)
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static HashMap<String, String>* caps = nullptr;
|
static HashMap<String, const char*>* caps = nullptr;
|
||||||
|
|
||||||
void ensure_caps()
|
void ensure_caps()
|
||||||
{
|
{
|
||||||
if (caps)
|
if (caps)
|
||||||
return;
|
return;
|
||||||
caps = new HashMap<String, String>;
|
caps = new HashMap<String, const char*>;
|
||||||
caps->set("DC", "\033[%p1%dP");
|
caps->set("DC", "\033[%p1%dP");
|
||||||
caps->set("IC", "\033[%p1%d@");
|
caps->set("IC", "\033[%p1%d@");
|
||||||
caps->set("ce", "\033[K");
|
caps->set("ce", "\033[K");
|
||||||
|
@ -60,12 +60,13 @@ void ensure_caps()
|
||||||
char* tgetstr(char* id, char** area)
|
char* tgetstr(char* id, char** area)
|
||||||
{
|
{
|
||||||
ensure_caps();
|
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);
|
auto it = caps->find(id);
|
||||||
if (it != caps->end()) {
|
if (it != caps->end()) {
|
||||||
char* ret = *area;
|
char* ret = *area;
|
||||||
strcpy(*area, (*it).value.characters());
|
const char* val = (*it).value;
|
||||||
*area += (*it).value.length() + 1;
|
strcpy(*area, val);
|
||||||
|
*area += strlen(val) + 1;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
assert(false);
|
assert(false);
|
||||||
|
@ -85,7 +86,7 @@ int tgetnum(char* id)
|
||||||
fprintf(stderr, "tgetnum: '%s'\n", id);
|
fprintf(stderr, "tgetnum: '%s'\n", id);
|
||||||
auto it = caps->find(id);
|
auto it = caps->find(id);
|
||||||
if (it != caps->end()) {
|
if (it != caps->end()) {
|
||||||
return atoi((*it).value.characters());
|
return atoi((*it).value);
|
||||||
}
|
}
|
||||||
assert(false);
|
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))
|
int tputs(const char* str, int affcnt, int (*putc)(int))
|
||||||
{
|
{
|
||||||
assert(false);
|
printf("%s", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
1
Userland/.gitignore
vendored
1
Userland/.gitignore
vendored
|
@ -19,3 +19,4 @@ tty
|
||||||
ft
|
ft
|
||||||
ft2
|
ft2
|
||||||
strsignal
|
strsignal
|
||||||
|
fgrep
|
||||||
|
|
|
@ -17,6 +17,7 @@ OBJS = \
|
||||||
ft.o \
|
ft.o \
|
||||||
ft2.o \
|
ft2.o \
|
||||||
strsignal.o \
|
strsignal.o \
|
||||||
|
fgrep.o \
|
||||||
tty.o
|
tty.o
|
||||||
|
|
||||||
APPS = \
|
APPS = \
|
||||||
|
@ -38,6 +39,7 @@ APPS = \
|
||||||
ft \
|
ft \
|
||||||
ft2 \
|
ft2 \
|
||||||
strsignal \
|
strsignal \
|
||||||
|
fgrep \
|
||||||
tty
|
tty
|
||||||
|
|
||||||
ARCH_FLAGS =
|
ARCH_FLAGS =
|
||||||
|
@ -70,6 +72,9 @@ ps: ps.o
|
||||||
ls: ls.o
|
ls: ls.o
|
||||||
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
|
fgrep: fgrep.o
|
||||||
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
sleep: sleep.o
|
sleep: sleep.o
|
||||||
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
|
|
20
Userland/fgrep.cpp
Normal file
20
Userland/fgrep.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue