如何打印包含路径目录(环境变量)的链表?

How can I print a linked list that contains the path directory (environment variable)?

我一直在尝试打印下面程序创建的链表,以查看其中的路径信息实际上是如何组织的,但在这个过程中没有成功。

#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);
}

虽然我没有按照我想要的方式打印链表,但我确实设法通过添加 [=11] 打印了函数 *_getpathdir 中添加到链表的每个标记=] 就在 pathNode->str = token; 下面,不做任何其他事情。

stdout 中的输出是这样的

/usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin,

虽然不多但是很诚恳