-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprint_args.c
More file actions
41 lines (39 loc) · 724 Bytes
/
print_args.c
File metadata and controls
41 lines (39 loc) · 724 Bytes
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
#include "main.h"
/**
* print_args - prints arguments to stdout
* @args: array of arguments
* Return: 1
*/
int print_args(char **args)
{
int index = 1;
char pid[20], *envar;
int pid_len;
while (args[index] != NULL)
{
if (args[index][0] == '$')
{
envar = getenv(&args[index][1]);
if (envar != NULL)
{
write(STDOUT_FILENO, envar, _strlen(envar));
}
}
else if (_strcmp(args[index], "$$") == 0)
{
pid_len = sprintf(pid, "%d", getpid());
write(STDOUT_FILENO, pid, pid_len);
}
else
{
write(STDOUT_FILENO, args[index], _strlen(args[index]));
}
if (args[index + 1] != NULL)
{
write(STDOUT_FILENO, " ", 1);
}
index++;
}
write(STDOUT_FILENO, "\n", 1);
return (1);
}