cmd_split.c 761 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "sh.h"
  2. char **cmd_split(char const *s, char c, char d)
  3. {
  4. int sp;
  5. char **tabe;
  6. int i;
  7. int j;
  8. int start;
  9. if (s == 0 || c == 0 || d == 0)
  10. return (NULL);
  11. sp = count_words(s, c, d);
  12. tabe = (char**)malloc((sizeof(char*) * (sp + 1)));
  13. i = 0;
  14. j = -1;
  15. while (++j < sp)
  16. {
  17. while (s[i] && (s[i] == c || s[i] == d))
  18. i++;
  19. start = i;
  20. while (s[i] && s[i] != c && s[i] != d)
  21. i++;
  22. tabe[j] = ft_strsub(s, start, i - start);
  23. i++;
  24. }
  25. tabe[j] = NULL;
  26. return (tabe);
  27. }
  28. int count_words(char const *s, char c, char d)
  29. {
  30. int sp;
  31. int sp_tmp;
  32. sp_tmp = 0;
  33. sp = 0;
  34. while (*s)
  35. {
  36. if (sp_tmp == 1 && (*s == c || *s == d))
  37. sp_tmp = 0;
  38. if (sp_tmp == 0 && *s != c && *s != d)
  39. {
  40. sp_tmp = 1;
  41. sp++;
  42. }
  43. s++;
  44. }
  45. return (sp);
  46. }