2014-09-15 20:22:02 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
2014-12-01 21:59:59 +01:00
|
|
|
* 2014 Johannes Schauer <j.schauer@email.de>
|
2014-09-15 20:22:02 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L // for getline
|
2014-12-27 17:15:24 -06:00
|
|
|
#define _GNU_SOURCE // for getline on old systems
|
2014-12-01 21:59:59 +01:00
|
|
|
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <ctype.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <stdarg.h>
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <string.h>
|
2014-12-01 21:59:59 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <termios.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
#include "strings.h"
|
|
|
|
|
#include "stringv.h"
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char *strdup_printf(char *fmt, ...)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
// Good for glibc 2.1 and above. Fedora5 is 2.4.
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
char *ret_str = NULL;
|
|
|
|
|
va_list ap;
|
|
|
|
|
int bytes_to_allocate;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
|
|
|
|
|
va_end(ap);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
// Add one for '\0'
|
|
|
|
|
bytes_to_allocate++;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
ret_str = (char *)malloc(bytes_to_allocate * sizeof(char));
|
2014-10-19 08:58:56 +02:00
|
|
|
if (ret_str == NULL) {
|
|
|
|
|
fprintf(stderr, "failed to allocate memory\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
|
|
|
|
|
va_end(ap);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
return ret_str;
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
char *string_line_from_stdin(bool hide)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-12-01 21:59:59 +01:00
|
|
|
char *line = NULL;
|
|
|
|
|
size_t len;
|
|
|
|
|
ssize_t bytes_read;
|
|
|
|
|
struct termios old,
|
|
|
|
|
new;
|
|
|
|
|
|
|
|
|
|
if (hide) {
|
|
|
|
|
if (tcgetattr(STDIN_FILENO, &old) != 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
new = old;
|
|
|
|
|
new.c_lflag &= ~ECHO;
|
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
bytes_read = getline(&line, &len, stdin);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
if (hide) {
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
|
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
if (bytes_read < 3) {
|
|
|
|
|
if (line != NULL) {
|
|
|
|
|
free(line);
|
|
|
|
|
line = NULL;
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
if (line[strlen(line) - 1] == '\n')
|
|
|
|
|
line[strlen(line) - 1] = '\0';
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-12-01 21:59:59 +01:00
|
|
|
return line;
|
2014-09-15 20:00:05 +02:00
|
|
|
}
|