-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_flags.c
72 lines (56 loc) · 1.41 KB
/
add_flags.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
#include "main.h"
#include <stdlib.h>
int chars_before_space(char *str);
/**
* add_option - adds a flag option to a command
* @command: original string input before split
* @flag: option to be added
* @value: option to be added
*
* Return: pointer to an array of strings
*/
char *add_option(char *command, char *flag, char *value)
{
char *option = NULL;
char *new_command = NULL;
int len = 0;
if (!command || !flag || !value)
return (NULL);
/* Length of flag, value plus = sign and null byte*/
len = _strlen(flag) + _strlen(value) + 2;
option = malloc(sizeof(char *) * len);
if (!option)
return (NULL);
/* Copy flag, value and = into option variable */
_strcpy(option, flag);
_strcat(option, "=");
_strcat(option, value);
/* Create new command string with flag included */
len = _strlen(option) + _strlen(command) + 2;
new_command = malloc(sizeof(char) * len);
_strncpy(new_command, command, chars_before_space(command));
_strcat(new_command, " ");
_strcat(new_command, option);
_strcat(new_command, (command + chars_before_space(command)));
free(option);
return (new_command);
}
/**
* chars_before_space - counts the number of characters before a
* space in string
* @str: string pointer
*
* Return: number of characters or -1 on error
*/
int chars_before_space(char *str)
{
int num = 0;
if (!str)
return (-1);
while (*str && *str != ' ')
{
num++;
str++;
}
return (num);
}