-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
90 lines (84 loc) · 1.65 KB
/
helper.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
#include "holberton.h"
/**
* _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));
}
/**
* _putstr - writes string to stdout
* @s: String to print
* Return: number of chars printed
**/
int _putstr(char *s)
{
int len;
for (len = 0; s[len]; len++)
_putchar(s[len]);
return (len);
}
/**
* _strdup - make a copy of a string
* @src: source string
* Return: a string that is a copy of the source string
**/
char *_strdup(const char *src)
{
char *str, *tmp;
char *p;
int len = 0;
if (!src)
return (NULL);
while (src[len])
len++;
str = malloc(len + 1);
if (!str)
exit(EXIT_FAILURE);
p = tmp = str;
while (*src)
*p++ = *src++;
*p = '\0';
return (tmp);
}
/**
* init_params - initialize all elements of a param struct
* @p: pointer to a list of parameters
* @format: a format string that printf will work on
* @ops: a group of pointers to operator structs
* Return: 0 = EXIT_SUCCESS
**/
int init_params(params_t *p, const char *format, op_t **ops)
{
p->ops = *ops;
p->dex = 0;
p->counter = 0;
p->format = _strdup(format);
/* TODO: extra copy of format will need to be freed */
return (0);
}
/**
* choose_op - pick an op function and return it
* @p: struct of parameters
* @valist: pointer into variadic list
* Return: int 0 = EXIT_SUCCESS
**/
int choose_op(params_t *p, va_list valist)
{
int j = 0;
int counter = 0;
while (p->ops[j].f)
{
if (p->ops[j].op[0] == p->format[p->dex])
{
counter = p->ops[j].f(valist);
break;
}
j++;
}
(p->dex)++;
return (counter);
}