User input to linux command in C -


#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <time.h> #include <string.h>  void getcommand(char* cmd, char** arg_list) { pid_t child_pid;  child_pid = fork();  if (child_pid == 0) {     execvp (cmd, arg_list);     fprintf(stderr, "error");     abort(); }  }  int main(void) {  printf("type command\n");  char *arg_list[] = {null, null, null, null, null, null, null};  char cmd[20]; char delim[2] = " "; char *token;  scanf("%[^\n]", cmd);  token = strtok(cmd, delim);  while (token != null) {     arg_list[0] = token;     token = strtok(null, cmd); }  getcommand (arg_list[0], arg_list); return 0; } 

what i'm trying achieve here want read user input, should linux command, , execute command. seems can't use strtok split string. i'm kinda lost, help.

your successive calls strtok wrong. need pass delimiters. also, writing first element of array. try this:

int n = 0; while (token != null && n < 7) {     arg_list[n++] = token;     token = strtok(null, delim); } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -