Skip to content

Commit

Permalink
Switch Options to options_t, use hardref instead of strdup() to preve…
Browse files Browse the repository at this point in the history
…nt memory leaks.
  • Loading branch information
AvianFlu committed Feb 25, 2012
1 parent b7bb6f7 commit 06e74ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
25 changes: 12 additions & 13 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
#include <assert.h>
#include "options.h"

struct Options *Options_parse(int argc, char *argv[]) {
options_t options_parse(int argc, char *argv[]) {
assert(argc > 1);

Options *opts = malloc(sizeof(Options));
assert(opts != NULL);
options_t opts;

opts->outfile = NULL;
opts->errfile = NULL;
opts->target = NULL;
opts->json = 0;
opts->child_args = NULL;
opts.outfile = NULL;
opts.errfile = NULL;
opts.target = NULL;
opts.json = 0;
opts.child_args = NULL;

int i;
for (i = 1; i < argc; i++) {
Expand All @@ -22,21 +21,21 @@ struct Options *Options_parse(int argc, char *argv[]) {
switch((int)argv[i][1]) {
case 'o':
if (argv[i + 1][0] != '-') {
opts->outfile = strdup(argv[i + 1]);
opts.outfile = &argv[i + 1][0];
}
break;
case 'e':
if (argv[i + 1][0] != '-') {
opts->errfile = strdup(argv[i + 1]);
opts.errfile = &argv[i + 1][0];
}
break;
case 'j':
opts->json = 1;
opts.json = 1;
break;
case '-':
if (argv[i + 1] != NULL) {
opts->target = strdup(argv[i + 1]);
opts->child_args = &argv[i + 1];
opts.target = &argv[i + 1][0];
opts.child_args = &argv[i + 1];
return opts;
}
break;
Expand Down
6 changes: 3 additions & 3 deletions options.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#ifndef _options_h
#define _options_h

struct Options {
struct options_s {
char *outfile;
char *errfile;
char *target;
int json;
char **child_args;
};

typedef struct Options Options;
typedef struct options_s options_t;

struct Options *Options_parse(int argc, char *argv[]);
options_t options_parse(int argc, char *argv[]);

#endif

0 comments on commit 06e74ed

Please sign in to comment.