I have a tricky one here I just can't seem to figure out. When using the whole if/else-if/else-if
it segfaults if the argument in the vector doesn't exist. If the argument exists then it runs fine it seems. When I comment out one of the else-if
blocks then it runs ok it seems also.
Edited:
void
priority_arg_create (gchar *arg)
{
gchar **argvp;
gint argcp;
gint i;
gint j;
argvp = get_arg_vector ();
argcp = 0;
i = 0;
/* obtain arg count not including the `NULL` terminator. */
while (argvp[argcp])
{
argcp++;
}
g_print ("From `rt_priority.c` line 42: %d\n", argcp);
/* Here we look for the jack arg `-Px` with the for loop then the if
statement. If it does not exist then we add it to arg vector with
`else/if` statement. */
for (i = 0; i <= argcp; i++)
{
if ((i == argcp -1) && (strncmp (argvp[i], "-P", 2) != 0))
{
g_print ("From `rt_priority.c` line 65\n");
/* Add space to arg vector for the jackd arg `-R`. */
argcp = argcp + 1;
/* If realtime arg exists then place priority arg right
after that.*/
if ((strncmp (argvp[1], "-r", 2) == 0) ||
(strncmp (argvp[1], "-R", 2) == 0))
{
/* Here we move the args over one to place `-Px` as the
third arg in the vector. */
for (j = argcp; j >= 2; j--)
{
argvp[j] = argvp[j - 1];
}
argvp[2] = arg;
}
else
{
/* Here we move the args over one to place `-Px` as the
second arg in the vector. */
for (j = argcp; j >= 1; j--)
{
argvp[j] = argvp[j - 1];
}
argvp[1] = arg;
}
break;
}
else if (g_strcmp0 (argvp[i], arg) == 0)
{
g_print ("From `rt_priority.c` line 51: %d\n", i);
break;
}
/* If `priority arg` is found but the number does not match
execute else/if statement. */
else if (strncmp (argvp[i], "-P", 2) == 0)
{
argvp[i] = arg;
break;
}
}
file_input (argvp, argcp);
}
And here's the output of g_print ()
inside the for ()
loop
with line 56
showing argvp
and line 57
showing argcp
:
From `rt_priority.c` line 42: 6
From `rt_priority.c` line 56: /usr/bin/jackd
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -dalsa
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -dhw:M2496
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -r48000
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -p128
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: -n2
From `rt_priority.c` line 57: 6
From `rt_priority.c` line 56: (null)
From `rt_priority.c` line 57: 6
Segmentation fault (core dumped)
Edited:
Here's the get_arg_vector ()
function:
gchar **argvp
get_arg_vector ()
{
gchar cmd[128];
gchar **argvp;
gchar *contents;
gint argcp;
gsize size;
/* Create path to file `.jackdrc` using `g_sprintf ()`. */
g_sprintf (cmd, "%s/.jackdrc", g_getenv ("HOME"));
/* Check if file path exists. */
if (g_file_test (cmd, G_FILE_TEST_EXISTS) == FALSE)
{
g_print ("File doesn't exist. Create file.");
return NULL;
}
g_file_get_contents (cmd, &contents, &size, NULL);
g_shell_parse_argv (contents, &argcp, &argvp, NULL);
return argvp;
}