progs/mkphrase.c: Fix trailing spaces in usage message.
[catacomb] / key / pixie-common.c
index f24b576..d3348bc 100644 (file)
@@ -44,6 +44,7 @@
 
 #include <mLib/alloc.h>
 #include <mLib/dstr.h>
+#include <mLib/macros.h>
 #include <mLib/str.h>
 
 #include "pixie.h"
@@ -181,6 +182,9 @@ void pixie_fdline(int fd, char *buf, size_t sz)
 int pixie_getpass(const char *prompt, char *buf, size_t sz)
 {
   const char *pfd = getenv("CATACOMB_PASSPHRASE_FD");
+  struct termios ta;
+  struct termios ota;
+  char nl = '\n';
   int fd = 0;
 
   /* --- See whether a terminal is what's wanted --- */
@@ -189,28 +193,25 @@ int pixie_getpass(const char *prompt, char *buf, size_t sz)
     fd = atoi(pfd);
     pixie_fdline(fd, buf, sz);
   } else {
-    struct termios ta;
-    struct termios ota;
-    char nl = '\n';
-
     if ((fd = open("/dev/tty", O_RDWR)) < 0)
       goto fail_0;
     if (tcgetattr(fd, &ta) < 0)
       goto fail_1;
     ota = ta;
     ta.c_lflag &= ~(ECHO | ISIG);
-    if (tcsetattr(fd, TCSAFLUSH, &ta))
-      goto fail_1;
-    write(fd, prompt, strlen(prompt));
+    if (tcsetattr(fd, TCSAFLUSH, &ta)) goto fail_1;
+    if (write(fd, prompt, strlen(prompt)) < 0) goto fail_2;
     pixie_fdline(fd, buf, sz);
     tcsetattr(fd, TCSAFLUSH, &ota);
-    write(fd, &nl, 1);
+    if (write(fd, &nl, 1) < 0) goto fail_1;
     close(fd);
   }
   return (0);
 
   /* --- Tidy up if things went wrong --- */
 
+fail_2:
+  tcsetattr(fd, TCSAFLUSH, &ota);
 fail_1:
   close(fd);
 fail_0:
@@ -273,7 +274,7 @@ int pixie_read(int fd, const char *tag, unsigned mode, char *buf, size_t sz)
   /* --- Send the request --- */
 
   dstr_putf(&d, "%s %s\n", mode == PMODE_READ ? "PASS" : "VERIFY", tag);
-  write(fd, d.buf, d.len);
+  if (write(fd, d.buf, d.len) < 0) return (-1);
   dstr_destroy(&d);
 
   /* --- Sort out the result --- */
@@ -283,11 +284,11 @@ again:
   p = buf;
   if ((q = str_getword(&p)) == 0)
     return (-1);
-  if (strcmp(q, "INFO") == 0)
+  if (STRCMP(q, ==, "INFO"))
     goto again;
-  else if (strcmp(q, "MISSING") == 0)
+  else if (STRCMP(q, ==, "MISSING"))
     return (+1);
-  else if (strcmp(q, "OK") != 0)
+  else if (STRCMP(q, !=, "OK"))
     return (-1);
 
   /* --- Return the final answer --- */
@@ -325,9 +326,10 @@ void pixie_set(int fd, const char *tag, const char *phrase)
    */
 
   dstr_putf(&d, "SET %s -- ", tag);
-  write(fd, d.buf, d.len);
-  write(fd, phrase, sz);
-  write(fd, &nl, 1);
+  if (write(fd, d.buf, d.len) < 0 ||
+      write(fd, phrase, sz) < 0 ||
+      write(fd, &nl, 1) < 0)
+    return;
   dstr_destroy(&d);
 
   /* --- Pick up the pieces --- */
@@ -335,7 +337,7 @@ void pixie_set(int fd, const char *tag, const char *phrase)
 again:
   pixie_fdline(fd, buf, sizeof(buf));
   p = buf;
-  if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
+  if ((q = str_getword(&p)) != 0 && STRCMP(q, ==, "INFO"))
     goto again;
 }
 
@@ -358,7 +360,7 @@ void pixie_cancel(int fd, const char *tag)
   /* --- Send the request --- */
 
   dstr_putf(&d, "FLUSH %s\n", tag);
-  write(fd, d.buf, d.len);
+  if (write(fd, d.buf, d.len) < 0) return;
   dstr_destroy(&d);
 
   /* --- Sort out the result --- */
@@ -366,9 +368,8 @@ void pixie_cancel(int fd, const char *tag)
 again:
   pixie_fdline(fd, buf, sizeof(buf));
   p = buf;
-  if ((q = str_getword(&p)) != 0 && strcmp(q, "INFO") == 0)
+  if ((q = str_getword(&p)) != 0 && STRCMP(q, ==, "INFO"))
     goto again;
 }
 
 /*----- That's all, folks -------------------------------------------------*/
-