1
1

Add a function to prepend a string to the beginning of an argv array - useful when building app_contexts from user input

This commit was SVN r23303.
Этот коммит содержится в:
Ralph Castain 2010-06-24 15:52:36 +00:00
родитель c22e6b3613
Коммит f325ac030a
2 изменённых файлов: 45 добавлений и 0 удалений

Просмотреть файл

@ -89,6 +89,40 @@ int opal_argv_append_nosize(char ***argv, const char *arg)
return OPAL_SUCCESS;
}
int opal_argv_prepend_nosize(char ***argv, const char *arg)
{
int argc;
int i;
/* Create new argv. */
if (NULL == *argv) {
*argv = (char**) malloc(2 * sizeof(char *));
if (NULL == *argv) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
(*argv)[0] = strdup(arg);
(*argv)[1] = NULL;
} else {
/* count how many entries currently exist */
argc = opal_argv_count(*argv);
*argv = (char**) realloc(*argv, (argc + 2) * sizeof(char *));
if (NULL == *argv) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
(*argv)[argc+1] = NULL;
/* shift all existing elements down 1 */
for (i=argc; 0 < i; i--) {
(*argv)[i] = (*argv)[i-1];
}
(*argv)[0] = strdup(arg);
}
return OPAL_SUCCESS;
}
int opal_argv_append_unique_nosize(char ***argv, const char *arg, bool overwrite)
{
int i;

Просмотреть файл

@ -85,6 +85,17 @@ OPAL_DECLSPEC int opal_argv_append(int *argc, char ***argv, const char *arg) __
*/
OPAL_DECLSPEC int opal_argv_append_nosize(char ***argv, const char *arg);
/**
* Insert the provided arg at the beginning of the array
*
* @param argv Pointer to an argv array
* @param str Pointer to the string to prepend
*
* @retval OPAL_SUCCESS On success
* @retval OPAL_ERROR On failure
*/
OPAL_DECLSPEC int opal_argv_prepend_nosize(char ***argv, const char *arg);
/**
* Append to an argv-style array, but only if the provided argument
* doesn't already exist somewhere in the array. Ignore the size of the array.