getenv的返回值是存放在哪里的

kanwolf 2015-04-12 04:15:48
参考代码如下:
#include <stdlib.h>

#include <stdio.h>

int main(void)

{

char *s;

s=getenv("COMSPEC"); /* get the comspec environment parameter */

printf("Command processor: %s\n",s); /* display comspec parameter */

return 0;

}


来源于好搜百科
问题1:事先不需要对s进行内存分配吗?
问题2:据说getenv的返回值是存在于一个全局的二维数组中的,这个二维数组和函数中的*s又是如何联系的呢
...全文
200 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuxi 2015-04-12
  • 打赏
  • 举报
回复

char *                                                                          
getenv (name)                                                                   
     const char *name;                                                          
{                                                                               
  size_t len = strlen (name);                                                   
  char **ep;                                                                       
  uint16_t name_start;                                                             
                                                                                   
  if (__environ == NULL || name[0] == '\0')                                        
    return NULL;                                                                   
                                                                                   
  if (name[1] == '\0')                                                             
    {                                                                              
      /* The name of the variable consists of only one character.  Therefore       
     the first two characters of the environment entry are this character          
     and a '=' character.  */                                                      
#if __BYTE_ORDER == __LITTLE_ENDIAN || !_STRING_ARCH_unaligned                     
      name_start = ('=' << 8) | *(const unsigned char *) name;                     
#else                                                                              
      name_start = '=' | ((*(const unsigned char *) name) << 8);                   
#endif                                                                             
      for (ep = __environ; *ep != NULL; ++ep)                                      
    {                                                                              
#if _STRING_ARCH_unaligned                                                         
      uint16_t ep_start = *(uint16_t *) *ep;                                       
#else                                                                              
      uint16_t ep_start = (((unsigned char *) *ep)[0]                           
                   | (((unsigned char *) *ep)[1] << 8));                        
#endif                                                                          
      if (name_start == ep_start)                                               
        return &(*ep)[2];                                                       
    }                                                                           
    }                                                                           
  else                                                                          
    {                                                                           
#if _STRING_ARCH_unaligned                                                      
      name_start = *(const uint16_t *) name;                                    
#else                                                                           
      name_start = (((const unsigned char *) name)[0]                           
            | (((const unsigned char *) name)[1] << 8));                        
#endif                                                                          
      len -= 2;                                                                 
      name += 2;                                                                
                                                                                
      for (ep = __environ; *ep != NULL; ++ep)                                   
    {                                                                           
#if _STRING_ARCH_unaligned                                                      
      uint16_t ep_start = *(uint16_t *) *ep;                                    
#else                                                                           
      uint16_t ep_start = (((unsigned char *) *ep)[0]                           
                   | (((unsigned char *) *ep)[1] << 8));                        
#endif                                                                          
                                                                                
      if (name_start == ep_start && !strncmp (*ep + 2, name, len)               
          && (*ep)[len + 2] == '=')                                             
        return &(*ep)[len + 3];                                                 
    }                                                                           
    }                                                                           
                                                                                
  return NULL;                                                                  
}                                                                               
libc_hidden_def (getenv)             
这是glibc源码里面getenv的实现代码,自己看看吧。

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