From 9c4195ebddd717cc0ca36dbf84efd568fcf41337 Mon Sep 17 00:00:00 2001 From: josch Date: Mon, 1 Dec 2014 21:59:59 +0100 Subject: [PATCH] fuse: allow entering username and password via stdin - when either username or password are not specified via commandline arguments or configuration file, ask them on standard input - unify function to read username and password - move function to read username and password from mfshell to utils/strings.c - remove commented cruft from utils/strings.c --- fuse/main.c | 14 ++++-- mfshell/commands/auth.c | 77 +++-------------------------- utils/strings.c | 107 +++++++++++++--------------------------- utils/strings.h | 7 +-- 4 files changed, 54 insertions(+), 151 deletions(-) diff --git a/fuse/main.c b/fuse/main.c index 957c09b..065f036 100644 --- a/fuse/main.c +++ b/fuse/main.c @@ -536,11 +536,6 @@ static void connect_mf(struct mediafirefs_user_options *options, options->server = "www.mediafire.com"; } - if (options->username == NULL || options->password == NULL) { - fprintf(stderr, "You must specify username and password\n"); - exit(1); - } - ctx->conn = mfconn_create(options->server, options->username, options->password, options->app_id, options->api_key); @@ -657,6 +652,15 @@ int main(int argc, char *argv[]) parse_arguments(&argc, &argv, &options, ctx->configfile); + if (options.username == NULL) { + printf("login: "); + options.username = string_line_from_stdin(false); + } + if (options.password == NULL) { + printf("passwd: "); + options.password = string_line_from_stdin(true); + } + connect_mf(&options, ctx); ret = fuse_main(argc, argv, &mediafirefs_oper, ctx); diff --git a/mfshell/commands/auth.c b/mfshell/commands/auth.c index bd538c0..a78c28f 100644 --- a/mfshell/commands/auth.c +++ b/mfshell/commands/auth.c @@ -17,23 +17,15 @@ * */ -#define _POSIX_C_SOURCE 200809L // for getline - #include #include -#include -#include -#include -#include +#include #include "../mfshell.h" +#include "../../utils/strings.h" #include "../../mfapi/mfconn.h" #include "../commands.h" // IWYU pragma: keep -static char *_get_login_from_user(void); - -static char *_get_passwd_from_user(void); - int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[]) { char *username; @@ -46,12 +38,15 @@ int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[]) switch (argc) { case 1: - username = _get_login_from_user(); - password = _get_passwd_from_user(); + printf("login: "); + username = string_line_from_stdin(false); + printf("passwd: "); + password = string_line_from_stdin(true); break; case 2: username = argv[1]; - password = _get_passwd_from_user(); + printf("passwd: "); + password = string_line_from_stdin(true); break; case 3: username = argv[1]; @@ -75,59 +70,3 @@ int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[]) return (mfshell->conn != NULL); } - -char *_get_login_from_user(void) -{ - char *login = NULL; - size_t len; - ssize_t bytes_read; - - printf("login: "); - bytes_read = getline(&login, &len, stdin); - - if (bytes_read < 3) { - if (login != NULL) { - free(login); - login = NULL; - } - } - - if (login[strlen(login) - 1] == '\n') - login[strlen(login) - 1] = '\0'; - - return login; -} - -char *_get_passwd_from_user(void) -{ - char *passwd = NULL; - size_t len; - ssize_t bytes_read; - struct termios old, - new; - - printf("passwd: "); - - if (tcgetattr(STDIN_FILENO, &old) != 0) - return NULL; - new = old; - new.c_lflag &= ~ECHO; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0) - return NULL; - - bytes_read = getline(&passwd, &len, stdin); - - tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); - - if (bytes_read < 3) { - if (passwd != NULL) { - free(passwd); - passwd = NULL; - } - } - - if (passwd[strlen(passwd) - 1] == '\n') - passwd[strlen(passwd) - 1] = '\0'; - - return passwd; -} diff --git a/utils/strings.c b/utils/strings.c index e17f7e0..dae20a6 100644 --- a/utils/strings.c +++ b/utils/strings.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2013 Bryan Christ + * 2014 Johannes Schauer * * 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 @@ -16,12 +17,16 @@ * */ +#define _POSIX_C_SOURCE 200809L // for getline + #include #include #include #include #include #include +#include +#include #include "strings.h" #include "stringv.h" @@ -180,84 +185,38 @@ void string_chomp(char *string) return; } -/* -void -string_strip_head(char *string,int c) +char *string_line_from_stdin(bool hide) { - int count = 0; - int len; - char *pos; + char *line = NULL; + size_t len; + ssize_t bytes_read; + struct termios old, + new; - if(string == NULL) return; - - len = strlen(string); - if(len == 0) return; - - pos = string; - - while(count < len) - { - - if(c <= 0) - { - if(isaspace((char)pos[0]) - { - pos++; - count++; - continue; - } - - break; - } - - if( - - // fix a path with leading slashes - while(strlen(string) > 0) - { - if(c > 0) - { - if(string[0] == c) - { - - - if(string[0] == (c) - { - string++; - continue; - } - - len = strlen(path); - buffer = (char*)calloc(len + 1,sizeof(char)); - strncpy(buffer,path,len); + 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; } -*/ -/* - A negative value shifts the string left, while a positive value - shifts the string right. The vacuum-space is zero filled. -*/ -/* -void -string_shift(char *string,ssize_t vector) -{ - char *pos; - ssize_t len; - ssize_t i; + bytes_read = getline(&line, &len, stdin); - if(string == NULL) return; - - len = strlen(string); - if(len <= ABSINT(vector)) return; - - // shift left - if(vector < 0) - { - pos = string + (ABSINT(vector)); - strcpy(string,pos); - string[len - ABSINT(vector) + 1] = '\0'; - - return; + if (hide) { + tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); } + + if (bytes_read < 3) { + if (line != NULL) { + free(line); + line = NULL; + } + } + + if (line[strlen(line) - 1] == '\n') + line[strlen(line) - 1] = '\0'; + + return line; } -*/ diff --git a/utils/strings.h b/utils/strings.h index d032369..2444e4f 100644 --- a/utils/strings.h +++ b/utils/strings.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2013 Bryan Christ + * 2014 Johannes Schauer * * 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 @@ -19,6 +20,8 @@ #ifndef _STR_TOOLS_H_ #define _STR_TOOLS_H_ +#include + char *strdup_printf(char *fmt, ...); char *strdup_join(char *string1, char *string2); @@ -28,8 +31,6 @@ char *strdup_subst(char *string, char *str_old, char *str_new, void string_chomp(char *string); -// void string_strip_head(char *string,char c); - -// void string_strip_tail(char *string,char c); +char *string_line_from_stdin(bool hide); #endif