forked from BD20171998/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf.c
More file actions
148 lines (115 loc) · 2.33 KB
/
Copy pathprintf.c
File metadata and controls
148 lines (115 loc) · 2.33 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "holberton.h"
/**
* _putchar - writes the character c to stdout
* @c: The character to print
* Return: On success 1. On error, -1 is returned..
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
/**
* print_str - prints string
* @s: string to be printed
* Return: void
*/
void print_str(char *s)
{
int i, bytes, wc;
for (i = 0; s[i] != '\0'; i++)
;
bytes = i;
wc = write(STDOUT_FILENO, s, bytes);
if (wc == EOF)
return;
}
/**
* pathstr - function that prints the path string
* @right: string after "PATH ="
* @first: first tokenized word
* Return: 0 for success
*/
char *pathstr(char *right, char *first)
{
char *new = NULL;
char *token = NULL;
int token_len = 0, first_len = 0;
token = right;
token_len = _strlen(token);
first_len = _strlen(first);
new = malloc((token_len + first_len + 2) * sizeof(char));
if (new == NULL)
return (NULL);
new[0] = '\0';
_strcat(new, right);
_strcat(new, "/");
_strcat(new, first);
_strcat(new, "\0");
return (new);
}
/**
* print_int - prints an integer
* @tally: pointer to the tally number
* Return: void
*/
void print_int(int *tally)
{
int count = 0, length = 0, j, n;
unsigned int base = 1, d, max;
n = *tally;
max = n;
d = max;
do {
d /= 10;
++length;
} while (d != 0);
count += length;
for (j = 0; j < length - 1; j++)
base = base * 10;
_putchar('0' + (max / base));
if (length > 1)
{
for (j = 0; j < length - 2; j++)
{
base /= 10;
d = max / base;
_putchar('0' + d % 10);
}
_putchar('0' + (max % 10));
}
}
/**
* parser - function that takes a string from the command line and returns the
* string as a parsed double pointer using a space as the delimiter
* @l: Char pointer storing user input
* Return: Char double pointer comprised of a char pointers that each contain
* an argument
*/
char **parser(char *l)
{
char **args;
char *parsed = NULL;
char *parsed2 = NULL;
char *linecopy = NULL;
int arg_num = 0, i = 0;
linecopy = _strdup(l);
parsed = strtok(linecopy, " \t");
while (parsed != NULL)
{
arg_num++;
parsed = strtok(NULL, " \t");
}
args = malloc(sizeof(char *) * (arg_num + 1));
if (args == NULL)
return (NULL);
parsed2 = strtok(l, " \t");
while (parsed2 != NULL)
{
args[i] = parsed2;
parsed2 = strtok(NULL, " \t");
i++;
}
args[i] = NULL;
free(linecopy);
return (args);
}