Index: pbx/pbx_spool.c =================================================================== --- pbx/pbx_spool.c (revision 69624) +++ pbx/pbx_spool.c (working copy) @@ -294,7 +294,7 @@ unlink(o->fn); return 0; } - if (mkdir(qdonedir, 0700) && (errno != EEXIST)) { + if (ast_mkdir(qdonedir, 0777)) { ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool archiving disabled\n", qdonedir); unlink(o->fn); return -1; @@ -485,7 +485,7 @@ pthread_t thread; int ret; snprintf(qdir, sizeof(qdir), "%s/%s", ast_config_AST_SPOOL_DIR, "outgoing"); - if (mkdir(qdir, 0700) && (errno != EEXIST)) { + if (ast_mkdir(qdir, 0777)) { ast_log(LOG_WARNING, "Unable to create queue directory %s -- outgoing spool disabled\n", qdir); return 0; } Index: apps/app_dictate.c =================================================================== --- apps/app_dictate.c (revision 69624) +++ apps/app_dictate.c (working copy) @@ -139,7 +139,7 @@ ast_copy_string(filein, filename, sizeof(filein)); filename = ""; } - mkdir(base, 0755); + ast_mkdir(base, 0755); len = strlen(base) + strlen(filein) + 2; if (!path || len > maxlen) { path = alloca(len); Index: apps/app_test.c =================================================================== --- apps/app_test.c (revision 69624) +++ apps/app_test.c (working copy) @@ -177,7 +177,7 @@ if ((res >=0) && (!ast_strlen_zero(testid))) { /* Make the directory to hold the test results in case it's not there */ snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); - mkdir(fn, 0777); + ast_mkdir(fn, 0777); snprintf(fn, sizeof(fn), "%s/testresults/%s-client.txt", ast_config_AST_LOG_DIR, testid); if ((f = fopen(fn, "w+"))) { setlinebuf(f); @@ -338,7 +338,7 @@ /* Got a Test ID! Whoo hoo! */ /* Make the directory to hold the test results in case it's not there */ snprintf(fn, sizeof(fn), "%s/testresults", ast_config_AST_LOG_DIR); - mkdir(fn, 0777); + ast_mkdir(fn, 0777); snprintf(fn, sizeof(fn), "%s/testresults/%s-server.txt", ast_config_AST_LOG_DIR, testid); if ((f = fopen(fn, "w+"))) { setlinebuf(f); Index: apps/app_minivm.c =================================================================== --- apps/app_minivm.c (revision 69624) +++ apps/app_minivm.c (working copy) @@ -1159,29 +1159,12 @@ */ static int create_dirpath(char *dest, int len, char *domain, char *username, char *folder) { - mode_t mode = VOICEMAIL_DIR_MODE; - - if(!ast_strlen_zero(domain)) { - make_dir(dest, len, domain, "", ""); - if(mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } + int res; + make_dir(dest, len, domain, username, folder); + if ((res = ast_mkdir(dest, 0777))) { + ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res)); + return -1; } - if(!ast_strlen_zero(username)) { - make_dir(dest, len, domain, username, ""); - if(mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } - } - if(!ast_strlen_zero(folder)) { - make_dir(dest, len, domain, username, folder); - if(mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } - } ast_debug(2, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest); return 0; } Index: apps/app_sms.c =================================================================== --- apps/app_sms.c (revision 69624) +++ apps/app_sms.c (working copy) @@ -260,7 +260,7 @@ #define is8bit(dcs) ( ((dcs) & 0xC0) ? ( ((dcs)&4) ) : (((dcs) & 0xc) == 4) ) #define is16bit(dcs) ( ((dcs) & 0xC0) ? 0 : (((dcs) & 0xc) == 8) ) -static void sms_messagetx (sms_t * h); +static void sms_messagetx(sms_t * h); /*! \brief copy number, skipping non digits apart from leading + */ static void numcpy(char *d, char *s) @@ -268,17 +268,19 @@ if (*s == '+') *d++ = *s++; while (*s) { - if (isdigit (*s)) - *d++ = *s; + if (isdigit(*s)) + *d++ = *s; s++; } *d = 0; } /*! \brief static, return a date/time in ISO format */ -static char * isodate(time_t t, char *buf, int len) +static char *isodate(time_t t, char *buf, int len) { - strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime (&t)); + struct tm tm; + ast_localtime(&t, &tm, NULL); + strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); return buf; } @@ -510,18 +512,19 @@ /*! \brief pack a date and return */ static void packdate(unsigned char *o, time_t w) { - struct tm *t = localtime (&w); + struct tm t; + ast_localtime(&w, &t, NULL); #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) - int z = -t->tm_gmtoff / 60 / 15; + int z = -t.tm_gmtoff / 60 / 15; #else int z = timezone / 60 / 15; #endif - *o++ = ((t->tm_year % 10) << 4) + (t->tm_year % 100) / 10; - *o++ = (((t->tm_mon + 1) % 10) << 4) + (t->tm_mon + 1) / 10; - *o++ = ((t->tm_mday % 10) << 4) + t->tm_mday / 10; - *o++ = ((t->tm_hour % 10) << 4) + t->tm_hour / 10; - *o++ = ((t->tm_min % 10) << 4) + t->tm_min / 10; - *o++ = ((t->tm_sec % 10) << 4) + t->tm_sec / 10; + *o++ = ((t.tm_year % 10) << 4) + (t.tm_year % 100) / 10; + *o++ = (((t.tm_mon + 1) % 10) << 4) + (t.tm_mon + 1) / 10; + *o++ = ((t.tm_mday % 10) << 4) + t.tm_mday / 10; + *o++ = ((t.tm_hour % 10) << 4) + t.tm_hour / 10; + *o++ = ((t.tm_min % 10) << 4) + t.tm_min / 10; + *o++ = ((t.tm_sec % 10) << 4) + t.tm_sec / 10; if (z < 0) *o++ = (((-z) % 10) << 4) + (-z) / 10 + 0x08; else @@ -543,7 +546,7 @@ t.tm_min += 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); else t.tm_min -= 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); - return mktime (&t); + return mktime(&t); } /*! \brief unpacks bytes (7 bit encoding) at i, len l septets, @@ -656,13 +659,13 @@ static int unpacksms(unsigned char dcs, unsigned char *i, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi) { int l = *i++; - if (is7bit (dcs)) { - unpacksms7 (i, l, udh, udhl, ud, udl, udhi); + if (is7bit(dcs)) { + unpacksms7(i, l, udh, udhl, ud, udl, udhi); l = (l * 7 + 7) / 8; /* adjust length to return */ - } else if (is8bit (dcs)) - unpacksms8 (i, l, udh, udhl, ud, udl, udhi); + } else if (is8bit(dcs)) + unpacksms8(i, l, udh, udhl, ud, udl, udhi); else - unpacksms16 (i, l, udh, udhl, ud, udl, udhi); + unpacksms16(i, l, udh, udhl, ud, udl, udhi); return l + 1; } @@ -694,7 +697,7 @@ } else o[1] = 0x81; for ( ; *i ; i++) { - if (!isdigit (*i)) /* ignore non-digits */ + if (!isdigit(*i)) /* ignore non-digits */ continue; if (o[0] & 1) o[p++] |= ((*i & 0xF) << 4); @@ -714,19 +717,19 @@ if (*h->oa == '\0' && *h->da == '\0') return; - o = open (log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE); + o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE); if (o >= 0) { char line[1000], mrs[3] = "", *p; char buf[30]; unsigned char n; if (h->mr >= 0) - snprintf (mrs, sizeof (mrs), "%02X", h->mr); - snprintf (line, sizeof (line), "%s %c%c%c%s %s %s %s ", + snprintf(mrs, sizeof(mrs), "%02X", h->mr); + snprintf (line, sizeof(line), "%s %c%c%c%s %s %s %s ", isodate(time(0), buf, sizeof(buf)), status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue, S_OR(h->oa, "-"), S_OR(h->da, "-") ); - p = line + strlen (line); + p = line + strlen(line); for (n = 0; n < h->udl; n++) { if (h->ud[n] == '\\') { *p++ = '\\'; @@ -744,8 +747,8 @@ } *p++ = '\n'; *p = 0; - write (o, line, strlen (line)); - close (o); + write(o, line, strlen(line)); + close(o); } *h->oa = *h->da = h->udl = 0; } @@ -760,14 +763,14 @@ h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp = h->vp = h->udhl = 0; h->mr = -1; h->dcs = 0xF1; /* normal messages class 1 */ - h->scts = time (0); - s = fopen (fn, "r"); + h->scts = time(NULL); + s = fopen(fn, "r"); if (s) { - if (unlink (fn)) { /* concurrent access, we lost */ - fclose (s); + if (unlink(fn)) { /* concurrent access, we lost */ + fclose(s); return; } - while (fgets (line, sizeof (line), s)) { /* process line in file */ + while (fgets(line, sizeof (line), s)) { /* process line in file */ char *p; void *pp = &p; for (p = line; *p && *p != '\n' && *p != '\r'; p++); @@ -775,43 +778,43 @@ p = line; if (!*p || *p == ';') continue; /* blank line or comment, ignore */ - while (isalnum (*p)) { - *p = tolower (*p); + while (isalnum(*p)) { + *p = tolower(*p); p++; } - while (isspace (*p)) + while (isspace(*p)) *p++ = 0; if (*p == '=') { *p++ = 0; - if (!strcmp (line, "ud")) { /* parse message (UTF-8) */ + if (!strcmp(line, "ud")) { /* parse message (UTF-8) */ unsigned char o = 0; - memcpy(h->udtxt,p,SMSLEN); /* for protocol 2 */ + memcpy(h->udtxt, p, SMSLEN); /* for protocol 2 */ while (*p && o < SMSLEN) h->ud[o++] = utf8decode(pp); h->udl = o; if (*p) - ast_log (LOG_WARNING, "UD too long in %s\n", fn); + ast_log(LOG_WARNING, "UD too long in %s\n", fn); } else { while (isspace (*p)) p++; - if (!strcmp (line, "oa") && strlen (p) < sizeof (h->oa)) + if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) numcpy (h->oa, p); - else if (!strcmp (line, "da") && strlen (p) < sizeof (h->oa)) - numcpy (h->da, p); - else if (!strcmp (line, "pid")) - h->pid = atoi (p); - else if (!strcmp (line, "dcs")) { - h->dcs = atoi (p); + else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) + numcpy(h->da, p); + else if (!strcmp(line, "pid")) + h->pid = atoi(p); + else if (!strcmp(line, "dcs")) { + h->dcs = atoi(p); dcsset = 1; - } else if (!strcmp (line, "mr")) - h->mr = atoi (p); - else if (!strcmp (line, "srr")) - h->srr = (atoi (p) ? 1 : 0); - else if (!strcmp (line, "vp")) - h->vp = atoi (p); - else if (!strcmp (line, "rp")) - h->rp = (atoi (p) ? 1 : 0); - else if (!strcmp (line, "scts")) { /* get date/time */ + } else if (!strcmp(line, "mr")) + h->mr = atoi(p); + else if (!strcmp(line, "srr")) + h->srr = (atoi(p) ? 1 : 0); + else if (!strcmp(line, "vp")) + h->vp = atoi(p); + else if (!strcmp(line, "rp")) + h->rp = (atoi(p) ? 1 : 0); + else if (!strcmp(line, "scts")) { /* get date/time */ int Y, m, d, @@ -827,52 +830,52 @@ t.tm_min = M; t.tm_sec = S; t.tm_isdst = -1; - h->scts = mktime (&t); + h->scts = mktime(&t); if (h->scts == (time_t) - 1) - ast_log (LOG_WARNING, "Bad date/timein %s: %s", fn, p); + ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p); } } else - ast_log (LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p); + ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p); } } else if (*p == '#') { /* raw hex format */ *p++ = 0; if (*p == '#') { p++; - if (!strcmp (line, "ud")) { /* user data */ + if (!strcmp(line, "ud")) { /* user data */ int o = 0; while (*p && o < SMSLEN) { - if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3])) { + if (isxdigit(*p) && isxdigit(p[1]) && isxdigit(p[2]) && isxdigit(p[3])) { h->ud[o++] = - (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) + - (((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) + - (((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF)); + (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 12) + + (((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) + + (((isalpha(p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha(p[3]) ? 9 : 0) + (p[3] & 0xF)); p += 4; } else break; } h->udl = o; if (*p) - ast_log (LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn); + ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn); } else - ast_log (LOG_WARNING, "Only ud can use ## format, %s\n", fn); - } else if (!strcmp (line, "ud")) { /* user data */ + ast_log(LOG_WARNING, "Only ud can use ## format, %s\n", fn); + } else if (!strcmp(line, "ud")) { /* user data */ int o = 0; while (*p && o < SMSLEN) { - if (isxdigit (*p) && isxdigit (p[1])) { - h->ud[o++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)); + if (isxdigit(*p) && isxdigit(p[1])) { + h->ud[o++] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); p += 2; } else break; } h->udl = o; if (*p) - ast_log (LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn); - } else if (!strcmp (line, "udh")) { /* user data header */ + ast_log(LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn); + } else if (!strcmp(line, "udh")) { /* user data header */ unsigned char o = 0; h->udhi = 1; while (*p && o < SMSLEN) { - if (isxdigit (*p) && isxdigit (p[1])) { - h->udh[o] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)); + if (isxdigit(*p) && isxdigit(p[1])) { + h->udh[o] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); o++; p += 2; } else @@ -880,32 +883,32 @@ } h->udhl = o; if (*p) - ast_log (LOG_WARNING, "UDH too long / invalid hex in %s\n", fn); + ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn); } else - ast_log (LOG_WARNING, "Only ud and udh can use # format, %s\n", fn); + ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn); } else - ast_log (LOG_WARNING, "Cannot parse in %s: %s\n", fn, line); + ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line); } fclose (s); - if (!dcsset && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0) { - if (packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0) { - if (packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0) - ast_log (LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn); + if (!dcsset && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) { + if (packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) { + if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) + ast_log(LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn); else { h->dcs = 0x08; /* default to 16 bit */ - ast_log (LOG_WARNING, "Sending in 16 bit format (%s)\n", fn); + ast_log(LOG_WARNING, "Sending in 16 bit format (%s)\n", fn); } } else { h->dcs = 0xF5; /* default to 8 bit */ - ast_log (LOG_WARNING, "Sending in 8 bit format (%s)\n", fn); + ast_log(LOG_WARNING, "Sending in 8 bit format (%s)\n", fn); } } - if (is7bit (h->dcs) && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0) - ast_log (LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn); - if (is8bit (h->dcs) && packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0) - ast_log (LOG_WARNING, "Invalid 8 bit data %s\n", fn); - if (is16bit (h->dcs) && packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0) - ast_log (LOG_WARNING, "Invalid 16 bit data %s\n", fn); + if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) + ast_log(LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn); + if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) + ast_log(LOG_WARNING, "Invalid 8 bit data %s\n", fn); + if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) + ast_log(LOG_WARNING, "Invalid 16 bit data %s\n", fn); } } @@ -916,89 +919,85 @@ char buf[30]; FILE *o; - ast_copy_string(fn, spool_dir, sizeof (fn)); - mkdir(fn, 0777); /* ensure it exists */ - snprintf(fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx"); - mkdir(fn, 0777); /* ensure it exists */ - ast_copy_string(fn2, fn, sizeof (fn2)); - snprintf(fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2), "/%s.%s-%d", h->queue, isodate(h->scts, buf, sizeof(buf)), seq++); - snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/.%s", fn2 + strlen (fn) + 1); - o = fopen (fn, "w"); + snprintf(fn, sizeof(fn), "%s/%s", spool_dir, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx"); + ast_mkdir(fn, 0777); /* ensure it exists */ + ast_copy_string(fn2, fn, sizeof(fn2)); + snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%d", h->queue, isodate(h->scts, buf, sizeof(buf)), seq++); + snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1); + o = fopen(fn, "w"); if (o == NULL) return; if (*h->oa) - fprintf (o, "oa=%s\n", h->oa); + fprintf(o, "oa=%s\n", h->oa); if (*h->da) - fprintf (o, "da=%s\n", h->da); + fprintf(o, "da=%s\n", h->da); if (h->udhi) { unsigned int p; - fprintf (o, "udh#"); + fprintf(o, "udh#"); for (p = 0; p < h->udhl; p++) - fprintf (o, "%02X", h->udh[p]); - fprintf (o, "\n"); + fprintf(o, "%02X", h->udh[p]); + fprintf(o, "\n"); } if (h->udl) { unsigned int p; for (p = 0; p < h->udl && h->ud[p] >= ' '; p++); if (p < h->udl) - fputc (';', o); /* cannot use ud=, but include as a comment for human readable */ - fprintf (o, "ud="); + fputc(';', o); /* cannot use ud=, but include as a comment for human readable */ + fprintf(o, "ud="); for (p = 0; p < h->udl; p++) { unsigned short v = h->ud[p]; if (v < 32) - fputc (191, o); + fputc(191, o); else if (v < 0x80) - fputc (v, o); - else if (v < 0x800) - { - fputc (0xC0 + (v >> 6), o); - fputc (0x80 + (v & 0x3F), o); - } else - { - fputc (0xE0 + (v >> 12), o); - fputc (0x80 + ((v >> 6) & 0x3F), o); - fputc (0x80 + (v & 0x3F), o); + fputc(v, o); + else if (v < 0x800) { + fputc(0xC0 + (v >> 6), o); + fputc(0x80 + (v & 0x3F), o); + } else { + fputc(0xE0 + (v >> 12), o); + fputc(0x80 + ((v >> 6) & 0x3F), o); + fputc(0x80 + (v & 0x3F), o); } } - fprintf (o, "\n"); + fprintf(o, "\n"); for (p = 0; p < h->udl && h->ud[p] >= ' '; p++); if (p < h->udl) { for (p = 0; p < h->udl && h->ud[p] < 0x100; p++); if (p == h->udl) { /* can write in ucs-1 hex */ - fprintf (o, "ud#"); + fprintf(o, "ud#"); for (p = 0; p < h->udl; p++) - fprintf (o, "%02X", h->ud[p]); - fprintf (o, "\n"); + fprintf(o, "%02X", h->ud[p]); + fprintf(o, "\n"); } else { /* write in UCS-2 */ - fprintf (o, "ud##"); + fprintf(o, "ud##"); for (p = 0; p < h->udl; p++) - fprintf (o, "%04X", h->ud[p]); - fprintf (o, "\n"); + fprintf(o, "%04X", h->ud[p]); + fprintf(o, "\n"); } } } if (h->scts) { char buf[30]; - fprintf (o, "scts=%s\n", isodate(h->scts, buf, sizeof(buf))); + fprintf(o, "scts=%s\n", isodate(h->scts, buf, sizeof(buf))); } if (h->pid) - fprintf (o, "pid=%d\n", h->pid); + fprintf(o, "pid=%d\n", h->pid); if (h->dcs != 0xF1) - fprintf (o, "dcs=%d\n", h->dcs); + fprintf(o, "dcs=%d\n", h->dcs); if (h->vp) - fprintf (o, "vp=%d\n", h->vp); + fprintf(o, "vp=%d\n", h->vp); if (h->srr) - fprintf (o, "srr=1\n"); + fprintf(o, "srr=1\n"); if (h->mr >= 0) - fprintf (o, "mr=%d\n", h->mr); + fprintf(o, "mr=%d\n", h->mr); if (h->rp) - fprintf (o, "rp=1\n"); - fclose (o); - if (rename (fn, fn2)) - unlink (fn); + fprintf(o, "rp=1\n"); + fclose(o); + if (rename(fn, fn2)) + unlink(fn); else - ast_log (LOG_EVENT, "Received to %s\n", fn2); + ast_log(LOG_EVENT, "Received to %s\n", fn2); } /*! \brief read dir skipping dot files... */ @@ -1006,8 +1005,8 @@ { struct dirent *f; do { - f = readdir (d); - } while (f && (*f->d_name == '.' || strncmp (f->d_name, queue, strlen (queue)) || f->d_name[strlen (queue)] != '.')); + f = readdir(d); + } while (f && (*f->d_name == '.' || strncmp(f->d_name, queue, strlen(queue)) || f->d_name[strlen(queue)] != '.')); return f; } @@ -1023,9 +1022,9 @@ h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0); h->rp = ((h->imsg[2] & 0x80) ? 1 : 0); ast_copy_string (h->oa, h->cli, sizeof (h->oa)); - h->scts = time (0); + h->scts = time(NULL); h->mr = h->imsg[p++]; - p += unpackaddress (h->da, h->imsg + p); + p += unpackaddress(h->da, h->imsg + p); h->pid = h->imsg[p++]; h->dcs = h->imsg[p++]; if ((h->imsg[2] & 0x18) == 0x10) { /* relative VP */ @@ -1040,15 +1039,15 @@ p++; } else if (h->imsg[2] & 0x18) p += 7; /* ignore enhanced / absolute VP */ - p += unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi); + p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi); h->rx = 1; /* received message */ - sms_writefile (h); /* write the file */ + sms_writefile(h); /* write the file */ if (p != h->imsg[1] + 2) { - ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); + ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); return 0xFF; /* duh! */ } } else { - ast_log (LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]); + ast_log(LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]); return 0xFF; } } else { /* client */ @@ -1058,20 +1057,20 @@ h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0); h->rp = ((h->imsg[2] & 0x80) ? 1 : 0); h->mr = -1; - p += unpackaddress (h->oa, h->imsg + p); + p += unpackaddress(h->oa, h->imsg + p); h->pid = h->imsg[p++]; h->dcs = h->imsg[p++]; - h->scts = unpackdate (h->imsg + p); + h->scts = unpackdate(h->imsg + p); p += 7; - p += unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi); + p += unpacksms(h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi); h->rx = 1; /* received message */ - sms_writefile (h); /* write the file */ + sms_writefile(h); /* write the file */ if (p != h->imsg[1] + 2) { - ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); + ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); return 0xFF; /* duh! */ } } else { - ast_log (LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]); + ast_log(LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]); return 0xFF; } } @@ -1086,56 +1085,56 @@ * Add data to a protocol 2 message. * Use the length field (h->omsg[1]) as a pointer to the next free position. */ -static void adddata_proto2 (sms_t *h, unsigned char msg, char *data, int size) +static void adddata_proto2(sms_t *h, unsigned char msg, char *data, int size) { - int x = h->omsg[1]+2; /* Get current position */ - if (x==2) + int x = h->omsg[1] + 2; /* Get current position */ + if (x == 2) x += 2; /* First: skip Payload length (set later) */ h->omsg[x++] = msg; /* Message code */ - h->omsg[x++]=(unsigned char)size; /* Data size Low */ - h->omsg[x++]=0; /* Data size Hi */ - for (; size>0 ; size--) + h->omsg[x++] = (unsigned char)size; /* Data size Low */ + h->omsg[x++] = 0; /* Data size Hi */ + for (; size > 0 ; size--) h->omsg[x++] = *data++; - h->omsg[1] = x-2; /* Frame size */ - h->omsg[2] = x-4; /* Payload length (Lo) */ + h->omsg[1] = x - 2; /* Frame size */ + h->omsg[2] = x - 4; /* Payload length (Lo) */ h->omsg[3] = 0; /* Payload length (Hi) */ } -static void putdummydata_proto2 (sms_t *h) +static void putdummydata_proto2(sms_t *h) { - adddata_proto2 (h, 0x10, "\0", 1); /* Media Identifier > SMS */ - adddata_proto2 (h, 0x11, "\0\0\0\0\0\0", 6); /* Firmware version */ - adddata_proto2 (h, 0x12, "\2\0\4", 3); /* SMS provider ID */ - adddata_proto2 (h, 0x13, h->udtxt, h->udl); /* Body */ + adddata_proto2(h, 0x10, "\0", 1); /* Media Identifier > SMS */ + adddata_proto2(h, 0x11, "\0\0\0\0\0\0", 6); /* Firmware version */ + adddata_proto2(h, 0x12, "\2\0\4", 3); /* SMS provider ID */ + adddata_proto2(h, 0x13, h->udtxt, h->udl); /* Body */ } static void sms_compose2(sms_t *h, int more) { - struct tm *tm; + struct tm tm; char stm[9]; h->omsg[0] = 0x00; /* set later... */ h->omsg[1] = 0; - putdummydata_proto2 (h); + putdummydata_proto2(h); if (h->smsc) { /* deliver */ h->omsg[0] = 0x11; /* SMS_DELIVERY */ - // Required: 10 11 12 13 14 15 17 (seems they must be ordered!) - tm=localtime(&h->scts); - sprintf (stm, "%02d%02d%02d%02d", tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min); // Date mmddHHMM - adddata_proto2 (h, 0x14, stm, 8); /* Date */ - if(*h->oa==0) - strcpy(h->oa,"00000000"); - adddata_proto2 (h, 0x15, h->oa, strlen(h->oa)); /* Originator */ - adddata_proto2 (h, 0x17, "\1", 1); /* Calling Terminal ID */ + /* Required: 10 11 12 13 14 15 17 (seems they must be ordered!) */ + ast_localtime(&h->scts, &tm, NULL); + snprintf(stm, sizeof(stm), "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); /* Date mmddHHMM */ + adddata_proto2(h, 0x14, stm, 8); /* Date */ + if(*h->oa == 0) + strcpy(h->oa, "00000000"); + adddata_proto2(h, 0x15, h->oa, strlen(h->oa)); /* Originator */ + adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */ } else { /* submit */ h->omsg[0] = 0x10; /* SMS_SUBMIT */ - // Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) - adddata_proto2 (h, 0x17, "\1", 1); /* Calling Terminal ID */ - if(*h->da==0) - strcpy(h->da,"00000000"); - adddata_proto2 (h, 0x18, h->da, strlen(h->da)); /* Originator */ - adddata_proto2 (h, 0x1B, "\1", 1); /* Called Terminal ID */ - adddata_proto2 (h, 0x1C, "\0\0\0", 3); /* Notification */ + /* Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) */ + adddata_proto2(h, 0x17, "\1", 1); /* Calling Terminal ID */ + if(*h->da == 0) + strcpy(h->da, "00000000"); + adddata_proto2(h, 0x18, h->da, strlen(h->da)); /* Originator */ + adddata_proto2(h, 0x1B, "\1", 1); /* Called Terminal ID */ + adddata_proto2(h, 0x1C, "\0\0\0", 3); /* Notification */ } } @@ -1147,78 +1146,78 @@ char *p; int f; - for (p=s,f=0; fimsg[1]+2; + sz = h->imsg[1] + 2; /* ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Frame: %s\n", sms_hexdump(h->imsg,sz, debug_buf)); */ /* Parse message body (called payload) */ - h->scts = time (0); - for(f=4; fimsg[f++]; - msgsz=h->imsg[f++]; - msgsz+=(h->imsg[f++]*256); - switch(msg) { + h->scts = time(NULL); + for(f = 4; f < sz; ) { + msg = h->imsg[f++]; + msgsz = h->imsg[f++]; + msgsz += h->imsg[f++] * 256; + switch (msg) { case 0x13: /* Body */ if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Body#%02X=[%.*s]\n",msg,msgsz,&h->imsg[f]); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Body#%02X=[%.*s]\n", msg, msgsz, &h->imsg[f]); if (msgsz >= sizeof(h->imsg)) - msgsz = sizeof(h->imsg)-1; - for (i=0; iud[i]=h->imsg[f+i]; + msgsz = sizeof(h->imsg) - 1; + for (i = 0; i < msgsz; i++) + h->ud[i] = h->imsg[f + i]; h->udl = msgsz; break; case 0x14: /* Date SCTS */ - h->scts = time (0); - tm = localtime (&h->scts); - tm->tm_mon = ( (h->imsg[f]*10) + h->imsg[f+1] ) - 1; - tm->tm_mday = ( (h->imsg[f+2]*10) + h->imsg[f+3] ); - tm->tm_hour = ( (h->imsg[f+4]*10) + h->imsg[f+5] ); - tm->tm_min = ( (h->imsg[f+6]*10) + h->imsg[f+7] ); - tm->tm_sec = 0; - h->scts = mktime (tm); + h->scts = time(NULL); + ast_localtime(&h->scts, &tm, NULL); + tm.tm_mon = ( (h->imsg[f] * 10) + h->imsg[f + 1] ) - 1; + tm.tm_mday = ( (h->imsg[f + 2] * 10) + h->imsg[f + 3] ); + tm.tm_hour = ( (h->imsg[f + 4] * 10) + h->imsg[f + 5] ); + tm.tm_min = ( (h->imsg[f + 6] * 10) + h->imsg[f + 7] ); + tm.tm_sec = 0; + h->scts = mktime(&tm); if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", msg, tm->tm_mday, tm->tm_mon+1, tm->tm_hour, tm->tm_min); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", msg, tm.tm_mday, tm.tm_mon + 1, tm.tm_hour, tm.tm_min); break; case 0x15: /* Calling line (from SMSC) */ - if (msgsz>=20) - msgsz=20-1; + if (msgsz >= 20) + msgsz = 20 - 1; if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Origin#%02X=[%.*s]\n",msg,msgsz,&h->imsg[f]); - ast_copy_string (h->oa, (char*)(&h->imsg[f]), msgsz+1); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Origin#%02X=[%.*s]\n", msg, msgsz, &h->imsg[f]); + ast_copy_string(h->oa, (char *)(&h->imsg[f]), msgsz + 1); break; case 0x18: /* Destination (from TE/phone) */ - if (msgsz>=20) - msgsz=20-1; + if (msgsz >= 20) + msgsz = 20 - 1; if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Destination#%02X=[%.*s]\n",msg,msgsz,&h->imsg[f]); - ast_copy_string (h->da, (char*)(&h->imsg[f]), msgsz+1); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Destination#%02X=[%.*s]\n", msg, msgsz, &h->imsg[f]); + ast_copy_string(h->da, (char *)(&h->imsg[f]), msgsz + 1); break; case 0x1C: /* Notify */ if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Notify#%02X=%s\n",msg, sms_hexdump(&h->imsg[f],3, debug_buf)); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Notify#%02X=%s\n",msg, sms_hexdump(&h->imsg[f], 3, debug_buf)); break; default: if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS-P2 Par#%02X [%d]: %s\n",msg,msgsz,sms_hexdump(&h->imsg[f],msgsz, debug_buf)); + ast_verbose(VERBOSE_PREFIX_3 "SMS-P2 Par#%02X [%d]: %s\n", msg, msgsz, sms_hexdump(&h->imsg[f], msgsz, debug_buf)); break; } f+=msgsz; /* Skip to next */ } h->rx = 1; /* received message */ - sms_writefile (h); /* write the file */ + sms_writefile(h); /* write the file */ return 0; /* no error */ } @@ -1234,7 +1233,7 @@ } #endif -static void sms_nextoutgoing (sms_t *h); +static void sms_nextoutgoing(sms_t *h); static void sms_messagerx2(sms_t * h) { @@ -1244,15 +1243,15 @@ #define DLL2_ACK(h) ((h->framenumber & 1) ? DLL2_SMS_ACK1: DLL2_SMS_ACK1) switch (p) { case DLL2_SMS_EST: /* Protocol 2: Connection ready (fake): send message */ - sms_nextoutgoing (h); - //smssend(h,"11 29 27 00 10 01 00 00 11 06 00 00 00 00 00 00 00 12 03 00 02 00 04 13 01 00 41 14 08 00 30 39 31 35 30 02 30 02 15 02 00 39 30 "); + sms_nextoutgoing(h); + /* smssend(h,"11 29 27 00 10 01 00 00 11 06 00 00 00 00 00 00 00 12 03 00 02 00 04 13 01 00 41 14 08 00 30 39 31 35 30 02 30 02 15 02 00 39 30 "); */ break; case DLL2_SMS_INFO_MO: /* transport SMS_SUBMIT */ case DLL2_SMS_INFO_MT: /* transport SMS_DELIVERY */ cause = sms_handleincoming_proto2(h); if (!cause) /* ACK */ - sms_log (h, 'Y'); + sms_log(h, 'Y'); h->omsg[0] = DLL2_ACK(h); h->omsg[1] = 0x06; /* msg len */ h->omsg[2] = 0x04; /* payload len */ @@ -1261,32 +1260,32 @@ h->omsg[5] = 0x01; /* parameter len */ h->omsg[6] = 0x00; /* parameter len */ h->omsg[7] = cause; /* CONFIRM or error */ - sms_messagetx (h); + sms_messagetx(h); break; case DLL2_SMS_NACK: /* Protocol 2: SMS_NAK */ h->omsg[0] = DLL2_SMS_REL; /* SMS_REL */ h->omsg[1] = 0x00; /* msg len */ - sms_messagetx (h); + sms_messagetx(h); break; case DLL2_SMS_ACK0: case DLL2_SMS_ACK1: /* SMS_ACK also transport SMS_SUBMIT or SMS_DELIVERY */ - if( (h->omsg[0] & DLL_SMS_MASK) == DLL2_SMS_REL) { + if ((h->omsg[0] & DLL_SMS_MASK) == DLL2_SMS_REL) { /* a response to our Release, just hangup */ h->hangup = 1; /* hangup */ } else { /* XXX depending on what we are.. */ ast_log(LOG_NOTICE, "SMS_SUBMIT or SMS_DELIVERY"); - sms_nextoutgoing (h); + sms_nextoutgoing(h); } break; case DLL2_SMS_REL: /* Protocol 2: SMS_REL (hangup req) */ h->omsg[0] = DLL2_ACK(h); h->omsg[1] = 0; - sms_messagetx (h); + sms_messagetx(h); break; } } @@ -1299,12 +1298,12 @@ h->omsg[0] = 0x91; /* SMS_DATA */ if (h->smsc) { /* deliver */ h->omsg[p++] = (more ? 4 : 0) + ((h->udhl > 0) ? 0x40 : 0); - p += packaddress (h->omsg + p, h->oa); + p += packaddress(h->omsg + p, h->oa); h->omsg[p++] = h->pid; h->omsg[p++] = h->dcs; - packdate (h->omsg + p, h->scts); + packdate(h->omsg + p, h->scts); p += 7; - p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); + p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); } else { /* submit */ h->omsg[p++] = 0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0); @@ -1326,42 +1325,40 @@ else h->omsg[p++] = 255; /* max */ } - p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); + p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); } h->omsg[1] = p - 2; } /*! \brief find and fill in next message, or send a REL if none waiting */ -static void sms_nextoutgoing (sms_t * h) +static void sms_nextoutgoing(sms_t * h) { char fn[100 + NAME_MAX] = ""; DIR *d; char more = 0; *h->da = *h->oa = '\0'; /* clear destinations */ - ast_copy_string (fn, spool_dir, sizeof (fn)); - mkdir(fn, 0777); /* ensure it exists */ h->rx = 0; /* outgoing message */ - snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? "mttx" : "motx"); - mkdir (fn, 0777); /* ensure it exists */ - d = opendir (fn); + snprintf(fn + strlen (fn), sizeof (fn) - strlen (fn), "%s/%s", spool_dir, h->smsc ? "mttx" : "motx"); + ast_mkdir(fn, 0777); /* ensure it exists */ + d = opendir(fn); if (d) { - struct dirent *f = readdirqueue (d, h->queue); + struct dirent *f = readdirqueue(d, h->queue); if (f) { - snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", f->d_name); - sms_readfile (h, fn); - if (readdirqueue (d, h->queue)) + snprintf(fn + strlen (fn), sizeof(fn) - strlen(fn), "/%s", f->d_name); + sms_readfile(h, fn); + if (readdirqueue(d, h->queue)) more = 1; /* more to send */ } - closedir (d); + closedir(d); } if (*h->da || *h->oa) { /* message to send */ - if (h->protocol==2) + if (h->protocol == 2) sms_compose2(h, more); else - sms_compose1(h,more); + sms_compose1(h, more); } else { /* no message */ - if (h->protocol==2) { + if (h->protocol == 2) { h->omsg[0] = 0x17; /* SMS_REL */ h->omsg[1] = 0; } else { @@ -1369,12 +1366,12 @@ h->omsg[1] = 0; } } - sms_messagetx (h); + sms_messagetx(h); } #define DIR_RX 1 #define DIR_TX 2 -static void sms_debug (int dir, sms_t *h) +static void sms_debug(int dir, sms_t *h) { char txt[259 * 3 + 1]; char *p = txt; /* always long enough */ @@ -1382,13 +1379,13 @@ int n = (dir == DIR_RX) ? h->ibytep : msg[1] + 2; int q = 0; while (q < n && q < 30) { - sprintf (p, " %02X", msg[q++]); + sprintf(p, " %02X", msg[q++]); p += 3; } if (q < n) - sprintf (p, "..."); + sprintf(p, "..."); if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt); + ast_verbose(VERBOSE_PREFIX_3 "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt); } @@ -1396,7 +1393,7 @@ { int cause; - sms_debug (DIR_RX, h); + sms_debug(DIR_RX, h); if (h->protocol == 2) { sms_messagerx2(h); return; @@ -1404,48 +1401,48 @@ /* parse incoming message for Protocol 1 */ switch (h->imsg[0]) { case 0x91: /* SMS_DATA */ - cause = sms_handleincoming (h); - if (!cause) { - sms_log (h, 'Y'); - h->omsg[0] = 0x95; /* SMS_ACK */ - h->omsg[1] = 0x02; - h->omsg[2] = 0x00; /* deliver report */ - h->omsg[3] = 0x00; /* no parameters */ - } else { /* NACK */ - sms_log (h, 'N'); - h->omsg[0] = 0x96; /* SMS_NACK */ - h->omsg[1] = 3; - h->omsg[2] = 0; /* delivery report */ - h->omsg[3] = cause; /* cause */ - h->omsg[4] = 0; /* no parameters */ - } - sms_messagetx (h); - break; + cause = sms_handleincoming (h); + if (!cause) { + sms_log(h, 'Y'); + h->omsg[0] = 0x95; /* SMS_ACK */ + h->omsg[1] = 0x02; + h->omsg[2] = 0x00; /* deliver report */ + h->omsg[3] = 0x00; /* no parameters */ + } else { /* NACK */ + sms_log(h, 'N'); + h->omsg[0] = 0x96; /* SMS_NACK */ + h->omsg[1] = 3; + h->omsg[2] = 0; /* delivery report */ + h->omsg[3] = cause; /* cause */ + h->omsg[4] = 0; /* no parameters */ + } + sms_messagetx(h); + break; case 0x92: /* SMS_ERROR */ h->err = 1; - sms_messagetx (h); /* send whatever we sent again */ + sms_messagetx(h); /* send whatever we sent again */ break; case 0x93: /* SMS_EST */ - sms_nextoutgoing (h); + sms_nextoutgoing(h); break; case 0x94: /* SMS_REL */ h->hangup = 1; /* hangup */ break; case 0x95: /* SMS_ACK */ - sms_log (h, 'Y'); - sms_nextoutgoing (h); + sms_log(h, 'Y'); + sms_nextoutgoing(h); break; case 0x96: /* SMS_NACK */ h->err = 1; - sms_log (h, 'N'); - sms_nextoutgoing (h); + sms_log(h, 'N'); + sms_nextoutgoing(h); break; default: /* Unknown */ h->omsg[0] = 0x92; /* SMS_ERROR */ h->omsg[1] = 1; h->omsg[2] = 3; /* unknown message type; */ - sms_messagetx (h); + sms_messagetx(h); break; } } @@ -1552,8 +1549,8 @@ } } } - if (ast_write (chan, &f) < 0) { - ast_log (LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror (errno)); + if (ast_write(chan, &f) < 0) { + ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror (errno)); return -1; } return 0; @@ -1563,12 +1560,12 @@ /*! * Just return the pointer to the descriptor that we received. */ -static void *sms_alloc (struct ast_channel *chan, void *sms_t_ptr) +static void *sms_alloc(struct ast_channel *chan, void *sms_t_ptr) { return sms_t_ptr; } -static void sms_release (struct ast_channel *chan, void *data) +static void sms_release(struct ast_channel *chan, void *data) { return; /* nothing to do here. */ } @@ -1606,18 +1603,18 @@ return; for ( ; samples-- ; data++) { unsigned long long m0, m1; - if (abs (*data) > h->imag) + if (abs(*data) > h->imag) h->imag = abs (*data); else h->imag = h->imag * 7 / 8; if (h->imag <= 500) { /* below [arbitrary] threahold: lost carrier */ if (h->idle++ == 80000) { /* nothing happening */ - ast_log (LOG_NOTICE, "No data, hanging up\n"); + ast_log(LOG_WARNING, "No data, hanging up\n"); h->hangup = 1; h->err = 1; } if (h->ierr) { /* error */ - ast_log (LOG_NOTICE, "Error %d, hanging up\n", h->ierr); + ast_log(LOG_ERROR, "Error %d, hanging up\n", h->ierr); /* Protocol 1 */ h->err = 1; h->omsg[0] = 0x92; /* error */ @@ -1669,15 +1666,15 @@ } if (bit && h->ibitc == 200) { /* sync, restart message */ /* Protocol 2: empty connnection ready (I am master) */ - if(h->framenumber<0 && h->ibytec>=160 && !memcmp(h->imsg,"UUUUUUUUUUUUUUUUUUUU",20)) { + if(h->framenumber < 0 && h->ibytec >= 160 && !memcmp(h->imsg, "UUUUUUUUUUUUUUUUUUUU", 20)) { h->framenumber = 1; if (option_verbose > 2) - ast_verbose (VERBOSE_PREFIX_3 "SMS protocol 2 detected\n"); + ast_verbose(VERBOSE_PREFIX_3 "SMS protocol 2 detected\n"); h->protocol = 2; h->imsg[0] = 0xff; /* special message (fake) */ h->imsg[1] = h->imsg[2] = 0x00; h->ierr = h->ibitn = h->ibytep = h->ibytec = 0; - sms_messagerx (h); + sms_messagerx(h); } h->ierr = h->ibitn = h->ibytep = h->ibytec = 0; } @@ -1687,7 +1684,7 @@ h->iphasep -= 80; if (h->ibitn++ == 9) { /* end of byte */ if (!bit) { /* bad stop bit */ - ast_log(LOG_NOTICE, "bad stop bit"); + ast_log(LOG_WARNING, "bad stop bit"); h->ierr = 0xFF; /* unknown error */ } else { if (h->ibytep < sizeof (h->imsg)) { @@ -1695,14 +1692,14 @@ h->ibytec += h->ibytev; h->ibytep++; } else if (h->ibytep == sizeof (h->imsg)) { - ast_log(LOG_NOTICE, "msg too large"); + ast_log(LOG_ERROR, "msg too large"); h->ierr = 2; /* bad message length */ } if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) { if (!h->ibytec) - sms_messagerx (h); + sms_messagerx(h); else { - ast_log(LOG_NOTICE, "bad checksum"); + ast_log(LOG_WARNING, "bad checksum"); h->ierr = 1; /* bad checksum */ } } @@ -1755,14 +1752,14 @@ char *parse, *sms_opts[OPTION_ARG_ARRAY_SIZE]; char *p; AST_DECLARE_APP_ARGS(sms_args, - AST_APP_ARG(queue); - AST_APP_ARG(options); + AST_APP_ARG(queue); + AST_APP_ARG(options); AST_APP_ARG(addr); AST_APP_ARG(body); ); if (!data) { - ast_log (LOG_ERROR, "Requires queue name at least\n"); + ast_log(LOG_ERROR, "Requires queue name at least\n"); return -1; } @@ -1782,14 +1779,14 @@ h.dcs = 0xF1; /* default */ if (chan->cid.cid_num) - ast_copy_string (h.cli, chan->cid.cid_num, sizeof (h.cli)); + ast_copy_string(h.cli, chan->cid.cid_num, sizeof (h.cli)); if (ast_strlen_zero(sms_args.queue)) { - ast_log (LOG_ERROR, "Requires queue name\n"); + ast_log(LOG_ERROR, "Requires queue name\n"); goto done; } if (strlen(sms_args.queue) >= sizeof(h.queue)) { - ast_log (LOG_ERROR, "Queue name too long\n"); + ast_log(LOG_ERROR, "Queue name too long\n"); goto done; } ast_copy_string(h.queue, sms_args.queue, sizeof(h.queue)); @@ -1831,33 +1828,33 @@ /* deprecated, use smsq instead */ h.scts = time (0); if (ast_strlen_zero(sms_args.addr) || strlen (sms_args.addr) >= sizeof (h.oa)) { - ast_log (LOG_ERROR, "Address too long %s\n", sms_args.addr); + ast_log(LOG_ERROR, "Address too long %s\n", sms_args.addr); goto done; } if (h.smsc) - ast_copy_string (h.oa, sms_args.addr, sizeof (h.oa)); + ast_copy_string(h.oa, sms_args.addr, sizeof(h.oa)); else { - ast_copy_string (h.da, sms_args.addr, sizeof (h.da)); - ast_copy_string (h.oa, h.cli, sizeof (h.oa)); + ast_copy_string(h.da, sms_args.addr, sizeof(h.da)); + ast_copy_string(h.oa, h.cli, sizeof(h.oa)); } h.udl = 0; if (ast_strlen_zero(sms_args.body)) { - ast_log (LOG_ERROR, "Missing body for %s\n", sms_args.addr); + ast_log(LOG_ERROR, "Missing body for %s\n", sms_args.addr); goto done; } up = (unsigned char *)sms_args.body; while (*up && h.udl < SMSLEN) h.ud[h.udl++] = utf8decode(&up); - if (is7bit (h.dcs) && packsms7 (0, h.udhl, h.udh, h.udl, h.ud) < 0) { - ast_log (LOG_WARNING, "Invalid 7 bit GSM data\n"); + if (is7bit(h.dcs) && packsms7(0, h.udhl, h.udh, h.udl, h.ud) < 0) { + ast_log(LOG_WARNING, "Invalid 7 bit GSM data\n"); goto done; } - if (is8bit (h.dcs) && packsms8 (0, h.udhl, h.udh, h.udl, h.ud) < 0) { - ast_log (LOG_WARNING, "Invalid 8 bit data\n"); + if (is8bit(h.dcs) && packsms8(0, h.udhl, h.udh, h.udl, h.ud) < 0) { + ast_log(LOG_WARNING, "Invalid 8 bit data\n"); goto done; } - if (is16bit (h.dcs) && packsms16 (0, h.udhl, h.udh, h.udl, h.ud) < 0) { - ast_log (LOG_WARNING, "Invalid 16 bit data\n"); + if (is16bit(h.dcs) && packsms16(0, h.udhl, h.udh, h.udl, h.ud) < 0) { + ast_log(LOG_WARNING, "Invalid 16 bit data\n"); goto done; } h.rx = 0; /* sent message */ @@ -1877,22 +1874,22 @@ h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE; h.omsg[1] = 0; } - sms_messagetx (&h); + sms_messagetx(&h); } if (chan->_state != AST_STATE_UP) - ast_answer (chan); + ast_answer(chan); - res = ast_set_write_format (chan, __OUT_FMT); + res = ast_set_write_format(chan, __OUT_FMT); if (res >= 0) - res = ast_set_read_format (chan, AST_FORMAT_SLINEAR); + res = ast_set_read_format(chan, AST_FORMAT_SLINEAR); if (res < 0) { - ast_log (LOG_ERROR, "Unable to set to linear mode, giving up\n"); + ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n"); goto done; } - if ( (res = ast_activate_generator (chan, &smsgen, &h)) < 0) { - ast_log (LOG_ERROR, "Failed to activate generator on '%s'\n", chan->name); + if ( (res = ast_activate_generator(chan, &smsgen, &h)) < 0) { + ast_log(LOG_ERROR, "Failed to activate generator on '%s'\n", chan->name); goto done; } @@ -1908,20 +1905,20 @@ ast_log(LOG_NOTICE, "channel hangup\n"); break; } - f = ast_read (chan); + f = ast_read(chan); if (!f) { ast_log(LOG_NOTICE, "ast_read failed\n"); break; } if (f->frametype == AST_FRAME_VOICE) { - sms_process (&h, f->samples, f->data); + sms_process(&h, f->samples, f->data); } - ast_frfree (f); + ast_frfree(f); } res = h.err; /* XXX */ - sms_log (&h, '?'); /* log incomplete message */ + sms_log(&h, '?'); /* log incomplete message */ done: ast_module_user_remove(u); return (res); @@ -1931,7 +1928,7 @@ { int res; - res = ast_unregister_application (app); + res = ast_unregister_application(app); ast_module_user_hangup_all(); @@ -1943,11 +1940,11 @@ #ifdef OUTALAW int p; for (p = 0; p < 80; p++) - wavea[p] = AST_LIN2A (wave[p]); + wavea[p] = AST_LIN2A(wave[p]); #endif - snprintf (log_file, sizeof (log_file), "%s/sms", ast_config_AST_LOG_DIR); - snprintf (spool_dir, sizeof (spool_dir), "%s/sms", ast_config_AST_SPOOL_DIR); - return ast_register_application (app, sms_exec, synopsis, descrip); + snprintf(log_file, sizeof (log_file), "%s/sms", ast_config_AST_LOG_DIR); + snprintf(spool_dir, sizeof (spool_dir), "%s/sms", ast_config_AST_SPOOL_DIR); + return ast_register_application(app, sms_exec, synopsis, descrip); } AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SMS/PSTN handler"); Index: apps/app_voicemail.c =================================================================== --- apps/app_voicemail.c (revision 69624) +++ apps/app_voicemail.c (working copy) @@ -938,8 +938,9 @@ #ifdef IMAP_STORAGE static int make_gsm_file(char *dest, char *imapuser, char *dir, int num) { - if (mkdir(dir, 01777) && (errno != EEXIST)) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dir, strerror(errno)); + int res; + if ((res = ast_mkdir(dir, 01777))) { + ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dir, strerror(res)); return sprintf(dest, "%s/msg%04d", dir, num); } /* return sprintf(dest, "%s/s/msg%04d", dir, imapuser, num); */ @@ -982,28 +983,13 @@ static int create_dirpath(char *dest, int len, const char *context, const char *ext, const char *folder) { mode_t mode = VOICEMAIL_DIR_MODE; + int res; - if (!ast_strlen_zero(context)) { - make_dir(dest, len, context, "", ""); - if (mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } + make_dir(dest, len, context, ext, folder); + if ((res = ast_mkdir(dest, mode))) { + ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res)); + return -1; } - if (!ast_strlen_zero(ext)) { - make_dir(dest, len, context, ext, ""); - if (mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } - } - if (!ast_strlen_zero(folder)) { - make_dir(dest, len, context, ext, folder); - if (mkdir(dest, mode) && errno != EEXIST) { - ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", dest, strerror(errno)); - return -1; - } - } return 0; } Index: apps/app_dial.c =================================================================== --- apps/app_dial.c (revision 69624) +++ apps/app_dial.c (working copy) @@ -1128,13 +1128,13 @@ /* make sure the priv-callerintros dir actually exists */ snprintf(pa->privintro, sizeof(pa->privintro), "%s/sounds/priv-callerintros", ast_config_AST_DATA_DIR); - if (mkdir(pa->privintro, 0755) && errno != EEXIST) { - ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(errno)); + if ((res = ast_mkdir(pa->privintro, 0755))) { + ast_log(LOG_WARNING, "privacy: can't create directory priv-callerintros: %s\n", strerror(res)); return -1; } - snprintf(pa->privintro,sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid); - if (ast_fileexists(pa->privintro,NULL,NULL ) > 0 && strncmp(pa->privcid,"NOCALLERID",10) != 0) { + snprintf(pa->privintro, sizeof(pa->privintro), "priv-callerintros/%s", pa->privcid); + if (ast_fileexists(pa->privintro, NULL, NULL ) > 0 && strncmp(pa->privcid, "NOCALLERID", 10) != 0) { /* the DELUX version of this code would allow this caller the option to hear and retape their previously recorded intro. */ Index: include/asterisk/utils.h =================================================================== --- include/asterisk/utils.h (revision 69624) +++ include/asterisk/utils.h (working copy) @@ -593,6 +593,16 @@ */ void ast_enable_packet_fragmentation(int sock); +/*! + \brief Recursively create directory path + \param path The directory path to create + \param mode The permissions with which to try to create the directory + \return 0 on success or an error code otherwise + + Creates a directory path, creating parent directories as needed. + */ +int ast_mkdir(const char *path, int mode); + #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) #include "asterisk/strings.h" Index: main/utils.c =================================================================== --- main/utils.c (revision 69624) +++ main/utils.c (working copy) @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -1058,3 +1059,39 @@ ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n"); #endif /* HAVE_IP_MTU_DISCOVER */ } + +int ast_mkdir(const char *path, int mode) +{ + char *ptr; + int len = strlen(path), count = 0, x, piececount = 0; + char *tmp = ast_strdupa(path); + char **pieces; + char *fullpath = alloca(len + 1); + int res = 0; + + for (ptr = tmp; *ptr; ptr++) { + if (*ptr == '/') + count++; + } + + /* Count the components to the directory path */ + pieces = alloca(count * sizeof(*pieces)); + for (ptr = tmp; *ptr; ptr++) { + if (*ptr == '/') { + *ptr = '\0'; + pieces[piececount++] = ptr + 1; + } + } + + *fullpath = '\0'; + for (x = 0; x < piececount; x++) { + /* This looks funky, but the buffer is always ideally-sized, so it's fine. */ + strcat(fullpath, "/"); + strcat(fullpath, pieces[x]); + res = mkdir(fullpath, mode); + if (res && errno != EEXIST) + return errno; + } + return 0; +} + Index: main/logger.c =================================================================== --- main/logger.c (revision 69624) +++ main/logger.c (working copy) @@ -417,7 +417,7 @@ queue_rotate = 0; qlog = NULL; - mkdir(ast_config_AST_LOG_DIR, 0755); + ast_mkdir(ast_config_AST_LOG_DIR, 0777); AST_LIST_TRAVERSE(&logchannels, f, list) { if (f->disabled) { @@ -796,14 +796,13 @@ /* register the logger cli commands */ ast_cli_register_multiple(cli_logger, sizeof(cli_logger) / sizeof(struct ast_cli_entry)); - mkdir(ast_config_AST_LOG_DIR, 0755); + ast_mkdir(ast_config_AST_LOG_DIR, 0777); /* create log channels */ init_logger_chain(); /* create the eventlog */ if (logfiles.event_log) { - mkdir(ast_config_AST_LOG_DIR, 0755); snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_LOG_DIR, EVENTLOG); eventlog = fopen(tmp, "a"); if (eventlog) { Index: res/res_monitor.c =================================================================== --- res/res_monitor.c (revision 69624) +++ res/res_monitor.c (working copy) @@ -128,7 +128,6 @@ const char *fname_base, int need_lock) { int res = 0; - char tmp[256]; LOCK_IF_NEEDED(chan, need_lock); @@ -137,12 +136,7 @@ char *channel_name, *p; /* Create monitoring directory if needed */ - if (mkdir(ast_config_AST_MONITOR_DIR, 0770) < 0) { - if (errno != EEXIST) { - ast_log(LOG_WARNING, "Unable to create audio monitor directory: %s\n", - strerror(errno)); - } - } + ast_mkdir(ast_config_AST_MONITOR_DIR, 0777); if (!(monitor = ast_calloc(1, sizeof(*monitor)))) { UNLOCK_IF_NEEDED(chan, need_lock); @@ -154,10 +148,8 @@ int directory = strchr(fname_base, '/') ? 1 : 0; /* try creating the directory just in case it doesn't exist */ if (directory) { - char *name = ast_strdup(fname_base); - snprintf(tmp, sizeof(tmp), "mkdir -p \"%s\"",dirname(name)); - ast_free(name); - ast_safe_system(tmp); + char *name = ast_strdupa(fname_base); + ast_mkdir(dirname(name), 0777); } snprintf(monitor->read_filename, FILENAME_MAX, "%s/%s-in", directory ? "" : ast_config_AST_MONITOR_DIR, fname_base); @@ -354,7 +346,6 @@ /* Change monitoring filename of a channel */ int ast_monitor_change_fname(struct ast_channel *chan, const char *fname_base, int need_lock) { - char tmp[256]; if (ast_strlen_zero(fname_base)) { ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to null\n", chan->name); return -1; @@ -366,10 +357,8 @@ int directory = strchr(fname_base, '/') ? 1 : 0; /* try creating the directory just in case it doesn't exist */ if (directory) { - char *name = ast_strdup(fname_base); - snprintf(tmp, sizeof(tmp), "mkdir -p %s",dirname(name)); - ast_free(name); - ast_safe_system(tmp); + char *name = ast_strdupa(fname_base); + ast_mkdir(dirname(name), 0777); } snprintf(chan->monitor->filename_base, FILENAME_MAX, "%s/%s", directory ? "" : ast_config_AST_MONITOR_DIR, fname_base);