more ast_copy_string() conversion
authorKevin P. Fleming <kpfleming@digium.com>
Sun, 5 Jun 2005 16:32:16 +0000 (16:32 +0000)
committerKevin P. Fleming <kpfleming@digium.com>
Sun, 5 Jun 2005 16:32:16 +0000 (16:32 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5849 65c4cc65-6c06-0410-ace0-fbb531ad65f3

app.c
asterisk.c
channel.c
file.c
pbx.c

diff --git a/app.c b/app.c
index 07799f8..b61ff55 100755 (executable)
--- a/app.c
+++ b/app.c
@@ -384,7 +384,7 @@ int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, in
                        return -1;
                autoclose = 1;
                if (filename[0] == '/') 
                        return -1;
                autoclose = 1;
                if (filename[0] == '/') 
-                       strncpy(tmpf, filename, sizeof(tmpf) - 1);
+                       ast_copy_string(tmpf, filename, sizeof(tmpf));
                else
                        snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
                fd = open(tmpf, O_RDONLY);
                else
                        snprintf(tmpf, sizeof(tmpf), "%s/%s/%s", (char *)ast_config_AST_VAR_DIR, "sounds", filename);
                fd = open(tmpf, O_RDONLY);
@@ -773,7 +773,7 @@ int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordf
                if (d < 0)
                        return -1;
        }
                if (d < 0)
                        return -1;
        }
-       strncpy(prependfile, recordfile, sizeof(prependfile) -1);       
+       ast_copy_string(prependfile, recordfile, sizeof(prependfile));  
        strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
                        
        fmts = ast_strdupa(fmt);
        strncat(prependfile, "-prepend", sizeof(prependfile) - strlen(prependfile) - 1);
                        
        fmts = ast_strdupa(fmt);
