-1

My program will receive a command like

./a.out echo $3 $1 ">" log.txt

i need to replace the $3 with 3 and 1 to use latter when reading from stdin, at this moment i have my code like this (its only doing for one $n at this moment):

int total = argc-1;
int coluna =0;
int i,n,cut,s, pid,status;
char *cmd[total]; 
char buffer[PIPE_BUF];
char col[10];
char field[10];


//passar argumentos para array
for(i=0;i<total;i++) {
    if(i+1 == total) cmd[total+1] = NULL;
    else cmd[i] = argv[i+1];
}
//verify for $n
char *tmp = cmd;
for(i=0;i<total-1;i++) {
    if(sscanf(tmp,"[$]%s",col) == 1) coluna = atoi(col);
}

the code executes everything else is working (tried without $n, and some other test), but i'm not getting the correct results, seems it ignores the value.

can someone point me my error?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    A complete minimal working example would be great...I am also not sure what is the problem in he behavior (what works and what doesn't, by your sayings). – gsamaras May 10 '17 at 12:09
  • 2
    Did you check `argc`? Typically, if you type `a.out echo $3 $1` it is the same typing just `a.out echo`, since the `$3` and the `$1` will each be replaced with the empty string and no argument will be passed. – William Pursell May 10 '17 at 12:11
  • 1
    Sounds like a XY problem. What do you want to do with the args after? Can you show that code. *the code executes everything else is working), but i'm not getting the correct results*. What is the expected output, what are you getting? Provide a Minimal, verifiable and reproducible example. – Ajay Brahmakshatriya May 10 '17 at 12:16
  • @WilliamPursell wow, never thought that might be the problem, will check it, i put "$n" to try, but will check if that isn't the problem, because when i tested printing the array it only gave me the argumentos without $: – José Moreira May 10 '17 at 18:39
  • @gsamaras it will do a loop readin from stdin, fork to a child, read stdin from father a string separated by columns, use the $n column to execute the program on the child and return the exit status to father. didn't include that because my problem is getting the number without the $. – José Moreira May 10 '17 at 18:39
  • @AjayBrahmakshatriya expected output would be coluna getting the int n from $n. – José Moreira May 10 '17 at 18:39
  • Okay, found the problem, the sscanf is not producing the expect results – José Moreira May 10 '17 at 18:57

1 Answers1

1

You repeatedly overwrite coluna:

for(...)
{
    if(sscanf(...) == 1)
        coluna = atoi(col); // <--
}

I cannot figure out what you exactly want to achive, if you just want to get the first such value (thus the name "coluna"?), then simply break your loop:

if(sscanf(...) == 1)
{
    coluna = atoi(col);
    break;
}

Otherwise, use coluna directly here instead of breaking or store your values in an array as below. Assuming every argument might contain $n:

int cols[argc-1]; // assuming you checked for argc > 1 already!
int numCols = 0;

if(sscanf(...) == 1)
    cols[numCols++] = atoi(col);

Side note: These two pieces of code:

int total = argc-1;

for(i=0;i<total;i++) {
    if(i+1 == total) cmd[total+1] = NULL;
    else cmd[i] = argv[i+1];
}

can be replaced by simply doing:

--argc;
++argv;

unless you really need for any reason a copy (like comparing modified cmd against original argv). If you modify argc/argv as above, you can then use argc and argv just as you do with cmd/total. To retain the names, you might even rename argc to total and argv to cmd...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59