Ok basically I'm writing a program that takes in two directories and processes them based on what options are supplied. Thing is it's giving me segmentation errors at times when I don't see a problem. The code that is causing the problem is the following:
EDIT: Update the code to include my entire source file
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define OPTLIST "am:npruv"
#define DEFAULT_MOD_TIMES 1
typedef struct
{
/* Boolean toggle to indicate whether hidden files should be
processed or not */
bool processHiddens;
/* Integer number of seconds such that files with modification
times that differ by less than this value are considered the
same */
int timeResolution;
/* Boolean toggle to indicate whether the actual synchronisation
(file creation and overwriting) should be performed or not */
bool performSync;
/* Boolean toggle to indicate whether subdirectories should be
recursively processed or not */
bool recursive;
/* Boolean toggle to indicate whether to print the combined
directory structure or not */
bool print;
/* Boolean toggle to indicate whether modification times and
permissions should be updated or not */
bool updateStatus;
/* Boolean toggle to indicate whether verbose output should be
printed or not */
bool verbose;
/* The name of the executable */
char *programname;
} OPTIONS;
int main(int argc, char *argv[])
{
static OPTIONS options;
//static TOPLEVELS tls;
int opt;
char **paths;
/*
* Initialise default without options input.
* Done the long way to be more verbose.
*/
opterr = 0;
options.processHiddens = false;
options.timeResolution = DEFAULT_MOD_TIMES;
options.performSync = true;
options.recursive = false;
options.print = false;
options.updateStatus = true;
options.verbose = false;
options.programname = malloc(BUFSIZ);
options.programname = argv[0];
/*
* Processing options.
*/
while ((opt = getopt(argc, argv, OPTLIST)) != -1)
{
switch (opt)
{
case 'a':
options.processHiddens = !(options.processHiddens);
break;
case 'm':
options.timeResolution = atoi(optarg);
break;
case 'n':
options.performSync = !(options.performSync);
break;
case 'p':
options.print = !(options.print);
break;
case 'r':
options.recursive = !(options.recursive);
break;
case 'u':
options.updateStatus = !(options.updateStatus);
break;
case 'v':
options.verbose = !(options.verbose);
break;
default:
argc = -1;
}
}
/*
* Processing the paths array for top level directory.
*/
char **tempPaths = paths;
while (optind < argc)
{
*tempPaths++ = argv[optind++];
}
if (argc -optind + 1 < 3)
{
fprintf(stderr, "Usage: %s [-amnpruv] dir1 dir2 [dirn ... ]\n", options.programname);
exit(EXIT_FAILURE);
}
else
{
//processTopLevelDirectories(tls, paths, nDirs, options);
exit(EXIT_SUCCESS);
}
return 0;
}
I have a bash script that when runs does the following:
#!/bin/bash
clear
echo Running testing script
echo Removing old TestDirectory
rm -r ./TD
echo Creating new copy of TestDirectory
cp -r ./TestDirectory ./TD
echo Building program
make clean
make
echo Running mysync
./mysync ./TD/Dir1 ./TD/Dir2
echo Finished running testing script
However if I were to try to run the program manually using the EXACT same command:
./mysync ./TD/Dir1 ./TD/Dir2
I get a segmentation fault between test1 and test2. But if I were to append a /
to just any one of the directories, or both, then it works again. Any ideas guys?
EDIT: source_collection.h is basically all of the supporting source codes, so far they have not been implemented yet so they shouldn't cause any problems. OPTIONS is a supplied structure, thus it should be error-free. The current source is still work in progress so there's still some code missing as well as having some codes commented out. Basically at the end of the day the program aims to take in n directories with options and sync the directories.