diff --git a/src/passbook.c b/src/passbook.c
index 5fc5c58a0a85e1df2834e73ee7518aefad3920f3..49df7ae5d1bffcec937a2ff58be1eb0c8c71963f 100644
--- a/src/passbook.c
+++ b/src/passbook.c
@@ -64,12 +64,12 @@ static node_t *node_new(const char *url, const cred_t cred){
/* updates a node's credential in place:
replaces p's credential with that from q and frees q */
-static void node_edit_cred(node_t * p, const node_t q){
+static void node_edit_cred(node_t * p, node_t *q){
free(p->cred.username);
free(p->cred.password);
- p->cred.username = q->username;
- p->cred.password = q->password;
+ p->cred.username = q->cred.username;
+ p->cred.password = q->cred.password;
free(q);
}
@@ -84,7 +84,7 @@ static void node_free(node_t *p){
we assume that if q has children then it cannot already
be present in p. Otherwise, if q has no children and we find its url in p,
then we edit the found entry in place while preserving its children */
-static node_t * node_insert(node_t *p, const node_t *q){
+static node_t * node_insert(node_t *p, node_t *q){
if (p == NULL){
return q;
}
@@ -94,10 +94,9 @@ static node_t * node_insert(node_t *p, const node_t *q){
node_t ** new = NULL;
node_t * const start = p;
while (new == NULL) {
- int ret = strcmp(p->url,p->url);
+ int ret = strcmp(q->url,p->url);
if (ret == 0){
assert (q->left == NULL && q->right == NULL && "illegal insertion");
-
/* edit the node in place */
node_edit_cred(p,q);
/* q is now freed so cannot be used anymore */
@@ -121,40 +120,10 @@ static node_t * node_insert(node_t *p, const node_t *q){
return start;
}
-/* FIXME: UP TO HERE. Now need to make this use node_insert */
/* returns a pointer to the tree with the node added or with the existing
node updated if it was already present */
static node_t * put(node_t *p, const char *url, const cred_t cred){
- if (p == NULL){
- return node_new(url,cred);
- }
-
- /* we store a pointer to a node pointer that remembers where in the
- tree the new node needs to be added */
- node_t ** new = NULL;
- node_t * const start = p;
- while (new == NULL) {
- int ret = strcmp(url,p->url);
- if (ret == 0){
- /* edit the node in place */
- node_edit(p,cred);
- return start;
- }else if (ret < 0){
- if (p->left == NULL){
- new = &(p->left);
- }else{
- p = p->left;
- }
- }else{
- if (p->right == NULL){
- new = &(p->right);
- }else{
- p = p->right;
- }
- }
- }
- *new = node_new(url,cred);
- return start;
+ return node_insert(p,node_new(url,cred));
}
/* returns a pointer to the tree with the node removed (if it was present) */
@@ -569,7 +538,7 @@ int main(void){
assert(NULL == lookup(p,"http://meh.com"));
assert(NULL == rem(p,"http://blah.com"));
p = put(NULL,"http://meh.com",cred2);
- p = put(p,"http://blah.com",cred1);
+ p = put(p,"http://blah.com",cred1);
assert(p == lookup(p,"http://meh.com"));
assert(strcmp(lookup(p,"http://blah.com")->cred.username,cred1.username) == 0);
p = put(p,"http://blah.com",cred2);