Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. int main(int argc, const char * argv[])
  9. {
  10.     char** arguments = malloc(1 * sizeof(char**));
  11.  
  12.     if (arguments)
  13.     {
  14.         //arguments[0] = malloc(strlen(argv[0]));
  15.         //arguments[1] = malloc(1 * sizeof(*arguments[0]));
  16.  
  17.         FILE *fp;
  18.  
  19.         fp=fopen("test.txt", "r");
  20.  
  21.         if (fp==NULL)
  22.         {  
  23.             fp=fopen("test.txt", "w");
  24.             int pid=getpid();
  25.             fprintf(fp,"%d", pid);
  26.             fclose(fp);
  27.  
  28.             char test[1024];
  29.  
  30.             int counter = 0;
  31.  
  32.             for(int i =1; i < argc; i++)
  33.             {
  34.                 for(int j = 0; j < (int)strlen(argv[i]); j++)
  35.                 {
  36.                     if (argv[i][j] != '\0') test[counter] = argv[i][j];
  37.                     counter++;
  38.                 }              
  39.             }
  40.             test[counter + 1] = '\0';
  41.  
  42.             printf("Test : %s\n", test);
  43.  
  44.            
  45.             int potega = 2;
  46.             int dlugosc = 0;
  47.  
  48.             while (test[dlugosc] !='\0')
  49.             {
  50.                 dlugosc++;
  51.             }          
  52.  
  53.             printf("Dlugosc : %d\n", dlugosc);
  54.  
  55.             while (1)
  56.             {
  57.                 if(potega == dlugosc) break;
  58.                 if(potega < dlugosc)
  59.                     {
  60.                         potega *= 2;
  61.                         if ( potega > dlugosc )
  62.                         {          
  63.                             potega = potega / 2;
  64.                             break;
  65.                         }
  66.                     }
  67.             }
  68.  
  69.             char argument[potega+1];
  70.  
  71.             for(int i=0; i < potega; i++)
  72.             {
  73.                 argument[i] = test[i];
  74.             }
  75.  
  76.             argument[potega+1] = '\0';
  77.  
  78.             printf("Argument: %s\n", argument);
  79.  
  80.             //strncpy(argument, test, potega);
  81.  
  82.             //printf("Argument: %s\n", argument);
  83.  
  84.             //char * argument = NULL;
  85.  
  86.             //size_t dlugosc;*/
  87.             /*
  88.             for(int i=1; i<argc; i++)
  89.             {
  90.                 char* tmp;
  91.                 dlugosc += strlen(argv[i]);
  92.                 tmp = realloc(argument, dlugosc);
  93.                 argument = tmp;
  94.                 strcat(argument, argv[i]);
  95.             }
  96.             */
  97.             //printf("Argument: %s\n", argument);
  98.             //printf("Dlugosc: %d\n", strlen(argument));
  99.  
  100.             /*char** tmp = realloc(arguments, 2 * sizeof(*arguments));
  101.  
  102.             size_t potega = 2;
  103.             dlugosc = strlen(argument);
  104.  
  105.  
  106.  
  107.             if(tmp)
  108.             {              
  109.                 //arguments[1] = malloc(potega);
  110.             }
  111.  
  112.             argc = 2;*/
  113.  
  114.             /*
  115.             char* argument = malloc(dlugosc + 1);
  116.             memcpy
  117.             int counter = 0;
  118.  
  119.             if(argument)
  120.             {
  121.                 argument[0] = '\0';
  122.  
  123.                 for(int i=1; i < argc; i++)
  124.                 {
  125.                     for(int j=0; j<(int)strlen(argv[i]); j++)
  126.                     {
  127.                         argument[counter] = argv[i][j];
  128.                         counter++;
  129.                     }
  130.                 }
  131.  
  132.                 //printf("Argument: %s\n", argument);
  133.  
  134.                 //int dlugosc = (int)strlen(argument);
  135.                 int potega = 2;
  136.  
  137.                 while (1)
  138.                 {
  139.                     if(potega == dlugosc) break;
  140.                     if(potega < dlugosc)
  141.                         {
  142.                             potega *= 2;
  143.                             if ( potega > dlugosc )
  144.                             {          
  145.                                 potega = potega / 2;
  146.                                 break;
  147.                             }
  148.                         }
  149.                 }
  150.  
  151.  
  152.  
  153.                
  154.                 //for(int i=0; i< potega;i++)
  155.                 //{
  156.                     //arguments[1][i] = argument[i];
  157.                 //}
  158.                 //arguments[potega + 1] = '\0';
  159.  
  160.                 char** tmp = realloc(arguments, 2 * sizeof(*arguments));
  161.  
  162.                 if(tmp)
  163.                 {                                              
  164.                     arguments = tmp;
  165.  
  166.                     size_t n = strlen(argument) + 1;
  167.                     arguments[1] = malloc(n);
  168.                     memcpy(arguments[1], argument, n);
  169.  
  170.                     arguments[0] = argv[0];
  171.                     argc = 2;
  172.                     //char* buf = realloc(arguments[1], (potega + 1) * sizeof(char));
  173.                     //char test[potega+1];
  174.  
  175.                     //for (int i=0; i < potega; i++)
  176.                     //{
  177.                     //  test[i] = argument[i];
  178.                     //}
  179.                     //test[potega] = '\0';
  180.                     //printf("Test: %s\n", test);
  181.  
  182.                     //memcpy(arguments[1], argument, potega);
  183.                     //arguments[1] = "";
  184.                     //arguments[1] = realloc(arguments[1], (potega + 1) * sizeof(char));
  185.                     //strncpy(arguments[1], argument, potega);
  186.                     free(argument);
  187.                 }
  188.  
  189.  
  190.  
  191.                 /*
  192.  
  193.                 char* linkedArgument = malloc(potega* sizeof(char));
  194.  
  195.                 if(linkedArgument)
  196.                 {
  197.                     for(int i = 0; i < potega; i++)
  198.                         {
  199.                             linkedArgument[i] = argument[i];
  200.                         }
  201.  
  202.                     free(argument);
  203.  
  204.                     char** tmp = realloc(arguments, 2 * sizeof(*arguments));
  205.  
  206.                     if(tmp)
  207.                     {                                              
  208.                         arguments = tmp;
  209.                         arguments[0] = argv[0];
  210.  
  211.  
  212.                         //char* buf = realloc(arguments[1], potega * sizeof(char));
  213.                         arguments[1] = linkedArgument;
  214.                         //arguments[1] = linkedArgument;
  215.                         //arguments[1] = malloc(potega * sizeof(char));
  216.                         strncat(arguments[1], linkedArgument, potega);
  217.                         //for(int i=0;i<potega;i++)
  218.                         //{
  219.  
  220.                             //arguments[1][i] = linkedArgument[i];
  221.                         //}
  222.                         argc = 2;                      
  223.                         //free(linkedArgument);
  224.                         free(linkedArgument);
  225.                     }                  
  226.                 }
  227.  
  228.                 */
  229.            
  230.         }
  231.         else
  232.         {
  233.             /*
  234.             char** tmp = realloc(arguments, argc * sizeof(*arguments));
  235.             if (tmp)
  236.             {
  237.                 arguments = tmp;
  238.                 for(int i=0; i<argc; i++)
  239.                 {
  240.                     arguments[i] = malloc (strlen(argv[i]));
  241.                     arguments[i] = argv[i];
  242.                 }                
  243.             }*/
  244.         }
  245.  
  246.         //int index = argc -1;
  247.         //int lenght = strlen(arguments[1]);
  248.  
  249.         /*
  250.         printf("Dlugosc: %d\n", lenght);
  251.        
  252.         if (lenght==1)
  253.         {    
  254.             printf("%d ", getpid());     
  255.             for (int i = 1; i<argc; i++)
  256.             {
  257.                 printf("%s ", arguments[i]);
  258.             }
  259.             printf("\n");
  260.             exit(0);
  261.         }
  262.  
  263.         char * pierwszy = malloc((lenght +1) * sizeof(char));  
  264.         char * drugi = malloc((lenght +1) * sizeof(char));
  265.      
  266.         for(int i=0; i< lenght; i++)
  267.         {
  268.             if(i < lenght /2)
  269.             {
  270.                 pierwszy[i] = arguments[index][i];
  271.             }
  272.             else
  273.             {
  274.                 drugi[i - lenght /2] = arguments[index][i];
  275.             }
  276.         }
  277.  
  278.         pierwszy[lenght] = '\0';
  279.         drugi[lenght] = '\0';
  280.  
  281.         char * tablica[argc + 2];
  282.         tablica[argc + 1] = NULL;
  283.        
  284.         for ( int i = 0; i<argc; i++ )
  285.         {
  286.             tablica[i] = arguments[i];     
  287.         }
  288.  
  289.         printf("Pierwszy: %s\n", pierwszy);
  290.         printf("Drugi: %s\n", drugi);*/
  291.  
  292.         /*
  293.         for(int i = 0; i< argc; i++)
  294.         {
  295.             free(arguments[i]);
  296.         }*/
  297.         free(arguments);
  298.         //free(pierwszy);
  299.         //free(drugi);
  300.  
  301.         int rootpid;
  302.         fp=fopen("test.txt", "r");
  303.         fscanf(fp, "%d", &rootpid);
  304.         fclose(fp);
  305.      
  306.         if (rootpid==getpid())
  307.         {
  308.             remove("test.txt");
  309.         }
  310.     }
  311.     return 0;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement