Fixed allocation of argument array

xethyrion-master
jmjatlanta 2016-10-26 22:32:23 -05:00
parent a38c3a8253
commit bc9bfff86e
3 changed files with 14 additions and 11 deletions

View File

@ -1,22 +1,30 @@
#include <stdlib.h>
#include "init.h"
int get_init_command(struct command* cmd) {
// help text
cmd->help_text.tagline = "Initializes IPFS config file.";
cmd->help_text.short_description = "\nInitializes IPFS configuration files and generates a new keypair.\n\nipfs uses a repository in the local file system. By default, the repo is\nlocated at ~/.ipfs. To change the repo location, set the $IPFS_PATH\nenvironment variable.:\n\n export IPFS_PATH=/path/to/ipfsrepo";
// arguments
// initialize an array of arguments with size of 1
struct argument argument;
int retVal = init_string_argument(&argument, "default-config", 0, 0, "Initialize with the given configuration");
argument.enable_stdin = 1;
struct argument* array[] = { &argument };
cmd->arguments = array;
cmd->argument_count = 1;
// allocate memory for array of pointers
cmd->arguments = malloc(cmd->argument_count * sizeof(struct argument*));
if (cmd->arguments == NULL)
return 0;
// allocate memory for argument
cmd->arguments[0] = malloc(sizeof(struct argument));
int retVal = init_string_argument(cmd->arguments[0], "default-config", 0, 0, "Initialize with the given configuration");
cmd->arguments[0]->enable_stdin = 1;
return retVal;
}
int uninit_command(struct command* cmd) {
for(int i = 0; i < cmd->argument_count; i++)
uninit_argument(cmd->arguments[i]);
free(cmd->arguments);
return 0;
}

View File

@ -4,15 +4,10 @@
#include "argument.h"
int uninit_argument(struct argument* argument) {
free(argument->name);
return 1;
}
int init_argument(struct argument* argument, char* name, int required, int variadic, char* description) {
argument->name = name == NULL ? NULL : strdup(name);
if (argument->name == NULL)
return 0;
strncpy(argument->name, name, strlen(name));
argument->name = name;
argument->required = required;
argument->variadic = variadic;

View File

@ -3,7 +3,7 @@
#include <stdint.h>
const char* datastore_default_directory = "datastore";
//const char* datastore_default_directory = "datastore";
struct datastore {
char* type;