-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_misc2.c
86 lines (76 loc) · 1.39 KB
/
com_misc2.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
#include "main.h"
/**
* is_path - checks if a string is a path
* we do so just by testing if a given string starts with / or ./
* Note that this is used only after checking that the file exists
* @str: the string to check
* Return: 1 if it is a path, 0 otherwise
*/
int is_path(char *str)
{
/*char *path = _strcat("./", str);*/
int errno_cpy = errno;
if (access(str, F_OK) == 0 && (str[0] == '/' || str[0] == '.'))
return (1);
errno = errno_cpy;
/*free(path);*/
return (0);
}
/**
* isdelim - checks whether the given character is part of the delimiter
* @c: the character
* @delim: the delimeter string
* Return: 1 - if true, and 0 if not
*/
int isdelim(char c, char *delim)
{
while (*delim != '\0')
{
if (c == *delim)
return (1);
delim++;
}
return (0);
}
/**
* _strtok - extract tokens from a given string
* @src: the source string
* @delim: the delimiter
* Return: a pointer to the next toke, or NULL if there are no more tokens
*/
char *_strtok(char *src, char *delim)
{
static char *bkp;
char *ret;
if (src == NULL)
src = bkp;
if (src == NULL)
return (NULL);
while (1)
{
if (isdelim(*src, delim))
{
src++;
continue;
}
if (*src == '\0')
return (NULL);
break;
}
ret = src;
while (1)
{
if (*src == '\0')
{
bkp = src;
return (ret);
}
if (isdelim(*src, delim))
{
*src = '\0';
bkp = src + 1;
return (ret);
}
src++;
}
}