MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / _args_override_argv0

Function _args_override_argv0

components/lwp/lwp_args.c:152–219  ·  view source on GitHub ↗

* @brief Override arguments 0 for script interpreter. * * @param[in,out] ai Pointer to the target argument info structure to be modified. * @param[in] ow_ai Pointer to the source argument info structure containing arguments to override * argv[0] with. * * @return rt_err_t RT_EOK on success, -RT_EINVAL for invalid input, -RT_ENOMEM on memory allocation failure * * @note Man

Source from the content-addressed store, hash-verified

150 * execve() call.
151 */
152static rt_err_t _args_override_argv0(struct lwp_args_info *ai, struct lwp_args_info *ow_ai)
153{
154 rt_err_t error = 0;
155 int i, new_argc, new_strbuf_size, ai_bytes_tobe_copied;
156 char **new_argv, *new_strbuf, *base;
157 rt_base_t off;
158
159 if (ow_ai == 0 || ow_ai->argv.string_count == 0)
160 {
161 return -RT_EINVAL;
162 }
163
164 /* for new argument vector */
165 new_argc = ai->argv.string_count - 1 + ow_ai->argv.string_count;
166 new_argv = rt_malloc(new_argc * sizeof(char *));
167 if (!new_argv)
168 {
169 return -RT_ENOMEM;
170 }
171
172 /* for new string buffer */
173 ai_bytes_tobe_copied = ai->strings_length - (ai->argv0_strlen + 1);
174 new_strbuf_size = ai_bytes_tobe_copied + ow_ai->strings_length;
175 new_strbuf = rt_malloc(new_strbuf_size);
176 if (!new_argv)
177 {
178 rt_free(new_argv);
179 return -RT_ENOMEM;
180 }
181
182 base = new_strbuf;
183 off = base - ow_ai->str_buf;
184 /* copy overriding argument strings and argv */
185 memcpy(base, ow_ai->str_buf, ow_ai->strings_length);
186 for (i = 0; i < ow_ai->argv.string_count; i++)
187 {
188 /* base + ow_ai->argv.strvec[i] - ow_ai->str_buf */
189 new_argv[i] = (char *)ow_ai->argv.strvec[i] + off;
190 }
191
192 base += ow_ai->strings_length;
193 off = base - (ai->str_buf + ai->argv0_strlen + 1);
194 /* copy old argument strings starting from argv[1] and setup new_argv */
195 memcpy(base, ai->str_buf + ai->argv0_strlen + 1, ai_bytes_tobe_copied);
196 for (size_t j = 1; j < ai->argv.string_count; i++, j++)
197 {
198 /* base + ai->argv->strvec[j] - ai->str_buf */
199 new_argv[i] = (char *)ai->argv.strvec[j] + off;
200 }
201
202 /* setup envp for ai */
203 for (i = 0; i < ai->envp.string_count; i++)
204 {
205 /* base + ai->envp->strvec[i] - ai->str_buf */
206 ai->envp.strvec[i] += off;
207 }
208
209 /* replace strings buffer and argv buffer */

Callers 1

lwp_args_load_scriptFunction · 0.85

Calls 2

rt_mallocFunction · 0.85
rt_freeFunction · 0.85

Tested by

no test coverage detected