Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. int main(int argc, char *argv[]) {
  2. if(argc <= 2){
  3. print_env_usage();
  4. return 0;
  5. }
  6. char * bin = malloc(10000);
  7. strcpy(bin, "/bin/");
  8. int length = 1000;
  9. int start = 1;
  10. if(strcmp(argv[1], "-n") == 0) {
  11. //provided the N information
  12. length = atoi(argv[2]);
  13. if(length == 0)
  14. length = 1;
  15. start = 3;
  16. }
  17. if((strcmp(argv[2], "--") == 0 && length == 1000))
  18. length = 1;
  19.  
  20.  
  21. static int dashlocation;
  22. int i = 0;
  23. while(argv[i] != NULL) {
  24. if(strcmp(argv[i], "--") == 0) {
  25. i++;
  26. strcat(bin, argv[i]);
  27. dashlocation = i-1;
  28. break;
  29. }
  30. i++;
  31. }
  32.  
  33. //CASE: there are no environment variables
  34. if(dashlocation == 1) {
  35. pid_t child = fork();
  36. if(child == -1) {
  37. free(bin);
  38. print_fork_failed();
  39. }
  40. else if (child == 0) {
  41. execv(bin, (argv+2));
  42. free(bin);
  43. print_exec_failed();
  44. }
  45. else {
  46. int status;
  47. waitpid(child, &status, 0);
  48. if(WIFEXITED(status) && WEXITSTATUS(status) != 1) {
  49. free(bin);
  50. return 0;
  51. }
  52. }
  53. }
  54.  
  55.  
  56. //CASE: there are environment variables
  57. char * token;
  58. int indexofkeys = 0;
  59. int return_code;
  60. char * important = NULL;
  61. char *nameofenvs [100];
  62. for(; indexofkeys < length; indexofkeys++) {
  63. int beginning = start;
  64. for(; beginning < dashlocation; beginning++) {
  65. if(indexofkeys == 0){
  66. token = strtok_r(argv[beginning], "=", &argv[beginning]);
  67. nameofenvs[beginning] = token;
  68. }
  69. if(token == NULL) {
  70. free(bin);
  71. print_env_usage();
  72. }
  73. int counter = 0;
  74. char crucial = *(argv[beginning]);
  75. while(counter < 1) {
  76. if (crucial == '%'){
  77. //percentage sign
  78. //printf("%s\n", argv[beginning]);
  79. if (important == NULL)
  80. important = argv[beginning];
  81. //printf("%s\n", "in percentage sign");
  82. char * percent = malloc(10000000);
  83. strcpy(percent, argv[beginning]);
  84. percent++;
  85. //printf("%s\n", percent);
  86. return_code = setenv(token, getenv(percent), 1);
  87. //printf("%d\n", return_code);
  88. if (return_code == -1){
  89. free(bin);
  90. print_environment_change_failed();
  91. }
  92. }
  93. else{
  94. char * value = strtok_r(argv[beginning], ",", &argv[beginning]);
  95. //no percent sign
  96. return_code = setenv(nameofenvs[beginning], value, 1);
  97. }
  98. counter++;
  99. }
  100. }
  101. pid_t child = fork();
  102. if(child == -1) {
  103. //fork did not happen
  104. free(bin);
  105. print_fork_failed();
  106. }
  107. else if (child == 0) {
  108. //child is executing
  109. execv(bin, (argv+dashlocation+1));
  110. free(bin);
  111. print_exec_failed();
  112. }
  113. else{
  114. //parent process
  115. int status;
  116. waitpid(child, &status, 0);
  117. }
  118. }
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement