-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
106 lines (93 loc) · 1.79 KB
/
io.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
102
103
104
105
106
#include "shell.h"
#include <stdarg.h>
/**
* printerr - print an error message to stderr
*
* @message: the message to print
*
* Return: nothing
*/
void printerr(const char *message)
{
write(STDERR_FILENO, message, _strlen(message));
}
/**
* printout - prints a message to stdout
*
* @message: the message to print
*
* Return: nothing
*/
void printout(const char *message)
{
write(STDOUT_FILENO, message, _strlen(message));
}
/**
* fprinterr - print an error message to stderr
*
* @message: the message to print
*
* Return: nothing
*/
void fprinterr(char *message)
{
write(STDERR_FILENO, message, _strlen(message));
free(message);
}
/**
* fprintout - prints a message to stdout
*
* @message: the message to print
*
* Return: nothing
*/
void fprintout(char *message)
{
write(STDOUT_FILENO, message, _strlen(message));
free(message);
}
/**
* format - resolves format specifiers in a string
* @fmt: the format string
* Return: a new string with format specifiers replaced
* with actual value
*/
char *format(const char *fmt, ...)
{
char *buffer, chr, nxt, *s;
size_t size;
int buf_index, index, i;
va_list args;
if (!fmt || !fmt[0])
return (NULL);
size = 4;
buffer = malloc(NCHARS(size));
index = buf_index = 0;
chr = fmt[index++];
nxt = fmt[index];
va_start(args, fmt);
for (index = 0; fmt[index]; index++)
{
chr = fmt[index];
nxt = fmt[index + 1];
if (chr == '%')
{
if (nxt == 'd')
{
buf_index += appendInt(&buffer, &size, va_arg(args, int), buf_index);
}
else if (nxt == 's')
{
s = va_arg(args, char *);
for (i = 0; s[i]; i++)
appendChar(&buffer, &size, s[i], buf_index++);
}
index++;
}
else
appendChar(&buffer, &size, chr, buf_index++);
}
va_end(args);
appendChar(&buffer, &size, '\0', buf_index);
return (buffer);
}