【问题标题】:Enviroment variables split to names and values c环境变量拆分为名称和值 c
【发布时间】:2013-05-28 05:57:56
【问题描述】:

我正在尝试解析 envp 数组以获取我的程序的变量名称和值(稍后将使用),但我想我在 realloc 方面还不够好,谁能帮我解决这个问题?

void envset(char *envp[]) {
   char *name = NULL;
   char *value = NULL;
   char *temp = NULL;
   char *tok;
   int i = 0;

   while (envp[i] != 0) {
     printf("envp[%d] = %s", envp[i]);
     temp = realloc(temp, sizeof(envp[i]));
     strcpy(temp, envp[i]);
     tok = strtok(temp, "=");
     while (tok!=NULL){
         name = realloc(name, sizeof(tok));
         strcpy(name, tok);
         tok = strtok(NULL, "=");
         value = realloc(value, sizeof(tok));
         strcpy(value, tok);
         printf("Name = %s, value = %s", name, value);
     }
     change_var(name, value);
     i++;
  }
} 

【问题讨论】:

  • tok 分配到哪里? realloc(sizeof(tok)) 没有多大意义,因为 valuetok 都是 char * 指针。 tok 是结构还是?你能把所有的代码都放上来吗?
  • tok 只是 strtok 的一个标记

标签: c++ c environment-variables realloc strtok


【解决方案1】:

我可以建议一种不同的、更标准的打印方式吗?

#include <stdio.h>
#include <string.h>

extern char **environ; /* this is an external variable provided by the environment */

int main()
{
    char** env_ptr; /* declare a char** pointer that we will use to iterate through 'environ' */

    char* ptr = 0; /* this is used as a helper variable to store what 'strtok' returns */

    /* while the current string pointed to by *env_variable is not empty, increment it. */
    for (env_ptr = environ; *env_ptr != NULL; env_ptr++)
    {
        int i = 0; /* this is a control variable that is used to separate the left from the right part (divided by '=') */

        char temp[255] = ""; /* temporary string that will hold the whole current environment variable */

        strcpy(temp, *env_ptr);

        ptr = strtok(temp, "="); /* we specify the delimiters here, 'ptr' first points to the left part */

        char temp_a[255]; /* these two strings will hold the left and the right part respectively*/
        char temp_b[255];

        while (ptr != NULL) 
        {            
            if (i == 0) /* in the first iteration we get the left part so we store it */
                strcpy(temp_a, ptr);

            if (i == 1) /* in the second iteration we get the right part so we store it */
                strcpy(temp_b, ptr);

            ptr = strtok(NULL, "="); /* set 'ptr' to point to the right part,
                                        if this is already the second iteration,
                                        it will point to NULL */

            i++;
        }

        printf("%s - %s\n", temp_a, temp_b); /* you can assign temp_a and temp_b to anything you like */

    }

    return 0;
}

编辑:代码已更新,我没有您严格的详细信息,但您可以将 temp_atemp_b 分配给其成员字段

输出如下:

$ ./a.out 
LC_PAPER - en_US.UTF-8
LC_ADDRESS - en_US.UTF-8
SSH_AGENT_PID - 1311
LC_MONETARY - en_US.UTF-8
GPG_AGENT_INFO - /tmp/keyring-YUiDNO/gpg:0:1
TERM - xterm
SHELL - /bin/bash

【讨论】:

  • 嗯,我需要的不仅仅是打印它们,我需要稍后将它们放入结构中,但我的函数调用是change_var(name, value)。抱歉,我忘记在帖子里打个电话了。
  • 我已经更新了我的代码,请试试看是否有帮助
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
相关资源
最近更新 更多