Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <unistd.h>
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.     if (argc < 2)
  10.         exit(EXIT_SUCCESS);
  11.    
  12.     char* string = argv[argc - 1];
  13.     int length = strlen(string);
  14.    
  15.     double logLength = log2(length);
  16.    
  17.     if (floor(logLength) != ceil(logLength))
  18.     {
  19.         int diff = abs((int) (pow(2, ceil(logLength)) - length));
  20.  
  21.         char temp[80] = "";
  22.  
  23.         for (int i = 0; i < diff; i++)
  24.             temp[i] = 'd';
  25.  
  26.         string = strcat(string, temp);
  27.         argv[argc - 1] = string;
  28.        
  29.         length = strlen(string);
  30.     }
  31.    
  32.     char **argv2 = (char **)malloc((argc + 2)*sizeof(char *));
  33.    
  34.     if (length > 1)
  35.     {
  36.         for (int i = 0; i < argc; i++)
  37.             argv2[i] = argv[i];
  38.        
  39.         argv2[argc] = malloc(length / 2);
  40.         argv2[argc + 1] = NULL;
  41.        
  42.         int process1, process2;
  43.        
  44.         process1 = fork();
  45.        
  46.         if (process1 == 0)
  47.         {
  48.             for (int i = 0; i < length / 2; i++)
  49.                 argv2[argc][i] = string[i];
  50.  
  51.             execv(argv[0], argv2);
  52.         }
  53.         else if (process1 > 0)
  54.         {
  55.             process2 = fork();
  56.            
  57.             if (process2 == 0)
  58.             {
  59.                 for (int i = length / 2; i < length; i++)
  60.                     argv2[argc][i - length / 2] = string[i];
  61.                
  62.                 execv(argv[0], argv2);
  63.             }
  64.         }
  65.     }
  66.  
  67.     wait(NULL);
  68.     wait(NULL);    
  69.  
  70.     printf("%d ", getpid());
  71.     for (int i = 1; i < argc; i++)
  72.         printf("%s ", argv[i]);
  73.  
  74.     printf("\n");
  75.  
  76.     free(argv2);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement