diff --git a/lib/banned.h b/lib/banned.h new file mode 100644 index 0000000..3e280f6 --- /dev/null +++ b/lib/banned.h @@ -0,0 +1,10 @@ +#ifndef _BANNED_H +# define BANNED_H + +# define Use_of_this_function_is_not_permitted(a) invalid_token +# define BAN(a) Use_of_this_function_is_not_permitted(a) + +# undef strcat +# define strcat(a,b) BAN(strcat) + +#endif /* ifndef _BANNED_H */ diff --git a/lib/libcharin.o b/lib/libcharin.o new file mode 100644 index 0000000..7701ec7 Binary files /dev/null and b/lib/libcharin.o differ diff --git a/lib/libio.c b/lib/libio.c new file mode 100644 index 0000000..775eb96 --- /dev/null +++ b/lib/libio.c @@ -0,0 +1,30 @@ +#ifndef _LIBIO_H + +# define _LIBIO_H + +int +fdputs(int fd, const char *s){ + int i; + int r; + + for(i = 0; s[i] != '\0'; ++i); + + /* DANGER */ + s[i] = '\n'; + r = write(fd, s, i + 1); + s[i] = '\0'; + /* /danger */ + + return r; +} + +# ifndef _STDIO_H + +int +puts(const char *s){ + return fdputs(1, s); +} + +# endif /* ifndef _STDIO_H */ + +#endif /* ifndef _LIBIO_H */ diff --git a/lib/libshell.c b/lib/libshell.c new file mode 100644 index 0000000..dfaec2d --- /dev/null +++ b/lib/libshell.c @@ -0,0 +1,59 @@ +#include "libshell.h" + +char ** +getpaths(void) +{ + char *p; + char **q; + char *path; + size_t path_s; + char *path_temp; + char **paths; + size_t paths_s; + + if((path_temp = getenv(PATH_NAME)) == NULL){ + errno = ENOMEM; /* Cannot allocate memory */ + return NULL; + } + + /* How long is path? */ + for(path_s = 0; path_temp[path_s] != '\0'; ++path_s); + /* path_s has the size of the path string, not including the null + * terminator */ + + /* getenv's return value mustn't be modified so copy it into memory we + * control */ + if((path = malloc(sizeof(char) * (path_s + 1))) == NULL){ + errno = ENOMEM; /* Cannot allocate memory */ + return NULL; + } + memcpy(path, path_temp, path_s + 1); + path_temp = NULL; + path_s = 0; /* This shouldn't be necessary anymore */ + + /* How many paths in $PATH? */ + for(paths_s = 1, p = path; *p != '\0'; paths_s += *p++ == PATH_DELIMITER); + + if((paths = malloc(sizeof(char *) * (paths_s + 1))) == NULL){ + free(path); + errno = ENOMEM; /* Cannot allocate memory */ + return NULL; + } + + /* Replace all instances of the path delimiter with 0, and then put the + * next path beginning into paths. */ + /* This way we can get multiple strings out of one span of memory. */ + for(*paths = p = path, q = paths + 1; *p != '\0';) + if(*p++ == PATH_DELIMITER){ + *(p - 1) = '\0'; + *q++ = p; + } + + paths[path_s] = NULL; + + /* Because paths[0] will always be path, we can just free(*paths) at + * the end and discard path */ + path = NULL; + + return paths; +} diff --git a/lib/libshell.h b/lib/libshell.h new file mode 100644 index 0000000..a2acac8 --- /dev/null +++ b/lib/libshell.h @@ -0,0 +1,11 @@ +#include +#include /* getenv(3), malloc(3) */ +#include /* memcpy(3) */ + +/* For the shell variable $PATH */ +#define PATH_NAME "PATH" +#define PATH_DELIMITER ':' + +/* Returns a list of the current paths ordered from first to last priority. + * output must be freed and *output must be freed! */ +char **getpaths(void); diff --git a/lib/libstr.c b/lib/libstr.c new file mode 100644 index 0000000..fcaebd4 --- /dev/null +++ b/lib/libstr.c @@ -0,0 +1,90 @@ +#include "libstr.h" + +/* Use strchr in */ +/* char *charin(char c, char *str){ return strchr(str, c); } */ +/* size_t charindex(char c, char *str){ return strchr(str, c) - str; } */ +/* bool charisin(char c, char *str){ return strchr(str, c) == NULL; } */ + +/* lower bound is a limitation of mathematics, + * upper bound is controlled by how many digits are defined + * in the header */ +#define CHECKBASE(b) ((b) >= 2 && base < (sizeof(ASCII_DIGITS_LOWER) / sizeof(*ASCII_DIGITS_LOWER)) + +int * +strtonumb(char *s, enum Strtype t, void *n, int base){ + int i; + int polarity; + int retval; + retval = 0; + while(*s != '\0'){ + if(!CHECKBASE(base)) + return NULL; + /* assume *s is in ASCII_DIGITS_LOWER or ASCII_DIGITS_UPPER */ + for(i = 0; i < base; ++i) + if(ASCII_DIGITS_LOWER[i] == *s + || ASCII_DIGITS_UPPER == *s){ + retval = retval * 10 + i; + break; + } + if(i == base) + return NULL; + ++s; + } + *n = retval; + return n; +} + +int +stris(enum Strtype t, char *s){ + return strisb(t, s, 10); +} + +int +strisb(enum Strtype t, char *s, int base) +{ + int retval; + retval = 0; + + int i; + + if(!CHECKBASE(base)) + return -1; + + /* negatives */ + if((t == STRIS_TYPE_FLOAT || t == STRIS_TYPE_INT) && *s == '-'){ + ++s; + if(t == STRIS_TYPE_INT) + t = STRIS_TYPE_UINT; + } + + while(*s != '\0') + switch(t){ + case STRIS_TYPE_FLOAT: + if(*s == '.'){ + ++s; + t = STRIS_TYPE_UINT; + break; + } + /* FALLTHROUGH */ + default: + for(i = 0; i < base; ++i) + if(ASCII_DIGITS_LOWER[i] == *s + || ASCII_DIGITS_UPPER[i] == *s){ + retval = 1; + break; + } + if(i == base) + return 0; + ++s; + } + return retval; +} + +/* +int +strisb(enum Strtype t, char *s, int base){ + int n; + return !(strtonumb(s, t, &n, base) == NULL); +} +*/ + diff --git a/lib/libstr.h b/lib/libstr.h new file mode 100644 index 0000000..89376f4 --- /dev/null +++ b/lib/libstr.h @@ -0,0 +1,10 @@ +#include "ascii.h" + +enum Strtype{ + STRIS_TYPE_UINT = 1, + STRIS_TYPE_INT, + STRIS_TYPE_FLOAT +}; + +int strisb(enum Strtype t, char *str, int base); +int stris(enum Strtype t, char *str); diff --git a/lib/libwakeonlan.c b/lib/libwakeonlan.c new file mode 100644 index 0000000..36ed0f2 --- /dev/null +++ b/lib/libwakeonlan.c @@ -0,0 +1,21 @@ +#include "libwakeonlan.h" + +/* Returns a byte array of 6 0xFF and 16 repetitions of the 6-byte MAC. */ +unsigned int* +magic_packet(unsigned int *mac){ + size_t i; + size_t j; + int *retval; + + if((retval = malloc(sizeof(unsigned int) * 6 + 6*16)) == NULL) + return NULL; + + for(i = 0; i < 6; ++i) + retval[i] = 255; + + for(i = 1; i < 16; ++i) + for(j = 0; j < 6; ++j) + retval[i*j] = mac[j]; + + return retval; /* must be freed */ +} diff --git a/lib/libwakeonlan.h b/lib/libwakeonlan.h new file mode 100644 index 0000000..748798c --- /dev/null +++ b/lib/libwakeonlan.h @@ -0,0 +1,4 @@ +#include +#include + +unsigned int *magic_packet(unsigned int *mac); diff --git a/lib/put.c b/lib/put.c new file mode 100644 index 0000000..a1bea88 --- /dev/null +++ b/lib/put.c @@ -0,0 +1,7 @@ +/* A similar function appeared in the original ed(1). This was written clean as + * the original is no longer proprietary but now still encumbered by "BSD-like + * licensing" (Wikipedia). */ + +int +putd(int l, int d){ + diff --git a/lib/test.c b/lib/test.c new file mode 100644 index 0000000..f9a596e --- /dev/null +++ b/lib/test.c @@ -0,0 +1,6 @@ +#include +#include "banned.h" +int main(){ + int *a, *b; + strcat(a, b); +} diff --git a/lib/xml.h b/lib/xml.h new file mode 100644 index 0000000..606c3fc --- /dev/null +++ b/lib/xml.h @@ -0,0 +1,30 @@ +/* There's already an excellent and popular XML library, libxml2, that + * accomplishes what this does but better. Use that instead. */ + +#define XML_LEFT_BRACKET '<' +#define XML_RIGHT_BRACKET '>' +#define XML_FORWARD_SLASH '/' + +struct XMLAttribute{ + char *name; + char *content; +} + +struct XMLDoc{ + struct XMLNode **nodes; +} + +struct XMLNode{ + char *name; + char *content; + union{ + struct XMLDoc *doc; + struct XMLNode *node; + }parent; + enum{ + XML_PARENT_IS_DOC = 0, + XML_PARENT_IS_NODE = 1 + }parent_type; + struct XMLAttribute **attributes; + struct XMLNode **children; +}