Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. char **parse_cmdline( const char *cmdline );
  9. char **parse_cmdline( const char *cmdline ) {
  10.     printf("commandline\n");
  11.     int size = 2;
  12.     int chsize = 1;
  13.     int a = 0;
  14.     int k = 0;
  15.     //char command[100][100];
  16.     printf("commandline\n");
  17.     //printf ("cmdline %s\n",cmdline);
  18.     char **command;
  19.     command = (char **)malloc(size * sizeof(char*));
  20.     command[a] = (char *)malloc(chsize * sizeof(char));
  21.     int i;
  22.     for (i = 0; 1 ; i++) {
  23.         if (cmdline[i] == ' ' || cmdline[i] == '\n') {
  24.             if (cmdline[i] == '\n') {break;}
  25.             if (size >= a ) {
  26.                 size ++;
  27.                 command = realloc(command, size*sizeof(char*));
  28.                 //printf ("size realloc %d\n",size);
  29.             }
  30.             //printf ("%s\n", command[a]);
  31.             chsize = 1;
  32.             k = 0;
  33.             command[++a] = (char *)malloc(chsize * sizeof(char));
  34.             continue;
  35.         }
  36.  
  37.        
  38.         if (chsize >= k) {
  39.             chsize ++;
  40.             command[a] = realloc(command[a], chsize);
  41.             //printf ("chsize realloc %d\n",chsize);
  42.         }
  43.         command[a][k++] = cmdline[i];
  44.         //command[a][k] = '\0';
  45.         //printf ("%d",i+1);
  46.        
  47.     }
  48.     //printf ("%d\n",size);
  49.     //printf ("%ld",sizeof(char*));
  50.     //printf ("%d\n",strlen(cmdline));
  51. //int f = 0;   
  52. return command;
  53. }
  54.  
  55. int main () {
  56.    
  57.     //char** command;
  58.         char *word;
  59.         const char *helper;
  60.         size_t word_size = 30;
  61.         getline(&word,&word_size,stdin);
  62.         helper = word;
  63.         char **command = parse_cmdline(helper);
  64.  
  65.    
  66.     /*pid_t pid = fork();
  67.    
  68.         if (pid < 0) {
  69.  
  70.             perror ("fork");
  71.         }
  72.         else if (pid == 0) {
  73.             //int result = execl(command[0],"/bin/ls",0);
  74.             int result = execv(command[0],command);
  75.             if (result < 0) perror ("execv");
  76.        
  77.         }
  78.  
  79.         else {
  80.             waitpid(pid,0,0);
  81.         }
  82.         exit(pid);
  83.     */
  84.     free (command);
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement