-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_char.c
More file actions
95 lines (84 loc) · 1.93 KB
/
Copy pathprintf_char.c
File metadata and controls
95 lines (84 loc) · 1.93 KB
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
#include "main.h"
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1 since only one char is needed to be written.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* _writeline - write a string to the stdout followed by newline
* @str: string to write
*
* Return: number of bytes written or -1 on error
* On error, -1 is returned, and errno is set appropriately.
*/
int _writeline(char *str)
{
ssize_t bytes_written;
bytes_written = _writestring(str);
if (bytes_written > 0)
{
if (_putchar('\n') != 1)
return (-1);
return (bytes_written + 1);
}
return (-1);
}
/**
* _writestring - write a string to the stdout followed by newline
* @str: string to write
*
* Return: number of bytes written or -1 on error
* On error, -1 is returned, and errno is set appropriately.
*/
int _writestring(char *str)
{
ssize_t bytes_written;
if (str == NULL)
return (-1);
bytes_written = write(STDOUT_FILENO, str, _strlen(str));
return (bytes_written > 0 ? bytes_written : -1);
}
/**
* _str_rev - write a string to the stdout followed by newline
* @str: pointer to string
*
* Return: pointer to reversed string
*/
char *_str_rev(char *str)
{
unsigned int len = _strlen(str);
char t, *p1, *p2;
if (str == NULL) /* bad inputs include: NULL, "" */
return (NULL);
p1 = str; /* pointer to the first char in str */
p2 = str + len - 1; /* pointer to last char */
while (p2 > p1)
{
t = *p2;
*p2-- = *p1;
*p1++ = t;
}
return (str);
}
/**
* _putstring - write a string to the stdout followed by newline
* @str: string to write
*
* Return: number of bytes written or -1 on error
* On error, -1 is returned, and errno is set appropriately.
*/
int _putstring(char *str)
{
if (str == NULL)
return (write(1, "(null)", 7));
return (_writestring(str));
}