mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
use gnu indent to enforce coding style and adapt source
- indent options are listed in ./.indent.pro - use test case to run indent
This commit is contained in:
33
.indent.pro
vendored
Normal file
33
.indent.pro
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
--blank-lines-after-declarations
|
||||
--blank-lines-after-procedures
|
||||
--blank-lines-after-commas
|
||||
--break-before-boolean-operator
|
||||
--honour-newlines
|
||||
--braces-on-if-line
|
||||
--braces-on-struct-decl-line
|
||||
--comment-indentation33
|
||||
--declaration-comment-column33
|
||||
--no-comment-delimiters-on-blank-lines
|
||||
--cuddle-else
|
||||
--continuation-indentation4
|
||||
--case-indentation4
|
||||
--line-comments-indentation0
|
||||
--declaration-indentation16
|
||||
--dont-format-first-column-comments
|
||||
--indent-level4
|
||||
--parameter-indentation0
|
||||
--line-length80
|
||||
--continue-at-parentheses
|
||||
--no-space-after-function-call-names
|
||||
--no-space-after-parentheses
|
||||
--dont-break-procedure-type
|
||||
--space-after-if
|
||||
--space-after-for
|
||||
--space-after-while
|
||||
--no-space-after-casts
|
||||
--swallow-optional-blank-lines
|
||||
--dont-format-comments
|
||||
--else-endif-column33
|
||||
--space-special-semicolon
|
||||
--indent-label1
|
||||
--no-tabs
|
||||
@@ -19,3 +19,4 @@ target_link_libraries(mfshell curl ssl crypto jansson mfapi mfutils)
|
||||
enable_testing()
|
||||
|
||||
add_test(iwyu ../tests/iwyu.py)
|
||||
add_test(indent ../tests/indent.sh)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFAPI_APICALLS_H_
|
||||
#define _MFAPI_APICALLS_H_
|
||||
|
||||
@@ -27,20 +26,29 @@
|
||||
#include "folder.h"
|
||||
#include "mfconn.h"
|
||||
|
||||
int mfconn_api_file_get_info(mfconn *conn, mffile *file, char *quickkey);
|
||||
int mfconn_api_file_get_info(mfconn * conn, mffile * file,
|
||||
char *quickkey);
|
||||
|
||||
int mfconn_api_file_get_links(mfconn *conn, mffile *file, char *quickkey);
|
||||
int mfconn_api_file_get_links(mfconn * conn, mffile * file,
|
||||
char *quickkey);
|
||||
|
||||
int mfconn_api_folder_create(mfconn *conn, char *parent, char *name);
|
||||
int mfconn_api_folder_create(mfconn * conn, char *parent,
|
||||
char *name);
|
||||
|
||||
long mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr);
|
||||
long mfconn_api_folder_get_content(mfconn * conn, int mode,
|
||||
mffolder * folder_curr);
|
||||
|
||||
int mfconn_api_folder_get_info(mfconn *conn, mffolder *folder, char *folderkey);
|
||||
int mfconn_api_folder_get_info(mfconn * conn, mffolder * folder,
|
||||
char *folderkey);
|
||||
|
||||
int mfconn_api_user_get_info(mfconn *conn);
|
||||
int mfconn_api_user_get_info(mfconn * conn);
|
||||
|
||||
int mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
||||
char *username, char *password, int app_id, char *app_key,
|
||||
uint32_t *secret_key, char **secret_time, char **session_token);
|
||||
int mfconn_api_user_get_session_token(mfconn * conn, char *server,
|
||||
char *username,
|
||||
char *password, int app_id,
|
||||
char *app_key,
|
||||
uint32_t * secret_key,
|
||||
char **secret_time,
|
||||
char **session_token);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -26,76 +25,80 @@
|
||||
#include "../../utils/json.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../file.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_file_get_info(mfhttp *conn, void *data);
|
||||
static int _decode_file_get_info(mfhttp * conn, void *data);
|
||||
|
||||
int
|
||||
mfconn_api_file_get_info(mfconn *conn,mffile *file,char *quickkey)
|
||||
int mfconn_api_file_get_info(mfconn * conn, mffile * file, char *quickkey)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
int len;
|
||||
char *api_call;
|
||||
int retval;
|
||||
int len;
|
||||
mfhttp *http;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if(file == NULL) return -1;
|
||||
if(quickkey == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (quickkey == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(quickkey);
|
||||
|
||||
// key must either be 11 or 15 chars
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
api_call = mfconn_create_signed_get(conn, 1, "file/get_info.php",
|
||||
"?quick_key=%s&response_format=json", quickkey);
|
||||
"?quick_key=%s&response_format=json",
|
||||
quickkey);
|
||||
|
||||
mfhttp *http = http_create();
|
||||
http = http_create();
|
||||
retval = http_get_buf(http, api_call, _decode_file_get_info, file);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_file_get_info(mfhttp *conn, void *data)
|
||||
static int _decode_file_get_info(mfhttp * conn, void *data)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *quickkey;
|
||||
json_t *file_hash;
|
||||
json_t *file_name;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *quickkey;
|
||||
json_t *file_hash;
|
||||
json_t *file_name;
|
||||
int retval = 0;
|
||||
mffile *file;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
file = (mffile *)data;
|
||||
file = (mffile *) data;
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root,"response/file_info");
|
||||
node = json_object_by_path(root, "response/file_info");
|
||||
|
||||
quickkey = json_object_get(node,"quickkey");
|
||||
if(quickkey != NULL)
|
||||
file_set_key(file,(char*)json_string_value(quickkey));
|
||||
quickkey = json_object_get(node, "quickkey");
|
||||
if (quickkey != NULL)
|
||||
file_set_key(file, (char *)json_string_value(quickkey));
|
||||
|
||||
file_name = json_object_get(node,"filename");
|
||||
if(file_name != NULL)
|
||||
file_set_name(file,(char*)json_string_value(file_name));
|
||||
file_name = json_object_get(node, "filename");
|
||||
if (file_name != NULL)
|
||||
file_set_name(file, (char *)json_string_value(file_name));
|
||||
|
||||
file_hash = json_object_get(node,"hash");
|
||||
if(file_hash != NULL)
|
||||
{
|
||||
file_set_hash(file,(char*)json_string_value(file_hash));
|
||||
file_hash = json_object_get(node, "hash");
|
||||
if (file_hash != NULL) {
|
||||
file_set_hash(file, (char *)json_string_value(file_hash));
|
||||
}
|
||||
|
||||
if(quickkey == NULL) retval = -1;
|
||||
if (quickkey == NULL)
|
||||
retval = -1;
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -26,95 +25,95 @@
|
||||
#include "../../utils/json.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../file.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_file_get_links(mfhttp *conn, void *data);
|
||||
static int _decode_file_get_links(mfhttp * conn, void *data);
|
||||
|
||||
int
|
||||
mfconn_api_file_get_links(mfconn *conn,mffile *file,char *quickkey)
|
||||
int mfconn_api_file_get_links(mfconn * conn, mffile * file, char *quickkey)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
int len;
|
||||
char *api_call;
|
||||
int retval;
|
||||
int len;
|
||||
mfhttp *http;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if(file == NULL) return -1;
|
||||
if(quickkey == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (quickkey == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(quickkey);
|
||||
|
||||
// key must either be 11 or 15 chars
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
api_call = mfconn_create_signed_get(conn,0,"file/get_links.php",
|
||||
"?quick_key=%s&response_format=json", quickkey);
|
||||
api_call = mfconn_create_signed_get(conn, 0, "file/get_links.php",
|
||||
"?quick_key=%s&response_format=json",
|
||||
quickkey);
|
||||
|
||||
mfhttp *http = http_create();
|
||||
http = http_create();
|
||||
retval = http_get_buf(http, api_call, _decode_file_get_links, file);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_file_get_links(mfhttp *conn, void *data)
|
||||
static int _decode_file_get_links(mfhttp * conn, void *data)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *quickkey;
|
||||
json_t *share_link;
|
||||
json_t *direct_link;
|
||||
json_t *onetime_link;
|
||||
json_t *links_array;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *quickkey;
|
||||
json_t *share_link;
|
||||
json_t *direct_link;
|
||||
json_t *onetime_link;
|
||||
json_t *links_array;
|
||||
int retval = 0;
|
||||
mffile *file;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
file = (mffile *)data;
|
||||
file = (mffile *) data;
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root,"response");
|
||||
node = json_object_by_path(root, "response");
|
||||
|
||||
links_array = json_object_get(node,"links");
|
||||
if(!json_is_array(links_array))
|
||||
{
|
||||
links_array = json_object_get(node, "links");
|
||||
if (!json_is_array(links_array)) {
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// just get the first one. maybe later support multi-quickkey
|
||||
node = json_array_get(links_array,0);
|
||||
node = json_array_get(links_array, 0);
|
||||
|
||||
quickkey = json_object_get(node,"quickkey");
|
||||
if(quickkey != NULL)
|
||||
file_set_key(file,(char*)json_string_value(quickkey));
|
||||
quickkey = json_object_get(node, "quickkey");
|
||||
if (quickkey != NULL)
|
||||
file_set_key(file, (char *)json_string_value(quickkey));
|
||||
|
||||
share_link = json_object_get(node,"normal_download");
|
||||
if(share_link != NULL)
|
||||
file_set_share_link(file,(char*)json_string_value(share_link));
|
||||
share_link = json_object_get(node, "normal_download");
|
||||
if (share_link != NULL)
|
||||
file_set_share_link(file, (char *)json_string_value(share_link));
|
||||
|
||||
direct_link = json_object_get(node,"direct_download");
|
||||
if(direct_link != NULL)
|
||||
{
|
||||
file_set_direct_link(file,(char*)json_string_value(direct_link));
|
||||
direct_link = json_object_get(node, "direct_download");
|
||||
if (direct_link != NULL) {
|
||||
file_set_direct_link(file, (char *)json_string_value(direct_link));
|
||||
}
|
||||
|
||||
onetime_link = json_object_get(node,"one_time_download");
|
||||
if(onetime_link != NULL)
|
||||
{
|
||||
file_set_onetime_link(file,(char*)json_string_value(onetime_link));
|
||||
onetime_link = json_object_get(node, "one_time_download");
|
||||
if (onetime_link != NULL) {
|
||||
file_set_onetime_link(file, (char *)json_string_value(onetime_link));
|
||||
}
|
||||
|
||||
// if this is false something went horribly wrong
|
||||
if(share_link == NULL) retval = -1;
|
||||
if (share_link == NULL)
|
||||
retval = -1;
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,53 +17,51 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../utils/http.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfconn_api_folder_create(mfconn *conn,char *parent,char *name)
|
||||
int mfconn_api_folder_create(mfconn * conn, char *parent, char *name)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
char *api_call;
|
||||
int retval;
|
||||
mfhttp *http;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if(name == NULL) return -1;
|
||||
if(strlen(name) < 1) return -1;
|
||||
if (name == NULL)
|
||||
return -1;
|
||||
if (strlen(name) < 1)
|
||||
return -1;
|
||||
|
||||
// key must either be 11 chars or "myfiles"
|
||||
if(parent != NULL)
|
||||
{
|
||||
if(strlen(parent) != 13)
|
||||
{
|
||||
if (parent != NULL) {
|
||||
if (strlen(parent) != 13) {
|
||||
// if it is myfiles, set paret to NULL
|
||||
if(strcmp(parent,"myfiles") == 0) parent = NULL;
|
||||
if (strcmp(parent, "myfiles") == 0)
|
||||
parent = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if(parent != NULL)
|
||||
{
|
||||
api_call = mfconn_create_signed_get(conn,0,"folder/create.php",
|
||||
"?parent_key=%s"
|
||||
"&foldername=%s"
|
||||
"&response_format=json",
|
||||
parent,name);
|
||||
}
|
||||
else
|
||||
{
|
||||
api_call = mfconn_create_signed_get(conn,0,"folder/create.php",
|
||||
"?foldername=%s&response_format=json", name);
|
||||
if (parent != NULL) {
|
||||
api_call =
|
||||
mfconn_create_signed_get(conn, 0, "folder/create.php",
|
||||
"?parent_key=%s" "&foldername=%s"
|
||||
"&response_format=json", parent, name);
|
||||
} else {
|
||||
api_call =
|
||||
mfconn_create_signed_get(conn, 0, "folder/create.php",
|
||||
"?foldername=%s&response_format=json",
|
||||
name);
|
||||
}
|
||||
|
||||
mfhttp *http = http_create();
|
||||
http = http_create();
|
||||
retval = http_get_buf(http, api_call, NULL, NULL);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -27,67 +26,69 @@
|
||||
#include "../../utils/strings.h"
|
||||
#include "../folder.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_folder_get_content_folders(mfhttp *conn, void *data);
|
||||
static int _decode_folder_get_content_folders(mfhttp * conn, void *data);
|
||||
|
||||
static int
|
||||
_decode_folder_get_content_files(mfhttp *conn, void *data);
|
||||
static int _decode_folder_get_content_files(mfhttp * conn, void *data);
|
||||
|
||||
long
|
||||
mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr)
|
||||
mfconn_api_folder_get_content(mfconn * conn, int mode, mffolder * folder_curr)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
char *content_type;
|
||||
char *api_call;
|
||||
int retval;
|
||||
char *content_type;
|
||||
mfhttp *http;
|
||||
const char *folderkey;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if(mode == 0)
|
||||
if (mode == 0)
|
||||
content_type = "folders";
|
||||
else
|
||||
content_type = "files";
|
||||
|
||||
const char *folderkey = folder_get_key(folder_curr);
|
||||
folderkey = folder_get_key(folder_curr);
|
||||
if (folderkey == NULL) {
|
||||
fprintf(stderr, "folder_get_key NULL\n");
|
||||
return 0;
|
||||
}
|
||||
/*if (folderkey[0] == '\0') {
|
||||
fprintf(stderr, "folder_get_key '\\0'\n");
|
||||
return 0;
|
||||
}*/
|
||||
api_call = mfconn_create_signed_get(conn,0,"folder/get_content.php",
|
||||
"?folder_key=%s"
|
||||
"&content_type=%s"
|
||||
"&response_format=json",
|
||||
folderkey,
|
||||
content_type);
|
||||
fprintf(stderr, "folder_get_key '\\0'\n");
|
||||
return 0;
|
||||
} */
|
||||
api_call = mfconn_create_signed_get(conn, 0, "folder/get_content.php",
|
||||
"?folder_key=%s"
|
||||
"&content_type=%s"
|
||||
"&response_format=json",
|
||||
folderkey, content_type);
|
||||
|
||||
mfhttp* http = http_create();
|
||||
|
||||
if(mode == 0)
|
||||
retval = http_get_buf(http, api_call, _decode_folder_get_content_folders, NULL);
|
||||
http = http_create();
|
||||
if (mode == 0)
|
||||
retval =
|
||||
http_get_buf(http, api_call,
|
||||
_decode_folder_get_content_folders, NULL);
|
||||
else
|
||||
retval = http_get_buf(http, api_call, _decode_folder_get_content_files, NULL);
|
||||
retval =
|
||||
http_get_buf(http, api_call,
|
||||
_decode_folder_get_content_files, NULL);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_folder_get_content_folders(mfhttp *conn, void *user_ptr)
|
||||
static int _decode_folder_get_content_folders(mfhttp * conn, void *user_ptr)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *data;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *data;
|
||||
|
||||
json_t *folders_array;
|
||||
json_t *folderkey;
|
||||
json_t *folder_name;
|
||||
char *folder_name_tmp;
|
||||
json_t *folders_array;
|
||||
json_t *folderkey;
|
||||
json_t *folder_name;
|
||||
char *folder_name_tmp;
|
||||
|
||||
int array_sz;
|
||||
int i = 0;
|
||||
@@ -98,58 +99,53 @@ _decode_folder_get_content_folders(mfhttp *conn, void *user_ptr)
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
/*json_t *result = json_object_by_path(root, "response/action");
|
||||
fprintf(stderr, "response/action: %s\n", (char*)json_string_value(result));*/
|
||||
fprintf(stderr, "response/action: %s\n", (char*)json_string_value(result)); */
|
||||
|
||||
node = json_object_by_path(root,"response/folder_content");
|
||||
node = json_object_by_path(root, "response/folder_content");
|
||||
|
||||
folders_array = json_object_get(node,"folders");
|
||||
if(!json_is_array(folders_array))
|
||||
{
|
||||
folders_array = json_object_get(node, "folders");
|
||||
if (!json_is_array(folders_array)) {
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
array_sz = json_array_size(folders_array);
|
||||
for(i = 0;i < array_sz;i++)
|
||||
{
|
||||
data = json_array_get(folders_array,i);
|
||||
for (i = 0; i < array_sz; i++) {
|
||||
data = json_array_get(folders_array, i);
|
||||
|
||||
if(json_is_object(data))
|
||||
{
|
||||
folderkey = json_object_get(data,"folderkey");
|
||||
if (json_is_object(data)) {
|
||||
folderkey = json_object_get(data, "folderkey");
|
||||
|
||||
folder_name = json_object_get(data,"name");
|
||||
folder_name = json_object_get(data, "name");
|
||||
|
||||
if(folderkey != NULL && folder_name != NULL)
|
||||
{
|
||||
if (folderkey != NULL && folder_name != NULL) {
|
||||
folder_name_tmp = strdup_printf("< %s >",
|
||||
json_string_value(folder_name));
|
||||
json_string_value(folder_name));
|
||||
|
||||
printf(" %-15.13s %s\n\r",
|
||||
json_string_value(folderkey),
|
||||
folder_name_tmp);
|
||||
json_string_value(folderkey), folder_name_tmp);
|
||||
|
||||
free(folder_name_tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
||||
static int _decode_folder_get_content_files(mfhttp * conn, void *user_ptr)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *data;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *data;
|
||||
|
||||
json_t *files_array;
|
||||
json_t *quickkey;
|
||||
json_t *file_name;
|
||||
json_t *files_array;
|
||||
json_t *quickkey;
|
||||
json_t *file_name;
|
||||
int array_sz;
|
||||
int i = 0;
|
||||
|
||||
@@ -158,36 +154,33 @@ _decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root,"response/folder_content");
|
||||
node = json_object_by_path(root, "response/folder_content");
|
||||
|
||||
files_array = json_object_get(node,"files");
|
||||
if(!json_is_array(files_array))
|
||||
{
|
||||
files_array = json_object_get(node, "files");
|
||||
if (!json_is_array(files_array)) {
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
array_sz = json_array_size(files_array);
|
||||
for(i = 0;i < array_sz;i++)
|
||||
{
|
||||
data = json_array_get(files_array,i);
|
||||
for (i = 0; i < array_sz; i++) {
|
||||
data = json_array_get(files_array, i);
|
||||
|
||||
if(json_is_object(data))
|
||||
{
|
||||
quickkey = json_object_get(data,"quickkey");
|
||||
if (json_is_object(data)) {
|
||||
quickkey = json_object_get(data, "quickkey");
|
||||
|
||||
file_name = json_object_get(data,"filename");
|
||||
file_name = json_object_get(data, "filename");
|
||||
|
||||
if(quickkey != NULL && file_name != NULL)
|
||||
{
|
||||
if (quickkey != NULL && file_name != NULL) {
|
||||
printf(" %-15.15s %s\n\r",
|
||||
json_string_value(quickkey),
|
||||
json_string_value(file_name));
|
||||
json_string_value(quickkey),
|
||||
json_string_value(file_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -26,79 +25,83 @@
|
||||
#include "../../utils/json.h"
|
||||
#include "../folder.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_folder_get_info(mfhttp *conn, void *data);
|
||||
static int _decode_folder_get_info(mfhttp * conn, void *data);
|
||||
|
||||
int
|
||||
mfconn_api_folder_get_info(mfconn *conn,mffolder *folder,char *folderkey)
|
||||
mfconn_api_folder_get_info(mfconn * conn, mffolder * folder, char *folderkey)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
char *api_call;
|
||||
int retval;
|
||||
mfhttp *http;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
if(folder == NULL) return -1;
|
||||
if(folderkey == NULL) return -1;
|
||||
if (folder == NULL)
|
||||
return -1;
|
||||
if (folderkey == NULL)
|
||||
return -1;
|
||||
|
||||
// key must either be 11 chars or "myfiles"
|
||||
if(strlen(folderkey) != 13)
|
||||
{
|
||||
if(strcmp(folderkey,"myfiles") == 0) return -1;
|
||||
if (strlen(folderkey) != 13) {
|
||||
if (strcmp(folderkey, "myfiles") == 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
api_call = mfconn_create_signed_get(conn,0,"folder/get_info.php",
|
||||
"?folder_key=%s&response_format=json", folderkey);
|
||||
api_call = mfconn_create_signed_get(conn, 0, "folder/get_info.php",
|
||||
"?folder_key=%s&response_format=json",
|
||||
folderkey);
|
||||
|
||||
mfhttp *http = http_create();
|
||||
http = http_create();
|
||||
retval = http_get_buf(http, api_call, _decode_folder_get_info, folder);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_folder_get_info(mfhttp *conn, void *data)
|
||||
static int _decode_folder_get_info(mfhttp * conn, void *data)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *folderkey;
|
||||
json_t *folder_name;
|
||||
json_t *parent_folder;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *folderkey;
|
||||
json_t *folder_name;
|
||||
json_t *parent_folder;
|
||||
int retval = 0;
|
||||
mffolder *folder;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
folder = (mffolder *)data;
|
||||
folder = (mffolder *) data;
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root,"response/folder_info");
|
||||
node = json_object_by_path(root, "response/folder_info");
|
||||
|
||||
folderkey = json_object_get(node,"folderkey");
|
||||
if(folderkey != NULL)
|
||||
folder_set_key(folder,(char*)json_string_value(folderkey));
|
||||
folderkey = json_object_get(node, "folderkey");
|
||||
if (folderkey != NULL)
|
||||
folder_set_key(folder, (char *)json_string_value(folderkey));
|
||||
|
||||
folder_name = json_object_get(node,"name");
|
||||
if(folder_name != NULL)
|
||||
folder_set_name(folder,(char*)json_string_value(folder_name));
|
||||
folder_name = json_object_get(node, "name");
|
||||
if (folder_name != NULL)
|
||||
folder_set_name(folder, (char *)json_string_value(folder_name));
|
||||
|
||||
parent_folder = json_object_get(node,"parent_folderkey");
|
||||
if(parent_folder != NULL)
|
||||
{
|
||||
folder_set_parent(folder,(char*)json_string_value(parent_folder));
|
||||
parent_folder = json_object_get(node, "parent_folderkey");
|
||||
if (parent_folder != NULL) {
|
||||
folder_set_parent(folder, (char *)json_string_value(parent_folder));
|
||||
}
|
||||
|
||||
// infer that the parent folder must be "myfiles" root
|
||||
if(parent_folder == NULL && folderkey != NULL)
|
||||
folder_set_parent(folder,"myfiles");
|
||||
if (parent_folder == NULL && folderkey != NULL)
|
||||
folder_set_parent(folder, "myfiles");
|
||||
|
||||
if(folderkey == NULL) retval = -1;
|
||||
if (folderkey == NULL)
|
||||
retval = -1;
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
@@ -17,69 +17,69 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../utils/http.h"
|
||||
#include "../../utils/json.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_user_get_info(mfhttp *conn, void *data);
|
||||
static int _decode_user_get_info(mfhttp * conn, void *data);
|
||||
|
||||
int
|
||||
mfconn_api_user_get_info(mfconn *conn)
|
||||
int mfconn_api_user_get_info(mfconn * conn)
|
||||
{
|
||||
char *api_call;
|
||||
int retval;
|
||||
char *api_call;
|
||||
int retval;
|
||||
mfhttp *http;
|
||||
|
||||
// char *rx_buffer;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
api_call = mfconn_create_signed_get(conn,0,"user/get_info.php",
|
||||
"&response_format=json");
|
||||
api_call = mfconn_create_signed_get(conn, 0, "user/get_info.php",
|
||||
"&response_format=json");
|
||||
|
||||
mfhttp* http = http_create();
|
||||
http = http_create();
|
||||
retval = http_get_buf(http, api_call, _decode_user_get_info, NULL);
|
||||
http_destroy(http);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_user_get_info(mfhttp *conn, void *data)
|
||||
static int _decode_user_get_info(mfhttp * conn, void *data)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *email;
|
||||
json_t *first_name;
|
||||
json_t *last_name;
|
||||
json_t *root;
|
||||
json_t *node;
|
||||
json_t *email;
|
||||
json_t *first_name;
|
||||
json_t *last_name;
|
||||
|
||||
if (data != NULL)
|
||||
return -1;
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
node = json_object_by_path(root,"response/user_info");
|
||||
node = json_object_by_path(root, "response/user_info");
|
||||
|
||||
email = json_object_get(node,"email");
|
||||
if(email != NULL)
|
||||
printf("Email: %s\n\r",(char*)json_string_value(email));
|
||||
email = json_object_get(node, "email");
|
||||
if (email != NULL)
|
||||
printf("Email: %s\n\r", (char *)json_string_value(email));
|
||||
|
||||
first_name = json_object_get(node,"first_name");
|
||||
if(first_name != NULL)
|
||||
printf("Name: %s ",(char*)json_string_value(first_name));
|
||||
first_name = json_object_get(node, "first_name");
|
||||
if (first_name != NULL)
|
||||
printf("Name: %s ", (char *)json_string_value(first_name));
|
||||
|
||||
last_name = json_object_get(node,"last_name");
|
||||
if(node != NULL)
|
||||
printf("%s",(char*)json_string_value(last_name));
|
||||
last_name = json_object_get(node, "last_name");
|
||||
if (node != NULL)
|
||||
printf("%s", (char *)json_string_value(last_name));
|
||||
|
||||
printf("\n\r");
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <jansson.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -28,49 +27,53 @@
|
||||
#include "../../utils/json.h"
|
||||
#include "../../utils/strings.h"
|
||||
#include "../mfconn.h"
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
#include "../apicalls.h" // IWYU pragma: keep
|
||||
|
||||
static int
|
||||
_decode_get_session_token(mfhttp *conn, void *data);
|
||||
static int _decode_get_session_token(mfhttp * conn, void *data);
|
||||
|
||||
struct user_get_session_token_response
|
||||
{
|
||||
uint32_t secret_key;
|
||||
char *secret_time;
|
||||
char *session_token;
|
||||
struct user_get_session_token_response {
|
||||
uint32_t secret_key;
|
||||
char *secret_time;
|
||||
char *session_token;
|
||||
};
|
||||
|
||||
int
|
||||
mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
||||
char *username, char *password, int app_id, char *app_key,
|
||||
uint32_t *secret_key, char **secret_time, char **session_token)
|
||||
mfconn_api_user_get_session_token(mfconn * conn, char *server,
|
||||
char *username, char *password,
|
||||
int app_id, char *app_key,
|
||||
uint32_t * secret_key,
|
||||
char **secret_time, char **session_token)
|
||||
{
|
||||
char *login_url;
|
||||
char *post_args;
|
||||
char *user_signature;
|
||||
int retval;
|
||||
char *login_url;
|
||||
char *post_args;
|
||||
char *user_signature;
|
||||
int retval;
|
||||
struct user_get_session_token_response response;
|
||||
mfhttp *http;
|
||||
|
||||
if(conn == NULL) return -1;
|
||||
if (conn == NULL)
|
||||
return -1;
|
||||
|
||||
// configure url for operation
|
||||
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
|
||||
server);
|
||||
server);
|
||||
|
||||
// create user signature
|
||||
user_signature = mfconn_create_user_signature(conn, username, password, app_id, app_key);
|
||||
user_signature =
|
||||
mfconn_create_user_signature(conn, username, password, app_id, app_key);
|
||||
|
||||
post_args = strdup_printf(
|
||||
"email=%s"
|
||||
"&password=%s"
|
||||
"&application_id=35860"
|
||||
"&signature=%s"
|
||||
"&token_version=2"
|
||||
"&response_format=json",
|
||||
username, password, user_signature);
|
||||
post_args = strdup_printf("email=%s"
|
||||
"&password=%s"
|
||||
"&application_id=35860"
|
||||
"&signature=%s"
|
||||
"&token_version=2"
|
||||
"&response_format=json",
|
||||
username, password, user_signature);
|
||||
|
||||
mfhttp *http = http_create();
|
||||
retval = http_post_buf(http, login_url, post_args, _decode_get_session_token, (void *)(&response));
|
||||
http = http_create();
|
||||
retval =
|
||||
http_post_buf(http, login_url, post_args,
|
||||
_decode_get_session_token, (void *)(&response));
|
||||
http_destroy(http);
|
||||
|
||||
free(login_url);
|
||||
@@ -83,48 +86,49 @@ mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int
|
||||
_decode_get_session_token(mfhttp *conn, void *user_ptr)
|
||||
static int _decode_get_session_token(mfhttp * conn, void *user_ptr)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root = NULL;
|
||||
json_t *data;
|
||||
json_t *session_token;
|
||||
json_t *secret_key;
|
||||
json_t *secret_time;
|
||||
json_t *root = NULL;
|
||||
json_t *data;
|
||||
json_t *session_token;
|
||||
json_t *secret_key;
|
||||
json_t *secret_time;
|
||||
struct user_get_session_token_response *response;
|
||||
|
||||
if(user_ptr == NULL) return -1;
|
||||
if (user_ptr == NULL)
|
||||
return -1;
|
||||
|
||||
response = (struct user_get_session_token_response *)user_ptr;
|
||||
|
||||
root = http_parse_buf_json(conn, 0, &error);
|
||||
|
||||
data = json_object_by_path(root,"response");
|
||||
if(data == NULL) return -1;
|
||||
data = json_object_by_path(root, "response");
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
session_token = json_object_get(data,"session_token");
|
||||
if(session_token == NULL)
|
||||
{
|
||||
session_token = json_object_get(data, "session_token");
|
||||
if (session_token == NULL) {
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
response->session_token = strdup(json_string_value(session_token));
|
||||
|
||||
secret_key = json_object_get(data,"secret_key");
|
||||
if(secret_key != NULL)
|
||||
secret_key = json_object_get(data, "secret_key");
|
||||
if (secret_key != NULL)
|
||||
response->secret_key = atoll(json_string_value(secret_key));
|
||||
|
||||
/*
|
||||
time looks like a float but we must store it as a string to
|
||||
remain congruent with the server on decimal place presentation.
|
||||
*/
|
||||
secret_time = json_object_get(data,"time");
|
||||
if(secret_time != NULL)
|
||||
response->secret_time = strdup(json_string_value(secret_time));
|
||||
time looks like a float but we must store it as a string to
|
||||
remain congruent with the server on decimal place presentation.
|
||||
*/
|
||||
secret_time = json_object_get(data, "time");
|
||||
if (secret_time != NULL)
|
||||
response->secret_time = strdup(json_string_value(secret_time));
|
||||
|
||||
if(root != NULL) json_decref(root);
|
||||
if (root != NULL)
|
||||
json_decref(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
211
mfapi/file.c
211
mfapi/file.c
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -29,122 +28,128 @@
|
||||
#include "../utils/strings.h"
|
||||
#include "file.h"
|
||||
|
||||
struct mffile {
|
||||
char quickkey[18];
|
||||
char hash[65];
|
||||
char name[256];
|
||||
char mtime[16];
|
||||
uint64_t revision;
|
||||
|
||||
struct mffile
|
||||
{
|
||||
char quickkey[18];
|
||||
char hash[65];
|
||||
char name[256];
|
||||
char mtime[16];
|
||||
uint64_t revision;
|
||||
|
||||
char *share_link;
|
||||
char *direct_link;
|
||||
char *onetime_link;
|
||||
char *share_link;
|
||||
char *direct_link;
|
||||
char *onetime_link;
|
||||
};
|
||||
|
||||
mffile*
|
||||
file_alloc(void)
|
||||
mffile *file_alloc(void)
|
||||
{
|
||||
mffile *file;
|
||||
mffile *file;
|
||||
|
||||
file = (mffile*)calloc(1,sizeof(mffile));
|
||||
file = (mffile *) calloc(1, sizeof(mffile));
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void
|
||||
file_free(mffile *file)
|
||||
void file_free(mffile * file)
|
||||
{
|
||||
if(file == NULL) return;
|
||||
if (file == NULL)
|
||||
return;
|
||||
|
||||
if(file->share_link != NULL) free(file->share_link);
|
||||
if(file->direct_link != NULL) free(file->direct_link);
|
||||
if(file->onetime_link != NULL) free(file->onetime_link);
|
||||
if (file->share_link != NULL)
|
||||
free(file->share_link);
|
||||
if (file->direct_link != NULL)
|
||||
free(file->direct_link);
|
||||
if (file->onetime_link != NULL)
|
||||
free(file->onetime_link);
|
||||
|
||||
free(file);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_key(mffile *file,const char *key)
|
||||
int file_set_key(mffile * file, const char *key)
|
||||
{
|
||||
int len;
|
||||
int len;
|
||||
|
||||
if(file == NULL) return -1;
|
||||
if(key == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (key == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(key);
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
memset(file->quickkey,0,sizeof(file->quickkey));
|
||||
strncpy(file->quickkey,key,sizeof(file->quickkey) - 1);
|
||||
memset(file->quickkey, 0, sizeof(file->quickkey));
|
||||
strncpy(file->quickkey, key, sizeof(file->quickkey) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_key(mffile *file)
|
||||
const char *file_get_key(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->quickkey;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_hash(mffile *file,const char *hash)
|
||||
int file_set_hash(mffile * file, const char *hash)
|
||||
{
|
||||
if(file == NULL) return -1;
|
||||
if(hash == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (hash == NULL)
|
||||
return -1;
|
||||
|
||||
// system supports SHA256 (current) and MD5 (legacy)
|
||||
if(strlen(hash) < 32) return -1;
|
||||
if (strlen(hash) < 32)
|
||||
return -1;
|
||||
|
||||
memset(file->hash,0,sizeof(file->hash));
|
||||
strncpy(file->hash,hash,sizeof(file->hash) - 1);
|
||||
memset(file->hash, 0, sizeof(file->hash));
|
||||
strncpy(file->hash, hash, sizeof(file->hash) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_hash(mffile *file)
|
||||
const char *file_get_hash(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->hash;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_name(mffile *file,const char *name)
|
||||
int file_set_name(mffile * file, const char *name)
|
||||
{
|
||||
if(file == NULL) return -1;
|
||||
if(name == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (name == NULL)
|
||||
return -1;
|
||||
|
||||
if(strlen(name) > 255) return -1;
|
||||
if (strlen(name) > 255)
|
||||
return -1;
|
||||
|
||||
memset(file->name,0,sizeof(file->name));
|
||||
strncpy(file->name,name,sizeof(file->name) - 1);
|
||||
memset(file->name, 0, sizeof(file->name));
|
||||
strncpy(file->name, name, sizeof(file->name) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_name(mffile *file)
|
||||
const char *file_get_name(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->name;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_share_link(mffile *file,const char *share_link)
|
||||
int file_set_share_link(mffile * file, const char *share_link)
|
||||
{
|
||||
if(file == NULL) return -1;
|
||||
if(share_link == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (share_link == NULL)
|
||||
return -1;
|
||||
|
||||
if(file->share_link != NULL)
|
||||
{
|
||||
if (file->share_link != NULL) {
|
||||
free(file->share_link);
|
||||
file->share_link = NULL;
|
||||
}
|
||||
@@ -154,22 +159,22 @@ file_set_share_link(mffile *file,const char *share_link)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_share_link(mffile *file)
|
||||
const char *file_get_share_link(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->share_link;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_direct_link(mffile *file,const char *direct_link)
|
||||
int file_set_direct_link(mffile * file, const char *direct_link)
|
||||
{
|
||||
if(file == NULL) return -1;
|
||||
if(direct_link == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (direct_link == NULL)
|
||||
return -1;
|
||||
|
||||
if(file->direct_link != NULL)
|
||||
{
|
||||
if (file->direct_link != NULL) {
|
||||
free(file->direct_link);
|
||||
file->direct_link = NULL;
|
||||
}
|
||||
@@ -179,22 +184,22 @@ file_set_direct_link(mffile *file,const char *direct_link)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_direct_link(mffile *file)
|
||||
const char *file_get_direct_link(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->direct_link;
|
||||
}
|
||||
|
||||
int
|
||||
file_set_onetime_link(mffile *file,const char *onetime_link)
|
||||
int file_set_onetime_link(mffile * file, const char *onetime_link)
|
||||
{
|
||||
if(file == NULL) return -1;
|
||||
if(onetime_link == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (onetime_link == NULL)
|
||||
return -1;
|
||||
|
||||
if(file->onetime_link != NULL)
|
||||
{
|
||||
if (file->onetime_link != NULL) {
|
||||
free(file->onetime_link);
|
||||
file->onetime_link = NULL;
|
||||
}
|
||||
@@ -204,53 +209,59 @@ file_set_onetime_link(mffile *file,const char *onetime_link)
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
file_get_onetime_link(mffile *file)
|
||||
const char *file_get_onetime_link(mffile * file)
|
||||
{
|
||||
if(file == NULL) return NULL;
|
||||
if (file == NULL)
|
||||
return NULL;
|
||||
|
||||
return file->onetime_link;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
file_download_direct(mffile *file, char *local_dir)
|
||||
ssize_t file_download_direct(mffile * file, char *local_dir)
|
||||
{
|
||||
const char *url;
|
||||
const char *file_name;
|
||||
char *file_path;
|
||||
const char *url;
|
||||
const char *file_name;
|
||||
char *file_path;
|
||||
struct stat file_info;
|
||||
ssize_t bytes_read = 0;
|
||||
int retval;
|
||||
mfhttp *conn;
|
||||
|
||||
if(file == NULL) return -1;
|
||||
if(local_dir == NULL) return -1;
|
||||
if (file == NULL)
|
||||
return -1;
|
||||
if (local_dir == NULL)
|
||||
return -1;
|
||||
|
||||
url = file_get_direct_link(file);
|
||||
if(url == NULL) return -1;
|
||||
if (url == NULL)
|
||||
return -1;
|
||||
|
||||
file_name = file_get_name(file);
|
||||
if(file_name == NULL) return -1;
|
||||
if(strlen(file_name) < 1) return -1;
|
||||
if (file_name == NULL)
|
||||
return -1;
|
||||
if (strlen(file_name) < 1)
|
||||
return -1;
|
||||
|
||||
if(local_dir[strlen(local_dir) - 1] == '/')
|
||||
file_path = strdup_printf("%s%s",local_dir,file_name);
|
||||
if (local_dir[strlen(local_dir) - 1] == '/')
|
||||
file_path = strdup_printf("%s%s", local_dir, file_name);
|
||||
else
|
||||
file_path = strdup_printf("%s/%s",local_dir,file_name);
|
||||
file_path = strdup_printf("%s/%s", local_dir, file_name);
|
||||
|
||||
mfhttp *conn = http_create();
|
||||
conn = http_create();
|
||||
retval = http_get_file(conn, url, file_path);
|
||||
http_destroy(conn);
|
||||
|
||||
/*
|
||||
it is preferable to have the vfs tell us how many bytes the
|
||||
transfer actually is. it's really all that matters.
|
||||
*/
|
||||
memset(&file_info,0,sizeof(file_info));
|
||||
retval = stat(file_path,&file_info);
|
||||
it is preferable to have the vfs tell us how many bytes the
|
||||
transfer actually is. it's really all that matters.
|
||||
*/
|
||||
memset(&file_info, 0, sizeof(file_info));
|
||||
retval = stat(file_path, &file_info);
|
||||
|
||||
free(file_path);
|
||||
|
||||
if(retval != 0) return -1;
|
||||
if (retval != 0)
|
||||
return -1;
|
||||
|
||||
bytes_read = file_info.st_size;
|
||||
|
||||
|
||||
30
mfapi/file.h
30
mfapi/file.h
@@ -24,34 +24,34 @@
|
||||
|
||||
typedef struct mffile mffile;
|
||||
|
||||
mffile* file_alloc(void);
|
||||
mffile *file_alloc(void);
|
||||
|
||||
void file_free(mffile *file);
|
||||
void file_free(mffile * file);
|
||||
|
||||
int file_set_key(mffile *file,const char *quickkey);
|
||||
int file_set_key(mffile * file, const char *quickkey);
|
||||
|
||||
const char* file_get_key(mffile *file);
|
||||
const char *file_get_key(mffile * file);
|
||||
|
||||
int file_set_hash(mffile *file,const char *hash);
|
||||
int file_set_hash(mffile * file, const char *hash);
|
||||
|
||||
const char* file_get_hash(mffile *file);
|
||||
const char *file_get_hash(mffile * file);
|
||||
|
||||
int file_set_name(mffile *file,const char *name);
|
||||
int file_set_name(mffile * file, const char *name);
|
||||
|
||||
const char* file_get_name(mffile *file);
|
||||
const char *file_get_name(mffile * file);
|
||||
|
||||
int file_set_share_link(mffile *file,const char *share_link);
|
||||
int file_set_share_link(mffile * file, const char *share_link);
|
||||
|
||||
const char* file_get_share_link(mffile *file);
|
||||
const char *file_get_share_link(mffile * file);
|
||||
|
||||
int file_set_direct_link(mffile *file,const char *direct_link);
|
||||
int file_set_direct_link(mffile * file, const char *direct_link);
|
||||
|
||||
const char* file_get_direct_link(mffile *file);
|
||||
const char *file_get_direct_link(mffile * file);
|
||||
|
||||
int file_set_onetime_link(mffile *file,const char *onetime_link);
|
||||
int file_set_onetime_link(mffile * file, const char *onetime_link);
|
||||
|
||||
const char* file_get_onetime_link(mffile *file);
|
||||
const char *file_get_onetime_link(mffile * file);
|
||||
|
||||
ssize_t file_download_direct(mffile *file, char *local_dir);
|
||||
ssize_t file_download_direct(mffile * file, char *local_dir);
|
||||
|
||||
#endif
|
||||
|
||||
100
mfapi/folder.c
100
mfapi/folder.c
@@ -17,110 +17,110 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "folder.h"
|
||||
|
||||
struct mffolder
|
||||
{
|
||||
char folderkey[20];
|
||||
char name[41];
|
||||
char parent[20];
|
||||
uint64_t revision;
|
||||
uint32_t folder_count;
|
||||
uint32_t file_count;
|
||||
struct mffolder {
|
||||
char folderkey[20];
|
||||
char name[41];
|
||||
char parent[20];
|
||||
uint64_t revision;
|
||||
uint32_t folder_count;
|
||||
uint32_t file_count;
|
||||
};
|
||||
|
||||
mffolder*
|
||||
folder_alloc(void)
|
||||
mffolder *folder_alloc(void)
|
||||
{
|
||||
mffolder *folder;
|
||||
mffolder *folder;
|
||||
|
||||
folder = (mffolder*)calloc(1,sizeof(mffolder));
|
||||
folder = (mffolder *) calloc(1, sizeof(mffolder));
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
void
|
||||
folder_free(mffolder *folder)
|
||||
void folder_free(mffolder * folder)
|
||||
{
|
||||
if(folder == NULL) return;
|
||||
if (folder == NULL)
|
||||
return;
|
||||
|
||||
free(folder);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
folder_set_key(mffolder *folder,const char *key)
|
||||
int folder_set_key(mffolder * folder, const char *key)
|
||||
{
|
||||
if(folder == NULL) return -1;
|
||||
if(key == NULL) return -1;
|
||||
if (folder == NULL)
|
||||
return -1;
|
||||
if (key == NULL)
|
||||
return -1;
|
||||
|
||||
if(strlen(key) != 13) return -1;
|
||||
if (strlen(key) != 13)
|
||||
return -1;
|
||||
|
||||
memset(folder->folderkey,0,sizeof(folder->folderkey));
|
||||
strncpy(folder->folderkey,key,sizeof(folder->folderkey) - 1);
|
||||
memset(folder->folderkey, 0, sizeof(folder->folderkey));
|
||||
strncpy(folder->folderkey, key, sizeof(folder->folderkey) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
folder_get_key(mffolder *folder)
|
||||
const char *folder_get_key(mffolder * folder)
|
||||
{
|
||||
if(folder == NULL) return NULL;
|
||||
if (folder == NULL)
|
||||
return NULL;
|
||||
|
||||
return folder->folderkey;
|
||||
}
|
||||
|
||||
int
|
||||
folder_set_parent(mffolder *folder,const char *parent_key)
|
||||
int folder_set_parent(mffolder * folder, const char *parent_key)
|
||||
{
|
||||
if(folder == NULL) return -1;
|
||||
if(parent_key == NULL) return -1;
|
||||
if (folder == NULL)
|
||||
return -1;
|
||||
if (parent_key == NULL)
|
||||
return -1;
|
||||
|
||||
if(strlen(parent_key) != 13)
|
||||
{
|
||||
if(strcmp(parent_key,"myfiles") != 0) return -1;
|
||||
if (strlen(parent_key) != 13) {
|
||||
if (strcmp(parent_key, "myfiles") != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(folder->parent,0,sizeof(folder->parent));
|
||||
strncpy(folder->parent,parent_key,sizeof(folder->parent) -1);
|
||||
memset(folder->parent, 0, sizeof(folder->parent));
|
||||
strncpy(folder->parent, parent_key, sizeof(folder->parent) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
folder_get_parent(mffolder *folder)
|
||||
const char *folder_get_parent(mffolder * folder)
|
||||
{
|
||||
if(folder == NULL) return NULL;
|
||||
if (folder == NULL)
|
||||
return NULL;
|
||||
|
||||
return folder->parent;
|
||||
}
|
||||
|
||||
int
|
||||
folder_set_name(mffolder *folder,const char *name)
|
||||
int folder_set_name(mffolder * folder, const char *name)
|
||||
{
|
||||
if(folder == NULL) return -1;
|
||||
if(name == NULL) return -1;
|
||||
if (folder == NULL)
|
||||
return -1;
|
||||
if (name == NULL)
|
||||
return -1;
|
||||
|
||||
if(strlen(name) > 40) return -1;
|
||||
if (strlen(name) > 40)
|
||||
return -1;
|
||||
|
||||
memset(folder->name,0,sizeof(folder->name));
|
||||
strncpy(folder->name,name,sizeof(folder->name) - 1);
|
||||
memset(folder->name, 0, sizeof(folder->name));
|
||||
strncpy(folder->name, name, sizeof(folder->name) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char*
|
||||
folder_get_name(mffolder *folder)
|
||||
const char *folder_get_name(mffolder * folder)
|
||||
{
|
||||
if(folder == NULL) return NULL;
|
||||
if (folder == NULL)
|
||||
return NULL;
|
||||
|
||||
return folder->name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,20 +22,20 @@
|
||||
|
||||
typedef struct mffolder mffolder;
|
||||
|
||||
mffolder* folder_alloc(void);
|
||||
mffolder *folder_alloc(void);
|
||||
|
||||
void folder_free(mffolder *folder);
|
||||
void folder_free(mffolder * folder);
|
||||
|
||||
int folder_set_key(mffolder *folder,const char *folderkey);
|
||||
int folder_set_key(mffolder * folder, const char *folderkey);
|
||||
|
||||
const char* folder_get_key(mffolder *folder);
|
||||
const char *folder_get_key(mffolder * folder);
|
||||
|
||||
int folder_set_parent(mffolder *folder,const char *folderkey);
|
||||
int folder_set_parent(mffolder * folder, const char *folderkey);
|
||||
|
||||
const char* folder_get_parent(mffolder *folder);
|
||||
const char *folder_get_parent(mffolder * folder);
|
||||
|
||||
int folder_set_name(mffolder *folder,const char *name);
|
||||
int folder_set_name(mffolder * folder, const char *name);
|
||||
|
||||
const char* folder_get_name(mffolder *folder);
|
||||
const char *folder_get_name(mffolder * folder);
|
||||
|
||||
#endif
|
||||
|
||||
169
mfapi/mfconn.c
169
mfapi/mfconn.c
@@ -28,26 +28,28 @@
|
||||
#include "apicalls.h"
|
||||
#include "mfconn.h"
|
||||
|
||||
struct mfconn
|
||||
{
|
||||
char *server;
|
||||
uint32_t secret_key;
|
||||
char *secret_time;
|
||||
char *session_token;
|
||||
struct mfconn {
|
||||
char *server;
|
||||
uint32_t secret_key;
|
||||
char *secret_time;
|
||||
char *session_token;
|
||||
};
|
||||
|
||||
mfconn*
|
||||
mfconn_create(char *server, char *username, char *password, int app_id, char *app_key)
|
||||
mfconn *mfconn_create(char *server, char *username, char *password,
|
||||
int app_id, char *app_key)
|
||||
{
|
||||
mfconn *conn;
|
||||
int retval;
|
||||
mfconn *conn;
|
||||
int retval;
|
||||
|
||||
conn = (mfconn *)calloc(1,sizeof(mfconn));
|
||||
conn = (mfconn *) calloc(1, sizeof(mfconn));
|
||||
|
||||
conn->server = strdup(server);
|
||||
retval = mfconn_api_user_get_session_token(conn, conn->server,
|
||||
username, password, app_id, app_key, &(conn->secret_key),
|
||||
&(conn->secret_time), &(conn->session_token));
|
||||
username, password, app_id,
|
||||
app_key,
|
||||
&(conn->secret_key),
|
||||
&(conn->secret_time),
|
||||
&(conn->session_token));
|
||||
|
||||
if (retval == 0)
|
||||
return conn;
|
||||
@@ -55,8 +57,7 @@ mfconn_create(char *server, char *username, char *password, int app_id, char *ap
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
mfconn_destroy(mfconn *conn)
|
||||
void mfconn_destroy(mfconn * conn)
|
||||
{
|
||||
free(conn->server);
|
||||
free(conn->secret_time);
|
||||
@@ -64,14 +65,14 @@ mfconn_destroy(mfconn *conn)
|
||||
free(conn);
|
||||
}
|
||||
|
||||
void
|
||||
mfconn_update_secret_key(mfconn *conn)
|
||||
void mfconn_update_secret_key(mfconn * conn)
|
||||
{
|
||||
uint64_t new_val;
|
||||
uint64_t new_val;
|
||||
|
||||
if(conn == NULL) return;
|
||||
if (conn == NULL)
|
||||
return;
|
||||
|
||||
new_val = ((uint64_t)conn->secret_key) * 16807;
|
||||
new_val = ((uint64_t) conn->secret_key) * 16807;
|
||||
new_val %= 0x7FFFFFFF;
|
||||
|
||||
conn->secret_key = new_val;
|
||||
@@ -79,98 +80,106 @@ mfconn_update_secret_key(mfconn *conn)
|
||||
return;
|
||||
}
|
||||
|
||||
char*
|
||||
mfconn_create_user_signature(mfconn *conn, char *username, char *password,
|
||||
int app_id, char *app_key)
|
||||
char *mfconn_create_user_signature(mfconn * conn, char *username,
|
||||
char *password, int app_id,
|
||||
char *app_key)
|
||||
{
|
||||
char *signature_raw;
|
||||
unsigned char signature_enc[20]; // sha1 is 160 bits
|
||||
char *signature_raw;
|
||||
unsigned char signature_enc[20]; // sha1 is 160 bits
|
||||
char signature_hex[41];
|
||||
int i;
|
||||
|
||||
if(conn == NULL) return NULL;
|
||||
if (conn == NULL)
|
||||
return NULL;
|
||||
|
||||
signature_raw = strdup_printf("%s%s%d%s",
|
||||
username, password, app_id, app_key);
|
||||
username, password, app_id, app_key);
|
||||
|
||||
SHA1((const unsigned char *)signature_raw,
|
||||
strlen(signature_raw),signature_enc);
|
||||
strlen(signature_raw), signature_enc);
|
||||
|
||||
free(signature_raw);
|
||||
|
||||
for(i = 0;i < 20;i++)
|
||||
{
|
||||
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
|
||||
for (i = 0; i < 20; i++) {
|
||||
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||
}
|
||||
signature_hex[40] = '\0';
|
||||
|
||||
return strdup((const char *)signature_hex);
|
||||
}
|
||||
|
||||
char*
|
||||
mfconn_create_call_signature(mfconn *conn,char *url,char *args)
|
||||
char *mfconn_create_call_signature(mfconn * conn, char *url,
|
||||
char *args)
|
||||
{
|
||||
char *signature_raw;
|
||||
unsigned char signature_enc[16]; // md5 is 128 bits
|
||||
char *signature_raw;
|
||||
unsigned char signature_enc[16]; // md5 is 128 bits
|
||||
char signature_hex[33];
|
||||
char *api;
|
||||
char *api;
|
||||
int i;
|
||||
|
||||
if(conn == NULL) return NULL;
|
||||
if(url == NULL) return NULL;
|
||||
if(args == NULL) return NULL;
|
||||
if (conn == NULL)
|
||||
return NULL;
|
||||
if (url == NULL)
|
||||
return NULL;
|
||||
if (args == NULL)
|
||||
return NULL;
|
||||
|
||||
// printf("url: %s\n\rargs: %s\n\r",url,args);
|
||||
|
||||
api = strstr(url,"/api/");
|
||||
api = strstr(url, "/api/");
|
||||
|
||||
if(api == NULL) return NULL;
|
||||
if (api == NULL)
|
||||
return NULL;
|
||||
|
||||
signature_raw = strdup_printf("%d%s%s%s",
|
||||
(conn->secret_key % 256),
|
||||
conn->secret_time,
|
||||
api,args);
|
||||
(conn->secret_key % 256),
|
||||
conn->secret_time, api, args);
|
||||
|
||||
MD5((const unsigned char *)signature_raw,
|
||||
strlen(signature_raw),signature_enc);
|
||||
strlen(signature_raw), signature_enc);
|
||||
|
||||
free(signature_raw);
|
||||
|
||||
for(i = 0;i < 16;i++)
|
||||
{
|
||||
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
|
||||
for (i = 0; i < 16; i++) {
|
||||
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||
}
|
||||
signature_hex[32] = '\0';
|
||||
|
||||
return strdup((const char *)signature_hex);
|
||||
}
|
||||
|
||||
char*
|
||||
mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
||||
char *mfconn_create_signed_get(mfconn * conn, int ssl, char *api,
|
||||
char *fmt, ...)
|
||||
{
|
||||
char *api_request = NULL;
|
||||
char *api_args = NULL;
|
||||
char *signature;
|
||||
char *call_hash;
|
||||
char *session_token;
|
||||
int bytes_to_alloc;
|
||||
int api_args_len;
|
||||
int api_len;
|
||||
va_list ap;
|
||||
char *api_request = NULL;
|
||||
char *api_args = NULL;
|
||||
char *signature;
|
||||
char *call_hash;
|
||||
char *session_token;
|
||||
int bytes_to_alloc;
|
||||
int api_args_len;
|
||||
int api_len;
|
||||
va_list ap;
|
||||
|
||||
if(conn == NULL) return NULL;
|
||||
if(conn->server == NULL) return NULL;
|
||||
if(conn->secret_time == NULL) return NULL;
|
||||
if(conn->session_token == NULL) return NULL;
|
||||
if (conn == NULL)
|
||||
return NULL;
|
||||
if (conn->server == NULL)
|
||||
return NULL;
|
||||
if (conn->secret_time == NULL)
|
||||
return NULL;
|
||||
if (conn->session_token == NULL)
|
||||
return NULL;
|
||||
|
||||
// make sure the api (ex: user/get_info.php) is sane
|
||||
if(api == NULL) return NULL;
|
||||
if (api == NULL)
|
||||
return NULL;
|
||||
api_len = strlen(api);
|
||||
if(api_len < 3) return NULL;
|
||||
if (api_len < 3)
|
||||
return NULL;
|
||||
|
||||
// calculate how big of a buffer we need
|
||||
va_start(ap, fmt);
|
||||
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
||||
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
|
||||
va_end(ap);
|
||||
|
||||
session_token = strdup_printf("&session_token=%s", conn->session_token);
|
||||
@@ -178,7 +187,7 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
||||
api_args_len += strlen(session_token);
|
||||
|
||||
// create the correctly sized buffer and process the args
|
||||
api_args = (char*)calloc(api_args_len,sizeof(char));
|
||||
api_args = (char *)calloc(api_args_len, sizeof(char));
|
||||
|
||||
// printf("\n\r%d\n\r",api_args_len);
|
||||
|
||||
@@ -190,25 +199,26 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
||||
free(session_token);
|
||||
|
||||
// correct user error of trailing slash
|
||||
if(api[api_len - 1] == '/') api[api_len - 1] = '\0';
|
||||
if (api[api_len - 1] == '/')
|
||||
api[api_len - 1] = '\0';
|
||||
|
||||
api_request = strdup_printf("%s//%s/api/%s",
|
||||
(ssl ? "https:" : "http:"), conn->server,api);
|
||||
(ssl ? "https:" : "http:"), conn->server, api);
|
||||
|
||||
call_hash = mfconn_create_call_signature(conn,api_request,api_args);
|
||||
signature = strdup_printf("&signature=%s",call_hash);
|
||||
call_hash = mfconn_create_call_signature(conn, api_request, api_args);
|
||||
signature = strdup_printf("&signature=%s", call_hash);
|
||||
free(call_hash);
|
||||
|
||||
// compute the amount of space requred to realloc() the request
|
||||
bytes_to_alloc = api_args_len;
|
||||
bytes_to_alloc += strlen(api_request);
|
||||
bytes_to_alloc += strlen(signature);
|
||||
bytes_to_alloc += 1; // null termination
|
||||
bytes_to_alloc += 1; // null termination
|
||||
|
||||
// append api GET args to api request
|
||||
api_request = (char*)realloc(api_request,bytes_to_alloc);
|
||||
strncat(api_request,api_args,api_args_len);
|
||||
strcat(api_request,signature);
|
||||
api_request = (char *)realloc(api_request, bytes_to_alloc);
|
||||
strncat(api_request, api_args, api_args_len);
|
||||
strcat(api_request, signature);
|
||||
|
||||
free(signature);
|
||||
free(api_args);
|
||||
@@ -216,20 +226,17 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
||||
return api_request;
|
||||
}
|
||||
|
||||
const char*
|
||||
mfconn_get_session_token(mfconn *conn)
|
||||
const char *mfconn_get_session_token(mfconn * conn)
|
||||
{
|
||||
return conn->session_token;
|
||||
}
|
||||
|
||||
const char*
|
||||
mfconn_get_secret_time(mfconn *conn)
|
||||
const char *mfconn_get_secret_time(mfconn * conn)
|
||||
{
|
||||
return conn->secret_time;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
mfconn_get_secret_key(mfconn *conn)
|
||||
uint32_t mfconn_get_secret_key(mfconn * conn)
|
||||
{
|
||||
return conn->secret_key;
|
||||
}
|
||||
|
||||
@@ -26,22 +26,25 @@
|
||||
|
||||
typedef struct mfconn mfconn;
|
||||
|
||||
mfconn* mfconn_create(char *server, char *username, char *password, int app_id, char *app_key);
|
||||
mfconn *mfconn_create(char *server, char *username, char *password,
|
||||
int app_id, char *app_key);
|
||||
|
||||
void mfconn_destroy(mfconn *conn);
|
||||
void mfconn_destroy(mfconn * conn);
|
||||
|
||||
ssize_t mfconn_download_direct(mffile *file,char *local_dir);
|
||||
ssize_t mfconn_download_direct(mffile * file, char *local_dir);
|
||||
|
||||
char* mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...);
|
||||
char *mfconn_create_signed_get(mfconn * conn, int ssl, char *api,
|
||||
char *fmt, ...);
|
||||
|
||||
char* mfconn_create_user_signature(mfconn *conn, char *username,
|
||||
char *password, int app_id, char *app_key);
|
||||
char *mfconn_create_user_signature(mfconn * conn, char *username,
|
||||
char *password, int app_id,
|
||||
char *app_key);
|
||||
|
||||
void mfconn_update_secret_key(mfconn *conn);
|
||||
void mfconn_update_secret_key(mfconn * conn);
|
||||
|
||||
const char* mfconn_get_session_token(mfconn *conn);
|
||||
const char *mfconn_get_session_token(mfconn * conn);
|
||||
|
||||
const char* mfconn_get_secret_time(mfconn *conn);
|
||||
const char *mfconn_get_secret_time(mfconn * conn);
|
||||
|
||||
uint32_t mfconn_get_secret_key(mfconn *conn);
|
||||
uint32_t mfconn_get_secret_key(mfconn * conn);
|
||||
#endif
|
||||
|
||||
@@ -17,40 +17,39 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFSHELL_COMMAND_H_
|
||||
#define _MFSHELL_COMMAND_H_
|
||||
|
||||
#include "mfshell.h"
|
||||
|
||||
int mfshell_cmd_help(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_help(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_debug(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_host(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_host(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_auth(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_whomai(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_whomai(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_list(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_list(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_chdir(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_pwd(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_lpwd(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_lpwd(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_lcd(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_file(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_file(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_links(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_links(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_mkdir(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_get(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_get(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_cmd_whoami(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -27,22 +26,21 @@
|
||||
|
||||
#include "../mfshell.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_login_from_user(void);
|
||||
|
||||
static char*
|
||||
_get_passwd_from_user(void);
|
||||
static char *_get_passwd_from_user(void);
|
||||
|
||||
int
|
||||
mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_auth(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
char *username;
|
||||
char *password;
|
||||
char *username;
|
||||
char *password;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if(mfshell->server == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
if (mfshell->server == NULL)
|
||||
return -1;
|
||||
|
||||
switch (argc) {
|
||||
case 1:
|
||||
@@ -62,10 +60,11 @@ mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(username == NULL || password == NULL) return -1;
|
||||
if (username == NULL || password == NULL)
|
||||
return -1;
|
||||
|
||||
mfshell->conn = mfconn_create(mfshell->server, username, password,
|
||||
mfshell->app_id, mfshell->app_key);
|
||||
mfshell->app_id, mfshell->app_key);
|
||||
|
||||
if (mfshell->conn != NULL)
|
||||
printf("\n\rAuthentication SUCCESS\n\r");
|
||||
@@ -75,39 +74,35 @@ mfshell_cmd_auth(mfshell *mfshell, int argc, char **argv)
|
||||
return (mfshell->conn != NULL);
|
||||
}
|
||||
|
||||
char*
|
||||
_get_login_from_user(void)
|
||||
char *_get_login_from_user(void)
|
||||
{
|
||||
char *login = NULL;
|
||||
size_t len;
|
||||
ssize_t bytes_read;
|
||||
char *login = NULL;
|
||||
size_t len;
|
||||
ssize_t bytes_read;
|
||||
|
||||
printf("login: ");
|
||||
bytes_read = getline(&login,&len,stdin);
|
||||
bytes_read = getline(&login, &len, stdin);
|
||||
|
||||
if(bytes_read < 3)
|
||||
{
|
||||
if(login != NULL)
|
||||
{
|
||||
if (bytes_read < 3) {
|
||||
if (login != NULL) {
|
||||
free(login);
|
||||
login = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (login[strlen(login)-1] == '\n')
|
||||
login[strlen(login)-1] = '\0';
|
||||
|
||||
if (login[strlen(login) - 1] == '\n')
|
||||
login[strlen(login) - 1] = '\0';
|
||||
|
||||
return login;
|
||||
}
|
||||
|
||||
char*
|
||||
_get_passwd_from_user(void)
|
||||
char *_get_passwd_from_user(void)
|
||||
{
|
||||
char *passwd = NULL;
|
||||
size_t len;
|
||||
ssize_t bytes_read;
|
||||
struct termios old, new;
|
||||
char *passwd = NULL;
|
||||
size_t len;
|
||||
ssize_t bytes_read;
|
||||
struct termios old,
|
||||
new;
|
||||
|
||||
printf("passwd: ");
|
||||
|
||||
@@ -118,22 +113,19 @@ _get_passwd_from_user(void)
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
|
||||
return NULL;
|
||||
|
||||
bytes_read = getline(&passwd,&len,stdin);
|
||||
bytes_read = getline(&passwd, &len, stdin);
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
||||
|
||||
if(bytes_read < 3)
|
||||
{
|
||||
if(passwd != NULL)
|
||||
{
|
||||
if (bytes_read < 3) {
|
||||
if (passwd != NULL) {
|
||||
free(passwd);
|
||||
passwd = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (passwd[strlen(passwd)-1] == '\n')
|
||||
passwd[strlen(passwd)-1] = '\0';
|
||||
if (passwd[strlen(passwd) - 1] == '\n')
|
||||
passwd[strlen(passwd) - 1] = '\0';
|
||||
|
||||
return passwd;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -25,18 +24,18 @@
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/folder.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_chdir(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
mffolder *folder_new;
|
||||
const char *folder_curr;
|
||||
const char *folder_parent;
|
||||
const char *folderkey;
|
||||
int retval;
|
||||
mffolder *folder_new;
|
||||
const char *folder_curr;
|
||||
const char *folder_parent;
|
||||
const char *folderkey;
|
||||
int retval;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -44,67 +43,60 @@ mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
folderkey = argv[1];
|
||||
if(folderkey == NULL) return -1;
|
||||
if (folderkey == NULL)
|
||||
return -1;
|
||||
|
||||
// change to root
|
||||
if(strcmp(folderkey,"/") == 0) folderkey = "myfiles";
|
||||
if (strcmp(folderkey, "/") == 0)
|
||||
folderkey = "myfiles";
|
||||
|
||||
// user wants to navigate up a level
|
||||
if(strcmp(folderkey,"..") == 0)
|
||||
{
|
||||
if (strcmp(folderkey, "..") == 0) {
|
||||
// do several sanity checks to see if we're already at the root
|
||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||
|
||||
if(folder_curr == NULL) return 0;
|
||||
if(strcmp(folder_curr,"myfiles") == 0) return 0;
|
||||
if (folder_curr == NULL)
|
||||
return 0;
|
||||
if (strcmp(folder_curr, "myfiles") == 0)
|
||||
return 0;
|
||||
|
||||
folder_parent = folder_get_parent(mfshell->folder_curr);
|
||||
|
||||
if(folder_parent == NULL) return 0;
|
||||
if (folder_parent == NULL)
|
||||
return 0;
|
||||
|
||||
// it's pretty sure that we're not at the root
|
||||
folderkey = folder_parent;
|
||||
}
|
||||
|
||||
// check the lenght of the key
|
||||
if(strlen(folderkey) != 13)
|
||||
{
|
||||
if (strlen(folderkey) != 13) {
|
||||
// as a folder moniker, "myfiles" is an exception
|
||||
if(strcmp(folderkey,"myfiles") != 0) return -1;
|
||||
if (strcmp(folderkey, "myfiles") != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
// create a new folder object to store the results
|
||||
folder_new = folder_alloc();
|
||||
|
||||
// navigate to root is a special case
|
||||
if(strcmp(folderkey,"myfiles") == 0)
|
||||
{
|
||||
folder_set_key(folder_new,"myfiles");
|
||||
if (strcmp(folderkey, "myfiles") == 0) {
|
||||
folder_set_key(folder_new, "myfiles");
|
||||
retval = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
retval = mfconn_api_folder_get_info(mfshell->conn,
|
||||
folder_new,(char*)folderkey);
|
||||
folder_new, (char *)folderkey);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
}
|
||||
|
||||
if(retval == 0)
|
||||
{
|
||||
if(mfshell->folder_curr != NULL)
|
||||
{
|
||||
if (retval == 0) {
|
||||
if (mfshell->folder_curr != NULL) {
|
||||
folder_free(mfshell->folder_curr);
|
||||
mfshell->folder_curr = NULL;
|
||||
}
|
||||
|
||||
mfshell->folder_curr = folder_new;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
folder_free(folder_new);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,17 +17,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_debug(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
if (argc != 1) {
|
||||
@@ -35,35 +33,21 @@ mfshell_cmd_debug(mfshell *mfshell, int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"server:",
|
||||
mfshell->server);
|
||||
printf(" %-15.15s %s\n\r", "server:", mfshell->server);
|
||||
|
||||
const char *session_token = mfconn_get_session_token(mfshell->conn);
|
||||
const char *secret_time = mfconn_get_secret_time(mfshell->conn);
|
||||
uint32_t secret_key = mfconn_get_secret_key(mfshell->conn);
|
||||
if(session_token != NULL && secret_time != NULL)
|
||||
{
|
||||
printf(" %-15.15s %"PRIu32"\n\r",
|
||||
"secret key:",
|
||||
secret_key);
|
||||
const char *session_token = mfconn_get_session_token(mfshell->conn);
|
||||
const char *secret_time = mfconn_get_secret_time(mfshell->conn);
|
||||
uint32_t secret_key = mfconn_get_secret_key(mfshell->conn);
|
||||
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"secret time:",
|
||||
secret_time);
|
||||
if (session_token != NULL && secret_time != NULL) {
|
||||
printf(" %-15.15s %" PRIu32 "\n\r", "secret key:", secret_key);
|
||||
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"status:",
|
||||
"Authenticated");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"status:",
|
||||
"Not authenticated");
|
||||
printf(" %-15.15s %s\n\r", "secret time:", secret_time);
|
||||
|
||||
printf(" %-15.15s %s\n\r", "status:", "Authenticated");
|
||||
} else {
|
||||
printf(" %-15.15s %s\n\r", "status:", "Not authenticated");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -25,19 +24,19 @@
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/file.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_file(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
const char *quickkey;
|
||||
const char *name;
|
||||
const char *hash;
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
const char *quickkey;
|
||||
const char *name;
|
||||
const char *hash;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -45,15 +44,17 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
quickkey = argv[1];
|
||||
if(quickkey == NULL) return -1;
|
||||
if (quickkey == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(quickkey);
|
||||
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
file = file_alloc();
|
||||
|
||||
retval = mfconn_api_file_get_info(mfshell->conn,file,(char*)quickkey);
|
||||
retval = mfconn_api_file_get_info(mfshell->conn, file, (char *)quickkey);
|
||||
if (retval != 0) {
|
||||
fprintf(stderr, "api call unsuccessful\n");
|
||||
}
|
||||
@@ -63,20 +64,16 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
||||
name = file_get_name(file);
|
||||
hash = file_get_hash(file);
|
||||
|
||||
if(name != NULL && name[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"filename:", name);
|
||||
if (name != NULL && name[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "filename:", name);
|
||||
|
||||
if(quickkey != NULL && quickkey[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"quickkey:", quickkey);
|
||||
if (quickkey != NULL && quickkey[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "quickkey:", quickkey);
|
||||
|
||||
if(hash != NULL && hash[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"hash:", hash);
|
||||
if (hash != NULL && hash[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "hash:", hash);
|
||||
|
||||
file_free(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -28,18 +27,18 @@
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/file.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_get(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
ssize_t bytes_read;
|
||||
const char *quickkey;
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
ssize_t bytes_read;
|
||||
const char *quickkey;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -47,45 +46,42 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
quickkey = argv[1];
|
||||
if(quickkey == NULL) return -1;
|
||||
if (quickkey == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(quickkey);
|
||||
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
file = file_alloc();
|
||||
|
||||
// get file name
|
||||
retval = mfconn_api_file_get_info(mfshell->conn,file,(char*)quickkey);
|
||||
retval = mfconn_api_file_get_info(mfshell->conn, file, (char *)quickkey);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
|
||||
if(retval == -1)
|
||||
{
|
||||
if (retval == -1) {
|
||||
file_free(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// request a direct download (streaming) link
|
||||
retval = mfconn_api_file_get_links(mfshell->conn,file,(char*)quickkey);
|
||||
retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
|
||||
if(retval == -1)
|
||||
{
|
||||
if (retval == -1) {
|
||||
file_free(file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// make sure we have a valid working directory to download to
|
||||
if(mfshell->local_working_dir == NULL)
|
||||
{
|
||||
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
|
||||
getcwd(mfshell->local_working_dir,PATH_MAX);
|
||||
if (mfshell->local_working_dir == NULL) {
|
||||
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||
}
|
||||
|
||||
bytes_read = file_download_direct(file, mfshell->local_working_dir);
|
||||
|
||||
if(bytes_read != -1)
|
||||
printf("\r Downloaded %zd bytes OK!\n\r",bytes_read);
|
||||
if (bytes_read != -1)
|
||||
printf("\r Downloaded %zd bytes OK!\n\r", bytes_read);
|
||||
else
|
||||
printf("\r\n Download FAILED!\n\r");
|
||||
|
||||
@@ -93,4 +89,3 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,33 +17,29 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../mfshell.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_help(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_help(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
unsigned int column1_width = 0;
|
||||
unsigned int column2_width = 0;
|
||||
mfcmd *curr_cmd;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf(
|
||||
" arguments:\n\r"
|
||||
" <optional>\n\r"
|
||||
" [required]\n\r");
|
||||
printf(" arguments:\n\r"
|
||||
" <optional>\n\r" " [required]\n\r");
|
||||
|
||||
printf("\n\r");
|
||||
|
||||
unsigned int column1_width = 0;
|
||||
unsigned int column2_width = 0;
|
||||
|
||||
mfcmd* curr_cmd;
|
||||
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||
if (strlen(curr_cmd->name) > column1_width)
|
||||
column1_width = strlen(curr_cmd->name);
|
||||
@@ -52,10 +48,9 @@ mfshell_cmd_help(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||
printf("%*s %*s %s\n", column1_width, curr_cmd->name, column2_width, curr_cmd->argstring, curr_cmd->help);
|
||||
printf("%*s %*s %s\n", column1_width, curr_cmd->name,
|
||||
column2_width, curr_cmd->argstring, curr_cmd->help);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -25,16 +24,14 @@
|
||||
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
static char*
|
||||
_get_host_from_user(void);
|
||||
static char *_get_host_from_user(void);
|
||||
|
||||
int
|
||||
mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_host(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
char *alt_host = NULL;
|
||||
char *host;
|
||||
char *alt_host = NULL;
|
||||
char *host;
|
||||
|
||||
switch (argc) {
|
||||
case 1:
|
||||
@@ -49,17 +46,15 @@ mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(mfshell->server != NULL)
|
||||
{
|
||||
if (mfshell->server != NULL) {
|
||||
// do nothing if the server is exactly the same
|
||||
if(strcmp(mfshell->server,host) == 0)
|
||||
{
|
||||
if(alt_host != NULL) free(alt_host);
|
||||
if (strcmp(mfshell->server, host) == 0) {
|
||||
if (alt_host != NULL)
|
||||
free(alt_host);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(mfshell->server != NULL)
|
||||
{
|
||||
if (mfshell->server != NULL) {
|
||||
free(mfshell->server);
|
||||
mfshell->server = NULL;
|
||||
}
|
||||
@@ -69,27 +64,26 @@ mfshell_cmd_host(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
mfconn_destroy(mfshell->conn);
|
||||
|
||||
if(alt_host != NULL) free(alt_host);
|
||||
if (alt_host != NULL)
|
||||
free(alt_host);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char*
|
||||
_get_host_from_user(void)
|
||||
char *_get_host_from_user(void)
|
||||
{
|
||||
size_t len = 0;
|
||||
char *host = NULL;
|
||||
size_t len = 0;
|
||||
char *host = NULL;
|
||||
|
||||
printf("host: [www.mediafire.com] ");
|
||||
getline(&host,&len,stdin);
|
||||
if (host[strlen(host)-1] == '\n')
|
||||
host[strlen(host)-1] = '\0';
|
||||
getline(&host, &len, stdin);
|
||||
if (host[strlen(host) - 1] == '\n')
|
||||
host[strlen(host) - 1] = '\0';
|
||||
|
||||
if(host == NULL)
|
||||
if (host == NULL)
|
||||
return strdup("www.mediafire.com");
|
||||
|
||||
if(strlen(host) < 2)
|
||||
{
|
||||
if (strlen(host) < 2) {
|
||||
free(host);
|
||||
return strdup("www.mediafire.com");
|
||||
}
|
||||
|
||||
@@ -17,22 +17,21 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../mfshell.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_lcd(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
int retval;
|
||||
const char *dir;
|
||||
int retval;
|
||||
const char *dir;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -40,15 +39,15 @@ mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
dir = argv[1];
|
||||
if(dir == NULL) return -1;
|
||||
if (dir == NULL)
|
||||
return -1;
|
||||
|
||||
if(strlen(dir) < 1) return -1;
|
||||
if (strlen(dir) < 1)
|
||||
return -1;
|
||||
|
||||
retval = chdir(dir);
|
||||
if(retval == 0)
|
||||
{
|
||||
if(mfshell->local_working_dir != NULL)
|
||||
{
|
||||
if (retval == 0) {
|
||||
if (mfshell->local_working_dir != NULL) {
|
||||
free(mfshell->local_working_dir);
|
||||
mfshell->local_working_dir = NULL;
|
||||
}
|
||||
@@ -58,4 +57,3 @@ mfshell_cmd_lcd(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -25,20 +24,20 @@
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/file.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_links(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
const char *quickkey;
|
||||
const char *share_link;
|
||||
const char *direct_link;
|
||||
const char *onetime_link;
|
||||
mffile *file;
|
||||
int len;
|
||||
int retval;
|
||||
const char *quickkey;
|
||||
const char *share_link;
|
||||
const char *direct_link;
|
||||
const char *onetime_link;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -46,15 +45,17 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
quickkey = argv[1];
|
||||
if(quickkey == NULL) return -1;
|
||||
if (quickkey == NULL)
|
||||
return -1;
|
||||
|
||||
len = strlen(quickkey);
|
||||
|
||||
if(len != 11 && len != 15) return -1;
|
||||
if (len != 11 && len != 15)
|
||||
return -1;
|
||||
|
||||
file = file_alloc();
|
||||
|
||||
retval = mfconn_api_file_get_links(mfshell->conn,file,(char*)quickkey);
|
||||
retval = mfconn_api_file_get_links(mfshell->conn, file, (char *)quickkey);
|
||||
if (retval != 0) {
|
||||
fprintf(stderr, "api call unsuccessful\n");
|
||||
}
|
||||
@@ -64,20 +65,16 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
||||
direct_link = file_get_direct_link(file);
|
||||
onetime_link = file_get_onetime_link(file);
|
||||
|
||||
if(share_link != NULL && share_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"sharing url:", share_link);
|
||||
if (share_link != NULL && share_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "sharing url:", share_link);
|
||||
|
||||
if(direct_link != NULL && direct_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"direct url:", direct_link);
|
||||
if (direct_link != NULL && direct_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "direct url:", direct_link);
|
||||
|
||||
if(onetime_link != NULL && onetime_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"1-time url:", onetime_link);
|
||||
if (onetime_link != NULL && onetime_link[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r", "1-time url:", onetime_link);
|
||||
|
||||
file_free(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,23 +17,22 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/folder.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_list(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_list(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
int retval;
|
||||
const char *folder_curr;
|
||||
int retval;
|
||||
const char *folder_curr;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -43,22 +42,22 @@ mfshell_cmd_list(mfshell *mfshell, int argc, char **argv)
|
||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||
|
||||
// safety check... this should never happen
|
||||
if(folder_curr == NULL)
|
||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
||||
if (folder_curr == NULL)
|
||||
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||
|
||||
// safety check... this should never happen
|
||||
if(folder_curr[0] == '\0')
|
||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
||||
if (folder_curr[0] == '\0')
|
||||
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||
|
||||
// first folders
|
||||
retval = mfconn_api_folder_get_content(mfshell->conn, 0, mfshell->folder_curr);
|
||||
retval =
|
||||
mfconn_api_folder_get_content(mfshell->conn, 0, mfshell->folder_curr);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
|
||||
// then files
|
||||
retval = mfconn_api_folder_get_content(mfshell->conn, 1, mfshell->folder_curr);
|
||||
retval =
|
||||
mfconn_api_folder_get_content(mfshell->conn, 1, mfshell->folder_curr);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,34 +17,31 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../mfshell.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_lpwd(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_lpwd(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of argumens\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(mfshell->local_working_dir == NULL)
|
||||
{
|
||||
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
|
||||
getcwd(mfshell->local_working_dir,PATH_MAX);
|
||||
if (mfshell->local_working_dir == NULL) {
|
||||
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||
}
|
||||
|
||||
printf("%s\n\r", mfshell->local_working_dir);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,23 +17,22 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/folder.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_mkdir(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
int retval;
|
||||
const char *folder_curr;
|
||||
const char *name;
|
||||
int retval;
|
||||
const char *folder_curr;
|
||||
const char *name;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -41,24 +40,25 @@ mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
name = argv[1];
|
||||
if (name == NULL) return -1;
|
||||
if (name == NULL)
|
||||
return -1;
|
||||
|
||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||
|
||||
// safety check... this should never happen
|
||||
if(folder_curr == NULL)
|
||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
||||
if (folder_curr == NULL)
|
||||
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||
|
||||
// safety check... this should never happen
|
||||
if(folder_curr[0] == '\0')
|
||||
folder_set_key(mfshell->folder_curr,"myfiles");
|
||||
if (folder_curr[0] == '\0')
|
||||
folder_set_key(mfshell->folder_curr, "myfiles");
|
||||
|
||||
folder_curr = folder_get_key(mfshell->folder_curr);
|
||||
|
||||
retval = mfconn_api_folder_create(mfshell->conn,(char*)folder_curr,(char*)name);
|
||||
retval =
|
||||
mfconn_api_folder_create(mfshell->conn, (char *)folder_curr,
|
||||
(char *)name);
|
||||
mfconn_update_secret_key(mfshell->conn);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,24 +17,24 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../../utils/strings.h"
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/folder.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_pwd(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
const char *folder_name;
|
||||
char *folder_name_tmp = NULL;
|
||||
const char *folder_name;
|
||||
char *folder_name_tmp = NULL;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if(mfshell->folder_curr == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
if (mfshell->folder_curr == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -42,16 +42,15 @@ mfshell_cmd_pwd(mfshell *mfshell, int argc, char **argv)
|
||||
}
|
||||
|
||||
folder_name = folder_get_name(mfshell->folder_curr);
|
||||
if(folder_name[0] == '\0') return -1;
|
||||
if (folder_name[0] == '\0')
|
||||
return -1;
|
||||
|
||||
folder_name_tmp = strdup_printf("< %s >",folder_name);
|
||||
folder_name_tmp = strdup_printf("< %s >", folder_name);
|
||||
|
||||
printf("%-15.13s %-50.50s\n\r",
|
||||
folder_get_key(mfshell->folder_curr),
|
||||
folder_name_tmp);
|
||||
folder_get_key(mfshell->folder_curr), folder_name_tmp);
|
||||
|
||||
free(folder_name_tmp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,19 +17,17 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
#include "../mfshell.h"
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
#include "../commands.h" // IWYU pragma: keep
|
||||
|
||||
int
|
||||
mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv)
|
||||
int mfshell_cmd_whoami(mfshell * mfshell, int argc, char **argv)
|
||||
{
|
||||
(void)argv;
|
||||
int retval;
|
||||
int retval;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -41,5 +39,3 @@ mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
110
mfshell/main.c
110
mfshell/main.c
@@ -27,11 +27,9 @@
|
||||
|
||||
#include "mfshell.h"
|
||||
|
||||
static void
|
||||
mfshell_run(mfshell *mfshell);
|
||||
static void mfshell_run(mfshell * mfshell);
|
||||
|
||||
static void
|
||||
mfshell_parse_commands(mfshell *mfshell, char *command);
|
||||
static void mfshell_parse_commands(mfshell * mfshell, char *command);
|
||||
|
||||
void print_help(char *cmd)
|
||||
{
|
||||
@@ -46,33 +44,36 @@ void print_help(char *cmd)
|
||||
fprintf(stderr, " -p, --password=<PASS> Login password\n");
|
||||
fprintf(stderr, " -s, --server=<SERVER> Login server\n");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "Username and password are optional. If not given, they\n"
|
||||
"have to be entered via standard input.\n");
|
||||
fprintf(stderr,
|
||||
"Username and password are optional. If not given, they\n"
|
||||
"have to be entered via standard input.\n");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "You should not pass your password as a commandline\n"
|
||||
"argument as other users with access to the list of\n"
|
||||
"running processes will then be able to see it.\n");
|
||||
"argument as other users with access to the list of\n"
|
||||
"running processes will then be able to see it.\n");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "Use the \"help\" command to print a list of available\n"
|
||||
"commands in the interactive environment:\n");
|
||||
fprintf(stderr,
|
||||
"Use the \"help\" command to print a list of available\n"
|
||||
"commands in the interactive environment:\n");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " %s -c help\n", cmd);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
void parse_argv(int argc, char **argv, char **username,
|
||||
char **password, char **server, char **command)
|
||||
void
|
||||
parse_argv(int argc, char **argv, char **username,
|
||||
char **password, char **server, char **command)
|
||||
{
|
||||
static struct option long_options[] = {
|
||||
{"command", required_argument, 0, 'c'},
|
||||
{"command", required_argument, 0, 'c'},
|
||||
{"username", required_argument, 0, 'u'},
|
||||
{"password", required_argument, 0, 'p'},
|
||||
{"server", required_argument, 0, 's'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'v'}
|
||||
{"server", required_argument, 0, 's'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'v'}
|
||||
};
|
||||
int c;
|
||||
|
||||
int c;
|
||||
for (;;) {
|
||||
c = getopt_long(argc, argv, "c:u:p:s:hv", long_options, NULL);
|
||||
if (c == -1)
|
||||
@@ -116,20 +117,21 @@ void parse_argv(int argc, char **argv, char **username,
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc,char **argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
mfshell *mfshell;
|
||||
char *server = "www.mediafire.com";
|
||||
char *username = NULL;
|
||||
char *password = NULL;
|
||||
char *command = NULL;
|
||||
mfshell *mfshell;
|
||||
char *server = "www.mediafire.com";
|
||||
char *username = NULL;
|
||||
char *password = NULL;
|
||||
char *command = NULL;
|
||||
|
||||
SSL_library_init();
|
||||
|
||||
parse_argv(argc, argv, &username, &password, &server, &command);
|
||||
|
||||
mfshell = mfshell_create(35860,
|
||||
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",server);
|
||||
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",
|
||||
server);
|
||||
|
||||
if (command == NULL) {
|
||||
// begin shell mode
|
||||
@@ -142,23 +144,33 @@ int main(int argc,char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
mfshell_parse_commands(mfshell *mfshell, char *command)
|
||||
static void mfshell_parse_commands(mfshell * mfshell, char *command)
|
||||
{
|
||||
char *next;
|
||||
int ret;
|
||||
wordexp_t p;
|
||||
char *next;
|
||||
int ret;
|
||||
wordexp_t p;
|
||||
|
||||
// FIXME: don't split by semicolon but by unescaped/unquoted semicolon
|
||||
while ((next = strsep(&command, ";")) != NULL) {
|
||||
// FIXME: handle non-zero return value of wordexp
|
||||
ret = wordexp(next, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
||||
if (ret != 0) {
|
||||
switch (ret) {
|
||||
case WRDE_BADCHAR: fprintf(stderr, "wordexp: WRDE_BADCHAR\n"); break;
|
||||
case WRDE_BADVAL: fprintf(stderr, "wordexp: WRDE_BADVAL\n"); break;
|
||||
case WRDE_CMDSUB: fprintf(stderr, "wordexp: WRDE_CMDSUB\n"); break;
|
||||
case WRDE_NOSPACE: fprintf(stderr, "wordexp: WRDE_NOSPACE\n"); break;
|
||||
case WRDE_SYNTAX: fprintf(stderr, "wordexp: WRDE_SYNTAX\n"); break;
|
||||
case WRDE_BADCHAR:
|
||||
fprintf(stderr, "wordexp: WRDE_BADCHAR\n");
|
||||
break;
|
||||
case WRDE_BADVAL:
|
||||
fprintf(stderr, "wordexp: WRDE_BADVAL\n");
|
||||
break;
|
||||
case WRDE_CMDSUB:
|
||||
fprintf(stderr, "wordexp: WRDE_CMDSUB\n");
|
||||
break;
|
||||
case WRDE_NOSPACE:
|
||||
fprintf(stderr, "wordexp: WRDE_NOSPACE\n");
|
||||
break;
|
||||
case WRDE_SYNTAX:
|
||||
fprintf(stderr, "wordexp: WRDE_SYNTAX\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (p.we_wordc < 1) {
|
||||
@@ -170,45 +182,41 @@ mfshell_parse_commands(mfshell *mfshell, char *command)
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
mfshell_run(mfshell *mfshell)
|
||||
static void mfshell_run(mfshell * mfshell)
|
||||
{
|
||||
char *cmd = NULL;
|
||||
size_t len;
|
||||
int abort = 0;
|
||||
int retval;
|
||||
char *cmd = NULL;
|
||||
size_t len;
|
||||
int abort = 0;
|
||||
int retval;
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
printf("\n\rmfshell > ");
|
||||
|
||||
retval = getline(&cmd,&len,stdin);
|
||||
retval = getline(&cmd, &len, stdin);
|
||||
if (retval == -1) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (cmd[strlen(cmd)-1] == '\n')
|
||||
cmd[strlen(cmd)-1] = '\0';
|
||||
if (cmd[strlen(cmd) - 1] == '\n')
|
||||
cmd[strlen(cmd) - 1] = '\0';
|
||||
|
||||
printf("\n\r");
|
||||
|
||||
if(strcmp(cmd,"exit") == 0)
|
||||
{
|
||||
if (strcmp(cmd, "exit") == 0) {
|
||||
abort = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(strcmp(cmd,"quit") == 0)
|
||||
{
|
||||
if (strcmp(cmd, "quit") == 0) {
|
||||
abort = 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
retval = mfshell_exec_shell_command(mfshell,cmd);
|
||||
retval = mfshell_exec_shell_command(mfshell, cmd);
|
||||
free(cmd);
|
||||
cmd = NULL;
|
||||
}
|
||||
while(abort == 0);
|
||||
while (abort == 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -27,46 +26,49 @@
|
||||
#include "mfshell.h"
|
||||
#include "../mfapi/folder.h"
|
||||
|
||||
struct mfcmd commands[] = {
|
||||
{"help", "", "show this help", mfshell_cmd_help},
|
||||
{"debug", "", "show debug information", mfshell_cmd_debug},
|
||||
{"host", "<server>", "change target server", mfshell_cmd_host},
|
||||
{"auth", "<user <pass>>", "authenticate with active server",
|
||||
mfshell_cmd_auth},
|
||||
{"whoami", "", "show basic user info", mfshell_cmd_whoami},
|
||||
{"ls", "", "show contents of active folder",
|
||||
mfshell_cmd_list},
|
||||
{"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir},
|
||||
{"pwd", "", "show the active folder", mfshell_cmd_pwd},
|
||||
{"lpwd", "", "show the local working directory",
|
||||
mfshell_cmd_lpwd},
|
||||
{"lcd", "[dir]", "change the local working directory",
|
||||
mfshell_cmd_lcd},
|
||||
{"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir},
|
||||
{"file", "[quickkey]", "show file information", mfshell_cmd_file},
|
||||
{"links", "[quickkey]", "show access urls for the file",
|
||||
mfshell_cmd_links},
|
||||
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
struct mfcmd commands[] = {
|
||||
{"help", "", "show this help", mfshell_cmd_help},
|
||||
{"debug", "", "show debug information", mfshell_cmd_debug},
|
||||
{"host", "<server>", "change target server", mfshell_cmd_host},
|
||||
{"auth", "<user <pass>>", "authenticate with active server",
|
||||
mfshell_cmd_auth},
|
||||
{"whoami", "", "show basic user info", mfshell_cmd_whoami},
|
||||
{"ls", "", "show contents of active folder",
|
||||
mfshell_cmd_list},
|
||||
{"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir},
|
||||
{"pwd", "", "show the active folder", mfshell_cmd_pwd},
|
||||
{"lpwd", "", "show the local working directory",
|
||||
mfshell_cmd_lpwd},
|
||||
{"lcd", "[dir]", "change the local working directory",
|
||||
mfshell_cmd_lcd},
|
||||
{"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir},
|
||||
{"file", "[quickkey]", "show file information", mfshell_cmd_file},
|
||||
{"links", "[quickkey]", "show access urls for the file",
|
||||
mfshell_cmd_links},
|
||||
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
|
||||
{NULL, NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
mfshell*
|
||||
mfshell_create(int app_id,char *app_key,char *server)
|
||||
mfshell *mfshell_create(int app_id, char *app_key, char *server)
|
||||
{
|
||||
mfshell *shell;
|
||||
mfshell *shell;
|
||||
|
||||
if(app_id <= 0) return NULL;
|
||||
if(app_key == NULL) return NULL;
|
||||
if(server == NULL) return NULL;
|
||||
if (app_id <= 0)
|
||||
return NULL;
|
||||
if (app_key == NULL)
|
||||
return NULL;
|
||||
if (server == NULL)
|
||||
return NULL;
|
||||
|
||||
/*
|
||||
check to see if the server contains a forward-slash. if so,
|
||||
the caller did not understand the API and passed in the wrong
|
||||
type of server resource.
|
||||
*/
|
||||
if(strchr(server,'/') != NULL) return NULL;
|
||||
check to see if the server contains a forward-slash. if so,
|
||||
the caller did not understand the API and passed in the wrong
|
||||
type of server resource.
|
||||
*/
|
||||
if (strchr(server, '/') != NULL)
|
||||
return NULL;
|
||||
|
||||
shell = (mfshell*)calloc(1,sizeof(mfshell));
|
||||
shell = (mfshell *) calloc(1, sizeof(mfshell));
|
||||
|
||||
shell->app_id = app_id;
|
||||
shell->app_key = strdup(app_key);
|
||||
@@ -74,7 +76,7 @@ mfshell_create(int app_id,char *app_key,char *server)
|
||||
|
||||
// object to track folder location
|
||||
shell->folder_curr = folder_alloc();
|
||||
folder_set_key(shell->folder_curr,"myfiles");
|
||||
folder_set_key(shell->folder_curr, "myfiles");
|
||||
|
||||
// shell commands
|
||||
shell->commands = commands;
|
||||
@@ -82,10 +84,10 @@ mfshell_create(int app_id,char *app_key,char *server)
|
||||
return shell;
|
||||
}
|
||||
|
||||
int
|
||||
mfshell_exec(mfshell *shell, int argc, char **argv)
|
||||
int mfshell_exec(mfshell * shell, int argc, char **argv)
|
||||
{
|
||||
mfcmd* curr_cmd;
|
||||
mfcmd *curr_cmd;
|
||||
|
||||
for (curr_cmd = shell->commands; curr_cmd->name != NULL; curr_cmd++) {
|
||||
if (strcmp(argv[0], curr_cmd->name) == 0) {
|
||||
return curr_cmd->handler(shell, argc, argv);
|
||||
@@ -94,24 +96,35 @@ mfshell_exec(mfshell *shell, int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
mfshell_exec_shell_command(mfshell *shell,char *command)
|
||||
int mfshell_exec_shell_command(mfshell * shell, char *command)
|
||||
{
|
||||
wordexp_t p;
|
||||
int retval;
|
||||
|
||||
if(shell == NULL) return -1;
|
||||
if(command == NULL) return -1;
|
||||
if (shell == NULL)
|
||||
return -1;
|
||||
if (command == NULL)
|
||||
return -1;
|
||||
|
||||
// FIXME: handle non-zero return value of wordexp
|
||||
retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF);
|
||||
if (retval != 0) {
|
||||
switch (retval) {
|
||||
case WRDE_BADCHAR: fprintf(stderr, "wordexp: WRDE_BADCHAR\n"); break;
|
||||
case WRDE_BADVAL: fprintf(stderr, "wordexp: WRDE_BADVAL\n"); break;
|
||||
case WRDE_CMDSUB: fprintf(stderr, "wordexp: WRDE_CMDSUB\n"); break;
|
||||
case WRDE_NOSPACE: fprintf(stderr, "wordexp: WRDE_NOSPACE\n"); break;
|
||||
case WRDE_SYNTAX: fprintf(stderr, "wordexp: WRDE_SYNTAX\n"); break;
|
||||
case WRDE_BADCHAR:
|
||||
fprintf(stderr, "wordexp: WRDE_BADCHAR\n");
|
||||
break;
|
||||
case WRDE_BADVAL:
|
||||
fprintf(stderr, "wordexp: WRDE_BADVAL\n");
|
||||
break;
|
||||
case WRDE_CMDSUB:
|
||||
fprintf(stderr, "wordexp: WRDE_CMDSUB\n");
|
||||
break;
|
||||
case WRDE_NOSPACE:
|
||||
fprintf(stderr, "wordexp: WRDE_NOSPACE\n");
|
||||
break;
|
||||
case WRDE_SYNTAX:
|
||||
fprintf(stderr, "wordexp: WRDE_SYNTAX\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,4 +141,3 @@ mfshell_exec_shell_command(mfshell *shell,char *command)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFSHELL_H_
|
||||
#define _MFSHELL_H_
|
||||
|
||||
@@ -27,42 +26,39 @@
|
||||
struct mfcmd;
|
||||
struct mfshell;
|
||||
|
||||
typedef struct mfcmd mfcmd;
|
||||
typedef struct mfshell mfshell;
|
||||
typedef struct mfcmd mfcmd;
|
||||
typedef struct mfshell mfshell;
|
||||
|
||||
struct mfcmd
|
||||
{
|
||||
char *name;
|
||||
char *argstring;
|
||||
char *help;
|
||||
int (*handler) (mfshell *mfshell, int argc, char **argv);
|
||||
struct mfcmd {
|
||||
char *name;
|
||||
char *argstring;
|
||||
char *help;
|
||||
int (*handler) (mfshell * mfshell, int argc, char **argv);
|
||||
};
|
||||
|
||||
struct mfshell
|
||||
{
|
||||
int app_id;
|
||||
char *app_key;
|
||||
char *server;
|
||||
struct mfshell {
|
||||
int app_id;
|
||||
char *app_key;
|
||||
char *server;
|
||||
|
||||
/* REST API tracking */
|
||||
mffolder *folder_curr;
|
||||
mffolder *folder_curr;
|
||||
|
||||
/* Local tracking */
|
||||
char *local_working_dir;
|
||||
char *local_working_dir;
|
||||
|
||||
/* shell commands */
|
||||
mfcmd *commands;
|
||||
mfcmd *commands;
|
||||
|
||||
mfconn *conn;
|
||||
mfconn *conn;
|
||||
};
|
||||
|
||||
mfshell* mfshell_create(int app_id,char *app_key,char *server);
|
||||
mfshell *mfshell_create(int app_id, char *app_key, char *server);
|
||||
|
||||
int mfshell_authenticate_user(mfshell *mfshell);
|
||||
int mfshell_authenticate_user(mfshell * mfshell);
|
||||
|
||||
int mfshell_exec(mfshell *mfshell, int argc, char **argv);
|
||||
int mfshell_exec(mfshell * mfshell, int argc, char **argv);
|
||||
|
||||
int mfshell_exec_shell_command(mfshell *mfshell,char *command);
|
||||
int mfshell_exec_shell_command(mfshell * mfshell, char *command);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
15
tests/indent.sh
Executable file
15
tests/indent.sh
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
|
||||
ret=0
|
||||
for f in ../**/*.c ../**/*.h; do
|
||||
case $f in
|
||||
../**/CMakeFiles/**/*)
|
||||
;;
|
||||
*)
|
||||
INDENT_PROFILE=../.indent.pro indent -st $f | diff -u $f -
|
||||
ret=$((ret+$?))
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
exit $ret
|
||||
154
utils/http.c
154
utils/http.c
@@ -24,23 +24,26 @@
|
||||
|
||||
#include "http.h"
|
||||
|
||||
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow, double ultotal, double ulnow);
|
||||
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
||||
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
||||
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
|
||||
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||
double ultotal, double ulnow);
|
||||
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb,
|
||||
void *user_ptr);
|
||||
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb,
|
||||
void *user_ptr);
|
||||
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb,
|
||||
void *user_ptr);
|
||||
|
||||
struct mfhttp
|
||||
{
|
||||
CURL *curl_handle;
|
||||
char *write_buf;
|
||||
size_t write_buf_len;
|
||||
double ul_len;
|
||||
double ul_now;
|
||||
double dl_len;
|
||||
double dl_now;
|
||||
bool show_progress;
|
||||
char error_buf[CURL_ERROR_SIZE];
|
||||
FILE *stream;
|
||||
struct mfhttp {
|
||||
CURL *curl_handle;
|
||||
char *write_buf;
|
||||
size_t write_buf_len;
|
||||
double ul_len;
|
||||
double ul_now;
|
||||
double dl_len;
|
||||
double dl_now;
|
||||
bool show_progress;
|
||||
char error_buf[CURL_ERROR_SIZE];
|
||||
FILE *stream;
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -51,20 +54,22 @@ struct mfhttp
|
||||
* keep-alive anyways.
|
||||
*/
|
||||
|
||||
mfhttp*
|
||||
http_create(void)
|
||||
mfhttp *http_create(void)
|
||||
{
|
||||
mfhttp *conn;
|
||||
CURL *curl_handle;
|
||||
curl_handle = curl_easy_init();
|
||||
if(curl_handle == NULL) return NULL;
|
||||
mfhttp *conn;
|
||||
CURL *curl_handle;
|
||||
|
||||
conn = (mfhttp*)calloc(1,sizeof(mfhttp));
|
||||
curl_handle = curl_easy_init();
|
||||
if (curl_handle == NULL)
|
||||
return NULL;
|
||||
|
||||
conn = (mfhttp *) calloc(1, sizeof(mfhttp));
|
||||
conn->curl_handle = curl_handle;
|
||||
|
||||
conn->show_progress = false;
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS,0);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION, http_progress_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS, 0);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION,
|
||||
http_progress_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
|
||||
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
|
||||
@@ -77,28 +82,29 @@ http_create(void)
|
||||
return conn;
|
||||
}
|
||||
|
||||
json_t *
|
||||
http_parse_buf_json(mfhttp* conn, size_t flags, json_error_t *error)
|
||||
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
|
||||
json_error_t * error)
|
||||
{
|
||||
return json_loadb(conn->write_buf, conn->write_buf_len, flags, error);
|
||||
}
|
||||
|
||||
void
|
||||
http_destroy(mfhttp* conn)
|
||||
void http_destroy(mfhttp * conn)
|
||||
{
|
||||
curl_easy_cleanup(conn->curl_handle);
|
||||
free(conn->write_buf);
|
||||
free(conn);
|
||||
}
|
||||
|
||||
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||
double ultotal, double ulnow)
|
||||
static int
|
||||
http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||
double ultotal, double ulnow)
|
||||
{
|
||||
mfhttp *conn;
|
||||
mfhttp *conn;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
conn = (mfhttp *)user_ptr;
|
||||
conn = (mfhttp *) user_ptr;
|
||||
|
||||
conn->ul_len = ultotal;
|
||||
conn->ul_now = ulnow;
|
||||
@@ -110,18 +116,21 @@ static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||
}
|
||||
|
||||
int
|
||||
http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, void *data), void *data)
|
||||
http_get_buf(mfhttp * conn, const char *url,
|
||||
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
||||
{
|
||||
int retval;
|
||||
int retval;
|
||||
|
||||
curl_easy_reset(conn->curl_handle);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||
http_write_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
||||
//fprintf(stderr, "GET: %s\n", url);
|
||||
retval = curl_easy_perform(conn->curl_handle);
|
||||
if(retval != CURLE_OK) {
|
||||
if (retval != CURLE_OK) {
|
||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||
return retval;
|
||||
}
|
||||
@@ -131,14 +140,15 @@ http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, vo
|
||||
return retval;
|
||||
}
|
||||
|
||||
static size_t
|
||||
static size_t
|
||||
http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
{
|
||||
size_t data_len;
|
||||
size_t data_len;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
data_len = size*nmemb;
|
||||
data_len = size * nmemb;
|
||||
|
||||
if (data_len > 0) {
|
||||
fwrite(data, size, nmemb, stderr);
|
||||
@@ -149,21 +159,22 @@ http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
static size_t
|
||||
http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
{
|
||||
mfhttp *conn;
|
||||
size_t data_len;
|
||||
mfhttp *conn;
|
||||
size_t data_len;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
conn = (mfhttp*)user_ptr;
|
||||
data_len = size*nmemb;
|
||||
conn = (mfhttp *) user_ptr;
|
||||
data_len = size * nmemb;
|
||||
|
||||
if (data_len > 0) {
|
||||
//fwrite(data, size, nmemb, stderr);
|
||||
conn->write_buf = (char *)realloc(conn->write_buf,
|
||||
conn->write_buf_len + data_len);
|
||||
conn->write_buf_len + data_len);
|
||||
memcpy(conn->write_buf + conn->write_buf_len, data, data_len);
|
||||
conn->write_buf_len += data_len;
|
||||
}
|
||||
@@ -172,18 +183,21 @@ http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
}
|
||||
|
||||
int
|
||||
http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_handler)(mfhttp *conn, void *data), void *data)
|
||||
http_post_buf(mfhttp * conn, const char *url, const char *post_args,
|
||||
int (*data_handler) (mfhttp * conn, void *data), void *data)
|
||||
{
|
||||
int retval;
|
||||
int retval;
|
||||
|
||||
curl_easy_reset(conn->curl_handle);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||
http_write_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
|
||||
retval = curl_easy_perform(conn->curl_handle);
|
||||
if(retval != CURLE_OK) {
|
||||
if (retval != CURLE_OK) {
|
||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||
return retval;
|
||||
}
|
||||
@@ -193,40 +207,42 @@ http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_h
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
http_get_file(mfhttp *conn, const char *url, const char *path)
|
||||
int http_get_file(mfhttp * conn, const char *url, const char *path)
|
||||
{
|
||||
int retval;
|
||||
int retval;
|
||||
|
||||
curl_easy_reset(conn->curl_handle);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_file_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void *)conn);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION,
|
||||
http_write_file_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void *)conn);
|
||||
// FIXME: handle fopen() return value
|
||||
conn->stream = fopen(path, "w+");
|
||||
retval = curl_easy_perform(conn->curl_handle);
|
||||
fclose(conn->stream);
|
||||
if(retval != CURLE_OK) {
|
||||
if (retval != CURLE_OK) {
|
||||
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
|
||||
return retval;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
static size_t
|
||||
static size_t
|
||||
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
{
|
||||
mfhttp *conn;
|
||||
mfhttp *conn;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
conn = (mfhttp*)user_ptr;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
conn = (mfhttp *) user_ptr;
|
||||
|
||||
fwrite(data, size, nmemb, conn->stream);
|
||||
|
||||
fprintf(stderr, "\r %.0f / %.0f", conn->dl_now, conn->dl_len);
|
||||
|
||||
return size*nmemb;
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
/*int
|
||||
|
||||
22
utils/http.h
22
utils/http.h
@@ -25,13 +25,19 @@
|
||||
|
||||
typedef struct mfhttp mfhttp;
|
||||
|
||||
mfhttp* http_create(void);
|
||||
void http_destroy(mfhttp* conn);
|
||||
int http_get_buf(mfhttp *conn, const char *url, int (*data_handler)(mfhttp *conn, void *data), void *data);
|
||||
int http_post_buf(mfhttp *conn, const char *url, const char *post_args, int (*data_handler)(mfhttp *conn, void *data), void *data);
|
||||
int http_get_file(mfhttp *conn, const char *url, const char *path);
|
||||
int http_post_file(mfhttp *conn, const char *url, const char *post_args, FILE *fd);
|
||||
json_t *http_parse_buf_json(mfhttp* conn, size_t flags, json_error_t *error);
|
||||
|
||||
mfhttp *http_create(void);
|
||||
void http_destroy(mfhttp * conn);
|
||||
int http_get_buf(mfhttp * conn, const char *url,
|
||||
int (*data_handler) (mfhttp * conn, void *data),
|
||||
void *data);
|
||||
int http_post_buf(mfhttp * conn, const char *url,
|
||||
const char *post_args,
|
||||
int (*data_handler) (mfhttp * conn, void *data),
|
||||
void *data);
|
||||
int http_get_file(mfhttp * conn, const char *url, const char *path);
|
||||
int http_post_file(mfhttp * conn, const char *url,
|
||||
const char *post_args, FILE * fd);
|
||||
json_t *http_parse_buf_json(mfhttp * conn, size_t flags,
|
||||
json_error_t * error);
|
||||
|
||||
#endif
|
||||
|
||||
43
utils/json.c
43
utils/json.c
@@ -16,39 +16,40 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "json.h"
|
||||
#include "stringv.h"
|
||||
|
||||
json_t*
|
||||
json_object_by_path(json_t *start,const char *path)
|
||||
json_t *json_object_by_path(json_t * start, const char *path)
|
||||
{
|
||||
char **path_nodes;
|
||||
char **curr;
|
||||
json_t *node = NULL;
|
||||
json_t *data = NULL;
|
||||
char **path_nodes;
|
||||
char **curr;
|
||||
json_t *node = NULL;
|
||||
json_t *data = NULL;
|
||||
|
||||
if(start == NULL) return NULL;
|
||||
if(path == NULL) return NULL;
|
||||
if (start == NULL)
|
||||
return NULL;
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
|
||||
path_nodes = stringv_split((char*)path,"/",10);
|
||||
path_nodes = stringv_split((char *)path, "/", 10);
|
||||
|
||||
if(path_nodes == NULL) return NULL;
|
||||
if (path_nodes == NULL)
|
||||
return NULL;
|
||||
curr = path_nodes;
|
||||
node = start;
|
||||
|
||||
while(curr != NULL)
|
||||
{
|
||||
if(*curr == NULL) break;
|
||||
while (curr != NULL) {
|
||||
if (*curr == NULL)
|
||||
break;
|
||||
|
||||
node = json_object_get(node,*curr);
|
||||
if(node == NULL) break;
|
||||
node = json_object_get(node, *curr);
|
||||
if (node == NULL)
|
||||
break;
|
||||
|
||||
if(!json_is_object(node))
|
||||
{
|
||||
stringv_free(path_nodes,STRINGV_FREE_ALL);
|
||||
if (!json_is_object(node)) {
|
||||
stringv_free(path_nodes, STRINGV_FREE_ALL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -56,7 +57,7 @@ json_object_by_path(json_t *start,const char *path)
|
||||
data = node;
|
||||
}
|
||||
|
||||
stringv_free(path_nodes,STRINGV_FREE_ALL);
|
||||
stringv_free(path_nodes, STRINGV_FREE_ALL);
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -101,5 +102,3 @@ json_object_by_path(json_t *start,const char *path)
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -16,14 +16,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFSHELL_JSON_H_
|
||||
#define _MFSHELL_JSON_H_
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
json_t* json_object_by_path(json_t *start,const char *path);
|
||||
json_t *json_object_by_path(json_t * start, const char *path);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
127
utils/strings.c
127
utils/strings.c
@@ -16,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
@@ -27,86 +26,90 @@
|
||||
#include "strings.h"
|
||||
#include "stringv.h"
|
||||
|
||||
char*
|
||||
strdup_printf(char* fmt, ...)
|
||||
char *strdup_printf(char *fmt, ...)
|
||||
{
|
||||
// Good for glibc 2.1 and above. Fedora5 is 2.4.
|
||||
// Good for glibc 2.1 and above. Fedora5 is 2.4.
|
||||
|
||||
char *ret_str = NULL;
|
||||
va_list ap;
|
||||
int bytes_to_allocate;
|
||||
char *ret_str = NULL;
|
||||
va_list ap;
|
||||
int bytes_to_allocate;
|
||||
|
||||
va_start(ap, fmt);
|
||||
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
va_start(ap, fmt);
|
||||
bytes_to_allocate = vsnprintf(ret_str, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
// Add one for '\0'
|
||||
bytes_to_allocate++;
|
||||
// Add one for '\0'
|
||||
bytes_to_allocate++;
|
||||
|
||||
ret_str = (char*)malloc(bytes_to_allocate * sizeof(char));
|
||||
ret_str = (char *)malloc(bytes_to_allocate * sizeof(char));
|
||||
|
||||
va_start(ap, fmt);
|
||||
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
|
||||
va_end(ap);
|
||||
va_start(ap, fmt);
|
||||
bytes_to_allocate = vsnprintf(ret_str, bytes_to_allocate, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return ret_str;
|
||||
return ret_str;
|
||||
}
|
||||
|
||||
char*
|
||||
strdup_join(char *string1,char *string2)
|
||||
char *strdup_join(char *string1, char *string2)
|
||||
{
|
||||
char *new_string;
|
||||
size_t string1_len;
|
||||
size_t string2_len;
|
||||
char *new_string;
|
||||
size_t string1_len;
|
||||
size_t string2_len;
|
||||
|
||||
if(string1 == NULL || string2 == NULL) return NULL;
|
||||
if (string1 == NULL || string2 == NULL)
|
||||
return NULL;
|
||||
|
||||
string1_len = strlen(string1);
|
||||
string2_len = strlen(string2);
|
||||
|
||||
new_string = (char*)malloc(string1_len + string2_len + 1);
|
||||
new_string = (char *)malloc(string1_len + string2_len + 1);
|
||||
|
||||
strncpy(new_string,string1,string1_len);
|
||||
strncat(new_string,string2,string2_len);
|
||||
strncpy(new_string, string1, string1_len);
|
||||
strncat(new_string, string2, string2_len);
|
||||
|
||||
new_string[string1_len + string2_len] = '\0';
|
||||
|
||||
return new_string;
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
||||
char *strdup_subst(char *string, char *token, char *subst,
|
||||
unsigned int max)
|
||||
{
|
||||
size_t string_len = 0;
|
||||
size_t subst_len = 0;
|
||||
size_t token_len = 0;
|
||||
size_t total_len = 0;
|
||||
size_t copy_len = 0;
|
||||
size_t token_count;
|
||||
char *str_new = NULL;
|
||||
char **vectors;
|
||||
char **rewind;
|
||||
size_t string_len = 0;
|
||||
size_t subst_len = 0;
|
||||
size_t token_len = 0;
|
||||
size_t total_len = 0;
|
||||
size_t copy_len = 0;
|
||||
size_t token_count;
|
||||
char *str_new = NULL;
|
||||
char **vectors;
|
||||
char **rewind;
|
||||
|
||||
if(string == NULL) return NULL;
|
||||
if (string == NULL)
|
||||
return NULL;
|
||||
|
||||
if(token == NULL || subst == NULL) return NULL;
|
||||
if (token == NULL || subst == NULL)
|
||||
return NULL;
|
||||
|
||||
string_len = strlen(string);
|
||||
token_len = strlen(token);
|
||||
|
||||
// return on conditions that we could never handle.
|
||||
if(token_len > string_len) return NULL;
|
||||
if(token[0] == '\0') return NULL;
|
||||
if (token_len > string_len)
|
||||
return NULL;
|
||||
if (token[0] == '\0')
|
||||
return NULL;
|
||||
|
||||
vectors = stringv_find(string,token,max);
|
||||
if(vectors == NULL) return NULL;
|
||||
vectors = stringv_find(string, token, max);
|
||||
if (vectors == NULL)
|
||||
return NULL;
|
||||
rewind = vectors;
|
||||
|
||||
// count the number of tokens found in the string
|
||||
token_count = stringv_len(vectors);
|
||||
|
||||
if(token_count > max) token_count = max;
|
||||
if (token_count > max)
|
||||
token_count = max;
|
||||
|
||||
// start with the original string size;
|
||||
total_len = string_len;
|
||||
@@ -117,24 +120,22 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
||||
// add back the total number of subst chars to be inserted
|
||||
total_len += (subst_len * token_count);
|
||||
|
||||
str_new = (char*)malloc((total_len + 1) * sizeof(char));
|
||||
str_new = (char *)malloc((total_len + 1) * sizeof(char));
|
||||
str_new[0] = '\0';
|
||||
|
||||
while(*vectors != NULL)
|
||||
{
|
||||
while (*vectors != NULL) {
|
||||
// calculate distance to the next token from current position
|
||||
copy_len = *vectors - string;
|
||||
|
||||
if(copy_len > 0)
|
||||
{
|
||||
strncat(str_new,string,copy_len);
|
||||
if (copy_len > 0) {
|
||||
strncat(str_new, string, copy_len);
|
||||
string += copy_len;
|
||||
|
||||
// when total_len == 0 the process is complete
|
||||
total_len -= copy_len;
|
||||
}
|
||||
|
||||
strncat(str_new,token,token_len);
|
||||
strncat(str_new, token, token_len);
|
||||
|
||||
// when total_len == 0 the process is complete
|
||||
total_len -= token_len;
|
||||
@@ -143,33 +144,31 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
||||
}
|
||||
|
||||
// might have one more copy operation to complete
|
||||
if(total_len > 0)
|
||||
{
|
||||
strcat(str_new,string);
|
||||
if (total_len > 0) {
|
||||
strcat(str_new, string);
|
||||
}
|
||||
|
||||
// todo: can't free vectors directly cuz it was incremented
|
||||
free(rewind);
|
||||
|
||||
return str_new;
|
||||
}
|
||||
|
||||
void
|
||||
string_chomp(char *string)
|
||||
void string_chomp(char *string)
|
||||
{
|
||||
size_t len;
|
||||
char *pos;
|
||||
size_t len;
|
||||
char *pos;
|
||||
|
||||
if(string == NULL) return;
|
||||
if (string == NULL)
|
||||
return;
|
||||
|
||||
len = strlen(string);
|
||||
|
||||
if(len == 0) return;
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
pos = &string[len - 1];
|
||||
|
||||
while(isspace((int)*pos) != 0)
|
||||
{
|
||||
while (isspace((int)*pos) != 0) {
|
||||
*pos = '\0';
|
||||
pos--;
|
||||
}
|
||||
|
||||
@@ -16,17 +16,17 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _STR_TOOLS_H_
|
||||
#define _STR_TOOLS_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);
|
||||
|
||||
char* strdup_subst(char *string,char *str_old,char *str_new, unsigned int max);
|
||||
char *strdup_subst(char *string, char *str_old, char *str_new,
|
||||
unsigned int max);
|
||||
|
||||
void string_chomp(char *string);
|
||||
void string_chomp(char *string);
|
||||
|
||||
// void string_strip_head(char *string,char c);
|
||||
|
||||
|
||||
143
utils/stringv.c
143
utils/stringv.c
@@ -16,74 +16,72 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "stringv.h"
|
||||
|
||||
size_t
|
||||
stringv_len(char **array)
|
||||
size_t stringv_len(char **array)
|
||||
{
|
||||
size_t count = 0;
|
||||
char **pos;
|
||||
size_t count = 0;
|
||||
char **pos;
|
||||
|
||||
if(array == NULL) return 0;
|
||||
if (array == NULL)
|
||||
return 0;
|
||||
|
||||
pos = array;
|
||||
while(pos[0] != NULL)
|
||||
{
|
||||
while (pos[0] != NULL) {
|
||||
pos++;
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
return count;
|
||||
}
|
||||
|
||||
void
|
||||
stringv_free(char **array,int b_free)
|
||||
void stringv_free(char **array, int b_free)
|
||||
{
|
||||
char **pos;
|
||||
char **pos;
|
||||
|
||||
if(array == NULL) return;
|
||||
if (array == NULL)
|
||||
return;
|
||||
|
||||
pos = array;
|
||||
pos = array;
|
||||
|
||||
while((*pos) != NULL)
|
||||
{
|
||||
free(*pos);
|
||||
++pos;
|
||||
}
|
||||
while ((*pos) != NULL) {
|
||||
free(*pos);
|
||||
++pos;
|
||||
}
|
||||
|
||||
if(b_free == STRINGV_FREE_ALL) free(array);
|
||||
if (b_free == STRINGV_FREE_ALL)
|
||||
free(array);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
char**
|
||||
stringv_copy(char **array)
|
||||
char **stringv_copy(char **array)
|
||||
{
|
||||
uint32_t array_len;
|
||||
char **array_pos;
|
||||
uint32_t array_len;
|
||||
char **array_pos;
|
||||
|
||||
char **dup_array;
|
||||
char **dup_pos;
|
||||
char **dup_array;
|
||||
char **dup_pos;
|
||||
|
||||
if(array == NULL) return (char**)NULL;
|
||||
if (array == NULL)
|
||||
return (char **)NULL;
|
||||
|
||||
array_pos = array;
|
||||
|
||||
array_len = stringv_len(array);
|
||||
|
||||
if(array_len > UINT32_MAX - 1) array_len = UINT32_MAX -1;
|
||||
if (array_len > UINT32_MAX - 1)
|
||||
array_len = UINT32_MAX - 1;
|
||||
|
||||
dup_array = (char**)calloc(array_len,sizeof(char*));
|
||||
dup_array = (char **)calloc(array_len, sizeof(char *));
|
||||
dup_pos = dup_array;
|
||||
|
||||
while((*array_pos) != NULL)
|
||||
{
|
||||
*dup_pos = strdup((const char*)*array_pos);
|
||||
while ((*array_pos) != NULL) {
|
||||
*dup_pos = strdup((const char *)*array_pos);
|
||||
|
||||
array_pos++;
|
||||
dup_pos++;
|
||||
@@ -92,82 +90,89 @@ stringv_copy(char **array)
|
||||
return dup_array;
|
||||
}
|
||||
|
||||
char**
|
||||
stringv_find(char *string,char *token,int limit)
|
||||
char **stringv_find(char *string, char *token, int limit)
|
||||
{
|
||||
char **results = NULL;
|
||||
char *pos = NULL;
|
||||
int count = 0;
|
||||
char **results = NULL;
|
||||
char *pos = NULL;
|
||||
int count = 0;
|
||||
|
||||
if(string == NULL) return (char**)NULL;
|
||||
if(token == NULL) return (char**)NULL;
|
||||
if(limit == 0) return (char**)NULL;
|
||||
if (string == NULL)
|
||||
return (char **)NULL;
|
||||
if (token == NULL)
|
||||
return (char **)NULL;
|
||||
if (limit == 0)
|
||||
return (char **)NULL;
|
||||
|
||||
pos = string;
|
||||
|
||||
if(strlen(token) > strlen(string)) return (char**)NULL;
|
||||
if (strlen(token) > strlen(string))
|
||||
return (char **)NULL;
|
||||
|
||||
while(count != limit)
|
||||
{
|
||||
pos = strstr(pos,token);
|
||||
if(pos == NULL) break;
|
||||
while (count != limit) {
|
||||
pos = strstr(pos, token);
|
||||
if (pos == NULL)
|
||||
break;
|
||||
|
||||
count++;
|
||||
results = (char**)realloc((void*)results,sizeof(char*) * count + 1);
|
||||
results = (char **)realloc((void *)results, sizeof(char *) * count + 1);
|
||||
|
||||
results[count - 1] = pos;
|
||||
}
|
||||
|
||||
if(count == 0) return (char**)NULL;
|
||||
if (count == 0)
|
||||
return (char **)NULL;
|
||||
|
||||
results[count] = (char*)NULL;
|
||||
results[count] = (char *)NULL;
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
char**
|
||||
stringv_split(char *string,char *token,int limit)
|
||||
char **stringv_split(char *string, char *token, int limit)
|
||||
{
|
||||
char **results = NULL;
|
||||
char *curr = NULL;
|
||||
char *next = NULL;
|
||||
int count = 0;
|
||||
unsigned int len;
|
||||
size_t copy_len = 0;
|
||||
char **results = NULL;
|
||||
char *curr = NULL;
|
||||
char *next = NULL;
|
||||
int count = 0;
|
||||
unsigned int len;
|
||||
size_t copy_len = 0;
|
||||
|
||||
if(string == NULL) return (char**)NULL;
|
||||
if(token == NULL) return (char**)NULL;
|
||||
if(limit == 0) return (char**)NULL;
|
||||
if (string == NULL)
|
||||
return (char **)NULL;
|
||||
if (token == NULL)
|
||||
return (char **)NULL;
|
||||
if (limit == 0)
|
||||
return (char **)NULL;
|
||||
|
||||
len = strlen(string);
|
||||
if(strlen(token) > len) return (char**)NULL;
|
||||
if (strlen(token) > len)
|
||||
return (char **)NULL;
|
||||
|
||||
curr = string;
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
// alloc space for current item plus NULL vector terminator
|
||||
results = (char**)realloc(results,sizeof(char*) * (count + 2));
|
||||
results = (char **)realloc(results, sizeof(char *) * (count + 2));
|
||||
|
||||
// find the next occurrence
|
||||
next = strstr(curr,token);
|
||||
next = strstr(curr, token);
|
||||
|
||||
if(next != NULL)
|
||||
if (next != NULL)
|
||||
copy_len = next - curr;
|
||||
else
|
||||
copy_len = strlen(curr);
|
||||
|
||||
results[count] = (char*)calloc(copy_len + 1,sizeof(char));
|
||||
memcpy(results[count],curr,copy_len);
|
||||
results[count] = (char *)calloc(copy_len + 1, sizeof(char));
|
||||
memcpy(results[count], curr, copy_len);
|
||||
|
||||
count++;
|
||||
|
||||
if(next == NULL) break;
|
||||
if (next == NULL)
|
||||
break;
|
||||
|
||||
curr = next;
|
||||
curr++;
|
||||
}
|
||||
while(count < limit);
|
||||
while (count < limit);
|
||||
|
||||
results[count] = NULL;
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _STRING_V_H_
|
||||
#define _STRING_V_H_
|
||||
|
||||
@@ -25,20 +24,18 @@
|
||||
#define STRINGV_FREE_ALL 1
|
||||
|
||||
// count number of strings in a NULL in a string vector
|
||||
size_t stringv_len(char **array);
|
||||
size_t stringv_len(char **array);
|
||||
|
||||
// free all of the strings in a vector and optionally the vector pointer
|
||||
void stringv_free(char **array,int b_free);
|
||||
void stringv_free(char **array, int b_free);
|
||||
|
||||
// deep copy of string vector. returns a new vector pointer
|
||||
char** stringv_copy(char **array);
|
||||
char **stringv_copy(char **array);
|
||||
|
||||
// returns a NULL terminated vector array to every location 'token' is found
|
||||
char** stringv_find(char *string,char *token,int limit);
|
||||
char **stringv_find(char *string, char *token, int limit);
|
||||
|
||||
// returns a NULL terminated vector array of items delimited by 'token'
|
||||
char** stringv_split(char *string,char *token,int limit);
|
||||
|
||||
|
||||
char **stringv_split(char *string, char *token, int limit);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user