Skip to content

Commit

Permalink
Use stdlib getline instead of our version
Browse files Browse the repository at this point in the history
Should close out issue #6
  • Loading branch information
joshkunz committed Oct 31, 2016
1 parent fea53dc commit f6773ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 50 deletions.
9 changes: 7 additions & 2 deletions src/ashuffle.c
@@ -1,6 +1,6 @@
#define _GNU_SOURCE

#include <mpd/client.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -85,6 +85,11 @@ int build_songs_file(struct mpd_connection * mpd, struct list * ruleset,
size_t ignored = 0;
length = getline(&uri, &ignored, input);
while (! feof(input) && ! ferror(input)) {
if (length < 1) {
fprintf(stderr, "invalid URI in input stream\n");
exit(1);
}

/* if this line has terminating newline attached, set it
* to null and decrement the length (effectively removing
* the newline). */
Expand Down Expand Up @@ -233,7 +238,7 @@ int shuffle_idle(struct mpd_connection * mpd,
void get_mpd_password(struct mpd_connection * mpd) {
/* keep looping till we get a bad error, or we get a good password. */
while (true) {
char * pass = getpass(stdin, stdout, "mpd password: ");
char * pass = as_getpass(stdin, stdout, "mpd password: ");
mpd_run_password(mpd, pass);
const enum mpd_error err = mpd_connection_get_error(mpd);
if (err == MPD_ERROR_SUCCESS) {
Expand Down
54 changes: 7 additions & 47 deletions src/getpass.c
@@ -1,4 +1,4 @@
#define _POSIX_SOURCE
#define _GNU_SOURCE
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
Expand All @@ -11,50 +11,6 @@

#define DEFAULT_GETLINE_BUFSIZE 100

static void * xmalloc(size_t size) {
void * mem = malloc(size);
if (mem == NULL) {
perror("xmalloc");
exit(1);
}
return mem;
}

static void getline(char ** lineptr, size_t *n, FILE *stream) {
char * buf = *lineptr;
size_t bufsize = *n;
if (bufsize == 0) {
assert(buf == NULL && "got zero bufsize, but non-null buffer");
}

size_t read = 0;
char c;
do {
if (read == bufsize) {
char * tmp_buf = buf;
size_t tmp_bufsize = bufsize;
/* +1 so I don't have to special-case when bufsize = 0 */
bufsize = (bufsize + 1) * 2;
buf = xmalloc(bufsize);
memcpy(buf, tmp_buf, tmp_bufsize);
}
if (fread(&c, 1, 1, stream) != 1) {
perror("getline");
exit(1);
}
if (c == '\n') { break; }
buf[read] = c;
read += 1;
} while (true);


buf = realloc(buf, read);
if (buf == NULL) { perror("getline"); exit(1); }
buf[read] = '\0';
*lineptr = buf;
*n = read;
}

#define set_flag(field, flag, state) \
do { \
if ((state)) { \
Expand All @@ -80,7 +36,7 @@ static void set_echo(FILE *stream, bool echo_state, bool echo_nl_state) {
}
}

char * getpass(FILE * in_stream, FILE * out_stream, const char *prompt) {
char * as_getpass(FILE * in_stream, FILE * out_stream, const char *prompt) {
if (fwrite(prompt, strlen(prompt), 1, out_stream) != 1) {
perror("getpass (fwrite)");
exit(1);
Expand All @@ -90,7 +46,11 @@ char * getpass(FILE * in_stream, FILE * out_stream, const char *prompt) {

char * result = NULL;
size_t result_size = 0;
getline(&result, &result_size, in_stream);
ssize_t result_len = getline(&result, &result_size, in_stream);
if (result_len < 0) {
perror("getline (getpass)");
exit(1);
}

set_echo(out_stream, true, true);

Expand Down
2 changes: 1 addition & 1 deletion src/getpass.h
Expand Up @@ -3,6 +3,6 @@

#include <stdio.h>

char * getpass(FILE * in_stream, FILE * out_stream, const char *prompt);
char * as_getpass(FILE * in_stream, FILE * out_stream, const char *prompt);

#endif

0 comments on commit f6773ca

Please sign in to comment.