Compare commits

..

5 Commits

Author SHA1 Message Date
00672c4b22
apps/hello: Demo fgets
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-23 21:35:21 +01:00
0e9890901f
libc: Add (f)getc, getchar, and fgets 2023-03-23 21:35:09 +01:00
937802964c
kernel: Invert O_NONBLOCK to check whether a thread should block 2023-03-23 21:34:38 +01:00
95e884db97
kernel: Fix compilation 2023-03-23 21:34:09 +01:00
03adaa356c
kernel: Avoid printing keypresses twice 2023-03-23 21:33:50 +01:00
6 changed files with 57 additions and 7 deletions

View File

@ -3,4 +3,10 @@
int main(int argc, char** argv)
{
printf("Hello world! argc=%d, argv[0]=%s, argv[1]=%s, argv[2]=%s\n", argc, argv[0], argv[1], argv[2]);
char buf[1024];
char* rc = fgets(buf, sizeof(buf), stdin);
if (!rc) perror("fgets");
fputs(buf, stdout);
}

View File

@ -126,11 +126,7 @@ void io_thread()
while (!scancode_queue.try_pop(scancode)) { kernel_sleep(10); }
char key;
if (Keyboard::decode_scancode(scancode).try_set_value(key))
{
TextConsole::putchar(key);
ConsoleDevice::did_press_key(key);
}
if (Keyboard::decode_scancode(scancode).try_set_value(key)) { ConsoleDevice::did_press_key(key); }
}
}

View File

@ -46,7 +46,7 @@ void ConsoleDevice::did_press_key(char key)
return;
}
g_temp_input.try_append(key).value();
g_temp_input.try_append((u8)key).value();
if (key == '\n')
{

View File

@ -52,7 +52,7 @@ bool FileDescriptor::should_append()
bool FileDescriptor::should_block()
{
return flags & O_NONBLOCK;
return !(flags & O_NONBLOCK);
}
bool FileDescriptor::is_readable()

View File

@ -81,6 +81,18 @@ extern "C"
/* Write a string to stream. */
int fputs(const char* str, FILE* stream);
/* Read a character from stream. */
int fgetc(FILE* stream);
/* Read a character from stream. */
int getc(FILE* stream);
/* Read a character from standard input. */
int getchar();
/* Read a line from stream. */
char* fgets(char* buf, size_t size, FILE* stream);
/* Clear the error and end-of-file indicators in stream. */
void clearerr(FILE* stream);

View File

@ -187,6 +187,42 @@ extern "C"
return (rc < 0) ? -1 : 0;
}
int fgetc(FILE* stream)
{
u8 value;
ssize_t rc = read(stream->_fd, &value, 1);
if (rc <= 0) return EOF;
return value;
}
int getc(FILE* stream)
{
return fgetc(stream);
}
int getchar()
{
return fgetc(stdin);
}
char* fgets(char* buf, size_t size, FILE* stream)
{
size_t i = 0;
while (i + 1 < size)
{
int c = fgetc(stream);
if (c == EOF) break;
buf[i++] = (char)c;
if (c == '\n') break;
}
if (i == 0) return NULL;
buf[i] = 0;
return buf;
}
void clearerr(FILE* stream)
{
stream->_eof = stream->_err = 0;