-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_utils.c
108 lines (97 loc) · 1.67 KB
/
str_utils.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
107
108
#include "shell.h"
/**
* _putchar - Outputs character to stdout
* @c: Character
* @fd: Std file descriptor
*
* Return: 0 (Success)
*/
int _putchar(char c, int fd)
{
write(fd, &c, 1);
return (0);
}
/**
* _print - Print string to stdout
* @string: String
* @fd: Std file description
*/
void _print(char *string, int fd)
{
int index = 0;
while (string[index] != '\0')
{
_putchar(string[index], fd);
index++;
}
}
/**
* prompt - Shell (hsh) prompt
*/
void prompt(void)
{
char *alert = "($) ";
_print(alert, STDOUT_FILENO);
}
/**
* _dprintf - Display a formated string to file descriptor
* @fd: File descriptor
* @format: Specified string format
*
* Return: Number of character displayed
*/
int _dprintf(int fd, const char *format, ...)
{
int f_index = 0, num;
/*int count = num_str_flags(format), num;*/
char *string;
va_list args;
va_start(args, 0);
while (format[f_index] != '\0')
{
if (format[f_index] == '%')
{
if (format[f_index + 1] == 'd')
{
num = va_arg(args, int);
number_to_string(num, fd);
f_index += 2;
continue;
}
else if (format[f_index + 1] == 's')
{
string = va_arg(args, char*);
_print(string, fd);
f_index += 2;
continue;
}
else
{
_putchar(format[f_index], fd);
f_index++;
continue;
}
}
_putchar(format[f_index], fd);
f_index++;
continue;
}
va_end(args);
return (EXIT_SUCCESS);
}
/**
* num_str_flags - Counts the number of flags in a string
* @format: String
*
* Return: Number of format flag
*/
int num_str_flags(const char *format)
{
int i, count = 0;
for (i = 0; format[i] != '\0'; i++)
{
if (format[i] == '%')
count++;
}
return (count);
}