ctype.h, getenv

This commit is contained in:
pommicket 2022-02-15 16:36:52 -05:00
parent f973ff8cb8
commit 23198d16f4
7 changed files with 148 additions and 12 deletions

7
05/assert.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef _ASSERT_H
#define _ASSERT_H
// assert is defined in stdc_common.h
#include <stdc_common.h>
#endif // _ASSERT_H

92
05/ctype.h Normal file
View file

@ -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

View file

@ -1,15 +1,16 @@
#define _STDLIB_DEBUG #define _STDLIB_DEBUG
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
int main(void) { typedef struct {
int count; float quant; char units[21], item[21]; int x;
while (!feof(stdin) && !ferror(stdin)) { long y;
count = fscanf(stdin, "%f%20s of %20s", } S;
&quant, units, item);
fscanf(stdin,"%*[^\n]"); int main(int argc, char **argv) {
printf("%d %g %s %s\n", count, quant, units, item);
}
return 0; return 0;
} }

View file

@ -431,10 +431,14 @@ double strtod(const char *nptr, char **endptr) {
int main(); int main();
static char **_envp;
int _main(int argc, char **argv) { int _main(int argc, char **argv) {
int i; int i;
_Float p = {1, 0}; _Float p = {1, 0};
_envp = argv + argc + 1; // this is where the environment variables will be
stdin = &_stdin; stdin = &_stdin;
stdout = &_stdout; stdout = &_stdout;
stderr = &_stderr; stderr = &_stderr;

8
05/stddef.h Normal file
View file

@ -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

View file

@ -1751,7 +1751,7 @@ FILE *tmpfile(void) {
return _FILE_from_fd(fd); return _FILE_from_fd(fd);
} }
int fgetc(FILE *stream) { int getc(FILE *stream) {
unsigned char c; unsigned char c;
long n; long n;
if (stream->eof) return EOF; if (stream->eof) return EOF;
@ -1760,7 +1760,9 @@ int fgetc(FILE *stream) {
return c; return c;
} }
#define getc(fp) fgetc(fp) int fgetc(FILE *stream) {
return getc(stream);
}
char *fgets(char *s, int n, FILE *stream) { char *fgets(char *s, int n, FILE *stream) {
char *p = s, *end = p + (n-1); char *p = s, *end = p + (n-1);
@ -1786,12 +1788,15 @@ char *fgets(char *s, int n, FILE *stream) {
return s; return s;
} }
int fputc(int c, FILE *stream) { int putc(int c, FILE *stream) {
size_t n = fwrite(&c, 1, 1, stream); size_t n = fwrite(&c, 1, 1, stream);
if (n == 1) return c; if (n == 1) return c;
return EOF; 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) { int fputs(const char *s, FILE *stream) {
size_t n = strlen(s); size_t n = strlen(s);

19
05/stdlib.h Normal file
View file

@ -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