Also attached some cleanup to menu_filesel.c, mainly to make it more

robust in case of lack of memory.
patch by Björn Sandell <biorn@dce.chalmers.se>


git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@9105 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
arpi 2003-01-26 16:03:16 +00:00
parent db1e3c66af
commit 9e46271417
1 changed files with 9 additions and 6 deletions

View File

@ -98,9 +98,6 @@ static int mylstat(char *dir, char *file,struct stat* st) {
} }
static int compare(char **a, char **b){ static int compare(char **a, char **b){
int la,lb;
la = strlen(*a);
lb = strlen(*b);
if((*a)[strlen(*a) - 1] == '/') { if((*a)[strlen(*a) - 1] == '/') {
if((*b)[strlen(*b) - 1] == '/') if((*b)[strlen(*b) - 1] == '/')
return strcmp(*b, *a) ; return strcmp(*b, *a) ;
@ -149,6 +146,7 @@ static int open_dir(menu_t* menu,char* args) {
if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *))) if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *)))
== NULL) { == NULL) {
printf("realloc error: %s", strerror(errno)); printf("realloc error: %s", strerror(errno));
n--;
goto bailout; goto bailout;
} }
namelist=tp; namelist=tp;
@ -157,6 +155,7 @@ static int open_dir(menu_t* menu,char* args) {
namelist[n] = (char *) malloc(strlen(dp->d_name) + 2); namelist[n] = (char *) malloc(strlen(dp->d_name) + 2);
if(namelist[n] == NULL){ if(namelist[n] == NULL){
printf("malloc error: %s", strerror(errno)); printf("malloc error: %s", strerror(errno));
n--;
goto bailout; goto bailout;
} }
@ -166,20 +165,24 @@ static int open_dir(menu_t* menu,char* args) {
strcat(namelist[n], "/"); strcat(namelist[n], "/");
n++; n++;
} }
qsort(namelist, n, sizeof(char *), (kill_warn)compare);
bailout: bailout:
qsort(namelist, n, sizeof(char *), (kill_warn)compare);
if (n < 0) { if (n < 0) {
printf("scandir error: %s\n",strerror(errno)); printf("readdir error: %s\n",strerror(errno));
return 0; return 0;
} }
while(n--) { while(n--) {
e = calloc(1,sizeof(list_entry_t)); if((e = calloc(1,sizeof(list_entry_t))) != NULL){
e->p.next = NULL; e->p.next = NULL;
e->p.txt = strdup(namelist[n]); e->p.txt = strdup(namelist[n]);
if(strchr(namelist[n], '/') != NULL) if(strchr(namelist[n], '/') != NULL)
e->d = 1; e->d = 1;
menu_list_add_entry(menu,e); menu_list_add_entry(menu,e);
}else{
printf("malloc error: %s", strerror(errno));
}
free(namelist[n]); free(namelist[n]);
} }
free(namelist); free(namelist);