-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite.c
101 lines (90 loc) · 1.66 KB
/
write.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "header.h"
/**
* write_history - writes all elemets of history to file
* @head: head of linked list to print
*
* Return: number of nodes printed
*/
unsigned int write_history(history_t *head)
{
unsigned int i = 0;
while (head)
{
write_uint(head->number);
write(STDOUT_FILENO, " ", 2);
write(STDOUT_FILENO, head->command, _strlen(head->command));
head = head->next;
i++;
}
return (i);
}
/**
* write_uint - writes unsigned integers to buffer or stdout in decimal
* @n: unsigned integer to write
* Return: number of wrote to buffer
*/
int write_uint(unsigned int n)
{
unsigned int copy, size;
int nth, chars_written = 0;
size = 1;
copy = n;
if (n < 10)
{
_putchar('0' + n);
return (1);
}
while (copy / 10 != 0)
copy /= 10, size *= 10;
while (size > 0)
{
nth = n / size;
_putchar('0' + nth);
n -= nth * size;
size /= 10;
chars_written++;
}
return (chars_written);
}
/**
* _puts - prints string from pointer to string
* @str: string to print
*
* Return: void
*/
void _puts(char *str)
{
write(STDOUT_FILENO, str, _strlen(str));
_putchar('\n');
}
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* print_list - prints all elements of linked list
* @head: head of linked list to print
*
* Return: number of nodes printed
*/
size_t print_list(env_t *head)
{
unsigned int i = 0;
char **_environ;
_environ = zelda_to_ganondorf(head);
while (_environ[i])
{
_puts(_environ[i]);
free(_environ[i]);
i++;
}
free(_environ);
return (i);
}