He estado tratando de imprimir la lista vinculada que crea el programa a continuación, para ver cómo la información del PATH está realmente organizada dentro de ella, sin éxito en el proceso.
#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");
_getpathdir(getEnv, pathCopy); /* Here pathCopy is passed as the address of itself pointing to NULL, i.e. `char *pathCopy = NULL` */
}
Intenté usar el siguiente bucle while en la función main para imprimir al sdtout, él compila, pero no imprime nada en realidad, así que me gustaría saber qué estoy haciendo mal aquí, o si hay otra forma de hacer que la función _getpathdir imprima la lista vinculada al stdout antes de regresar head
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);
}
Sí existe otra forma de crear una lista enlazada que contenga el PATH dividido en varios tokens, e imprimirla luego, es bienvenida.