char *get_home_dir(const char *name) { hash_struct *current; unsigned int hash; hash = get_homedir_hash_value(name); current = hash_find(passwd_hashtable, name, hash); if (!current) { /* not found */ struct passwd *passwdbuf; passwdbuf = getpwnam(name); if (!passwdbuf) /* does not exist */ return NULL; current = hash_insert(passwd_hashtable, hash, name, passwdbuf->pw_dir); } return (current ? current->value : NULL); }
char *get_home_dir(char *name) { struct passwd *passwdbuf; hash_struct *current, *next; unsigned int hash; /* first check hash table -- if username is less than four characters, just hash to zero (this should be very rare) */ hash = get_homedir_hash_value(name); for(current = passwd_hashtable[hash];current;current = current->next) { if (!strcmp(current->key, name)) return current->value; if (!current->next) break; } passwdbuf = getpwnam(name); if (!passwdbuf) return NULL; next = (hash_struct *) malloc(sizeof (hash_struct)); if (!next) { WARN("malloc of hash_struct for passwd_hashtable failed!"); return NULL; } next->key = strdup(name); if (!next->key) { WARN("malloc of passwd_hashtable[hash]->key failed!"); free(next); return NULL; } next->value = strdup(passwdbuf->pw_dir); if (!next->value) { WARN("malloc of passwd_hashtable[hash]->value failed!"); free(next->key); free(next); return NULL; } next->next = NULL; if (!current) { passwd_hashtable[hash] = next; } else { current->next = next; } return next->value; }