如何打印包含路径目录(环境变量)的链表?
Posted
技术标签:
【中文标题】如何打印包含路径目录(环境变量)的链表?【英文标题】:How can I print a linked list that contains the path directory (environment variable)? 【发布时间】:2022-01-10 19:44:14 【问题描述】:我一直在尝试打印下面程序创建的链表,以查看路径信息在其中的实际组织方式,但在此过程中没有成功。
#define TRUE 1
#define FALSE 0
extern char **environ;
/*
* struct Node - singly linked list
* @str: string - (malloc'ed string)
* @next: points to the next node
* Description: singly linked list node structure
*/
typedef struct Node
char *str;
struct Node *next;
/**
* _getenv - searches for the environment string
* pointed to by name
* @name: This is the C string containing the name
* of the requested variable
* Return: the associated value to the string.
*/
char *_getenv(const char *name)
int i, j; /* Counters */
int status; /* boolean variable */
for (i = 0; environ[i] != NULL; i++) /* loop over the array of pointers to strings called "environment" until it finds a string value called NULL (the last one) */
status = 1; /* initialize the status variable as TRUE (1) */
for (j = 0; environ[i][j] != '='; j++) /* loop over the the current string value until the symbol '=' is found */
if (name[j] != environ[i][j]) /* Check if each byte of the current string value is exactly the same as name */
status = 0; /* if they are not, we notify (set the status variable as FALSE (0)) and break */
break;
if (status) /* IF and ONLY IF we have found that each byte of the current string value is exactly the same as name */
return (&environ[i][j + 1]); /* return the address of the current string value excluding the "=" sign */
return (NULL); /* This function returns NULL if the environment variable requested does not exist */
/* if the previous return was successfully executed, this return is not executed */
/**
* _getpathdir - creates a linked list for
* any environment string
* @path: the selected environment string
* @pathCopy: a duplicate of @path
* Return: a linked list of @path
*/
Node *_getpathdir(char *path, char **pathCopy)
char *token = NULL;
Node *head;
Node *pathNode;
if (path == NULL) /* If path passed is NULL, return NULL */
return (NULL);
*pathCopy = strdup(path); /* Make a duplicate of path parameter */
head = NULL; /* Initialize the very first token of the linked list to NULL */
pathNode = malloc(sizeof(Node)); /* This allocates pathNode for its use in the very first head */
if (pathNode == NULL) /* If there's not enough memory to allocate pathNode, return NULL */
return (NULL);
token = strtok(*pathCopy, ":"); /* Break the pathCopy string into tokens, separator used is ':' */
pathNode->str = token; /* The first element to add to the linked list is token */
pathNode->next = head; /* The "next" element to add to the linked list will be the new head */
head = pathNode; /* Assign pathNode to the head */
while (token != NULL) /* Fill the linked list with the rest of the string */
token = strtok(NULL, ":"); /* By passing NULL as the first parameter to strtok(), we make sure that strtok() keeps working with */
/* the previous parameter (pathCopy) */
if (token == NULL) /* Don't save token NULL in list */
break;
pathNode = malloc(sizeof(Node)); /* This allocates pathNode for its use in the rest of the heads */
if (pathNode == NULL) /* If there are no more tokens left to add to the linked list, return NULL */
return (NULL);
pathNode->str = token; /* Add to the linked list the current token */
pathNode->next = head; /* The "next" element to add to the linked list will be the new head */
head = pathNode; /* Assign pathNode to the head */
return (head);
/**
* listpath - Calls the functions _getenv and _getpathdir
* @pathCopy: A variable that will store a duplication
* of the "PATH" parameter
* Return: A linked list of PATH directories
*/
Node *listpath(char **pathCopy)
char *getEnv;
Node *pathDirs;
getEnv = _getenv("PATH");
pathDirs = _getpathdir(getEnv, pathCopy); /* Here pathCopy is passed as the address of itself pointing to NULL, i.e. `char *pathCopy = NULL` */
return (pathDirs);
我尝试使用以下循环打印到sdtout
,它编译,但实际上不打印任何内容,所以,我想知道我在这里做错了什么,或者是否有另一种方法其中_getpathdir
函数可以在返回head
之前将最终的链表打印到stdout
int main()
Node *head = NULL;
Node *current_node = head;
char *pathCopy = NULL;
listpath(&pathCopy);
while ( current_node != NULL)
printf("\n %s\n", current_node->str);
current_node = current_node->next;
return (0);
【问题讨论】:
请以minimal reproducible example 的形式提供实际代码。目前main
甚至没有调用任何函数,所以它当然不会打印任何东西,因为列表是空的。
我明白了,我可以在 main
函数中的 while 循环之前键入 char *pathCopy = NULL
后跟 listpath(char **pathCopy)
以打印链接列表吗?
你为什么不试试呢?
我刚做了,没用。它仍然可以编译,但不打印任何内容。
listpath(char **pathCopy)
不可能是您尝试过的真实代码,因为它甚至无法编译。请更新问题以显示您尝试过的实际代码。
【参考方案1】:
虽然我没有按照我想要的方式打印链接列表,但我确实设法打印了在函数 *_getpathdir
中添加到链接列表中的每个标记,只需在 printf("%s, ", token);
正下方添加 printf("%s, ", token);
@ 不做任何其他事情。
stdout
中的输出是这样的
/usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin,
虽然不多,但却是诚实的工作
【讨论】:
以上是关于如何打印包含路径目录(环境变量)的链表?的主要内容,如果未能解决你的问题,请参考以下文章