sl_get_inf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sl_get_inf.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/24 00:05:18 by bchanot #+# #+# */
  9. /* Updated: 2018/10/14 04:07:11 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. static t_files *sl_get_infos(char *name, t_bool chk)
  16. {
  17. t_files *files;
  18. int fd;
  19. files = (t_files *)ft_memalloc(sizeof(t_files));
  20. files->s_opt = chk;
  21. if (chk)
  22. {
  23. files->name = NULL;
  24. files->string = ft_strdup(name);
  25. return (files);
  26. }
  27. fd = !name ? 0 : open(name, O_RDONLY);
  28. if (fd > -1)
  29. files->string = ft_get_file_content(fd);
  30. if (fd > 0)
  31. close(fd);
  32. if (!files->string && fd == 0)
  33. files->string = ft_strdup("");
  34. files->name = ft_strdup(name);
  35. return (files);
  36. }
  37. static void sl_add_file(t_inf *inf, char *name, t_bool chk)
  38. {
  39. t_files *tmp;
  40. if (!inf->files)
  41. {
  42. inf->files = sl_get_infos(name, chk);
  43. inf->files->next = NULL;
  44. }
  45. else
  46. {
  47. tmp = inf->files;
  48. while (tmp->next)
  49. tmp = tmp->next;
  50. tmp->next = sl_get_infos(name, chk);
  51. tmp->next->next = NULL;
  52. }
  53. }
  54. static int sl_check_command(t_inf *inf, char *cmd)
  55. {
  56. const char *(hash[NB_HASH + 1]) = {HASH_TAB, NULL};
  57. size_t i;
  58. i = -1;
  59. while (hash[++i])
  60. {
  61. if (!ft_strcmp(cmd, hash[i]))
  62. {
  63. inf->hash_tag = cmd;
  64. return (0);
  65. }
  66. }
  67. return (1);
  68. }
  69. int sl_get_inf(t_inf *inf, int ac, char **av)
  70. {
  71. size_t i;
  72. char **args;
  73. if (!ac)
  74. return (1);
  75. if (sl_check_command(inf, *av))
  76. return (2);
  77. av++;
  78. inf->opts = ft_take_opts_begin(ac - 1, av);
  79. args = ft_take_args_begin(ac - 1, av);
  80. if (!args || ft_check_opts(inf->opts, "p"))
  81. sl_add_file(inf, NULL, false);
  82. i = 0;
  83. if (args && args[i] && ft_check_opts(inf->opts, "s"))
  84. {
  85. sl_add_file(inf, args[i], true);
  86. i++;
  87. }
  88. while (args && args[i])
  89. {
  90. sl_add_file(inf, args[i], false);
  91. i++;
  92. }
  93. ft_memdel2((void ***)&args);
  94. return (0);
  95. }