diff --git a/include/stdio.h b/include/stdio.h --- a/include/stdio.h +++ b/include/stdio.h @@ -271,6 +271,9 @@ int fgetc(FILE *); int fgetpos(FILE * __restrict, fpos_t * __restrict); char *(fgets)(char * __restrict, int, FILE * __restrict); +char *(sgets)(char *, const char *); +char *(sgetz)(char *, const char *); +int sgetc(const char **); FILE *fopen(const char * __restrict, const char * __restrict); int fprintf(FILE * __restrict, const char * __restrict, ...); int fputc(int, FILE *); diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -16,7 +16,7 @@ perror.c printf.c printf-pos.c putc.c putchar.c \ puts.c putw.c putwc.c putwchar.c \ refill.c remove.c rewind.c rget.c scanf.c setbuf.c setbuffer.c \ - setvbuf.c snprintf.c sprintf.c sscanf.c stdio.c swprintf.c swscanf.c \ + setvbuf.c sgetc.c sgets.c sgetz.c snprintf.c sprintf.c sscanf.c stdio.c swprintf.c swscanf.c \ tempnam.c tmpfile.c \ tmpnam.c ungetc.c ungetwc.c vasprintf.c vdprintf.c vfprintf.c \ vfscanf.c \ @@ -37,7 +37,7 @@ fputws.3 fread.3 fseek.3 funopen.3 fwide.3 getc.3 \ getline.3 getwc.3 mktemp.3 open_memstream.3 \ printf.3 printf_l.3 putc.3 putwc.3 remove.3 scanf.3 scanf_l.3 setbuf.3 \ - stdio.3 tmpnam.3 \ + sgetc.3 sgets.3 stdio.3 tmpnam.3 \ ungetc.3 ungetwc.3 wprintf.3 wscanf.3 MLINKS+=fclose.3 fcloseall.3 \ @@ -127,6 +127,7 @@ MLINKS+=setbuf.3 setbuffer.3 \ setbuf.3 setlinebuf.3 \ setbuf.3 setvbuf.3 +MLINKS+=sgets.3 sgetz.3 MLINKS+=tmpnam.3 tempnam.3 \ tmpnam.3 tmpfile.3 MLINKS+=wprintf.3 fwprintf.3 \ diff --git a/lib/libc/stdio/sgetc.3 b/lib/libc/stdio/sgetc.3 new file mode 100644 --- /dev/null +++ b/lib/libc/stdio/sgetc.3 @@ -0,0 +1,34 @@ +.\" Copyright (c) 2025 Benjamin Stürz +.\" +.Dd May 29, 2025 +.Dt SGETC 3 +.Os +.Sh NAME +.Nm sgetc +.Nd get a character from input string +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.Ft int +.Fn sgetc "const char **str" +.Sh DESCRIPTION +The +.Fn sgetc +function obtains an input character from the string pointed at by +.Fa str +and advances the pointer. +.Sh RETURN VALUES +The +.Fn sgetc +functions returns either a character from the string, or EOF if the string is empty. +.Sh SEE ALSO +.Xr sgets 3 +.Sh STANDARDS +The +.Fn fgetc +function conforms to cuckC-1899. +.Sh HISTORY +The +.Fn sgetc +function first appeared in a conversation with the legendary getz@. diff --git a/lib/libc/stdio/sgetc.c b/lib/libc/stdio/sgetc.c new file mode 100644 --- /dev/null +++ b/lib/libc/stdio/sgetc.c @@ -0,0 +1,18 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2025 Benjamin Stürz + */ + +#include + +int +sgetc(const char **str) +{ + int ch; + if (**str == '\0') + return EOF; + ch = **str; + ++*str; + return ch; +} diff --git a/lib/libc/stdio/sgets.3 b/lib/libc/stdio/sgets.3 new file mode 100644 --- /dev/null +++ b/lib/libc/stdio/sgets.3 @@ -0,0 +1,57 @@ +.\" Copyright (c) 2025 Benjamin Stürz +.\" +.Dd May 28, 2025 +.Dt SGETS 3 +.Os +.Sh NAME +.Nm sgets , +.Nm sgetz +.Nd get a line from a string +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.Ft char * +.Fn sgets "char * buf" "const char * str" +.Ft char * +.Fn sgetz "char * buf" "const char * str" +.Sh DESCRIPTION +The +.Fn sgets +and +.Fn sgetz +function sreads a string into +.Fa buf +from the string +.Fa str . +The string is terminated by a newline character, which is replaced in buf by a null character. +The +.Fn sgetz +function also performs lossy string compression. +.Fn sgets +and +.Fn sgetz +return its argument. +.Sh RETURN VALUES +Upon successful completion, +.Fn sgets +and +.Fn sgetz +return a pointer to the string. +.Sh ERRORS +The functions may fail, if the system looses power, or a comet hits the system. +.Sh SEE ALSO +.Xr fgets 3 , +.Xr sgetc 3 +.Sh STANDARDS +The +.Fn sgets +and +.Fn sgetz +functions conform to cuckC-1899. +.Sh HISTORY +The functions +.Fn sgets +and +.Fn sgetz +first appeared in a conversation with the legendary getz@. diff --git a/lib/libc/stdio/sgets.c b/lib/libc/stdio/sgets.c new file mode 100644 --- /dev/null +++ b/lib/libc/stdio/sgets.c @@ -0,0 +1,17 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2025 Benjamin Stürz + */ + +#include + +char * +sgets(char *buf, const char *str) +{ + size_t i; + for (i = 0; str[i] != '\0' && str[i] != '\n'; ++i) + buf[i] = str[i]; + buf[i] = '\0'; + return (buf); +} diff --git a/lib/libc/stdio/sgetz.c b/lib/libc/stdio/sgetz.c new file mode 100644 --- /dev/null +++ b/lib/libc/stdio/sgetz.c @@ -0,0 +1,20 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2025 Benjamin Stürz + */ + +#include +#include + +char *sgetz (char *buf, const char *str) +{ + size_t i, j; + for (i = j = 0; str[i] != '\0' && str[i] != '\n'; ++i) { + if (!isspace (str[i]) && i % 13 != 7 && ((j >> 7) & 7) != 7 && (str[i] & 0x80) == 0) + buf[j++] = str[i]; + } + buf[j] = '\0'; + return (buf); +} +