maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_split.c File Reference
#include "libft.h"
Include dependency graph for ft_split.c:

Go to the source code of this file.

Functions

static int count_words (const char *s, char c)
 
static char * strndup (const char *s, size_t n)
 
static void split_into_words (char **result, const char *s, char c)
 
char ** ft_split (const char *s, char c)
 

Function Documentation

◆ count_words()

static int count_words ( const char *  s,
char  c 
)
static

Definition at line 32 of file ft_split.c.

33{
34 int count;
35 int in_word;
36
37 count = 0;
38 in_word = 0;
39 while (*s)
40 {
41 if (*s != c)
42 {
43 if (!in_word)
44 {
45 in_word = 1;
46 count++;
47 }
48 }
49 else
50 in_word = 0;
51 s++;
52 }
53 return (count);
54}

Referenced by ft_split().

Here is the caller graph for this function:

◆ ft_split()

char ** ft_split ( const char *  s,
char  c 
)

Definition at line 95 of file ft_split.c.

96{
97 char **result;
98 int word_count;
99
100 if (!s)
101 return (NULL);
102 word_count = count_words(s, c);
103 result = malloc(sizeof(char *) * (word_count + 1));
104 if (!result)
105 return (NULL);
106 split_into_words(result, s, c);
107 return (result);
108}
static void split_into_words(char **result, const char *s, char c)
Definition ft_split.c:68
static int count_words(const char *s, char c)
Definition ft_split.c:32

References count_words(), and split_into_words().

Referenced by builtin_pipe_test(), ft_find_path(), process_and_reassemble(), and split_loc_vars().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ split_into_words()

static void split_into_words ( char **  result,
const char *  s,
char  c 
)
static

Definition at line 68 of file ft_split.c.

69{
70 int i;
71 int in_word;
72 const char *start;
73
74 i = 0;
75 in_word = 0;
76 while (*s)
77 {
78 if (*s != c && !in_word)
79 {
80 start = s;
81 in_word = 1;
82 }
83 else if (*s == c && in_word)
84 {
85 result[i++] = strndup(start, s - start);
86 in_word = 0;
87 }
88 s++;
89 }
90 if (in_word)
91 result[i++] = strndup(start, s - start);
92 result[i] = NULL;
93}
static char * strndup(const char *s, size_t n)
Definition ft_split.c:56

References strndup().

Referenced by ft_split().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ strndup()

static char * strndup ( const char *  s,
size_t  n 
)
static

Definition at line 56 of file ft_split.c.

57{
58 char *dup;
59
60 dup = malloc(n + 1);
61 if (!dup)
62 return (NULL);
63 ft_memcpy(dup, s, n);
64 dup[n] = '\0';
65 return (dup);
66}
void * ft_memcpy(void *dest, const void *src, size_t n)
Definition ft_memcpy.c:22

References ft_memcpy().

Referenced by split_into_words().

Here is the call graph for this function:
Here is the caller graph for this function: