use CDR API calls instead of re-implementing them (bug #4726)
[asterisk/asterisk.git] / apps / app_forkcdr.c
index 9300dc2..23be3bc 100755 (executable)
@@ -40,28 +40,21 @@ STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-
-static void ast_cdr_clone(struct ast_cdr *cdr) 
-{
-       struct ast_cdr *newcdr = ast_cdr_alloc();
-       memcpy(newcdr,cdr,sizeof(struct ast_cdr));
-       ast_cdr_append(cdr,newcdr);
-       newcdr->start = ast_tvnow();
-       memset(&newcdr->answer, 0, sizeof(newcdr->answer));
-       memset(&newcdr->varshead, 0, sizeof(newcdr->varshead));
-       ast_cdr_copy_vars(newcdr, cdr);
-       if (!ast_test_flag(cdr, AST_CDR_FLAG_KEEP_VARS)) {
-               ast_cdr_free_vars(cdr, 0);
-       }
-       newcdr->disposition = AST_CDR_NOANSWER;
-       ast_set_flag(cdr, AST_CDR_FLAG_CHILD|AST_CDR_FLAG_LOCKED);
-}
-
 static void ast_cdr_fork(struct ast_channel *chan) 
 {
-       if(chan && chan->cdr) {
-               ast_cdr_clone(chan->cdr);
-       }
+       struct ast_cdr *cdr;
+       struct ast_cdr *newcdr;
+       if (!chan || !(cdr = chan->cdr))
+               return;
+       while (cdr->next)
+               cdr = cdr->next;
+       if (!(newcdr = ast_cdr_dup(cdr)))
+               return;
+       ast_cdr_append(cdr, newcdr);
+       ast_cdr_reset(newcdr, AST_CDR_FLAG_KEEP_VARS);
+       if (!ast_test_flag(cdr, AST_CDR_FLAG_KEEP_VARS))
+               ast_cdr_free_vars(cdr, 0);
+       ast_set_flag(cdr, AST_CDR_FLAG_CHILD | AST_CDR_FLAG_LOCKED);
 }
 
 static int forkcdr_exec(struct ast_channel *chan, void *data)