summaryrefslogtreecommitdiff
path: root/libhashtable
diff options
context:
space:
mode:
authorEpoch Qwert <epoch@53flpnlls43fcguy.onion>2014-09-15 02:46:47 -0500
committerEpoch Qwert <epoch@53flpnlls43fcguy.onion>2014-09-15 02:48:47 -0500
commit7f82cd9b726f69f01c243833058e438c4e22b84f (patch)
treed486bd12def255048b27fa1d357036059451ee52 /libhashtable
parent9b8d802ca89027de46ee45fbc9adf978a16e7927 (diff)
downloadsegfault-7f82cd9b726f69f01c243833058e438c4e22b84f.tar.gz
segfault-7f82cd9b726f69f01c243833058e438c4e22b84f.zip
did the TODO of making builtins use a hashtable.
added two commands for dealing with them. if you want you can override builtins for security reasons? :) added a small program for doing tail -f for systems that may not have tail -f
Diffstat (limited to 'libhashtable')
-rw-r--r--libhashtable/example.c2
-rwxr-xr-xlibhashtable/genheader.sh3
-rw-r--r--libhashtable/hashtable.h16
-rw-r--r--libhashtable/libhashtable.c24
4 files changed, 25 insertions, 20 deletions
diff --git a/libhashtable/example.c b/libhashtable/example.c
index bc42a67..919d75c 100644
--- a/libhashtable/example.c
+++ b/libhashtable/example.c
@@ -19,5 +19,7 @@ int main(int argc,char *argv[]) {
free(name);
}
printf("PATH='%s'\n",ht_getvalue(&ht,"PATH"));
+ //if you want to get a whole entry struct use ht_getnode();
+ //getentry() just returns the first struct in the linked list in that bucket.
return 0;
}
diff --git a/libhashtable/genheader.sh b/libhashtable/genheader.sh
new file mode 100755
index 0000000..3995579
--- /dev/null
+++ b/libhashtable/genheader.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+cat libhashtable.c | head -n 22 | tail -n 16 > hashtable.h
+cat libhashtable.c | grep '(.*) *{' | egrep -v 'if|for|while' | sed 's/ {/;/' >> hashtable.h
diff --git a/libhashtable/hashtable.h b/libhashtable/hashtable.h
index 820d230..f0e2a15 100644
--- a/libhashtable/hashtable.h
+++ b/libhashtable/hashtable.h
@@ -1,23 +1,23 @@
-struct entry {//linked list node.
+struct entry {/*linked list node.*/
char *original;
- char *target;
- struct entry *prev;// doubly linked list. why?
+ void *target;
+ struct entry *prev;/* doubly linked list. why? */
struct entry *next;
};
-struct hitem {//dunno why I don't just have this as a linked list.
+struct hitem {/*dunno why I don't just have this as a linked list. */
struct entry *ll;
};
struct hashtable {
- int kl;//number of keys in the table
+ int kl;/*number of keys in the table*/
struct hitem **bucket;
int *keys;
};
-unsigned short hash(char *v);//maybe use a seeded rand()? :) Thanks FreeArtMan
+unsigned short hash(char *key);/*maybe use a seeded rand()? :) Thanks FreeArtMan*/
void inittable(struct hashtable *ht,int tsize);
-int ht_setkey(struct hashtable *ht,char *key,char *value);
+int ht_setkey(struct hashtable *ht,char *key,void *value);
struct entry *ll_getentry(struct entry *start,char *msg);
struct entry *ht_getentry(struct hashtable *ht,char *key);
struct entry *ht_getnode(struct hashtable *ht,char *msg);
-char *ht_getvalue(struct hashtable *ht,char *msg);
+void *ht_getvalue(struct hashtable *ht,char *msg);
diff --git a/libhashtable/libhashtable.c b/libhashtable/libhashtable.c
index c4ac2d3..e89d0cd 100644
--- a/libhashtable/libhashtable.c
+++ b/libhashtable/libhashtable.c
@@ -6,7 +6,7 @@
/*
struct entry {//linked list node.
char *original;
- char *target;
+ void *target;
struct entry *prev;// doubly linked list. why?
struct entry *next;
};
@@ -22,11 +22,8 @@ struct hashtable {
};
*/
-unsigned short hash(char *v) {//maybe use a seeded rand()? :) Thanks FreeArtMan
- unsigned short h=0;
- h=((*v)<<8)+(v?*(v+1):0);
- srand(h);
- return rand();
+unsigned short hash(char *key) {//maybe use a seeded rand()? :) Thanks FreeArtMan
+ return (strlen(key)<<8)+(key[0]<<4)+key[1];
}
void inittable(struct hashtable *ht,int tsize) {
@@ -44,7 +41,7 @@ void inittable(struct hashtable *ht,int tsize) {
}
//this seems too complicated.
-int ht_setkey(struct hashtable *ht,char *key,char *value) {
+int ht_setkey(struct hashtable *ht,char *key,void *value) {
unsigned short h=hash(key);
struct entry *tmp;
int i;
@@ -63,8 +60,9 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) {
}
if((tmp=ll_getentry(ht->bucket[h]->ll,key)) != NULL) {
//we found this alias in the ll. now to replace the value
- free(tmp->target);
- if(!(tmp->target=strdup(value))) return 2;
+ //free(tmp->target);//WHY? no.
+ //if(!(tmp->target=strdup(value))) return 2;
+ tmp->target=value;//can't strdup for non-strings. do it yourself.
return 0;
}
if(ht->bucket[h]->ll == NULL) {
@@ -72,7 +70,8 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) {
ht->bucket[h]->ll->next=0;
ht->bucket[h]->ll->prev=0;
if(!(ht->bucket[h]->ll->original=strdup(key))) return 4;
- if(!(ht->bucket[h]->ll->target=strdup(value))) return 5;
+ //if(!(ht->bucket[h]->ll->target=strdup(value))) return 5;
+ ht->bucket[h]->ll->target=value;
} else {
//go to the end and add another entry to the ll.
for(tmp=ht->bucket[h]->ll;tmp->next;tmp=tmp->next);
@@ -80,7 +79,8 @@ int ht_setkey(struct hashtable *ht,char *key,char *value) {
tmp->next->prev=tmp;
tmp=tmp->next;
if(!(tmp->original=strdup(key))) return 7;
- if(!(tmp->target=strdup(value))) return 8;
+ //if(!(tmp->target=strdup(value))) return 8;
+ tmp->target=value;
tmp->next=0;
}
return 0;
@@ -109,7 +109,7 @@ struct entry *ht_getnode(struct hashtable *ht,char *msg) {
}
//you'll probably want to use me.
-char *ht_getvalue(struct hashtable *ht,char *msg) {
+void *ht_getvalue(struct hashtable *ht,char *msg) {
struct entry *tmp=ll_getentry(ht_getentry(ht,msg),msg);
return tmp?tmp->target:0;
}