args.c 2.86 KB
Newer Older
1
2
3
4
5
6
/* -*- Mode: C; c-basic-offset:4 ; -*- */
/*
 *  (C) 2008 by Argonne National Laboratory.
 *      See COPYRIGHT in top-level directory.
 */

7
#include "hydra_utils.h"
8

9
HYD_Status HYDU_Append_env(HYD_Env_t * env_list, char **client_arg)
10
{
11
    int i, j;
12
    HYD_Env_t *env;
13
    char *envstr, *tmp[HYDU_NUM_JOIN_STR];
14
15
16
17
18
19
20
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

    for (i = 0; client_arg[i]; i++);
    env = env_list;
    while (env) {
21
        j = 0;
22

23
        tmp[j++] = MPIU_Strdup("'");
24
25
        tmp[j++] = MPIU_Strdup(env->env_name);
        tmp[j++] = MPIU_Strdup("=");
26
        tmp[j++] = env->env_value ? MPIU_Strdup(env->env_value) : MPIU_Strdup("");
27
        tmp[j++] = MPIU_Strdup("'");
28
        tmp[j++] = NULL;
29
30

        status = HYDU_String_alloc_and_join(tmp, &envstr);
31
        HYDU_ERR_POP(status, "unable to join strings\n");
32

33
34
35
36
        client_arg[i++] = MPIU_Strdup(envstr);
        HYDU_FREE(envstr);
        for (j = 0; tmp[j]; j++)
            HYDU_FREE(tmp[j]);
37

38
        env = env->next;
39
40
41
42
43
44
45
46
47
48
49
50
    }
    client_arg[i++] = NULL;

  fn_exit:
    HYDU_FUNC_EXIT();
    return status;

  fn_fail:
    goto fn_exit;
}


51
HYD_Status HYDU_Append_exec(char **exec, char **client_arg)
52
53
54
55
56
57
58
59
{
    int i, j;
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

    for (i = 0; client_arg[i]; i++);
    for (j = 0; exec[j]; j++)
60
        client_arg[i++] = MPIU_Strdup(exec[j]);
61
62
63
64
65
66
67
    client_arg[i++] = NULL;

    HYDU_FUNC_EXIT();
    return status;
}


68
HYD_Status HYDU_Append_wdir(char **client_arg, char *wdir)
69
70
71
72
73
74
75
76
{
    int arg;
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

    for (arg = 0; client_arg[arg]; arg++);
    client_arg[arg++] = MPIU_Strdup("cd");
77
    client_arg[arg++] = MPIU_Strdup(wdir);
78
79
80
81
82
83
    client_arg[arg++] = MPIU_Strdup(";");
    client_arg[arg++] = NULL;

    HYDU_FUNC_EXIT();
    return status;
}
84
85
86
87
88
89
90
91
92
93


HYD_Status HYDU_Dump_args(char **args)
{
    int arg;
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

    for (arg = 0; args[arg]; arg++)
94
95
        printf("%s ", args[arg]);
    printf("\n");
96
97
98
99

    HYDU_FUNC_EXIT();
    return status;
}
100
101


102
103
104
105
106
107
108
109
110
111
112
113
114
void HYDU_Free_args(char **args)
{
    int arg;

    HYDU_FUNC_ENTER();

    for (arg = 0; args[arg]; arg++)
        HYDU_FREE(args[arg]);

    HYDU_FUNC_EXIT();
}


115
HYD_Status HYDU_Get_base_path(char *execname, char **path)
116
{
117
    char *loc;
118
119
120
121
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

122
123
124
125
126
127
128
129
130
    *path = MPIU_Strdup(execname);
    loc = strrchr(*path, '/');
    if (loc) {
        loc++;
        *loc = 0;
    }
    else {
        HYDU_FREE(*path);
        *path = MPIU_Strdup("");
131
    }
132
133
134
135

    HYDU_FUNC_EXIT();
    return status;
}
136
137
138
139
140
141
142
143


HYD_Status HYDU_Chdir(const char *dir)
{
    HYD_Status status = HYD_SUCCESS;

    HYDU_FUNC_ENTER();

144
145
    if (chdir(dir) < 0)
        HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "chdir failed\n");
146
147
148
149
150
151
152
153

  fn_exit:
    HYDU_FUNC_EXIT();
    return status;

  fn_fail:
    goto fn_exit;
}