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

Function lwp_get_envp

components/lwp/lwp_args.c:900–962  ·  view source on GitHub ↗

* @brief Get environment variables of light-weight process * * @param[in] lwp Pointer to the light-weight process structure * @param[out] penvp_counts Pointer to store the number of environment variables * * @return char** Returns a NULL-terminated array of environment variable strings on success: * - The array and each string are allocated in kernel space * - The caller is

Source from the content-addressed store, hash-verified

898 * RT_NULL Returns NULL on failure (invalid LWP, memory allocation failure, or copy error)
899 */
900char** lwp_get_envp(struct rt_lwp *lwp, rt_size_t *penvp_counts)
901{
902 int ret, len;
903 rt_base_t argc;
904 char **p_kenvp = RT_NULL;
905 char *envp, **p_envp;
906 size_t envp_counts = 0;
907
908 if (lwp)
909 {
910 ret = lwp_data_get(lwp, &argc, lwp->args, sizeof(argc));
911 if (ret == 0)
912 {
913 return RT_NULL;
914 }
915 p_envp = (char **)lwp->args + 1 + argc + 1;
916
917 /* counts envp */
918 while (lwp_data_get(lwp, &envp, p_envp, sizeof(void *)) == sizeof(void *)
919 && envp != NULL)
920 {
921 p_envp++;
922 envp_counts++;
923 }
924
925 p_kenvp = (char **)rt_malloc((envp_counts + 1) * sizeof(char *));
926 if (p_kenvp)
927 {
928 /* copy env from envp array */
929 p_envp = (char **)lwp->args + 1 + argc + 1;
930 for (size_t i = 0; i < envp_counts; i++)
931 {
932 ret = lwp_data_get(lwp, &envp, &p_envp[i], sizeof(char *));
933 if (ret != sizeof(char **))
934 {
935 lwp_free_command_line_args(p_kenvp);
936 return RT_NULL;
937 }
938
939 len = lwp_user_strlen_ext(lwp, envp);
940 if (len > 0)
941 {
942 p_kenvp[i] = (char*)rt_malloc(len + 1);
943 ret = lwp_data_get(lwp, p_kenvp[i], envp, len + 1);
944 if (ret != len + 1)
945 {
946 lwp_free_command_line_args(p_kenvp);
947 return RT_NULL;
948 }
949 }
950 else
951 {
952 p_kenvp[i] = NULL;
953 }
954 }
955 if (penvp_counts)
956 *penvp_counts = envp_counts;
957 p_kenvp[envp_counts] = NULL;

Callers 1

lwp_print_envpFunction · 0.85

Calls 4

lwp_data_getFunction · 0.85
rt_mallocFunction · 0.85
lwp_user_strlen_extFunction · 0.85

Tested by

no test coverage detected