From 23198d16f445e3596e6d309a5d36b144fb32e058 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 15 Feb 2022 16:36:52 -0500 Subject: ctype.h, getenv --- 05/assert.h | 7 +++++ 05/ctype.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 05/main.c | 17 ++++++----- 05/stdc_common.h | 4 +++ 05/stddef.h | 8 +++++ 05/stdio.h | 13 +++++--- 05/stdlib.h | 19 ++++++++++++ 7 files changed, 148 insertions(+), 12 deletions(-) create mode 100644 05/assert.h create mode 100644 05/ctype.h create mode 100644 05/stddef.h create mode 100644 05/stdlib.h diff --git a/05/assert.h b/05/assert.h new file mode 100644 index 0000000..8cac979 --- /dev/null +++ b/05/assert.h @@ -0,0 +1,7 @@ +#ifndef _ASSERT_H +#define _ASSERT_H + +// assert is defined in stdc_common.h +#include + +#endif // _ASSERT_H diff --git a/05/ctype.h b/05/ctype.h new file mode 100644 index 0000000..ed6833d --- /dev/null +++ b/05/ctype.h @@ -0,0 +1,92 @@ +#ifndef _CTYPE_H +#define _CTYPE_H + +#include + +int islower(int c) { + return c >= 'a' && c <= 'z'; +} + +int isupper(int c) { + return c >= 'A' && c <= 'Z'; +} + +int isalpha(int c) { + return isupper(c) || islower(c); +} + +int isalnum(int c) { + return isalpha(c) || isdigit(c); +} + +int isprint(int c) { + if (isalnum(c)) return 1; + switch (c) { + case '!': return 1; + case '@': return 1; + case '#': return 1; + case '$': return 1; + case '%': return 1; + case '^': return 1; + case '&': return 1; + case '*': return 1; + case '(': return 1; + case ')': return 1; + case '-': return 1; + case '=': return 1; + case '_': return 1; + case '+': return 1; + case '`': return 1; + case '~': return 1; + case '[': return 1; + case '{': return 1; + case ']': return 1; + case '}': return 1; + case '\\': return 1; + case '|': return 1; + case ';': return 1; + case ':': return 1; + case '\'': return 1; + case '"': return 1; + case ',': return 1; + case '<': return 1; + case '.': return 1; + case '>': return 1; + case '/': return 1; + case '?': return 1; + } + return 0; +} + +int iscntrl(int c) { + return !isprint(c); +} + +int isgraph(int c) { + return isprint(c) && c != ' '; +} + +int ispunct(int c) { + return isprint(c) && c != ' ' && !isalnum(c); +} + +int isxdigit(int c) { + if (isdigit(c)) return 1; + if (c >= 'a' && c <= 'f') return 1; + if (c >= 'A' && c <= 'F') return 1; + return 0; +} + +int tolower(int c) { + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + return c; +} + +int toupper(int c) { + if (c >= 'a' && c <= 'z') + return c - 'a' + 'A'; + return c; +} + +#endif // _CTYPE_H diff --git a/05/main.c b/05/main.c index 7ddf588..b02f95f 100644 --- a/05/main.c +++ b/05/main.c @@ -1,15 +1,16 @@ #define _STDLIB_DEBUG #include #include +#include +#include +#include -int main(void) { - int count; float quant; char units[21], item[21]; - while (!feof(stdin) && !ferror(stdin)) { - count = fscanf(stdin, "%f%20s of %20s", - &quant, units, item); - fscanf(stdin,"%*[^\n]"); - printf("%d %g %s %s\n", count, quant, units, item); - } +typedef struct { + int x; + long y; +} S; + +int main(int argc, char **argv) { return 0; } diff --git a/05/stdc_common.h b/05/stdc_common.h index c40fd3d..9e5d7a6 100644 --- a/05/stdc_common.h +++ b/05/stdc_common.h @@ -431,10 +431,14 @@ double strtod(const char *nptr, char **endptr) { int main(); +static char **_envp; + int _main(int argc, char **argv) { int i; _Float p = {1, 0}; + _envp = argv + argc + 1; // this is where the environment variables will be + stdin = &_stdin; stdout = &_stdout; stderr = &_stderr; diff --git a/05/stddef.h b/05/stddef.h new file mode 100644 index 0000000..f012d95 --- /dev/null +++ b/05/stddef.h @@ -0,0 +1,8 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +#include +#define offsetof(struct, member) ((size_t)(&((struct *)NULL)->member)) +// @NONSTANDARD: we don't have wchar_t + +#endif // _STDDEF_H diff --git a/05/stdio.h b/05/stdio.h index 8320b4b..3b53d2b 100644 --- a/05/stdio.h +++ b/05/stdio.h @@ -1751,7 +1751,7 @@ FILE *tmpfile(void) { return _FILE_from_fd(fd); } -int fgetc(FILE *stream) { +int getc(FILE *stream) { unsigned char c; long n; if (stream->eof) return EOF; @@ -1760,7 +1760,9 @@ int fgetc(FILE *stream) { return c; } -#define getc(fp) fgetc(fp) +int fgetc(FILE *stream) { + return getc(stream); +} char *fgets(char *s, int n, FILE *stream) { char *p = s, *end = p + (n-1); @@ -1786,12 +1788,15 @@ char *fgets(char *s, int n, FILE *stream) { return s; } -int fputc(int c, FILE *stream) { +int putc(int c, FILE *stream) { size_t n = fwrite(&c, 1, 1, stream); if (n == 1) return c; return EOF; } -#define putc(c, fp) fputc(c, fp) + +int fputc(int c, FILE *stream) { + return putc(c, stream); +} int fputs(const char *s, FILE *stream) { size_t n = strlen(s); diff --git a/05/stdlib.h b/05/stdlib.h new file mode 100644 index 0000000..e98156b --- /dev/null +++ b/05/stdlib.h @@ -0,0 +1,19 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#include + +char *getenv(const char *name) { + int i, j; + for (i = 0; _envp[i]; ++i) { + char *key = _envp[i]; + for (j = 0; key[j] != '=' && name[j]; ++j) + if (name[j] != key[j]) + break; + if (key[j] == '=' && !name[j]) + return key + (j+1); + } + return NULL; +} + +#endif // _STDLIB_H -- cgit v1.2.3