mardi 17 juillet 2018

combination of conditional expressions within an if statement context doesn't work

I've written a function to re-arrange argv into a new one which corrects broken arguments (for example it converts -h to --help=1). Now I want the function skips the argv[i] if i is equal to 1 or 2 and in the same time is equal to some pre-defined phrases which are acceptable main-commands and sub-commands. this is the code I've wrote:

if (i == 1 && (strcmp(argv[i], "mirror") == 0 || strcmp(argv[i], "server") == 0 || strcmp(argv[i], "init") == 0, strcmp(argv[i], "merge") == 0)) {
      newArg = (char*) malloc(sizeof(char) * strlen(argv[i]) + 1);
      strcpy(newArg, argv[i]);
  }
  else if (i == 2 && (strcmp(argv[i], "create") == 0 || strcmp(argv[i], "edit") == 0 || strcmp(argv[i], "delete") == 0)) {
      newArg = (char*) malloc(sizeof(char) * strlen(argv[i]) + 1);
      strcpy(newArg, argv[i]);
  } else {
      i++;
      continue;
  }

I debugged the code. The problem issues when i reaches 1 and also argv[i] is equal to one phrase defined (e.g. i is equal to "mirror"). But the code defined within the third conditional branch ("else", the last one) will become executed, though I expect it to executes the first one. Notice: I've split conditions into two if statements for the sake of simplicity for now. If you know where is the problem, I will become so pleased to know. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire