X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fdb_iterator.c;h=67467d4251a99552ce9b8889117361d44318826d;hp=3a4827df04627c8d710bd6e0177f62e98322fa88;hb=6c611e66965f5a8fb1a6696611f18441132852a2;hpb=43bb25186a3fa2cc24c08b3a1eedbb1e860c59a9 diff --git a/ncurses/tinfo/db_iterator.c b/ncurses/tinfo/db_iterator.c index 3a4827df..67467d42 100644 --- a/ncurses/tinfo/db_iterator.c +++ b/ncurses/tinfo/db_iterator.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2006-2010,2011 Free Software Foundation, Inc. * + * Copyright (c) 2006-2011,2012 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -43,7 +43,7 @@ #include #endif -MODULE_ID("$Id: db_iterator.c,v 1.26 2012/01/07 20:09:36 juergen Exp $") +MODULE_ID("$Id: db_iterator.c,v 1.34 2012/06/30 16:30:10 tom Exp $") #define HaveTicDirectory _nc_globals.have_tic_directory #define KeepTicDirectory _nc_globals.keep_tic_directory @@ -55,13 +55,15 @@ MODULE_ID("$Id: db_iterator.c,v 1.26 2012/01/07 20:09:36 juergen Exp $") #define my_vars _nc_globals.dbd_vars static void -add_to_blob(const char *text) +add_to_blob(const char *text, size_t limit) { + (void) limit; + if (*text != '\0') { char *last = my_blob + strlen(my_blob); if (last != my_blob) *last++ = NCURSES_PATHSEP; - strcpy(last, text); + _nc_STRCPY(last, text, limit); } } @@ -69,22 +71,16 @@ static bool check_existence(const char *name, struct stat *sb) { bool result = FALSE; + if (stat(name, sb) == 0 -#ifndef __MINGW32__ - && sb->st_size -#endif - ) { + && (S_ISDIR(sb->st_mode) || S_ISREG(sb->st_mode))) { result = TRUE; } #if USE_HASHED_DB else if (strlen(name) < PATH_MAX - sizeof(DBM_SUFFIX)) { char temp[PATH_MAX]; - sprintf(temp, "%s%s", name, DBM_SUFFIX); - if (stat(temp, sb) == 0 -#ifndef __MINGW32__ - && sb->st_size -#endif - ) { + _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%s%s", name, DBM_SUFFIX); + if (stat(temp, sb) == 0 && S_ISREG(sb->st_mode)) { result = TRUE; } } @@ -100,10 +96,13 @@ static bool update_getenv(const char *name, DBDIRS which) { bool result = FALSE; - char *value = getenv(name); if (which < dbdLAST) { - if (my_vars[which].name == 0 || strcmp(my_vars[which].name, name)) { + char *value; + + if ((value = getenv(name)) == 0 || (value = strdup(value)) == 0) { + ; + } else if (my_vars[which].name == 0 || strcmp(my_vars[which].name, name)) { FreeIfNeeded(my_vars[which].value); my_vars[which].name = name; my_vars[which].value = value; @@ -116,6 +115,8 @@ update_getenv(const char *name, DBDIRS which) FreeIfNeeded(my_vars[which].value); my_vars[which].value = value; result = TRUE; + } else { + free(value); } } return result; @@ -244,6 +245,7 @@ _nc_next_db(DBDIRS * state, int *offset) NCURSES_EXPORT(void) _nc_first_db(DBDIRS * state, int *offset) { + bool cache_has_expired = FALSE; *state = dbdTIC; *offset = 0; @@ -252,12 +254,15 @@ _nc_first_db(DBDIRS * state, int *offset) /* build a blob containing all of the strings we will use for a lookup * table. */ - if (my_blob == 0) { + if (my_blob == 0 || (cache_has_expired = cache_expired())) { size_t blobsize = 0; const char *values[dbdLAST]; struct stat *my_stat; int j, k; + if (cache_has_expired) + free_cache(); + for (j = 0; j < dbdLAST; ++j) values[j] = 0; @@ -309,7 +314,7 @@ _nc_first_db(DBDIRS * state, int *offset) if (my_blob != 0) { *my_blob = '\0'; for (j = 0; j < dbdLAST; ++j) { - add_to_blob(values[j]); + add_to_blob(values[j], blobsize); } /* Now, build an array which will be pointers to the distinct @@ -391,9 +396,16 @@ _nc_first_db(DBDIRS * state, int *offset) void _nc_db_iterator_leaks(void) { + DBDIRS which; + if (my_blob != 0) FreeAndNull(my_blob); if (my_list != 0) FreeAndNull(my_list); + for (which = 0; (int) which < dbdLAST; ++which) { + my_vars[which].name = 0; + FreeIfNeeded(my_vars[which].value); + my_vars[which].value = 0; + } } #endif