#include <mLib/alloc.h>
#include <mLib/dstr.h>
+#include <mLib/macros.h>
#include <mLib/str.h>
#include "pixie.h"
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 --- */
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:
/* --- 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 --- */
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 --- */
*/
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 --- */
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;
}
/* --- 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 --- */
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 -------------------------------------------------*/
-