-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_handler.c
82 lines (71 loc) · 1.37 KB
/
string_handler.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_handler.h"
int pipeAreNull(char** argS, int size){
int res=0;
for(int i=0 ; i < size ; i++)
if(argS[i][0]=='|'){
argS[i]=NULL;
res++;
}
return res;
}
char** getArg(char *str, int *s){
char *c;
int size=0, Olen = strlen(str);
int i = 0, b=0;
char **tab = malloc(sizeof(char*));
tab[0] = strtok(str, " \t\r");
while((c = strtok(NULL, " \t\r")) != NULL){
if(c[0]=='"'){
i = 1;
b = strlen(c);
c[strlen(c)]=' ';
while(c[i]!='"' && c[i]!='\0')
i++;
if(c[i]=='"'){
c[i] = '\0';
c += 1;
tabAdd(&tab, c, &size);
if(c-str+i >= Olen){
strtok(c+i-1, " ");
c=NULL;
}
else
c=strtok(c+i, " \t\r");
}else{
c[b]='\0';
}
}
if(c != NULL)
tabAdd(&tab, c, &size);
}
tabAdd(&tab, NULL, &size);
if(tab[0] != NULL)
*s = size;
else
*s = 0;
/*
for(int k=0;;k++){
if(tab[k]!=NULL)
printf("DEBUG : %s\n", tab[k]);
else break;
}
*/
return tab;
}
void tabAdd(char ***str, char *c, int *size){
if((*str = realloc(*str, sizeof(char*) * (++(*size) + 1) )) == NULL) perror("\033[31mYarrrr !\033[0m");
(*str)[*size] = c;
}
void printMotd(){
FILE *motd;
char stdCol[81];
if((motd=fopen("motd", "r"))!=NULL){
printf("\033[32m");
while(fgets(stdCol, 81, motd)!=NULL)
printf("%s",stdCol);
printf("\033[0m\n");
}
}