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_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 *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>
|
||||
@@ -28,38 +27,41 @@
|
||||
#include "../file.h"
|
||||
#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;
|
||||
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;
|
||||
@@ -70,7 +72,8 @@ _decode_file_get_info(mfhttp *conn, void *data)
|
||||
int retval = 0;
|
||||
mffile *file;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
file = (mffile *) data;
|
||||
|
||||
@@ -87,15 +90,15 @@ _decode_file_get_info(mfhttp *conn, void *data)
|
||||
file_set_name(file, (char *)json_string_value(file_name));
|
||||
|
||||
file_hash = json_object_get(node, "hash");
|
||||
if(file_hash != NULL)
|
||||
{
|
||||
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>
|
||||
@@ -28,38 +27,41 @@
|
||||
#include "../file.h"
|
||||
#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;
|
||||
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);
|
||||
"?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;
|
||||
@@ -72,7 +74,8 @@ _decode_file_get_links(mfhttp *conn, void *data)
|
||||
int retval = 0;
|
||||
mffile *file;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
file = (mffile *) data;
|
||||
|
||||
@@ -81,12 +84,10 @@ _decode_file_get_links(mfhttp *conn, void *data)
|
||||
node = json_object_by_path(root, "response");
|
||||
|
||||
links_array = json_object_get(node, "links");
|
||||
if(!json_is_array(links_array))
|
||||
{
|
||||
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);
|
||||
|
||||
@@ -99,22 +100,20 @@ _decode_file_get_links(mfhttp *conn, void *data)
|
||||
file_set_share_link(file, (char *)json_string_value(share_link));
|
||||
|
||||
direct_link = json_object_get(node, "direct_download");
|
||||
if(direct_link != NULL)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -25,45 +24,44 @@
|
||||
#include "../mfconn.h"
|
||||
#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;
|
||||
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>
|
||||
@@ -29,11 +28,9 @@
|
||||
#include "../mfconn.h"
|
||||
#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)
|
||||
@@ -41,15 +38,18 @@ mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr)
|
||||
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)
|
||||
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;
|
||||
@@ -62,22 +62,23 @@ mfconn_api_folder_get_content(mfconn *conn, int mode, mffolder *folder_curr)
|
||||
"?folder_key=%s"
|
||||
"&content_type=%s"
|
||||
"&response_format=json",
|
||||
folderkey,
|
||||
content_type);
|
||||
|
||||
mfhttp* http = http_create();
|
||||
folderkey, content_type);
|
||||
|
||||
http = http_create();
|
||||
if (mode == 0)
|
||||
retval = http_get_buf(http, api_call, _decode_folder_get_content_folders, NULL);
|
||||
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;
|
||||
@@ -103,44 +104,39 @@ _decode_folder_get_content_folders(mfhttp *conn, void *user_ptr)
|
||||
node = json_object_by_path(root, "response/folder_content");
|
||||
|
||||
folders_array = json_object_get(node, "folders");
|
||||
if(!json_is_array(folders_array))
|
||||
{
|
||||
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++)
|
||||
{
|
||||
for (i = 0; i < array_sz; i++) {
|
||||
data = json_array_get(folders_array, i);
|
||||
|
||||
if(json_is_object(data))
|
||||
{
|
||||
if (json_is_object(data)) {
|
||||
folderkey = json_object_get(data, "folderkey");
|
||||
|
||||
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));
|
||||
|
||||
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;
|
||||
@@ -161,25 +157,21 @@ _decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
||||
node = json_object_by_path(root, "response/folder_content");
|
||||
|
||||
files_array = json_object_get(node, "files");
|
||||
if(!json_is_array(files_array))
|
||||
{
|
||||
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++)
|
||||
{
|
||||
for (i = 0; i < array_sz; i++) {
|
||||
data = json_array_get(files_array, i);
|
||||
|
||||
if(json_is_object(data))
|
||||
{
|
||||
if (json_is_object(data)) {
|
||||
quickkey = json_object_get(data, "quickkey");
|
||||
|
||||
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));
|
||||
@@ -187,7 +179,8 @@ _decode_folder_get_content_files(mfhttp *conn, void *user_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
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>
|
||||
@@ -28,38 +27,41 @@
|
||||
#include "../mfconn.h"
|
||||
#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)
|
||||
{
|
||||
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);
|
||||
"?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;
|
||||
@@ -70,7 +72,8 @@ _decode_folder_get_info(mfhttp *conn, void *data)
|
||||
int retval = 0;
|
||||
mffolder *folder;
|
||||
|
||||
if(data == NULL) return -1;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
folder = (mffolder *) data;
|
||||
|
||||
@@ -87,18 +90,18 @@ _decode_folder_get_info(mfhttp *conn, void *data)
|
||||
folder_set_name(folder, (char *)json_string_value(folder_name));
|
||||
|
||||
parent_folder = json_object_get(node, "parent_folderkey");
|
||||
if(parent_folder != NULL)
|
||||
{
|
||||
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(folderkey == NULL) retval = -1;
|
||||
if (folderkey == 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>
|
||||
|
||||
@@ -26,30 +25,30 @@
|
||||
#include "../mfconn.h"
|
||||
#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;
|
||||
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");
|
||||
|
||||
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;
|
||||
@@ -79,7 +78,8 @@ _decode_user_get_info(mfhttp *conn, void *data)
|
||||
|
||||
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>
|
||||
@@ -30,11 +29,9 @@
|
||||
#include "../mfconn.h"
|
||||
#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
|
||||
{
|
||||
struct user_get_session_token_response {
|
||||
uint32_t secret_key;
|
||||
char *secret_time;
|
||||
char *session_token;
|
||||
@@ -42,26 +39,30 @@ struct user_get_session_token_response
|
||||
|
||||
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)
|
||||
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;
|
||||
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);
|
||||
|
||||
// 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"
|
||||
post_args = strdup_printf("email=%s"
|
||||
"&password=%s"
|
||||
"&application_id=35860"
|
||||
"&signature=%s"
|
||||
@@ -69,8 +70,10 @@ mfconn_api_user_get_session_token(mfconn *conn, char *server,
|
||||
"&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,8 +86,7 @@ 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;
|
||||
@@ -94,18 +96,19 @@ _decode_get_session_token(mfhttp *conn, void *user_ptr)
|
||||
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;
|
||||
if (data == NULL)
|
||||
return -1;
|
||||
|
||||
session_token = json_object_get(data, "session_token");
|
||||
if(session_token == NULL)
|
||||
{
|
||||
if (session_token == NULL) {
|
||||
json_decref(root);
|
||||
return -1;
|
||||
}
|
||||
@@ -124,7 +127,8 @@ _decode_get_session_token(mfhttp *conn, void *user_ptr)
|
||||
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;
|
||||
}
|
||||
|
||||
155
mfapi/file.c
155
mfapi/file.c
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -29,9 +28,7 @@
|
||||
#include "../utils/strings.h"
|
||||
#include "file.h"
|
||||
|
||||
|
||||
struct mffile
|
||||
{
|
||||
struct mffile {
|
||||
char quickkey[18];
|
||||
char hash[65];
|
||||
char name[256];
|
||||
@@ -43,8 +40,7 @@ struct mffile
|
||||
char *onetime_link;
|
||||
};
|
||||
|
||||
mffile*
|
||||
file_alloc(void)
|
||||
mffile *file_alloc(void)
|
||||
{
|
||||
mffile *file;
|
||||
|
||||
@@ -53,30 +49,35 @@ file_alloc(void)
|
||||
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;
|
||||
|
||||
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);
|
||||
@@ -84,22 +85,24 @@ file_set_key(mffile *file,const char *key)
|
||||
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);
|
||||
@@ -107,21 +110,23 @@ file_set_hash(mffile *file,const char *hash)
|
||||
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);
|
||||
@@ -129,22 +134,22 @@ file_set_name(mffile *file,const char *name)
|
||||
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,16 +209,15 @@ 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;
|
||||
@@ -221,23 +225,29 @@ file_download_direct(mffile *file, char *local_dir)
|
||||
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);
|
||||
else
|
||||
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);
|
||||
|
||||
@@ -250,7 +260,8 @@ file_download_direct(mffile *file, char *local_dir)
|
||||
|
||||
free(file_path);
|
||||
|
||||
if(retval != 0) return -1;
|
||||
if (retval != 0)
|
||||
return -1;
|
||||
|
||||
bytes_read = file_info.st_size;
|
||||
|
||||
|
||||
@@ -17,15 +17,13 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "folder.h"
|
||||
|
||||
struct mffolder
|
||||
{
|
||||
struct mffolder {
|
||||
char folderkey[20];
|
||||
char name[41];
|
||||
char parent[20];
|
||||
@@ -34,8 +32,7 @@ struct mffolder
|
||||
uint32_t file_count;
|
||||
};
|
||||
|
||||
mffolder*
|
||||
folder_alloc(void)
|
||||
mffolder *folder_alloc(void)
|
||||
{
|
||||
mffolder *folder;
|
||||
|
||||
@@ -44,23 +41,25 @@ folder_alloc(void)
|
||||
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);
|
||||
@@ -68,23 +67,24 @@ folder_set_key(mffolder *folder,const char *key)
|
||||
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));
|
||||
@@ -93,21 +93,23 @@ folder_set_parent(mffolder *folder,const char *parent_key)
|
||||
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);
|
||||
@@ -115,12 +117,10 @@ folder_set_name(mffolder *folder,const char *name)
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,16 +28,15 @@
|
||||
#include "apicalls.h"
|
||||
#include "mfconn.h"
|
||||
|
||||
struct mfconn
|
||||
{
|
||||
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;
|
||||
@@ -46,8 +45,11 @@ mfconn_create(char *server, char *username, char *password, int app_id, char *ap
|
||||
|
||||
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,12 +65,12 @@ mfconn_destroy(mfconn *conn)
|
||||
free(conn);
|
||||
}
|
||||
|
||||
void
|
||||
mfconn_update_secret_key(mfconn *conn)
|
||||
void mfconn_update_secret_key(mfconn * conn)
|
||||
{
|
||||
uint64_t new_val;
|
||||
|
||||
if(conn == NULL) return;
|
||||
if (conn == NULL)
|
||||
return;
|
||||
|
||||
new_val = ((uint64_t) conn->secret_key) * 16807;
|
||||
new_val %= 0x7FFFFFFF;
|
||||
@@ -79,16 +80,17 @@ 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_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);
|
||||
@@ -98,8 +100,7 @@ mfconn_create_user_signature(mfconn *conn, char *username, char *password,
|
||||
|
||||
free(signature_raw);
|
||||
|
||||
for(i = 0;i < 20;i++)
|
||||
{
|
||||
for (i = 0; i < 20; i++) {
|
||||
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||
}
|
||||
signature_hex[40] = '\0';
|
||||
@@ -107,8 +108,8 @@ mfconn_create_user_signature(mfconn *conn, char *username, char *password,
|
||||
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
|
||||
@@ -116,28 +117,30 @@ mfconn_create_call_signature(mfconn *conn,char *url,char *args)
|
||||
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/");
|
||||
|
||||
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_time, api, args);
|
||||
|
||||
MD5((const unsigned char *)signature_raw,
|
||||
strlen(signature_raw), signature_enc);
|
||||
|
||||
free(signature_raw);
|
||||
|
||||
for(i = 0;i < 16;i++)
|
||||
{
|
||||
for (i = 0; i < 16; i++) {
|
||||
sprintf(&signature_hex[i * 2], "%02x", signature_enc[i]);
|
||||
}
|
||||
signature_hex[32] = '\0';
|
||||
@@ -145,8 +148,8 @@ mfconn_create_call_signature(mfconn *conn,char *url,char *args)
|
||||
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;
|
||||
@@ -158,15 +161,21 @@ mfconn_create_signed_get(mfconn *conn,int ssl,char *api,char *fmt,...)
|
||||
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);
|
||||
@@ -190,7 +199,8 @@ 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);
|
||||
@@ -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,16 +26,19 @@
|
||||
|
||||
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);
|
||||
|
||||
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 *password, int app_id,
|
||||
char *app_key);
|
||||
|
||||
void mfconn_update_secret_key(mfconn * conn);
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFSHELL_COMMAND_H_
|
||||
#define _MFSHELL_COMMAND_H_
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -29,20 +28,19 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
|
||||
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,7 +60,8 @@ 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);
|
||||
@@ -75,8 +74,7 @@ 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;
|
||||
@@ -85,10 +83,8 @@ _get_login_from_user(void)
|
||||
printf("login: ");
|
||||
bytes_read = getline(&login, &len, stdin);
|
||||
|
||||
if(bytes_read < 3)
|
||||
{
|
||||
if(login != NULL)
|
||||
{
|
||||
if (bytes_read < 3) {
|
||||
if (login != NULL) {
|
||||
free(login);
|
||||
login = NULL;
|
||||
}
|
||||
@@ -97,17 +93,16 @@ _get_login_from_user(void)
|
||||
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;
|
||||
struct termios old,
|
||||
new;
|
||||
|
||||
printf("passwd: ");
|
||||
|
||||
@@ -122,10 +117,8 @@ _get_passwd_from_user(void)
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
|
||||
|
||||
if(bytes_read < 3)
|
||||
{
|
||||
if(passwd != NULL)
|
||||
{
|
||||
if (bytes_read < 3) {
|
||||
if (passwd != NULL) {
|
||||
free(passwd);
|
||||
passwd = NULL;
|
||||
}
|
||||
@@ -136,4 +129,3 @@ _get_passwd_from_user(void)
|
||||
|
||||
return passwd;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -27,8 +26,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -36,7 +34,8 @@ mfshell_cmd_chdir(mfshell *mfshell, int argc, char **argv)
|
||||
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)
|
||||
{
|
||||
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);
|
||||
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,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
@@ -26,8 +25,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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);
|
||||
|
||||
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>
|
||||
|
||||
@@ -27,8 +26,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -37,7 +35,8 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
||||
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,11 +44,13 @@ 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();
|
||||
|
||||
@@ -64,19 +65,15 @@ mfshell_cmd_file(mfshell *mfshell, int argc, char **argv)
|
||||
hash = file_get_hash(file);
|
||||
|
||||
if (name != NULL && name[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"filename:", name);
|
||||
printf(" %-15.15s %s\n\r", "filename:", name);
|
||||
|
||||
if (quickkey != NULL && quickkey[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"quickkey:", quickkey);
|
||||
printf(" %-15.15s %s\n\r", "quickkey:", quickkey);
|
||||
|
||||
if (hash != NULL && hash[0] != '\0')
|
||||
printf(" %-15.15s %s\n\r",
|
||||
"hash:", hash);
|
||||
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>
|
||||
@@ -30,8 +29,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -39,7 +37,8 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
||||
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,11 +46,13 @@ 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();
|
||||
|
||||
@@ -59,25 +60,20 @@ mfshell_cmd_get(mfshell *mfshell, int argc, char **argv)
|
||||
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);
|
||||
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)
|
||||
{
|
||||
if (mfshell->local_working_dir == NULL) {
|
||||
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
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>
|
||||
@@ -27,11 +26,9 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -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,13 +64,13 @@ 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;
|
||||
@@ -88,8 +83,7 @@ _get_host_from_user(void)
|
||||
if (host == NULL)
|
||||
return strdup("www.mediafire.com");
|
||||
|
||||
if(strlen(host) < 2)
|
||||
{
|
||||
if (strlen(host) < 2) {
|
||||
free(host);
|
||||
return strdup("www.mediafire.com");
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -26,13 +25,13 @@
|
||||
#include "../mfshell.h"
|
||||
#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;
|
||||
|
||||
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>
|
||||
|
||||
@@ -27,8 +26,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -38,7 +36,8 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
||||
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,11 +45,13 @@ 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();
|
||||
|
||||
@@ -65,19 +66,15 @@ mfshell_cmd_links(mfshell *mfshell, int argc, char **argv)
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
printf(" %-15.15s %s\n\r", "1-time url:", onetime_link);
|
||||
|
||||
file_free(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
@@ -26,14 +25,14 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -51,14 +50,14 @@ mfshell_cmd_list(mfshell *mfshell, int argc, char **argv)
|
||||
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,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -26,19 +25,18 @@
|
||||
#include "../mfshell.h"
|
||||
#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)
|
||||
{
|
||||
if (mfshell->local_working_dir == NULL) {
|
||||
mfshell->local_working_dir = (char *)calloc(PATH_MAX + 1, sizeof(char));
|
||||
getcwd(mfshell->local_working_dir, PATH_MAX);
|
||||
}
|
||||
@@ -47,4 +45,3 @@ mfshell_cmd_lpwd(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
@@ -26,14 +25,14 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
|
||||
if(mfshell == NULL) return -1;
|
||||
if (mfshell == NULL)
|
||||
return -1;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
@@ -41,7 +40,8 @@ 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);
|
||||
|
||||
@@ -55,10 +55,10 @@ mfshell_cmd_mkdir(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
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,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -26,15 +25,16 @@
|
||||
#include "../../mfapi/folder.h"
|
||||
#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;
|
||||
|
||||
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);
|
||||
|
||||
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,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../../mfapi/apicalls.h"
|
||||
@@ -25,8 +24,7 @@
|
||||
#include "../../mfapi/mfconn.h"
|
||||
#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;
|
||||
@@ -41,5 +39,3 @@ mfshell_cmd_whoami(mfshell *mfshell, int argc, char **argv)
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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,21 +44,24 @@ 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"
|
||||
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");
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, "Use the \"help\" command to print a list of available\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,
|
||||
void
|
||||
parse_argv(int argc, char **argv, char **username,
|
||||
char **password, char **server, char **command)
|
||||
{
|
||||
static struct option long_options[] = {
|
||||
@@ -71,8 +72,8 @@ void parse_argv(int argc, char **argv, char **username,
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"version", no_argument, 0, 'v'}
|
||||
};
|
||||
|
||||
int c;
|
||||
|
||||
for (;;) {
|
||||
c = getopt_long(argc, argv, "c:u:p:s:hv", long_options, NULL);
|
||||
if (c == -1)
|
||||
@@ -129,7 +130,8 @@ int main(int argc,char **argv)
|
||||
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;
|
||||
|
||||
// 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,16 +182,14 @@ 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;
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
printf("\n\rmfshell > ");
|
||||
|
||||
retval = getline(&cmd, &len, stdin);
|
||||
@@ -192,14 +202,12 @@ mfshell_run(mfshell *mfshell)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -50,21 +49,24 @@ struct mfcmd commands[] = {
|
||||
{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;
|
||||
|
||||
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;
|
||||
if (strchr(server, '/') != NULL)
|
||||
return NULL;
|
||||
|
||||
shell = (mfshell *) calloc(1, sizeof(mfshell));
|
||||
|
||||
@@ -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;
|
||||
|
||||
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_
|
||||
|
||||
@@ -30,16 +29,14 @@ struct mfshell;
|
||||
typedef struct mfcmd mfcmd;
|
||||
typedef struct mfshell mfshell;
|
||||
|
||||
struct mfcmd
|
||||
{
|
||||
struct mfcmd {
|
||||
char *name;
|
||||
char *argstring;
|
||||
char *help;
|
||||
int (*handler) (mfshell * mfshell, int argc, char **argv);
|
||||
};
|
||||
|
||||
struct mfshell
|
||||
{
|
||||
struct mfshell {
|
||||
int app_id;
|
||||
char *app_key;
|
||||
char *server;
|
||||
@@ -65,4 +62,3 @@ int mfshell_exec(mfshell *mfshell, int argc, char **argv);
|
||||
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
|
||||
68
utils/http.c
68
utils/http.c
@@ -24,13 +24,16 @@
|
||||
|
||||
#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
|
||||
{
|
||||
struct mfhttp {
|
||||
CURL *curl_handle;
|
||||
char *write_buf;
|
||||
size_t write_buf_len;
|
||||
@@ -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;
|
||||
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_PROGRESSFUNCTION,
|
||||
http_progress_cb);
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
|
||||
|
||||
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
|
||||
@@ -77,26 +82,27 @@ 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,
|
||||
static int
|
||||
http_progress_cb(void *user_ptr, double dltotal, double dlnow,
|
||||
double ultotal, double ulnow)
|
||||
{
|
||||
mfhttp *conn;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
conn = (mfhttp *) user_ptr;
|
||||
|
||||
@@ -110,14 +116,17 @@ 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;
|
||||
|
||||
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_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);
|
||||
@@ -136,7 +145,8 @@ http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
{
|
||||
size_t data_len;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
|
||||
data_len = size * nmemb;
|
||||
|
||||
@@ -155,7 +165,8 @@ http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
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;
|
||||
@@ -172,14 +183,17 @@ 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;
|
||||
|
||||
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_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);
|
||||
@@ -193,15 +207,16 @@ 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;
|
||||
|
||||
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_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+");
|
||||
@@ -219,7 +234,8 @@ http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
|
||||
{
|
||||
mfhttp *conn;
|
||||
|
||||
if (user_ptr == NULL) return 0;
|
||||
if (user_ptr == NULL)
|
||||
return 0;
|
||||
conn = (mfhttp *) user_ptr;
|
||||
|
||||
fwrite(data, size, nmemb, conn->stream);
|
||||
|
||||
16
utils/http.h
16
utils/http.h
@@ -27,11 +27,17 @@ 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_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);
|
||||
|
||||
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
|
||||
|
||||
27
utils/json.c
27
utils/json.c
@@ -16,38 +16,39 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#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;
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
if (node == NULL)
|
||||
break;
|
||||
|
||||
if(!json_is_object(node))
|
||||
{
|
||||
if (!json_is_object(node)) {
|
||||
stringv_free(path_nodes, STRINGV_FREE_ALL);
|
||||
return NULL;
|
||||
}
|
||||
@@ -101,5 +102,3 @@ json_object_by_path(json_t *start,const char *path)
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _MFSHELL_JSON_H_
|
||||
#define _MFSHELL_JSON_H_
|
||||
|
||||
@@ -25,5 +24,3 @@
|
||||
json_t *json_object_by_path(json_t * start, const char *path);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
@@ -27,8 +26,7 @@
|
||||
#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.
|
||||
|
||||
@@ -52,14 +50,14 @@ strdup_printf(char* fmt, ...)
|
||||
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;
|
||||
|
||||
if(string1 == NULL || string2 == NULL) return NULL;
|
||||
if (string1 == NULL || string2 == NULL)
|
||||
return NULL;
|
||||
|
||||
string1_len = strlen(string1);
|
||||
string2_len = strlen(string2);
|
||||
@@ -74,9 +72,8 @@ strdup_join(char *string1,char *string2)
|
||||
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;
|
||||
@@ -88,25 +85,31 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
||||
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;
|
||||
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;
|
||||
@@ -120,13 +123,11 @@ strdup_subst(char *string,char *token,char *subst, unsigned int max)
|
||||
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)
|
||||
{
|
||||
if (copy_len > 0) {
|
||||
strncat(str_new, string, copy_len);
|
||||
string += copy_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)
|
||||
{
|
||||
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;
|
||||
|
||||
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,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _STR_TOOLS_H_
|
||||
#define _STR_TOOLS_H_
|
||||
|
||||
@@ -24,7 +23,8 @@ char* strdup_printf(char* fmt, ...);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -16,24 +16,22 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#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;
|
||||
|
||||
if(array == NULL) return 0;
|
||||
if (array == NULL)
|
||||
return 0;
|
||||
|
||||
pos = array;
|
||||
while(pos[0] != NULL)
|
||||
{
|
||||
while (pos[0] != NULL) {
|
||||
pos++;
|
||||
count++;
|
||||
}
|
||||
@@ -41,28 +39,27 @@ stringv_len(char **array)
|
||||
return count;
|
||||
}
|
||||
|
||||
void
|
||||
stringv_free(char **array,int b_free)
|
||||
void stringv_free(char **array, int b_free)
|
||||
{
|
||||
char **pos;
|
||||
|
||||
if(array == NULL) return;
|
||||
if (array == NULL)
|
||||
return;
|
||||
|
||||
pos = array;
|
||||
|
||||
while((*pos) != NULL)
|
||||
{
|
||||
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;
|
||||
@@ -70,19 +67,20 @@ stringv_copy(char **array)
|
||||
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_pos = dup_array;
|
||||
|
||||
while((*array_pos) != NULL)
|
||||
{
|
||||
while ((*array_pos) != NULL) {
|
||||
*dup_pos = strdup((const char *)*array_pos);
|
||||
|
||||
array_pos++;
|
||||
@@ -92,25 +90,28 @@ 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;
|
||||
|
||||
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)
|
||||
{
|
||||
while (count != limit) {
|
||||
pos = strstr(pos, token);
|
||||
if(pos == NULL) break;
|
||||
if (pos == NULL)
|
||||
break;
|
||||
|
||||
count++;
|
||||
results = (char **)realloc((void *)results, sizeof(char *) * count + 1);
|
||||
@@ -118,15 +119,15 @@ stringv_find(char *string,char *token,int limit)
|
||||
results[count - 1] = pos;
|
||||
}
|
||||
|
||||
if(count == 0) return (char**)NULL;
|
||||
if (count == 0)
|
||||
return (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;
|
||||
@@ -135,17 +136,20 @@ stringv_split(char *string,char *token,int limit)
|
||||
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));
|
||||
|
||||
@@ -162,7 +166,8 @@ stringv_split(char *string,char *token,int limit)
|
||||
|
||||
count++;
|
||||
|
||||
if(next == NULL) break;
|
||||
if (next == NULL)
|
||||
break;
|
||||
|
||||
curr = next;
|
||||
curr++;
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _STRING_V_H_
|
||||
#define _STRING_V_H_
|
||||
|
||||
@@ -39,6 +38,4 @@ 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);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user