@@ -971,7 +971,7 @@ int ast_app_group_split_group(char *data, char *group, int group_max, char *cate
        char *grp=NULL, *cat=NULL;
 
        if (data && !ast_strlen_zero(data)) {
        char *grp=NULL, *cat=NULL;
 
        if (data && !ast_strlen_zero(data)) {
-               strncpy(tmp, data, sizeof(tmp) - 1);
+               ast_copy_string(tmp, data, sizeof(tmp));
                grp = tmp;
                cat = strchr(tmp, '@');
                if (cat) {
                grp = tmp;
                cat = strchr(tmp, '@');
                if (cat) {
@@ -981,14 +981,14 @@ int ast_app_group_split_group(char *data, char *group, int group_max, char *cate
        }
 
        if (grp && !ast_strlen_zero(grp))
        }
 
        if (grp && !ast_strlen_zero(grp))
-               strncpy(group, grp, group_max -1);
+               ast_copy_string(group, grp, group_max);
        else
                res = -1;
 
        if (cat)
                snprintf(category, category_max, "%s_%s", GROUP_CATEGORY_PREFIX, cat);
        else
        else
                res = -1;
 
        if (cat)
                snprintf(category, category_max, "%s_%s", GROUP_CATEGORY_PREFIX, cat);
        else
-               strncpy(category, GROUP_CATEGORY_PREFIX, category_max - 1);
+               ast_copy_string(category, GROUP_CATEGORY_PREFIX, category_max);
 
        return res;
 }
 
        return res;
 }
@@ -1015,9 +1015,9 @@ int ast_app_group_get_count(char *group, char *category)
        char cat[80] = "";
 
        if (category && !ast_strlen_zero(category)) {
        char cat[80] = "";
 
        if (category && !ast_strlen_zero(category)) {
-               strncpy(cat, category, sizeof(cat) - 1);
+               ast_copy_string(cat, category, sizeof(cat));
        } else {
        } else {
-               strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
+               ast_copy_string(cat, GROUP_CATEGORY_PREFIX, sizeof(cat));
        }
 
        if (group && !ast_strlen_zero(group)) {
        }
 
        if (group && !ast_strlen_zero(group)) {
@@ -1050,9 +1050,9 @@ int ast_app_group_match_get_count(char *groupmatch, char *category)
                return 0;
 
        if (category && !ast_strlen_zero(category)) {
                return 0;
 
        if (category && !ast_strlen_zero(category)) {
-               strncpy(cat, category, sizeof(cat) - 1);
+               ast_copy_string(cat, category, sizeof(cat));
        } else {
        } else {
-               strncpy(cat, GROUP_CATEGORY_PREFIX, sizeof(cat) - 1);
+               ast_copy_string(cat, GROUP_CATEGORY_PREFIX, sizeof(cat));
        }
 
        chan = ast_channel_walk_locked(NULL);
        }
 
        chan = ast_channel_walk_locked(NULL);
index 734b568..94f1f34 100755 (executable)
@@ -282,7 +282,7 @@ static void *netconsole(void *vconsole)
        struct pollfd fds[2];
        
        if (gethostname(hostname, sizeof(hostname)-1))
        struct pollfd fds[2];
        
        if (gethostname(hostname, sizeof(hostname)-1))
-               strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
+               ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
        snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, ast_mainpid, ASTERISK_VERSION);
        fdprint(con->fd, tmp);
        for(;;) {
        snprintf(tmp, sizeof(tmp), "%s/%d/%s\n", hostname, ast_mainpid, ASTERISK_VERSION);
        fdprint(con->fd, tmp);
        for(;;) {
@@ -413,7 +413,7 @@ static int ast_makesocket(void)
        }               
        memset(&sunaddr, 0, sizeof(sunaddr));
        sunaddr.sun_family = AF_LOCAL;
        }               
        memset(&sunaddr, 0, sizeof(sunaddr));
        sunaddr.sun_family = AF_LOCAL;
-       strncpy(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path)-1);
+       ast_copy_string(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
        res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
        if (res) {
                ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", (char *)ast_config_AST_SOCKET, strerror(errno));
        res = bind(ast_socket, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
        if (res) {
                ast_log(LOG_WARNING, "Unable to bind socket to %s: %s\n", (char *)ast_config_AST_SOCKET, strerror(errno));
@@ -486,7 +486,7 @@ static int ast_tryconnect(void)
        }
        memset(&sunaddr, 0, sizeof(sunaddr));
        sunaddr.sun_family = AF_LOCAL;
        }
        memset(&sunaddr, 0, sizeof(sunaddr));
        sunaddr.sun_family = AF_LOCAL;
-       strncpy(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path)-1);
+       ast_copy_string(sunaddr.sun_path, (char *)ast_config_AST_SOCKET, sizeof(sunaddr.sun_path));
        res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
        if (res) {
                close(ast_consock);
        res = connect(ast_consock, (struct sockaddr *)&sunaddr, sizeof(sunaddr));
        if (res) {
                close(ast_consock);
@@ -1558,17 +1558,17 @@ static void ast_readconfig(void) {
        }
 
        /* init with buildtime config */
        }
 
        /* init with buildtime config */
-       strncpy((char *)ast_config_AST_CONFIG_DIR,AST_CONFIG_DIR,sizeof(ast_config_AST_CONFIG_DIR)-1);
-       strncpy((char *)ast_config_AST_SPOOL_DIR,AST_SPOOL_DIR,sizeof(ast_config_AST_SPOOL_DIR)-1);
-       strncpy((char *)ast_config_AST_MODULE_DIR,AST_MODULE_DIR,sizeof(ast_config_AST_VAR_DIR)-1);
-       strncpy((char *)ast_config_AST_VAR_DIR,AST_VAR_DIR,sizeof(ast_config_AST_VAR_DIR)-1);
-       strncpy((char *)ast_config_AST_LOG_DIR,AST_LOG_DIR,sizeof(ast_config_AST_LOG_DIR)-1);
-       strncpy((char *)ast_config_AST_AGI_DIR,AST_AGI_DIR,sizeof(ast_config_AST_AGI_DIR)-1);
-       strncpy((char *)ast_config_AST_DB,AST_DB,sizeof(ast_config_AST_DB)-1);
-       strncpy((char *)ast_config_AST_KEY_DIR,AST_KEY_DIR,sizeof(ast_config_AST_KEY_DIR)-1);
-       strncpy((char *)ast_config_AST_PID,AST_PID,sizeof(ast_config_AST_PID)-1);
-       strncpy((char *)ast_config_AST_SOCKET,AST_SOCKET,sizeof(ast_config_AST_SOCKET)-1);
-       strncpy((char *)ast_config_AST_RUN_DIR,AST_RUN_DIR,sizeof(ast_config_AST_RUN_DIR)-1);
+       ast_copy_string((char *)ast_config_AST_CONFIG_DIR,AST_CONFIG_DIR,sizeof(ast_config_AST_CONFIG_DIR));
+       ast_copy_string((char *)ast_config_AST_SPOOL_DIR,AST_SPOOL_DIR,sizeof(ast_config_AST_SPOOL_DIR));
+       ast_copy_string((char *)ast_config_AST_MODULE_DIR,AST_MODULE_DIR,sizeof(ast_config_AST_VAR_DIR));
+       ast_copy_string((char *)ast_config_AST_VAR_DIR,AST_VAR_DIR,sizeof(ast_config_AST_VAR_DIR));
+       ast_copy_string((char *)ast_config_AST_LOG_DIR,AST_LOG_DIR,sizeof(ast_config_AST_LOG_DIR));
+       ast_copy_string((char *)ast_config_AST_AGI_DIR,AST_AGI_DIR,sizeof(ast_config_AST_AGI_DIR));
+       ast_copy_string((char *)ast_config_AST_DB,AST_DB,sizeof(ast_config_AST_DB));
+       ast_copy_string((char *)ast_config_AST_KEY_DIR,AST_KEY_DIR,sizeof(ast_config_AST_KEY_DIR));
+       ast_copy_string((char *)ast_config_AST_PID,AST_PID,sizeof(ast_config_AST_PID));
+       ast_copy_string((char *)ast_config_AST_SOCKET,AST_SOCKET,sizeof(ast_config_AST_SOCKET));
+       ast_copy_string((char *)ast_config_AST_RUN_DIR,AST_RUN_DIR,sizeof(ast_config_AST_RUN_DIR));
        
        /* no asterisk.conf? no problem, use buildtime config! */
        if (!cfg) {
        
        /* no asterisk.conf? no problem, use buildtime config! */
        if (!cfg) {
@@ -1585,22 +1585,22 @@ static void ast_readconfig(void) {
        v = ast_variable_browse(cfg, "directories");
        while(v) {
                if (!strcasecmp(v->name, "astetcdir")) {
        v = ast_variable_browse(cfg, "directories");
        while(v) {
                if (!strcasecmp(v->name, "astetcdir")) {
-                       strncpy((char *)ast_config_AST_CONFIG_DIR,v->value,sizeof(ast_config_AST_CONFIG_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_CONFIG_DIR,v->value,sizeof(ast_config_AST_CONFIG_DIR));
                } else if (!strcasecmp(v->name, "astspooldir")) {
                } else if (!strcasecmp(v->name, "astspooldir")) {
-                       strncpy((char *)ast_config_AST_SPOOL_DIR,v->value,sizeof(ast_config_AST_SPOOL_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_SPOOL_DIR,v->value,sizeof(ast_config_AST_SPOOL_DIR));
                } else if (!strcasecmp(v->name, "astvarlibdir")) {
                } else if (!strcasecmp(v->name, "astvarlibdir")) {
-                       strncpy((char *)ast_config_AST_VAR_DIR,v->value,sizeof(ast_config_AST_VAR_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_VAR_DIR,v->value,sizeof(ast_config_AST_VAR_DIR));
                        snprintf((char *)ast_config_AST_DB,sizeof(ast_config_AST_DB),"%s/%s",v->value,"astdb");    
                } else if (!strcasecmp(v->name, "astlogdir")) {
                        snprintf((char *)ast_config_AST_DB,sizeof(ast_config_AST_DB),"%s/%s",v->value,"astdb");    
                } else if (!strcasecmp(v->name, "astlogdir")) {
-                       strncpy((char *)ast_config_AST_LOG_DIR,v->value,sizeof(ast_config_AST_LOG_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_LOG_DIR,v->value,sizeof(ast_config_AST_LOG_DIR));
                } else if (!strcasecmp(v->name, "astagidir")) {
                } else if (!strcasecmp(v->name, "astagidir")) {
-                       strncpy((char *)ast_config_AST_AGI_DIR,v->value,sizeof(ast_config_AST_AGI_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_AGI_DIR,v->value,sizeof(ast_config_AST_AGI_DIR));
                } else if (!strcasecmp(v->name, "astrundir")) {
                        snprintf((char *)ast_config_AST_PID,sizeof(ast_config_AST_PID),"%s/%s",v->value,"asterisk.pid");
                        snprintf((char *)ast_config_AST_SOCKET,sizeof(ast_config_AST_SOCKET),"%s/%s",v->value,v_ctlfile==NULL?"asterisk.ctl":v_ctlfile->value);
                } else if (!strcasecmp(v->name, "astrundir")) {
                        snprintf((char *)ast_config_AST_PID,sizeof(ast_config_AST_PID),"%s/%s",v->value,"asterisk.pid");
                        snprintf((char *)ast_config_AST_SOCKET,sizeof(ast_config_AST_SOCKET),"%s/%s",v->value,v_ctlfile==NULL?"asterisk.ctl":v_ctlfile->value);
-                       strncpy((char *)ast_config_AST_RUN_DIR,v->value,sizeof(ast_config_AST_RUN_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_RUN_DIR,v->value,sizeof(ast_config_AST_RUN_DIR));
                } else if (!strcasecmp(v->name, "astmoddir")) {
                } else if (!strcasecmp(v->name, "astmoddir")) {
-                       strncpy((char *)ast_config_AST_MODULE_DIR,v->value,sizeof(ast_config_AST_MODULE_DIR)-1);
+                       ast_copy_string((char *)ast_config_AST_MODULE_DIR,v->value,sizeof(ast_config_AST_MODULE_DIR));
                }
                v = v->next;
        }
                }
                v = v->next;
        }
@@ -1650,7 +1650,7 @@ static void ast_readconfig(void) {
                        option_cache_record_files = ast_true(v->value);
                /* Specify cache directory */
                }  else if (!strcasecmp(v->name, "record_cache_dir")) {
                        option_cache_record_files = ast_true(v->value);
                /* Specify cache directory */
                }  else if (!strcasecmp(v->name, "record_cache_dir")) {
-                       strncpy(record_cache_dir,v->value,AST_CACHE_DIR_LEN);
+                       ast_copy_string(record_cache_dir,v->value,AST_CACHE_DIR_LEN);
                /* Build transcode paths via SLINEAR, instead of directly */
                } else if (!strcasecmp(v->name, "transcode_via_sln")) {
                        option_transcode_slin = ast_true(v->value);
                /* Build transcode paths via SLINEAR, instead of directly */
                } else if (!strcasecmp(v->name, "transcode_via_sln")) {
                        option_transcode_slin = ast_true(v->value);
@@ -1694,7 +1694,7 @@ int main(int argc, char *argv[])
                option_nofork++;
        }
        if (gethostname(hostname, sizeof(hostname)-1))
                option_nofork++;
        }
        if (gethostname(hostname, sizeof(hostname)-1))
-               strncpy(hostname, "<Unknown>", sizeof(hostname)-1);
+               ast_copy_string(hostname, "<Unknown>", sizeof(hostname));
        ast_mainpid = getpid();
        ast_ulaw_init();
        ast_alaw_init();
        ast_mainpid = getpid();
        ast_ulaw_init();
        ast_alaw_init();
@@ -1761,7 +1761,7 @@ int main(int argc, char *argv[])
                        xarg = optarg;
                        break;
                case 'C':
                        xarg = optarg;
                        break;
                case 'C':
-                       strncpy((char *)ast_config_AST_CONFIG_FILE,optarg,sizeof(ast_config_AST_CONFIG_FILE) - 1);
+                       ast_copy_string((char *)ast_config_AST_CONFIG_FILE,optarg,sizeof(ast_config_AST_CONFIG_FILE));
                        option_overrideconfig++;
                        break;
                case 'i':
                        option_overrideconfig++;
                        break;
                case 'i':
index 0018b63..d321759 100755 (executable)
--- a/channel.c
+++ b/channel.c
@@ -393,7 +393,7 @@ struct ast_channel *ast_channel_alloc(int needqueue)
        tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
        /* And timing pipe */
        tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
        tmp->fds[AST_MAX_FDS-1] = tmp->alertpipe[0];
        /* And timing pipe */
        tmp->fds[AST_MAX_FDS-2] = tmp->timingfd;
-       strncpy(tmp->name, "**Unknown**", sizeof(tmp->name)-1);
+       ast_copy_string(tmp->name, "**Unknown**", sizeof(tmp->name));
        /* Initial state */
        tmp->_state = AST_STATE_DOWN;
        tmp->streamid = -1;
        /* Initial state */
        tmp->_state = AST_STATE_DOWN;
        tmp->streamid = -1;
@@ -405,12 +405,12 @@ struct ast_channel *ast_channel_alloc(int needqueue)
        headp = &tmp->varshead;
        ast_mutex_init(&tmp->lock);
        AST_LIST_HEAD_INIT(headp);
        headp = &tmp->varshead;
        ast_mutex_init(&tmp->lock);
        AST_LIST_HEAD_INIT(headp);
-       strncpy(tmp->context, "default", sizeof(tmp->context)-1);
-       strncpy(tmp->language, defaultlanguage, sizeof(tmp->language)-1);
-       strncpy(tmp->exten, "s", sizeof(tmp->exten)-1);
+       ast_copy_string(tmp->context, "default", sizeof(tmp->context));
+       ast_copy_string(tmp->language, defaultlanguage, sizeof(tmp->language));
+       ast_copy_string(tmp->exten, "s", sizeof(tmp->exten));
        tmp->priority = 1;
        tmp->amaflags = ast_default_amaflags;
        tmp->priority = 1;
        tmp->amaflags = ast_default_amaflags;
-       strncpy(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode)-1);
+       ast_copy_string(tmp->accountcode, ast_default_accountcode, sizeof(tmp->accountcode));
 
        tmp->tech = &null_tech;
 
 
        tmp->tech = &null_tech;
 
@@ -674,7 +674,7 @@ void ast_channel_free(struct ast_channel *chan)
                free(chan->tech_pvt);
        }
 
                free(chan->tech_pvt);
        }
 
-       strncpy(name, chan->name, sizeof(name)-1);
+       ast_copy_string(name, chan->name, sizeof(name));
        
        /* Stop monitoring */
        if (chan->monitor) {
        
        /* Stop monitoring */
        if (chan->monitor) {
@@ -1994,9 +1994,9 @@ struct ast_channel *__ast_request_and_dial(const char *type, int format, void *d
                /* Final fixups */
                if (oh) {
                        if (oh->context && *oh->context)
                /* Final fixups */
                if (oh) {
                        if (oh->context && *oh->context)
-                               strncpy(chan->context, oh->context, sizeof(chan->context) - 1);
+                               ast_copy_string(chan->context, oh->context, sizeof(chan->context));
                        if (oh->exten && *oh->exten)
                        if (oh->exten && *oh->exten)
-                               strncpy(chan->exten, oh->exten, sizeof(chan->exten) - 1);
+                               ast_copy_string(chan->exten, oh->exten, sizeof(chan->exten));
                        chan->priority = oh->priority;
                }
                if (chan->_state == AST_STATE_UP) 
                        chan->priority = oh->priority;
                }
                if (chan->_state == AST_STATE_UP) 
@@ -2093,7 +2093,7 @@ int ast_parse_device_state(char *device)
 
        chan = ast_channel_walk_locked(NULL);
        while (chan) {
 
        chan = ast_channel_walk_locked(NULL);
        while (chan) {
-               strncpy(name, chan->name, sizeof(name)-1);
+               ast_copy_string(name, chan->name, sizeof(name));
                ast_mutex_unlock(&chan->lock);
                cut = strchr(name,'-');
                if (cut)
                ast_mutex_unlock(&chan->lock);
                cut = strchr(name,'-');
                if (cut)
@@ -2117,7 +2117,7 @@ int ast_device_state(char *device)
        struct chanlist *chanls;
        int res = 0;
        
        struct chanlist *chanls;
        int res = 0;
        
-       strncpy(tech, device, sizeof(tech)-1);
+       ast_copy_string(tech, device, sizeof(tech));
        number = strchr(tech, '/');
        if (!number) {
            return AST_DEVICE_INVALID;
        number = strchr(tech, '/');
        if (!number) {
            return AST_DEVICE_INVALID;
@@ -2373,8 +2373,8 @@ int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clo
 void ast_change_name(struct ast_channel *chan, char *newname)
 {
        char tmp[256];
 void ast_change_name(struct ast_channel *chan, char *newname)
 {
        char tmp[256];
-       strncpy(tmp, chan->name, sizeof(tmp) - 1);
-       strncpy(chan->name, newname, sizeof(chan->name) - 1);
+       ast_copy_string(tmp, chan->name, sizeof(tmp));
+       ast_copy_string(chan->name, newname, sizeof(chan->name));
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
 }
 
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", tmp, chan->name, chan->uniqueid);
 }
 
@@ -2495,17 +2495,17 @@ int ast_do_masquerade(struct ast_channel *original)
        clone->masqr = NULL;
        
        /* Save the original name */
        clone->masqr = NULL;
        
        /* Save the original name */
-       strncpy(orig, original->name, sizeof(orig) - 1);
+       ast_copy_string(orig, original->name, sizeof(orig));
        /* Save the new name */
        /* Save the new name */
-       strncpy(newn, clone->name, sizeof(newn) - 1);
+       ast_copy_string(newn, clone->name, sizeof(newn));
        /* Create the masq name */
        snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
                
        /* Copy the name from the clone channel */
        /* Create the masq name */
        snprintf(masqn, sizeof(masqn), "%s<MASQ>", newn);
                
        /* Copy the name from the clone channel */
-       strncpy(original->name, newn, sizeof(original->name)-1);
+       ast_copy_string(original->name, newn, sizeof(original->name));
 
        /* Mangle the name of the clone channel */
 
        /* Mangle the name of the clone channel */
-       strncpy(clone->name, masqn, sizeof(clone->name) - 1);
+       ast_copy_string(clone->name, masqn, sizeof(clone->name));
        
        /* Notify any managers of the change, first the masq then the other */
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
        
        /* Notify any managers of the change, first the masq then the other */
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn, masqn, clone->uniqueid);
@@ -2589,14 +2589,14 @@ int ast_do_masquerade(struct ast_channel *original)
        
        snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
        /* Mangle the name of the clone channel */
        
        snprintf(zombn, sizeof(zombn), "%s<ZOMBIE>", orig);
        /* Mangle the name of the clone channel */
-       strncpy(clone->name, zombn, sizeof(clone->name) - 1);
+       ast_copy_string(clone->name, zombn, sizeof(clone->name));
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
 
        /* Update the type. */
        original->type = clone->type;
        
        /* Keep the same language.  */
        manager_event(EVENT_FLAG_CALL, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn, zombn, clone->uniqueid);
 
        /* Update the type. */
        original->type = clone->type;
        
        /* Keep the same language.  */
-       strncpy(original->language, clone->language, sizeof(original->language));
+       ast_copy_string(original->language, clone->language, sizeof(original->language));
        /* Copy the FD's */
        for (x=0;x<AST_MAX_FDS;x++) {
                original->fds[x] = clone->fds[x];
        /* Copy the FD's */
        for (x=0;x<AST_MAX_FDS;x++) {
                original->fds[x] = clone->fds[x];
@@ -2638,7 +2638,7 @@ int ast_do_masquerade(struct ast_channel *original)
        ast_set_read_format(original, rformat);
 
        /* Copy the music class */
        ast_set_read_format(original, rformat);
 
        /* Copy the music class */
-       strncpy(original->musicclass, clone->musicclass, sizeof(original->musicclass) - 1);
+       ast_copy_string(original->musicclass, clone->musicclass, sizeof(original->musicclass));
 
        ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
 
 
        ast_log(LOG_DEBUG, "Putting channel %s in %d/%d formats\n", original->name, wformat, rformat);
 
diff --git a/file.c b/file.c
index 1c4e42b..cbe7280 100755 (executable)
--- a/file.c
+++ b/file.c
@@ -119,8 +119,8 @@ int ast_format_register(const char *name, const char *exts, int format,
                ast_mutex_unlock(&formatlock);
                return -1;
        }
                ast_mutex_unlock(&formatlock);
                return -1;
        }
-       strncpy(tmp->name, name, sizeof(tmp->name)-1);
-       strncpy(tmp->exts, exts, sizeof(tmp->exts)-1);
+       ast_copy_string(tmp->name, name, sizeof(tmp->name));
+       ast_copy_string(tmp->exts, exts, sizeof(tmp->exts));
        tmp->open = open;
        tmp->rewrite = rewrite;
        tmp->read = read;
        tmp->open = open;
        tmp->rewrite = rewrite;
        tmp->read = read;
@@ -285,9 +285,9 @@ static char *build_filename(const char *filename, const char *ext)
        int fnsize = 0;
 
        if (!strcmp(ext, "wav49")) {
        int fnsize = 0;
 
        if (!strcmp(ext, "wav49")) {
-               strncpy(type, "WAV", sizeof(type) - 1);
+               ast_copy_string(type, "WAV", sizeof(type));
        } else {
        } else {
-               strncpy(type, ext, sizeof(type) - 1);
+               ast_copy_string(type, ext, sizeof(type));
        }
 
        if (filename[0] == '/') {
        }
 
        if (filename[0] == '/') {
@@ -313,7 +313,7 @@ static int exts_compare(const char *exts, const char *type)
        char *stringp = NULL, *ext;
        char tmp[256];
 
        char *stringp = NULL, *ext;
        char tmp[256];
 
-       strncpy(tmp, exts, sizeof(tmp) - 1);
+       ast_copy_string(tmp, exts, sizeof(tmp));
        stringp = tmp;
        while ((ext = strsep(&stringp, "|"))) {
                if (!strcmp(ext, type)) {
        stringp = tmp;
        while ((ext = strsep(&stringp, "|"))) {
                if (!strcmp(ext, type)) {
@@ -472,7 +472,7 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
                        ast_deactivate_generator(chan);
        }
        if (preflang && !ast_strlen_zero(preflang)) {
                        ast_deactivate_generator(chan);
        }
        if (preflang && !ast_strlen_zero(preflang)) {
-               strncpy(filename3, filename, sizeof(filename3) - 1);
+               ast_copy_string(filename3, filename, sizeof(filename3));
                endpart = strrchr(filename3, '/');
                if (endpart) {
                        *endpart = '\0';
                endpart = strrchr(filename3, '/');
                if (endpart) {
                        *endpart = '\0';
@@ -483,7 +483,7 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
                fmts = ast_fileexists(filename2, NULL, NULL);
        }
        if (fmts < 1) {
                fmts = ast_fileexists(filename2, NULL, NULL);
        }
        if (fmts < 1) {
-               strncpy(filename2, filename, sizeof(filename2)-1);
+               ast_copy_string(filename2, filename, sizeof(filename2));
                fmts = ast_fileexists(filename2, NULL, NULL);
        }
        if (fmts < 1) {
                fmts = ast_fileexists(filename2, NULL, NULL);
        }
        if (fmts < 1) {
@@ -524,13 +524,13 @@ struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *fil
                snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename);
                fmts = ast_fileexists(filename2, fmt, NULL);
                if (fmts < 1) {
                snprintf(filename2, sizeof(filename2), "%s/%s", preflang, filename);
                fmts = ast_fileexists(filename2, fmt, NULL);
                if (fmts < 1) {
-                       strncpy(lang2, preflang, sizeof(lang2)-1);
+                       ast_copy_string(lang2, preflang, sizeof(lang2));
                        snprintf(filename2, sizeof(filename2), "%s/%s", lang2, filename);
                        fmts = ast_fileexists(filename2, fmt, NULL);
                }
        }
        if (fmts < 1) {
                        snprintf(filename2, sizeof(filename2), "%s/%s", lang2, filename);
                        fmts = ast_fileexists(filename2, fmt, NULL);
                }
        }
        if (fmts < 1) {
-               strncpy(filename2, filename, sizeof(filename2)-1);
+               ast_copy_string(filename2, filename, sizeof(filename2));
                fmts = ast_fileexists(filename2, fmt, NULL);
        }
        if (fmts < 1) {
                fmts = ast_fileexists(filename2, fmt, NULL);
        }
        if (fmts < 1) {
@@ -723,7 +723,7 @@ int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
        int res = -1;
        if (preflang && !ast_strlen_zero(preflang)) {
                /* Insert the language between the last two parts of the path */
        int res = -1;
        if (preflang && !ast_strlen_zero(preflang)) {
                /* Insert the language between the last two parts of the path */
-               strncpy(tmp, filename, sizeof(tmp) - 1);
+               ast_copy_string(tmp, filename, sizeof(tmp));
                c = strrchr(tmp, '/');
                if (c) {
                        *c = '\0';
                c = strrchr(tmp, '/');
                if (c) {
                        *c = '\0';
@@ -738,7 +738,7 @@ int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
                res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS);
                if (res < 1) {
                        char *stringp=NULL;
                res = ast_filehelper(filename2, NULL, fmt, ACTION_EXISTS);
                if (res < 1) {
                        char *stringp=NULL;
-                       strncpy(lang2, preflang, sizeof(lang2)-1);
+                       ast_copy_string(lang2, preflang, sizeof(lang2));
                        stringp=lang2;
                        strsep(&stringp, "_");
                        /* If language is a specific locality of a language (like es_MX), strip the locality and try again */
                        stringp=lang2;
                        strsep(&stringp, "_");
                        /* If language is a specific locality of a language (like es_MX), strip the locality and try again */
diff --git a/pbx.c b/pbx.c
index f7f1a02..23b000e 100755 (executable)
--- a/pbx.c
+++ b/pbx.c
@@ -913,7 +913,7 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                headp=&c->varshead;
        *ret=NULL;
        if ((first=strchr(var,':'))) {  /* : Remove characters counting from end or start of string */
                headp=&c->varshead;
        *ret=NULL;
        if ((first=strchr(var,':'))) {  /* : Remove characters counting from end or start of string */
-               strncpy(tmpvar, var, sizeof(tmpvar) - 1);
+               ast_copy_string(tmpvar, var, sizeof(tmpvar));
                first = strchr(tmpvar, ':');
                if (!first)
                        first = tmpvar + strlen(tmpvar);
                first = strchr(tmpvar, ':');
                if (!first)
                        first = tmpvar + strlen(tmpvar);
@@ -959,25 +959,25 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                                                if (c->cid.cid_name) {
                                                        snprintf(workspace, workspacelen, "\"%s\" <%s>", c->cid.cid_name, c->cid.cid_num);
                                                } else {
                                                if (c->cid.cid_name) {
                                                        snprintf(workspace, workspacelen, "\"%s\" <%s>", c->cid.cid_name, c->cid.cid_num);
                                                } else {
-                                                       strncpy(workspace, c->cid.cid_num, workspacelen - 1);
+                                                       ast_copy_string(workspace, c->cid.cid_num, workspacelen);
                                                }
                                                *ret = workspace;
                                        } else if (c->cid.cid_name) {
                                                }
                                                *ret = workspace;
                                        } else if (c->cid.cid_name) {
-                                               strncpy(workspace, c->cid.cid_name, workspacelen - 1);
+                                               ast_copy_string(workspace, c->cid.cid_name, workspacelen);
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
                                } else if (!strcmp(var + 8, "NUM")) {
                                        /* CALLERIDNUM */
                                        if (c->cid.cid_num) {
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
                                } else if (!strcmp(var + 8, "NUM")) {
                                        /* CALLERIDNUM */
                                        if (c->cid.cid_num) {
-                                               strncpy(workspace, c->cid.cid_num, workspacelen - 1);
+                                               ast_copy_string(workspace, c->cid.cid_num, workspacelen);
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
                                } else if (!strcmp(var + 8, "NAME")) {
                                        /* CALLERIDNAME */
                                        if (c->cid.cid_name) {
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
                                } else if (!strcmp(var + 8, "NAME")) {
                                        /* CALLERIDNAME */
                                        if (c->cid.cid_name) {
-                                               strncpy(workspace, c->cid.cid_name, workspacelen - 1);
+                                               ast_copy_string(workspace, c->cid.cid_name, workspacelen);
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
                                                *ret = workspace;
                                        } else
                                                *ret = NULL;
@@ -985,7 +985,7 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                        } else if (!strcmp(var + 6, "ANI")) {
                                /* CALLERANI */
                                if (c->cid.cid_ani) {
                        } else if (!strcmp(var + 6, "ANI")) {
                                /* CALLERANI */
                                if (c->cid.cid_ani) {
-                                       strncpy(workspace, c->cid.cid_ani, workspacelen - 1);
+                                       ast_copy_string(workspace, c->cid.cid_ani, workspacelen);
                                        *ret = workspace;
                                } else
                                        *ret = NULL;
                                        *ret = workspace;
                                } else
                                        *ret = NULL;
@@ -1014,7 +1014,7 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                        goto icky;
        } else if (c && !strcmp(var, "DNID")) {
                if (c->cid.cid_dnid) {
                        goto icky;
        } else if (c && !strcmp(var, "DNID")) {
                if (c->cid.cid_dnid) {
-                       strncpy(workspace, c->cid.cid_dnid, workspacelen - 1);
+                       ast_copy_string(workspace, c->cid.cid_dnid, workspacelen);
                        *ret = workspace;
                } else
                        *ret = NULL;
                        *ret = workspace;
                } else
                        *ret = NULL;
@@ -1029,22 +1029,22 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                else
                        *ret = workspace;
        } else if (c && !strcmp(var, "EXTEN")) {
                else
                        *ret = workspace;
        } else if (c && !strcmp(var, "EXTEN")) {
-               strncpy(workspace, c->exten, workspacelen - 1);
+               ast_copy_string(workspace, c->exten, workspacelen);
                *ret = workspace;
        } else if (c && !strcmp(var, "RDNIS")) {
                if (c->cid.cid_rdnis) {
                *ret = workspace;
        } else if (c && !strcmp(var, "RDNIS")) {
                if (c->cid.cid_rdnis) {
-                       strncpy(workspace, c->cid.cid_rdnis, workspacelen - 1);
+                       ast_copy_string(workspace, c->cid.cid_rdnis, workspacelen);
                        *ret = workspace;
                } else
                        *ret = NULL;
        } else if (c && !strcmp(var, "CONTEXT")) {
                        *ret = workspace;
                } else
                        *ret = NULL;
        } else if (c && !strcmp(var, "CONTEXT")) {
-               strncpy(workspace, c->context, workspacelen - 1);
+               ast_copy_string(workspace, c->context, workspacelen);
                *ret = workspace;
        } else if (c && !strcmp(var, "PRIORITY")) {
                snprintf(workspace, workspacelen, "%d", c->priority);
                *ret = workspace;
        } else if (c && !strcmp(var, "CHANNEL")) {
                *ret = workspace;
        } else if (c && !strcmp(var, "PRIORITY")) {
                snprintf(workspace, workspacelen, "%d", c->priority);
                *ret = workspace;
        } else if (c && !strcmp(var, "CHANNEL")) {
-               strncpy(workspace, c->name, workspacelen - 1);
+               ast_copy_string(workspace, c->name, workspacelen);
                *ret = workspace;
        } else if (!strcmp(var, "EPOCH")) {
                snprintf(workspace, workspacelen, "%u",(int)time(NULL));
                *ret = workspace;
        } else if (!strcmp(var, "EPOCH")) {
                snprintf(workspace, workspacelen, "%u",(int)time(NULL));
@@ -1081,10 +1081,10 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
                snprintf(workspace, workspacelen, "%d", c->hangupcause);
                *ret = workspace;
        } else if (c && !strcmp(var, "ACCOUNTCODE")) {
                snprintf(workspace, workspacelen, "%d", c->hangupcause);
                *ret = workspace;
        } else if (c && !strcmp(var, "ACCOUNTCODE")) {
-               strncpy(workspace, c->accountcode, workspacelen - 1);
+               ast_copy_string(workspace, c->accountcode, workspacelen);
                *ret = workspace;
        } else if (c && !strcmp(var, "LANGUAGE")) {
                *ret = workspace;
        } else if (c && !strcmp(var, "LANGUAGE")) {
-               strncpy(workspace, c->language, workspacelen - 1);
+               ast_copy_string(workspace, c->language, workspacelen);
                *ret = workspace;
        } else {
 icky:
                *ret = workspace;
        } else {
 icky:
@@ -1096,7 +1096,7 @@ icky:
                                if (strcasecmp(ast_var_name(variables),var)==0) {
                                        *ret=ast_var_value(variables);
                                        if (*ret) {
                                if (strcasecmp(ast_var_name(variables),var)==0) {
                                        *ret=ast_var_value(variables);
                                        if (*ret) {
-                                               strncpy(workspace, *ret, workspacelen - 1);
+                                               ast_copy_string(workspace, *ret, workspacelen);
                                                *ret = workspace;
                                        }
                                        break;
                                                *ret = workspace;
                                        }
                                        break;
@@ -1112,7 +1112,7 @@ icky:
                                if (strcasecmp(ast_var_name(variables),var)==0) {
                                        *ret=ast_var_value(variables);
                                        if (*ret) {
                                if (strcasecmp(ast_var_name(variables),var)==0) {
                                        *ret=ast_var_value(variables);
                                        if (*ret) {
-                                               strncpy(workspace, *ret, workspacelen - 1);
+                                               ast_copy_string(workspace, *ret, workspacelen);
                                                *ret = workspace;
                                        }
                                }
                                                *ret = workspace;
                                        }
                                }
@@ -1444,7 +1444,7 @@ static void pbx_substitute_variables_helper_full(struct ast_channel *c, const ch
 
                        /* Store variable name (and truncate) */
                        memset(var, 0, sizeof(var));
 
                        /* Store variable name (and truncate) */
                        memset(var, 0, sizeof(var));
-                       strncpy(var, vars, sizeof(var) - 1);
+                       ast_copy_string(var, vars, sizeof(var));
                        var[len] = '\0';
 
                        /* Substitute if necessary */
                        var[len] = '\0';
 
                        /* Substitute if necessary */
@@ -1508,7 +1508,7 @@ static void pbx_substitute_variables_helper_full(struct ast_channel *c, const ch
                        
                        /* Store variable name (and truncate) */
                        memset(var, 0, sizeof(var));
                        
                        /* Store variable name (and truncate) */
                        memset(var, 0, sizeof(var));
-                       strncpy(var, vars, sizeof(var) - 1);
+                       ast_copy_string(var, vars, sizeof(var));
                        var[len] = '\0';
                        
                        /* Substitute if necessary */
                        var[len] = '\0';
                        
                        /* Substitute if necessary */
@@ -1555,7 +1555,7 @@ static void pbx_substitute_variables(char *passdata, int datalen, struct ast_cha
                
        /* No variables or expressions in e->data, so why scan it? */
        if (!strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
                
        /* No variables or expressions in e->data, so why scan it? */
        if (!strstr(e->data,"${") && !strstr(e->data,"$[") && !strstr(e->data,"$(")) {
-               strncpy(passdata, e->data, datalen - 1);
+               ast_copy_string(passdata, e->data, datalen);
                passdata[datalen-1] = '\0';
                return;
        }
                passdata[datalen-1] = '\0';
                return;
        }
@@ -1613,9 +1613,9 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
                        ast_mutex_unlock(&conlock);
                        if (app) {
                                if (c->context != context)
                        ast_mutex_unlock(&conlock);
                        if (app) {
                                if (c->context != context)
-                                       strncpy(c->context, context, sizeof(c->context)-1);
+                                       ast_copy_string(c->context, context, sizeof(c->context));
                                if (c->exten != exten)
                                if (c->exten != exten)
-                                       strncpy(c->exten, exten, sizeof(c->exten)-1);
+                                       ast_copy_string(c->exten, exten, sizeof(c->exten));
                                c->priority = priority;
                                pbx_substitute_variables(passdata, sizeof(passdata), c, e);
                                if (option_debug) {
                                c->priority = priority;
                                pbx_substitute_variables(passdata, sizeof(passdata), c, e);
                                if (option_debug) {
@@ -1741,7 +1741,7 @@ static int ast_extension_state2(struct ast_exten *e)
        if (!e)
                return -1;
 
        if (!e)
                return -1;
 
-       strncpy(hint, ast_get_extension_app(e), sizeof(hint)-1);
+       ast_copy_string(hint, ast_get_extension_app(e), sizeof(hint));
 
        cur = hint;     /* On or more devices separated with a & character */
        do {
 
        cur = hint;     /* On or more devices separated with a & character */
        do {
@@ -1845,7 +1845,7 @@ int ast_device_state_changed(const char *fmt, ...)
 
        while (list) {
 
 
        while (list) {
 
-               strncpy(hint, ast_get_extension_app(list->exten), sizeof(hint) - 1);
+               ast_copy_string(hint, ast_get_extension_app(list->exten), sizeof(hint));
                cur = hint;
                do {
                        rest = strchr(cur, '&');
                cur = hint;
                do {
                        rest = strchr(cur, '&');
@@ -2186,14 +2186,15 @@ int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_
 {
        struct ast_exten *e;
        void *tmp;
 {
        struct ast_exten *e;
        void *tmp;
+
        e = ast_hint_extension(c, context, exten);
        if (e) {
                if (hint) 
        e = ast_hint_extension(c, context, exten);
        if (e) {
                if (hint) 
-                   strncpy(hint, ast_get_extension_app(e), hintsize - 1);
+                   ast_copy_string(hint, ast_get_extension_app(e), hintsize);
                if (name) {
                        tmp = ast_get_extension_app_data(e);
                        if (tmp)
                if (name) {
                        tmp = ast_get_extension_app_data(e);
                        if (tmp)
-                               strncpy(name, (char *)tmp, namesize - 1);
+                               ast_copy_string(name, (char *) tmp, namesize);
                }
            return -1;
        }
                }
            return -1;
        }
@@ -2277,12 +2278,12 @@ static int __ast_pbx_run(struct ast_channel *c)
                /* If not successful fall back to 's' */
                if (option_verbose > 1)
                        ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
                /* If not successful fall back to 's' */
                if (option_verbose > 1)
                        ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
-               strncpy(c->exten, "s", sizeof(c->exten)-1);
+               ast_copy_string(c->exten, "s", sizeof(c->exten));
                if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
                        /* JK02: And finally back to default if everything else failed */
                        if (option_verbose > 1)
                                ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
                if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
                        /* JK02: And finally back to default if everything else failed */
                        if (option_verbose > 1)
                                ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
-                       strncpy(c->context, "default", sizeof(c->context)-1);
+                       ast_copy_string(c->context, "default", sizeof(c->context));
                }
                c->priority = 1;
        }
                }
                c->priority = 1;
        }
@@ -2332,7 +2333,7 @@ static int __ast_pbx_run(struct ast_channel *c)
                                }
                        }
                        if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->cid.cid_num))) {
                                }
                        }
                        if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->cid.cid_num))) {
-                               strncpy(c->exten,"T",sizeof(c->exten) - 1);
+                               ast_copy_string(c->exten, "T", sizeof(c->exten));
                                /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
                                c->whentohangup = 0;
                                c->priority = 0;
                                /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
                                c->whentohangup = 0;
                                c->priority = 0;
@@ -2351,7 +2352,7 @@ static int __ast_pbx_run(struct ast_channel *c)
                                if (option_verbose > 2)
                                        ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
                                pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
                                if (option_verbose > 2)
                                        ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
                                pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
-                               strncpy(c->exten, "i", sizeof(c->exten)-1);
+                               ast_copy_string(c->exten, "i", sizeof(c->exten));
                                c->priority = 1;
                        } else {
                                ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
                                c->priority = 1;
                        } else {
                                ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
@@ -2388,7 +2389,7 @@ static int __ast_pbx_run(struct ast_channel *c)
                                }
                                if (ast_exists_extension(c, c->context, exten, 1, c->cid.cid_num)) {
                                        /* Prepare the next cycle */
                                }
                                if (ast_exists_extension(c, c->context, exten, 1, c->cid.cid_num)) {
                                        /* Prepare the next cycle */
-                                       strncpy(c->exten, exten, sizeof(c->exten)-1);
+                                       ast_copy_string(c->exten, exten, sizeof(c->exten));
                                        c->priority = 1;
                                } else {
                                        /* No such extension */
                                        c->priority = 1;
                                } else {
                                        /* No such extension */
@@ -2398,7 +2399,7 @@ static int __ast_pbx_run(struct ast_channel *c)
                                                        if (option_verbose > 2)
                                                                ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
                                                        pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
                                                        if (option_verbose > 2)
                                                                ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
                                                        pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
-                                                       strncpy(c->exten, "i", sizeof(c->exten)-1);
+                                                       ast_copy_string(c->exten, "i", sizeof(c->exten));
                                                        c->priority = 1;
                                                } else {
                                                        ast_log(LOG_WARNING, "Invalid extension '%s', but no rule 'i' in context '%s'\n", exten, c->context);
                                                        c->priority = 1;
                                                } else {
                                                        ast_log(LOG_WARNING, "Invalid extension '%s', but no rule 'i' in context '%s'\n", exten, c->context);
@@ -2409,7 +2410,7 @@ static int __ast_pbx_run(struct ast_channel *c)
                                                if (ast_exists_extension(c, c->context, "t", 1, c->cid.cid_num)) {
                                                        if (option_verbose > 2)
                                                                ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
                                                if (ast_exists_extension(c, c->context, "t", 1, c->cid.cid_num)) {
                                                        if (option_verbose > 2)
                                                                ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
-                                                       strncpy(c->exten, "t", sizeof(c->exten)-1);
+                                                       ast_copy_string(c->exten, "t", sizeof(c->exten));
                                                        c->priority = 1;
                                                } else {
                                                        ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
                                                        c->priority = 1;
                                                } else {
                                                        ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
@@ -3941,7 +3942,7 @@ int ast_build_timing(struct ast_timing *i, char *info_in)
        if (ast_strlen_zero(info_in))
                return 0;
        /* make a copy just in case we were passed a static string */
        if (ast_strlen_zero(info_in))
                return 0;
        /* make a copy just in case we were passed a static string */
-       strncpy(info_save, info_in, sizeof(info_save));
+       ast_copy_string(info_save, info_in, sizeof(info_save));
        info = info_save;
        /* Assume everything except time */
        i->monthmask = (1 << 12) - 1;
        info = info_save;
        /* Assume everything except time */
        i->monthmask = (1 << 12) - 1;
@@ -4385,9 +4386,9 @@ int ast_explicit_goto(struct ast_channel *chan, const char *context, const char
                return -1;
 
        if (context && !ast_strlen_zero(context))
                return -1;
 
        if (context && !ast_strlen_zero(context))
-               strncpy(chan->context, context, sizeof(chan->context) - 1);
+               ast_copy_string(chan->context, context, sizeof(chan->context));
        if (exten && !ast_strlen_zero(exten))
        if (exten && !ast_strlen_zero(exten))
-               strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
+               ast_copy_string(chan->exten, exten, sizeof(chan->exten));
        if (priority > -1) {
                chan->priority = priority;
                /* see flag description in channel.h for explanation */
        if (priority > -1) {
                chan->priority = priority;
                /* see flag description in channel.h for explanation */
@@ -4769,9 +4770,9 @@ static void *async_wait(void *data)
                                ast_log(LOG_WARNING, "No such application '%s'\n", as->app);
                } else {
                        if (!ast_strlen_zero(as->context))
                                ast_log(LOG_WARNING, "No such application '%s'\n", as->app);
                } else {
                        if (!ast_strlen_zero(as->context))
-                               strncpy(chan->context, as->context, sizeof(chan->context) - 1);
+                               ast_copy_string(chan->context, as->context, sizeof(chan->context));
                        if (!ast_strlen_zero(as->exten))
                        if (!ast_strlen_zero(as->exten))
-                               strncpy(chan->exten, as->exten, sizeof(chan->exten) - 1);
+                               ast_copy_string(chan->exten, as->exten, sizeof(chan->exten));
                        if (as->priority > 0)
                                chan->priority = as->priority;
                        /* Run the PBX */
                        if (as->priority > 0)
                                chan->priority = as->priority;
                        /* Run the PBX */
@@ -4911,10 +4912,10 @@ int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout
                        if (ast_exists_extension(chan, context, "failed", 1, NULL)) {
                                chan = ast_channel_alloc(0);
                                if(chan) {
                        if (ast_exists_extension(chan, context, "failed", 1, NULL)) {
                                chan = ast_channel_alloc(0);
                                if(chan) {
-                                       strncpy(chan->name, "OutgoingSpoolFailed", sizeof(chan->name) - 1);
+                                       ast_copy_string(chan->name, "OutgoingSpoolFailed", sizeof(chan->name));
                                        if (context && !ast_strlen_zero(context))
                                        if (context && !ast_strlen_zero(context))
-                                               strncpy(chan->context, context, sizeof(chan->context) - 1);
-                                       strncpy(chan->exten, "failed", sizeof(chan->exten) - 1);
+                                               ast_copy_string(chan->context, context, sizeof(chan->context));
+                                       ast_copy_string(chan->exten, "failed", sizeof(chan->exten));
                                        chan->priority = 1;
                                        if (variable) {
                                                tmp = ast_strdupa(variable);
                                        chan->priority = 1;
                                        if (variable) {
                                                tmp = ast_strdupa(variable);
@@ -4945,8 +4946,8 @@ int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout
                if (account)
                        ast_cdr_setaccount(chan, account);
                as->chan = chan;
                if (account)
                        ast_cdr_setaccount(chan, account);
                as->chan = chan;
-               strncpy(as->context, context, sizeof(as->context) - 1);
-               strncpy(as->exten,  exten, sizeof(as->exten) - 1);
+               ast_copy_string(as->context, context, sizeof(as->context));
+               ast_copy_string(as->exten,  exten, sizeof(as->exten));
                as->priority = priority;
                as->timeout = timeout;
                if (variable) {
                as->priority = priority;
                as->timeout = timeout;
                if (variable) {
@@ -5037,7 +5038,7 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
                                tmp = malloc(sizeof(struct app_tmp));
                                if (tmp) {
                                        memset(tmp, 0, sizeof(struct app_tmp));
                                tmp = malloc(sizeof(struct app_tmp));
                                if (tmp) {
                                        memset(tmp, 0, sizeof(struct app_tmp));
-                                       strncpy(tmp->app, app, sizeof(tmp->app) - 1);
+                                       ast_copy_string(tmp->app, app, sizeof(tmp->app));
                                        if (appdata)
                                                ast_copy_string(tmp->data, appdata, sizeof(tmp->data));
                                        tmp->chan = chan;
                                        if (appdata)
                                                ast_copy_string(tmp->data, appdata, sizeof(tmp->data));
                                        tmp->chan = chan;
@@ -5101,9 +5102,9 @@ int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout,
                if (account)
                        ast_cdr_setaccount(chan, account);
                as->chan = chan;
                if (account)
                        ast_cdr_setaccount(chan, account);
                as->chan = chan;
-               strncpy(as->app, app, sizeof(as->app) - 1);
+               ast_copy_string(as->app, app, sizeof(as->app));
                if (appdata)
                if (appdata)
-                       strncpy(as->appdata,  appdata, sizeof(as->appdata) - 1);
+                       ast_copy_string(as->appdata,  appdata, sizeof(as->appdata));
                as->timeout = timeout;
                if (variable) {
                        vartmp = ast_strdupa(variable);
                as->timeout = timeout;
                if (variable) {
                        vartmp = ast_strdupa(variable);
@@ -5290,7 +5291,7 @@ static int pbx_builtin_setlanguage(struct ast_channel *chan, void *data)
 
        /* Copy the language as specified */
        if (data)
 
        /* Copy the language as specified */
        if (data)
-               strncpy(chan->language, (char *)data, sizeof(chan->language)-1);
+               ast_copy_string(chan->language, (char *) data, sizeof(chan->language));
 
        return 0;
 }
 
        return 0;
 }
@@ -5347,9 +5348,9 @@ static int pbx_builtin_stripmsd(struct ast_channel *chan, void *data)
                return 0;
        }
        if (strlen(chan->exten) > atoi(data)) {
                return 0;
        }
        if (strlen(chan->exten) > atoi(data)) {
-               strncpy(newexten, chan->exten + atoi(data), sizeof(newexten)-1);
+               ast_copy_string(newexten, chan->exten + atoi(data), sizeof(newexten));
        }
        }
-       strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
+       ast_copy_string(chan->exten, newexten, sizeof(chan->exten));
        return 0;
 }
 
        return 0;
 }
 
@@ -5362,7 +5363,7 @@ static int pbx_builtin_prefix(struct ast_channel *chan, void *data)
                return 0;
        }
        snprintf(newexten, sizeof(newexten), "%s%s", (char *)data, chan->exten);
                return 0;
        }
        snprintf(newexten, sizeof(newexten), "%s%s", (char *)data, chan->exten);
-       strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
+       ast_copy_string(chan->exten, newexten, sizeof(chan->exten));
        if (option_verbose > 2)
                ast_verbose(VERBOSE_PREFIX_3 "Prepended prefix, new extension is %s\n", chan->exten);
        return 0;
        if (option_verbose > 2)
                ast_verbose(VERBOSE_PREFIX_3 "Prepended prefix, new extension is %s\n", chan->exten);
        return 0;
@@ -5377,7 +5378,7 @@ static int pbx_builtin_suffix(struct ast_channel *chan, void *data)
                return 0;
        }
        snprintf(newexten, sizeof(newexten), "%s%s", chan->exten, (char *)data);
                return 0;
        }
        snprintf(newexten, sizeof(newexten), "%s%s", chan->exten, (char *)data);
-       strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
+       ast_copy_string(chan->exten, newexten, sizeof(chan->exten));
        if (option_verbose > 2)
                ast_verbose(VERBOSE_PREFIX_3 "Appended suffix, new extension is %s\n", chan->exten);
        return 0;
        if (option_verbose > 2)
                ast_verbose(VERBOSE_PREFIX_3 "Appended suffix, new extension is %s\n", chan->exten);
        return 0;
@@ -5514,7 +5515,7 @@ static int pbx_builtin_waitexten(struct ast_channel *chan, void *data)
                } else if (ast_exists_extension(chan, chan->context, "t", 1, chan->cid.cid_num)) {
                        if (option_verbose > 2)
                                ast_verbose(VERBOSE_PREFIX_3 "Timeout on %s, going to 't'\n", chan->name);
                } else if (ast_exists_extension(chan, chan->context, "t", 1, chan->cid.cid_num)) {
                        if (option_verbose > 2)
                                ast_verbose(VERBOSE_PREFIX_3 "Timeout on %s, going to 't'\n", chan->name);
-                       strncpy(chan->exten, "t", sizeof(chan->exten));
+                       ast_copy_string(chan->exten, "t", sizeof(chan->exten));
                        chan->priority = 0;
                } else {
                        ast_log(LOG_WARNING, "Timeout but no rule 't' in context '%s'\n", chan->context);
                        chan->priority = 0;
                } else {
                        ast_log(LOG_WARNING, "Timeout but no rule 't' in context '%s'\n", chan->context);
@@ -5916,7 +5917,7 @@ static int pbx_builtin_saynumber(struct ast_channel *chan, void *data)
                ast_log(LOG_WARNING, "SayNumber requires an argument (number)\n");
                return -1;
        }
                ast_log(LOG_WARNING, "SayNumber requires an argument (number)\n");
                return -1;
        }
-       strncpy(tmp, (char *)data, sizeof(tmp)-1);
+       ast_copy_string(tmp, (char *) data, sizeof(tmp));
        number=tmp;
        strsep(&number, "|");
        options = strsep(&number, "|");
        number=tmp;
        strsep(&number, "|");
        options = strsep(&number, "|");