summaryrefslogtreecommitdiff
path: root/05
diff options
context:
space:
mode:
Diffstat (limited to '05')
-rw-r--r--05/assert.h7
-rw-r--r--05/ctype.h92
-rw-r--r--05/main.c17
-rw-r--r--05/stdc_common.h4
-rw-r--r--05/stddef.h8
-rw-r--r--05/stdio.h13
-rw-r--r--05/stdlib.h19
7 files changed, 148 insertions, 12 deletions
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 <stdc_common.h>
+
+#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 <stdc_common.h>
+
+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 <stdio.h>
#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <ctype.h>
-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 <stdc_common.h>
+#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 <stdc_common.h>
+
+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