Issue
I have been trying to print the linked list the program below creates, to see how the path information is actually organized inside of it, having no success in the process.
#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);
}
I tried to use the following loop to print to the sdtout
, it compiles, but doesn't print anything actually, so, I would like to know what I'm doing wrong here, or if there's another way in which _getpathdir
function could print the final linked list to the stdout
before returning 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);
}
Solution
Although I didn't manage to print the linked list the way I wanted, I did manage to print each token that was being added to the linked list in the function *_getpathdir
by just adding printf("%s, ", token);
right below pathNode->str = token;
without doing anything else.
The output in the stdout
was something like this
/usr/local/bin, /usr/sbin, /usr/bin, /sbin, /bin,
It ain't that much but it's honest work
Answered By - NoahVerner Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.