piler/src/list.c

98 lines
1.2 KiB
C
Raw Normal View History

2011-11-14 15:57:52 +01:00
/*
* list.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "list.h"
#include "config.h"
2011-11-16 14:47:47 +01:00
int append_list(struct list **list, char *p){
struct list *q, *t, *u=NULL;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
q = *list;
2011-11-14 15:57:52 +01:00
while(q){
2011-11-16 14:47:47 +01:00
if(strcmp(q->s, p) == 0)
2011-11-14 15:57:52 +01:00
return 0;
u = q;
q = q->r;
}
2011-11-16 14:47:47 +01:00
t = create_list_item(p);
2011-11-14 15:57:52 +01:00
if(t){
2011-11-16 14:47:47 +01:00
if(*list == NULL)
*list = t;
2011-11-14 15:57:52 +01:00
else if(u)
u->r = t;
return 1;
}
return -1;
}
2011-11-16 14:47:47 +01:00
struct list *create_list_item(char *s){
struct list *h=NULL;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
if((h = malloc(sizeof(struct list))) == NULL)
2011-11-14 15:57:52 +01:00
return NULL;
2011-11-16 14:47:47 +01:00
snprintf(h->s, SMALLBUFSIZE-1, "%s", s);
2011-11-14 15:57:52 +01:00
h->r = NULL;
return h;
}
2011-11-16 14:47:47 +01:00
int is_string_on_list(struct list *list, char *s){
struct list *p;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
p = list;
2011-11-14 15:57:52 +01:00
while(p != NULL){
if(strcasecmp(p->s, s) == 0) return 1;
2011-11-16 14:47:47 +01:00
p = p->r;
}
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
return 0;
}
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
int is_item_on_string(struct list *list, char *s){
struct list *p;
p = list;
while(p != NULL){
if(strstr(s, p->s)) return 1;
p = p->r;
2011-11-14 15:57:52 +01:00
}
return 0;
}
2011-11-16 14:47:47 +01:00
void free_list(struct list *list){
struct list *p, *q;
2011-11-14 15:57:52 +01:00
2011-11-16 14:47:47 +01:00
p = list;
2011-11-14 15:57:52 +01:00
while(p != NULL){
q = p->r;
if(p)
free(p);
p = q;
}
}