-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvprint.c
45 lines (42 loc) · 1.03 KB
/
envprint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "main.h"
#include "lists.h"
#include "shell.h"
/**
* _printenv - prints the environment
* @params: parameters
*
* Prints all the environment variables stored in params->env_head.
* If no environment variables exist, prints an error message.
*
* Return: void
*/
void _printenv(param_t *params)
{
if (params->tokCount != 1)
{
_printf("env: %s: No such file or directory\n",
params->args[1]);
params->status = 2;
return;
}
print_list_env(params->env_head);
params->status = 0;
}
/**
* print_list_env - print the environment variables
* @head: pointer to the head of the environment list
*
* This function takes in a pointer to the head of the environment list and
* recursively prints the list in reverse order. For each node in the list,
* the function checks if the str member is not NULL, and if so, it prints
* the variable name and value in the format "name=value".
*/
void print_list_env(list_t *head)
{
if (head)
{
print_list_env(head->next);
if (head->str)
_printf("%s=%s\n", head->str, head->val);
}
}