home /
secrets /
src /
util /
inputu.c
inputu.c
//
// inputu.c
// secrets
//
// Created by Anders on 10/07/2020.
//
#if defined linux || defined __linux__ || defined __APPLE__
#include <termios.h>
#define UNIX 1
#endif
#include <string.h>
#include "inputu.h"
#include "require.h"
char* input_fgets(char* dest, uint32_t buffer_size, FILE* stream) {
char* result = fgets(dest, (int) buffer_size, stream);
if (result != NULL) {
require(result[strlen(result) - 1] == '\n', "input was too big\n");
}
return result;
}
#ifdef UNIX
char* input_fgets_hidden(char* dest, uint32_t buffer_size, FILE* stream) {
struct termios old, new;
/* Turn echoing off and fail if we can’t. */
int echo_on = tcgetattr(fileno (stream), &old);
require(!echo_on, "failed to turn off echo");
new = old;
new.c_lflag &= (tcflag_t) ~ECHO;
echo_on = tcsetattr (fileno (stream), TCSAFLUSH, &new);
require(!echo_on, "failed to turn off echo");
char* result = input_fgets(dest, buffer_size, stream);
/* Restore terminal. */
(void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
return result;
}
#else
//todo
char* input_fgets_hidden(char* result, uint32_t buffer_size, FILE* stream) {
return input_fgets(result, buffer_size, stream);
}
#endif