-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.c
More file actions
149 lines (115 loc) · 2.18 KB
/
Copy pathprint.c
File metadata and controls
149 lines (115 loc) · 2.18 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 "shell.h"
/**
* put_char - write char ch in stdout
* @ch: a char to print
* Return: if success return 1.if error, -1
*/
int put_char(char ch)
{
return (write(1, &ch, 1));
}
/**
* printstr_ - prints string
* @st: string to be printed
* Return: void
*/
void printstr_(char *st)
{
int i, byt, w;
for (i = 0; st[i] != '\0'; i++)
;
byt = i;
w = write(STDOUT_FILENO, st, byt);
if (w == EOF)
return;
}
/**
* path_str - function that prints path string
* @p_right: string after "PATH ="
* @xfirst: first tokenized word
* Return: 0 for success
*/
char *path_str(char *p_right, char *xfirst)
{
char *new = NULL;
char *token = NULL;
int token_len = 0, xfirst_len = 0;
token = p_right;
token_len = str_len(token);
xfirst_len = str_len(xfirst);
new = malloc((token_len + xfirst_len + 2) * sizeof(char));
if (new == NULL)
return (NULL);
new[0] = '\0';
str_cat(new, p_right);
str_cat(new, "/");
str_cat(new, xfirst);
str_cat(new, "\0");
return (new);
}
/**
* print_int - prints int
* @tally: pointer to tally nbr
* Return: void
*/
void print_int(int *tally)
{
int cnt = 0, lent = 0, j, nb;
unsigned int base = 1, dd, max;
nb = *tally;
max = nb;
dd = max;
do {
dd /= 10;
++lent;
} while (dd != 0);
cnt += lent;
for (j = 0; j < lent - 1; j++)
base = base * 10;
put_char('0' + (max / base));
if (lent > 1)
{
for (j = 0; j < lent - 2; j++)
{
base /= 10;
dd = max / base;
put_char('0' + dd % 10);
}
put_char('0' + (max % 10));
}
}
/**
* _parser - function takes string frm command line,returns string
* using space as delim
* @l: Charter pter storing user inpt
* Return: char pointers contain an arg
*
*/
char **_parser(char *l)
{
char **args;
char *parss = NULL;
char *parss2 = NULL;
char *lin_cp = NULL;
int arg_num = 0, i = 0;
lin_cp = str_dup(l);
parss = strtok(lin_cp, " \t");
while (parss != NULL)
{
arg_num++;
parss = strtok(NULL, " \t");
}
args = malloc(sizeof(char *) * (arg_num + 1));
if (args == NULL)
return (NULL);
parss2 = strtok(l, " \t");
while (parss2 != NULL)
{
args[i] = parss2;
parss2 = strtok(NULL, " \t");
i++;
}
args[i] = NULL;
free(lin_cp);
return (args);
}