fix memory leak
[rigol] / commands.c
index f5b397352371040eb7e67f8c0c2e2d8a9056ad0f..498690240602c3a3c86c92482531c486a9eeba28 100644 (file)
@@ -106,17 +106,11 @@ void do_get_buf (struct usb_dev_handle *sc)
        usbtmc_sendscpi (sc, ":TIM:OFFSET 0", NULL, 0);
 }
 
-void do_get_screen(struct usb_dev_handle *sc)
+static unsigned char* get_lcd(struct usb_dev_handle *sc, int *imglen)
 {
        unsigned char screen[320*234];
-       time_t lt;
-       char filename[256];
        unsigned char *png;
-       int imglen;
-       int ret;
        int l;
-       int fd;
-       pid_t display;
 
        /* Hide "RMT" from screen */
        l = usbtmc_sendscpi(sc, ":KEY:LOCK DISABLE", NULL, 0); 
@@ -128,21 +122,40 @@ void do_get_screen(struct usb_dev_handle *sc)
                printf ("hmm. didnt' get %d bytes, but %d\n\n", sizeof(screen), l); 
        }
 
-       png = lcd2png(screen, &imglen);
+       png = lcd2png(screen, imglen);
+
+       return png;
+}
+
+void do_get_screen(struct usb_dev_handle *sc)
+{
+       time_t lt;
+       char filename[256];
+       unsigned char *png;
+       int imglen;
+       int ret;
+       int fd;
+       pid_t display;
+
+       png = get_lcd(sc, &imglen);
+       if (png == NULL) {
+               perror("get_lcd");
+               return;
+       }
 
        lt = time(NULL);
        strftime(filename, sizeof(filename), "screen_%Y%m%d-%H%M%S.png", localtime(&lt));
        fd=open(filename, O_CREAT|O_WRONLY, 0644);
        if (fd == -1) {
                perror("open");
-               exit(EXIT_FAILURE);
+               return;
        }
 
        while(imglen > 0) {
                ret = write(fd, png, imglen);
                if (ret == -1) {
                        perror("write");
-                       exit(EXIT_FAILURE);
+                       return;
                }
                imglen -= ret;
        }
@@ -165,3 +178,50 @@ void do_get_screen(struct usb_dev_handle *sc)
        }
 }
 
+void do_display_screen(struct usb_dev_handle *sc)
+{
+       unsigned char *png;
+       int imglen;
+       int ret;
+       int pipefd[2];
+       pid_t display;
+
+       png = get_lcd(sc, &imglen);
+       if (png == NULL) {
+               perror("get_lcd");
+               return;
+       }
+
+       if (pipe(pipefd) == -1) {
+               perror("pipe");
+               return;
+       }
+
+       display = fork();
+       switch(display) {
+               case 0:
+                       close(pipefd[1]);
+                       close(STDIN_FILENO);
+                       dup2(pipefd[0], STDIN_FILENO);
+                       execlp("display", "display", "-", NULL);
+                       exit(0);
+                       break;
+               case -1:
+                       perror("fork");
+                       break;
+               default:
+                       close(pipefd[0]);
+                       while(imglen > 0) {
+                               ret = write(pipefd[1], png, imglen);
+                               if (ret == -1) {
+                                       perror("write");
+                                       exit(EXIT_FAILURE);
+                               }
+                               imglen -= ret;
+                       }
+                       close(pipefd[1]);
+                       free(png);
+                       break;
+       }
+}
+
Impressum, Datenschutz