1
0

utilities/lib

This commit is contained in:
dtb 2022-05-14 20:54:10 -04:00
parent ce8ac30706
commit 5fbbf96062
12 changed files with 278 additions and 0 deletions

10
lib/banned.h Normal file
View File

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

BIN
lib/libcharin.o Normal file

Binary file not shown.

30
lib/libio.c Normal file
View File

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

59
lib/libshell.c Normal file
View File

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

11
lib/libshell.h Normal file
View File

@ -0,0 +1,11 @@
#include <errno.h>
#include <stdlib.h> /* getenv(3), malloc(3) */
#include <string.h> /* 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);

90
lib/libstr.c Normal file
View File

@ -0,0 +1,90 @@
#include "libstr.h"
/* Use strchr in <string.h> */
/* 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);
}
*/

10
lib/libstr.h Normal file
View File

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

21
lib/libwakeonlan.c Normal file
View File

@ -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 */
}

4
lib/libwakeonlan.h Normal file
View File

@ -0,0 +1,4 @@
#include <stddef.h>
#include <stdlib.h>
unsigned int *magic_packet(unsigned int *mac);

7
lib/put.c Normal file
View File

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

6
lib/test.c Normal file
View File

@ -0,0 +1,6 @@
#include <string.h>
#include "banned.h"
int main(){
int *a, *b;
strcat(a, b);
}

30
lib/xml.h Normal file
View File

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