Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. void aff_tab(char **tab) {
  8. int i = 0;
  9. int j = 0;
  10.  
  11. while (tab[i] != NULL) {
  12. printf("%s", tab[i]);
  13. printf("\n");
  14. i++;
  15. }
  16. }
  17.  
  18. char **str_to_word_tab(char *str) {
  19. int i = 0;
  20. int j = 0;
  21. int k = 0;
  22. char **tab = malloc(sizeof(char *) * strlen(str) + 1);
  23.  
  24. while (str[k] != '=' && str[k] != '\0')
  25. k++;
  26. k++;
  27. while (str[k] != '\0') {
  28. j = 0;
  29. tab[i] = malloc(sizeof(char) * strlen(str) + 1);
  30. while (str[k] != ':' && str[k] != '\0') {
  31. tab[i][j++] = str[k++];
  32. }
  33. tab[i][j] = '/';
  34. k++;
  35. i++;
  36. }
  37. return (tab);
  38. }
  39. char *get_path(char **env) {
  40. int i = 0;
  41. int v = 0;
  42. while (env[i]) {
  43. if (v == 0 && env[i][0] == 'P' && env[i][1] == 'A' && env[i][2] == 'T' && env[i][3] == 'H' && env[i][4] == '=') {
  44. v = 1;
  45. return (env[i]);
  46. }
  47. else
  48. i++;
  49. }
  50. return (NULL);
  51. }
  52.  
  53. char **concat_line(char **tab, char *cmd, char **env) {
  54. int i = 0;
  55. int j = 0;
  56. int k = 0;
  57. int c = 0;
  58. char *src;
  59.  
  60. while (tab[i] != NULL) {
  61. j = k = c = 0;
  62. src = malloc(sizeof(char) * (strlen(tab[i]) + strlen(cmd) + 1));
  63. while (tab[i][j] != '\0') {
  64. src[k++] = tab[i][j++];
  65. }
  66. while (cmd[c] != '\0') {
  67. src[k++] = cmd[c++];
  68. }
  69. tab[i] = src;
  70. if (execve(tab[i], NULL, env) == -1)
  71. printf("%s", tab[i]);
  72. free(src);
  73. i++;
  74. }
  75. // aff_tab(tab);
  76. return (tab);
  77. }
  78.  
  79. int main(int ac, char **av, char **env) {
  80. char *array = NULL;
  81. size_t len = 0;
  82. char **tab;
  83.  
  84. while (1) {
  85. printf("$>");
  86. getline(&array, &len, stdin);
  87. tab = str_to_word_tab(get_path(env));
  88. tab = concat_line(tab, array, env);
  89. //aff_tab(tab);
  90. //try_command(tab = concat_line(tab, array), array, env);
  91. }
  92. return (0);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement