list.c

Go to the documentation of this file.
00001 /*
00002  * (C)opyright 2007 Nico Golde <nico@ngolde.de>
00003  * See LICENSE file for license details
00004  * This is no complete linked list implementation, it is just used
00005  * to get a list of directory entries and delete the list
00006  */
00007 
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include <dirent.h>
00012 
00013 #include "list.h"
00014 
00015 /* create a new list */
00016 static list_t *
00017 new_list(void){
00018         list_t *l = malloc(sizeof(list_t));
00019         if(!l) return NULL;
00020         l->top = l->last = NULL;
00021         l->length = 0;
00022         return l;
00023 }
00024 
00025 static void
00026 append_node(list_t *lst, char *name){
00027         node_t *n;
00028         if(!lst) return;
00029         if((n = malloc(sizeof(node_t))) == NULL) return;
00030         if((n->name = strdup(name)) == NULL) {
00031                 free(n);
00032                 return;
00033         }
00034         n->next = NULL;
00035         if(lst->top){
00036                 lst->last->next = n;
00037                 lst->last = lst->last->next;
00038         } else {
00039                 lst->top = lst->last = n;
00040         }
00041         lst->length++;
00042 }
00043 
00044 /* delete the whole list */
00045 void
00046 delete_list(list_t *lst){
00047         node_t *tmp;
00048         tmp = lst->top;
00049         while(tmp != NULL){
00050                 lst->top = tmp->next;
00051                 if(tmp->name) free(tmp->name);
00052                 free(tmp);
00053                 tmp = lst->top;
00054         }
00055         lst->top = lst->last = NULL;
00056 }
00057 
00058 /* return a linked list with directory entries or NULL on error */
00059 list_t *
00060 dir_list(char *dir){
00061         list_t *list = new_list();
00062         DIR *rddir = NULL;
00063         struct dirent *rd;
00064 
00065         if((rddir = opendir(dir)) == NULL)
00066                 return NULL;
00067         while((rd = readdir(rddir))){
00068                 if(!strncmp(".", rd->d_name, 1) || !strncmp("..", rd->d_name, 2))
00069                         continue;
00070 
00071                 append_node(list, rd->d_name);
00072         }
00073         closedir(rddir);
00074         return list;
00075 }

Generated on Thu Jun 14 18:39:49 2007 for libacpi by  doxygen 1.5.2