ftab.c
// insert or update a fd indexed by key
insert or update a fd indexed by key
insert or update a fd indexed by key
int ftab_put2(struct ftab* ft, char* key, int fd)
{
	int idx = times_33(key) % FTAB_SIZE;
	struct ftab_node* p = ft->tab[idx];
	struct ftab_node* pre = p;
	while (p)
	{
		if (strcmp(p->key, key) == 0) {
			p->fd = fd;
			break;
		}
		pre = p;
		p = p->next;
	}
	if (p == NULL) {
		unsigned int klen = strlen(key) + 1;
		char* kstr = malloc(klen);
		bzero(kstr, klen);
		if (kstr == NULL) {
			return -1;
		}
		struct ftab_node* node = malloc(sizeof(struct ftab_node));
		if (node == NULL) {
			free(node);
			kstr = NULL;
			return -1;
		}
		init_ftab_node(node);
		snprintf(kstr, klen, "%s", key);
		node->key = kstr;
		node->fd = fd;
		if (pre == NULL) {
			ft->tab[idx] = node;
		} else {
			pre->next = node;
		}
	}
	return 0;
}
int ftab_put2 ( struct ftab * ft , char * key , int fd ) { int idx = times_33 ( key ) % FTAB_SIZE ; struct ftab_node * p = ft -> tab [ idx ] ; struct ftab_node * pre = p ; while ( p ) { if ( strcmp ( p -> key , key ) == 0 ) { p -> fd = fd ; break ; } pre = p ; p = p -> next ; } if ( p == NULL ) { unsigned int klen = strlen ( key ) + 1 ; char * kstr = malloc ( klen ) ; bzero ( kstr , klen ) ; if ( kstr == NULL ) { return -1 ; } struct ftab_node * node = malloc ( sizeof ( struct ftab_node ) ) ; if ( node == NULL ) { free ( node ) ; kstr = NULL ; return -1 ; } init_ftab_node ( node ) ; snprintf ( kstr , klen , " " , key ) ; node -> key = kstr ; node -> fd = fd ; if ( pre == NULL ) { ft -> tab [ idx ] = node ; } else { pre -> next = node ; } } return 0 ; }
insert or update a fd indexed by key
insert or update a fd indexed by key
// if key is already stored, update // if key has not been stored, add it