ctype.h, getenv
This commit is contained in:
parent
f973ff8cb8
commit
23198d16f4
7 changed files with 148 additions and 12 deletions
7
05/assert.h
Normal file
7
05/assert.h
Normal 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
92
05/ctype.h
Normal 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
|
17
05/main.c
17
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
8
05/stddef.h
Normal file
8
05/stddef.h
Normal 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
|
13
05/stdio.h
13
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);
|
||||
|
|
19
05/stdlib.h
Normal file
19
05/stdlib.h
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue