I'm trying to grab one of the function arguments and store it in a char array.
The problem with your code is that you don't have a character array in which to store the copied argument. What you have is a pointer. You must allocate storage in which to copy the argument. Or, you can make rootdir
be an array instead of a pointer, but you must be careful to prevent the copy from overflowing the fixed-size array.
Allocate storage
bb_data->rootdir = malloc(strlen(argv[argc - 2]) + 1); // +1 is for the NULL
Make rootdir an array
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
...
struct bb_state {
FILE *logfile;
char rootdir [PATH_MAX];
};
struct bb_state *bb_data;
size_t len;
len = strlen(argv[argc - 2]);
if (len >= PATH_MAX) {
// Argument is too long.
fprintf(stderr, "Argument is too long: %s\n", argv[argc - 2]);
return EXIT_FAILURE;
}
bb_data = malloc(sizeof(struct bb_state));
strcpy(bb_data->rootdir, argv[argc - 2]);