mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
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
This commit is contained in:
14
fuse/main.c
14
fuse/main.c
@@ -536,11 +536,6 @@ static void connect_mf(struct mediafirefs_user_options *options,
|
|||||||
options->server = "www.mediafire.com";
|
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,
|
ctx->conn = mfconn_create(options->server, options->username,
|
||||||
options->password, options->app_id,
|
options->password, options->app_id,
|
||||||
options->api_key);
|
options->api_key);
|
||||||
@@ -657,6 +652,15 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
parse_arguments(&argc, &argv, &options, ctx->configfile);
|
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);
|
connect_mf(&options, ctx);
|
||||||
|
|
||||||
ret = fuse_main(argc, argv, &mediafirefs_oper, ctx);
|
ret = fuse_main(argc, argv, &mediafirefs_oper, ctx);
|
||||||
|
|||||||
@@ -17,23 +17,15 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _POSIX_C_SOURCE 200809L // for getline
|
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
|
||||||
#include <termios.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "../mfshell.h"
|
#include "../mfshell.h"
|
||||||
|
#include "../../utils/strings.h"
|
||||||
#include "../../mfapi/mfconn.h"
|
#include "../../mfapi/mfconn.h"
|
||||||
#include "../commands.h" // IWYU pragma: keep
|
#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[])
|
int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[])
|
||||||
{
|
{
|
||||||
char *username;
|
char *username;
|
||||||
@@ -46,12 +38,15 @@ int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[])
|
|||||||
|
|
||||||
switch (argc) {
|
switch (argc) {
|
||||||
case 1:
|
case 1:
|
||||||
username = _get_login_from_user();
|
printf("login: ");
|
||||||
password = _get_passwd_from_user();
|
username = string_line_from_stdin(false);
|
||||||
|
printf("passwd: ");
|
||||||
|
password = string_line_from_stdin(true);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
username = argv[1];
|
username = argv[1];
|
||||||
password = _get_passwd_from_user();
|
printf("passwd: ");
|
||||||
|
password = string_line_from_stdin(true);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
username = argv[1];
|
username = argv[1];
|
||||||
@@ -75,59 +70,3 @@ int mfshell_cmd_auth(mfshell * mfshell, int argc, char *const argv[])
|
|||||||
|
|
||||||
return (mfshell->conn != NULL);
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
101
utils/strings.c
101
utils/strings.c
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
||||||
|
* 2014 Johannes Schauer <j.schauer@email.de>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
* 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
|
* 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 <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
#include "strings.h"
|
#include "strings.h"
|
||||||
#include "stringv.h"
|
#include "stringv.h"
|
||||||
@@ -180,84 +185,38 @@ void string_chomp(char *string)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
char *string_line_from_stdin(bool hide)
|
||||||
void
|
|
||||||
string_strip_head(char *string,int c)
|
|
||||||
{
|
{
|
||||||
int count = 0;
|
char *line = NULL;
|
||||||
int len;
|
size_t len;
|
||||||
char *pos;
|
ssize_t bytes_read;
|
||||||
|
struct termios old,
|
||||||
|
new;
|
||||||
|
|
||||||
if(string == NULL) return;
|
if (hide) {
|
||||||
|
if (tcgetattr(STDIN_FILENO, &old) != 0)
|
||||||
len = strlen(string);
|
return NULL;
|
||||||
if(len == 0) return;
|
new = old;
|
||||||
|
new.c_lflag &= ~ECHO;
|
||||||
pos = string;
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
|
||||||
|
return NULL;
|
||||||
while(count < len)
|
|
||||||
{
|
|
||||||
|
|
||||||
if(c <= 0)
|
|
||||||
{
|
|
||||||
if(isaspace((char)pos[0])
|
|
||||||
{
|
|
||||||
pos++;
|
|
||||||
count++;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
bytes_read = getline(&line, &len, stdin);
|
||||||
|
|
||||||
|
if (hide) {
|
||||||
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(
|
if (bytes_read < 3) {
|
||||||
|
if (line != NULL) {
|
||||||
// fix a path with leading slashes
|
free(line);
|
||||||
while(strlen(string) > 0)
|
line = NULL;
|
||||||
{
|
}
|
||||||
if(c > 0)
|
|
||||||
{
|
|
||||||
if(string[0] == c)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
if(string[0] == (c)
|
|
||||||
{
|
|
||||||
string++;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
len = strlen(path);
|
if (line[strlen(line) - 1] == '\n')
|
||||||
buffer = (char*)calloc(len + 1,sizeof(char));
|
line[strlen(line) - 1] = '\0';
|
||||||
strncpy(buffer,path,len);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
return line;
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
||||||
|
* 2014 Johannes Schauer <j.schauer@email.de>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
* 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
|
* under the terms of the GNU General Public License version 2, as published by
|
||||||
@@ -19,6 +20,8 @@
|
|||||||
#ifndef _STR_TOOLS_H_
|
#ifndef _STR_TOOLS_H_
|
||||||
#define _STR_TOOLS_H_
|
#define _STR_TOOLS_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
char *strdup_printf(char *fmt, ...);
|
char *strdup_printf(char *fmt, ...);
|
||||||
|
|
||||||
char *strdup_join(char *string1, char *string2);
|
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_chomp(char *string);
|
||||||
|
|
||||||
// void string_strip_head(char *string,char c);
|
char *string_line_from_stdin(bool hide);
|
||||||
|
|
||||||
// void string_strip_tail(char *string,char c);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user