Merge "res_pjsip_send_to_voicemail.c: Fix off-nominal double channel unref."
[asterisk/asterisk.git] / agi / eagi-sphinx-test.c
old mode 100755 (executable)
new mode 100644 (file)
index 95c47c9..3593602
@@ -6,6 +6,15 @@
  *
  */
 
+/*! \file
+ * Extended AGI test application
+ *
+ * This code is released into public domain
+ * without any warranty of any kind.
+ *
+ *     \ingroup agi
+ */
+
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
-#include <asterisk/compat.h>
+
+#include "asterisk.h"
+
+#include "asterisk/compat.h"
 
 #define AUDIO_FILENO (STDERR_FILENO + 1)
 
@@ -67,7 +79,9 @@ static int read_environment(void)
        char *val;
        /* Read environment */
        for(;;) {
-               fgets(buf, sizeof(buf), stdin);
+               if (!fgets(buf, sizeof(buf), stdin)) {
+                       return -1;
+               }
                if (feof(stdin))
                        return -1;
                buf[strlen(buf) - 1] = '\0';
@@ -118,7 +132,9 @@ static char *wait_result(void)
                        return NULL;
                }
                if (FD_ISSET(STDIN_FILENO, &fds)) {
-                       fgets(astresp, sizeof(astresp), stdin);
+                       if (!fgets(astresp, sizeof(astresp), stdin)) {
+                               return NULL;
+                       }
                        if (feof(stdin)) {
                                fprintf(stderr, "Got hungup on apparently\n");
                                return NULL;
@@ -129,9 +145,10 @@ static char *wait_result(void)
                }
                if (FD_ISSET(AUDIO_FILENO, &fds)) {
                        res = read(AUDIO_FILENO, audiobuf, sizeof(audiobuf));
-                       if (res > 0) {
-                               if (sphinx_sock > -1) 
-                                       write(sphinx_sock, audiobuf, res);
+                       if ((res > 0) && (sphinx_sock > -1)) {
+                               if (write(sphinx_sock, audiobuf, res) < 0) {
+                                       fprintf(stderr, "write() failed: %s\n", strerror(errno));
+                               }
                        }
                }
                if ((sphinx_sock > -1) && FD_ISSET(sphinx_sock, &fds)) {
@@ -206,7 +223,7 @@ int main(int argc, char *argv[])
        connect_sphinx();
        tmp = getenv("agi_enhanced");
        if (tmp) {
-               if (sscanf(tmp, "%d.%d", &ver, &subver) != 2)
+               if (sscanf(tmp, "%30d.%30d", &ver, &subver) != 2)
                        ver = 0;
        }
        if (ver < 1) {