summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-01-20 20:55:54 +0900
committerFreeArtMan <dos21h@gmail.com>2015-01-20 20:55:54 +0900
commitc49166a323b9a1bb777e949c0bcbedc7eceab3cd (patch)
tree3d6cdedfdf0d6884131c8c0fa59b811c99517cfd
parent4985e5e287683b16ff46c2b3069388f11bcb5698 (diff)
downloadmicrobbs-c49166a323b9a1bb777e949c0bcbedc7eceab3cd.tar.gz
microbbs-c49166a323b9a1bb777e949c0bcbedc7eceab3cd.zip
Articles replaces pure string manipulation with sds
-rw-r--r--articles.c32
-rw-r--r--articles.h2
2 files changed, 25 insertions, 9 deletions
diff --git a/articles.c b/articles.c
index d651069..7db8b39 100644
--- a/articles.c
+++ b/articles.c
@@ -135,25 +135,24 @@ int bbs_article_list( term_screen *ts, const char *dir_name )
int cnt;
for ( cnt=0; cnt<n; cnt++)
{
- size_t tmp_size = strlen( eps[cnt]->d_name );
- char *cnct_path = malloc( tmp_size + strlen(dir_name)+1);
- memcpy( cnct_path, dir_name, strlen(dir_name));
- memcpy( cnct_path+sizeof(dir_name)+2, eps[cnt]->d_name,
- tmp_size+1 );
+ sds d_name = sds_new( eps[cnt]->d_name );
+ sds pathname = sds_new( dir_name );
+ pathname = sds_cat( pathname, d_name );
+ sds_free( d_name );
//check if its file
- if ( stat( cnct_path, &path_node ) == 0 )
+ if ( stat( pathname, &path_node ) == 0 )
{
if ( path_node.st_mode & S_IFREG )
{
- llist_push( dir_list, cnct_path );
+ llist_push( dir_list, pathname );
} else
{
- free( cnct_path );
+ sds_free( pathname );
}
} else
{
- free( cnct_path );
+ sds_free( pathname );
}
free( eps[cnt] );
}
@@ -240,7 +239,22 @@ int bbs_article_list( term_screen *ts, const char *dir_name )
printf("Try more\n");
}
}
+ //needed special care to free sds strings
+ //dangerous stuff as list contains sds strings then we need to call sds_free
+ //not just free, maybe try to use llist_manager it should handle that
+ {
+ struct ListNode *iter=dir_list->first;
+ while (iter != NULL)
+ {
+ if ( iter->val != NULL )
+ {
+ sds_free( iter->val );
+ iter->val = NULL;
+ }
+ iter = iter->next;
+ }
+ }
llist_free( dir_list );
diff --git a/articles.h b/articles.h
index 2240904..a0ad530 100644
--- a/articles.h
+++ b/articles.h
@@ -25,6 +25,8 @@
#include "libterm/term_io.h"
#include "logs.h"
+#include "sds.h"
+
//part of libadt
#include "list.h"