X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fdb_iterator.c;h=94a4082047b3d4978afda6ed7617f7315c308328;hp=ad1b20b30513ae0506a35bdc6795a0cdcf2b3ddd;hb=b8cca229aaa89adcc41dbd7c7710515e983e60f2;hpb=29b24b6ba47681c8a26007492567c3daecb3defe diff --git a/ncurses/tinfo/db_iterator.c b/ncurses/tinfo/db_iterator.c index ad1b20b3..94a40820 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-2013,2014 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.22 2011/11/27 01:32:34 tom Exp $") +MODULE_ID("$Id: db_iterator.c,v 1.39 2014/11/01 14:47:00 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.22 2011/11/27 01:32:34 tom 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++ = ':'; - strcpy(last, text); + *last++ = NCURSES_PATHSEP; + _nc_STRCPY(last, text, limit); } } @@ -70,19 +72,19 @@ check_existence(const char *name, struct stat *sb) { bool result = FALSE; - if (stat(name, sb) == 0 && sb->st_size) { + if (stat(name, sb) == 0 + && (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 && sb->st_size) { + _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%s%s", name, DBM_SUFFIX); + if (stat(temp, sb) == 0 && S_ISREG(sb->st_mode)) { result = TRUE; } } #endif - return result; } @@ -94,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; @@ -110,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; @@ -148,7 +155,7 @@ cache_expired(void) result = TRUE; } else { DBDIRS n; - for (n = 0; n < dbdLAST; ++n) { + for (n = (DBDIRS) 0; n < dbdLAST; ++n) { if (my_vars[n].name != 0 && update_getenv(my_vars[n].name, n)) { result = TRUE; @@ -178,10 +185,12 @@ _nc_tic_dir(const char *path) if (path != 0) { TicDirectory = path; HaveTicDirectory = TRUE; - } else if (!HaveTicDirectory && use_terminfo_vars()) { - char *envp; - if ((envp = getenv("TERMINFO")) != 0) - return _nc_tic_dir(envp); + } else if (HaveTicDirectory == 0) { + if (use_terminfo_vars()) { + const char *envp; + if ((envp = getenv("TERMINFO")) != 0) + return _nc_tic_dir(envp); + } } } return TicDirectory ? TicDirectory : TERMINFO; @@ -238,6 +247,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; @@ -246,12 +256,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; @@ -261,7 +274,7 @@ _nc_first_db(DBDIRS * state, int *offset) */ values[dbdTIC] = TicDirectory; -#if USE_DATABASE +#if NCURSES_USE_DATABASE #ifdef TERMINFO_DIRS values[dbdCfgList] = TERMINFO_DIRS; #endif @@ -270,18 +283,19 @@ _nc_first_db(DBDIRS * state, int *offset) #endif #endif -#if USE_TERMCAP +#if NCURSES_USE_TERMCAP values[dbdCfgList2] = TERMPATH; #endif if (use_terminfo_vars()) { -#if USE_DATABASE +#if NCURSES_USE_DATABASE values[dbdEnvOnce] = cache_getenv("TERMINFO", dbdEnvOnce); values[dbdHome] = _nc_home_terminfo(); (void) cache_getenv("HOME", dbdHome); values[dbdEnvList] = cache_getenv("TERMINFO_DIRS", dbdEnvList); + #endif -#if USE_TERMCAP +#if NCURSES_USE_TERMCAP values[dbdEnvOnce2] = cache_getenv("TERMCAP", dbdEnvOnce2); /* only use $TERMCAP if it is an absolute path */ if (values[dbdEnvOnce2] != 0 @@ -289,7 +303,7 @@ _nc_first_db(DBDIRS * state, int *offset) values[dbdEnvOnce2] = 0; } values[dbdEnvList2] = cache_getenv("TERMPATH", dbdEnvList2); -#endif /* USE_TERMCAP */ +#endif /* NCURSES_USE_TERMCAP */ } for (j = 0; j < dbdLAST; ++j) { @@ -302,7 +316,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 @@ -310,7 +324,7 @@ _nc_first_db(DBDIRS * state, int *offset) */ blobsize = 2; for (j = 0; my_blob[j] != '\0'; ++j) { - if (my_blob[j] == ':') + if (my_blob[j] == NCURSES_PATHSEP) ++blobsize; } my_list = typeCalloc(char *, blobsize); @@ -319,7 +333,7 @@ _nc_first_db(DBDIRS * state, int *offset) k = 0; my_list[k++] = my_blob; for (j = 0; my_blob[j] != '\0'; ++j) { - if (my_blob[j] == ':') { + if (my_blob[j] == NCURSES_PATHSEP) { my_blob[j] = '\0'; my_list[k++] = &my_blob[j + 1]; } @@ -331,7 +345,7 @@ _nc_first_db(DBDIRS * state, int *offset) for (j = 0; my_list[j] != 0; ++j) { #ifdef TERMINFO if (*my_list[j] == '\0') - my_list[j] = TERMINFO; + my_list[j] = strdup(TERMINFO); #endif for (k = 0; k < j; ++k) { if (!strcmp(my_list[j], my_list[k])) { @@ -384,9 +398,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