execvp wont print echo command [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to understand execvp command, when i type /bin/ls it print the right output out but not when i print echo something.
char* argumentCommand[11];
char args[10][256]; // inside of this are arguments
//args[0] = echo args[1] = "some text"
//point it to the argument so i can pass it as a vector
for(int i = 0; i<10;i++)
{
argumentCommand[i] = args[i];
}
//wont print anything when i type echo something or wont take the second parameter when i use /bin/ls -l
execvp(argumentCommand[0],argumentCommand);
Does anyone know where the mistake it is?

To wrap this up:
You need to terminate your argument vector argumentCommand with a NULL pointer, and you need to do it at the correct position!
So if you have three arguments:
char* argumentCommand[11];
char args[10][256]; // inside of this are arguments
strcpy(args[0], "ls");
strcpy(args[1], "-l");
strcpy(args[2], "/");
and you fill your argumentCommand-vector like you did:
for(int i = 0; i<10;i++)
{
argumentCommand[i] = args[i];
}
you still have to NULL-terminate it after the third argument:
argumentCommand[3] = NULL;
before executing:
execvp(argumentCommand[0],argumentCommand);
Then it should work as you expect.
Otherwise, the argumentVector is either not NULL-terminated at all, leading to an -EFAULT on execvp(), or it is incidentally NULL-terminated somewhere in memory, which results in the command receiving a number of pseudo-random arguments, leading to unexpected behaviour.

Replace this
execvp(argument[0],argumentCommand);
with
execvp(argumentCommand[0],argumentCommand + 1);
if argumentCommand holds correct executables with valid option & null terminated. For e.g
int main(void) {
char* argumentCommand[] = {"/bin/ls","ls","-l",NULL}; /* Or you can use as you are trying but make sure when you are doing argumentCommand[i] = args[i];, at the end argumentCommand should have valid input */
execvp(argumentCommand[0],argumentCommand + 1);
return 0;
}

Related

Converting char array to upper case in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to convert a char array to upper case in C. But it still prints out in lower case. The logic in conversion function looks okay to me. I don't know if the problem could be that the input I'm passing to this function is member of a structure variable.
This is the structure whose member I'm passing to conversion function:
typedef struct AESParams {
unsigned char output[192];
} AESParams;
My conversion fucntion is shown below:
void stringUpr(char *s)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
++i;
}
}
I call conversion function as follows:
AESParams parameters;
stringUpr(parameters.output);
If I print the output after calling "stringUpr" function, the value is still in lower case. Can anyone tell me what might be causing the issue here?
TIA...
Update: Code that writes value to output.
EVP_EncryptUpdate(&ctx, parameters->output, &outLen, input, length);
// This call happens in some other file. parameters is passed to the function that calls this function and after computation parameters is passed back to the place where I'm calling stringUpr().
I'm confident that this lines works and it gives the correct results after computation. It's just that I want to take this value and convert to upper case and write to a file.
Your program assumes that it's running using an ASCII character set, which is not guaranteed. Use the standard functions defined in ctype.h
void stringUpr(char* s)
{
int i = 0;
while(s[i] != '\0')
{
s[i++] = toupper((unsigned char)s[i]);
}
}

impacting Strings with pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The code which does not work. In function main after initializing variables by the user, it goes to if part and after that crash happens. This program's goal is to impact the repetitive characters in the string. So I decided to have a pointer in a function f1 and the pointer should point to 0th char of the array kar[]
then compare the 0th char to 1st char but the program crashes exactly after the last scanf("%s",kar[1000]).
where is the problem?
#include <stdio.h>
char f1(char* p)
{
int i=0,c=1;
while(*(p+i))
{
if(*p==*(++p))
{
c+=1;
p++;
i++;
continue;
}
else
{
if(c>1)
{
printf("%c%d",*p,c);
}
else
{
printf("%c",*p);
}
}
i++;
}
}
int main()
{
int n,i,m;
char kar[1000];
scanf("%d",&n);
for(i=1;i<=(2*n);++i)
{
scanf("%d",&m);
scanf("%s",kar[1000]);
if(m==1)
{
f1(kar);
}
}
}
This
scanf("%s",kar[1000]);
should trigger a compiler warning, as you pass a char where a char* is expected. You pass the 1001st element of kar (which is out of kar's bounds, BTW). In C array indices start with 0 for the 1st element.
To scan into kar, just pass the address of kar's 1st element.
scanf("%s", &kar[0]);
As arrays get decayed to the address of their 1st element when being passed to a function the following statement is equivalent
scanf("%s", kar);
Please note that the above two statements very well allow the user to enter more characters then kar can hold and with this make scanf() overflow kar, that is write beyond it bounds, invoking undefined behaviour, which might crash your program or whatever ...
To avoid this tell scanf() the maximum to scan by doing:
scanf("%999s", kar);
The maximum for chars a strings can hold in C is always one less then defined as C strings need one more char to have their end marked by a '\0'terminator.

Filling an Array in C with 0 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ive got a problem with this code, im trying to add up array1 with array2.
I enter the numbers for array2 by Command line parameters.
When i enter 10 numbers it is working but when i add less than 10 I get an Memory access error.
My question is now: how do i fill up the missing array fields with the number 0? For example : I enter 9 numbers and the 10th field should be 0.
You are not checking how many command line arguments are passed, and when you index into the command line argument array, you will get an out-of-bounds error.
In you addiren function, you should take advantage of the argc that is passed and used that in your for loop limit.
#include <stdio.h>
#include <stdlib.h>
int addiren(int argc, char**argv){
int array_one[10] = {0,1,1,2,3,5,8,13,21,35};
int array_two[10] = {0}; //Quick way to set the array to all zeros
int array_three[10] = {0};
//Set array_two with your cmd-line args, notice the use of argc
for(int i = 1; i<argc && i<=10; i++){
array_two[i-1] = atoi(argv[i]);
}
//Add array_one with array_two to array_three
for(int i = 0; i<10; i++){
array_three[i] = array_one[i]+array_two[i];
}
//Return an int since that's what the function return type requires
return 0;
}
Hope this helps!

argc and argv[] in C language [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm learning C and in one of the examples we write a program like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
// go through each string in argv
int i = 0;
while(i < argc)
{
printf("arg %d: %s\n", i, argv[i]);
i++;
}
// let's make our own array of stringd
char *states[] = {"California", "Oregon", "Washington", "Texas"};
int num_states = 4;
i = 0; // watch for this
while(i < num_states)
{
printf("state %d: %s\n", i, states[i]);
i++;
}
return 0;
}
and if I run it in terminal like this:
./ex11 test arguments
I get an output of:
arg 0: ./ex11
arg 1: test
arg 2: arguments
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas
However I don't understand why the "Test argument" part gets printed, i know it has something to do with argc and argv but I don't know how.
Can someone explain this to me (preferably in a simple manner)?
When a command, any command, is run by the shell (actually, any shell) on Unix, then the command line is converted into an array of strings:
cmd_argv[0] = "./ex11";
cmd_argv[1] = "test";
cmd_argv[2] = "arguments";
cmd_argv[3] = NULL;
cmd_argc = 3;
and then invokes:
execvp(cmd_argv[0], cmd_argv);
Note that all I/O redirections are removed, etc. Internally, the system ends up counting the number of non-null pointers at the start of the cmd_argv array and passes them as argv in the new program, and the count as argc. The new program is guaranteed that argc >= 0 and argv[argc] == NULL.
This list is almost the same form as the list of states; the primary difference is that cmd_argv[cmd_argc] is a null pointer, which is certainly not guaranteed with the states data.
That's what your first loop does, it iterates through the strings in argv (which contains the name of the program and the arguments you typed after it), and prints them out. Argc is the number of arguments you passed, or equivalently the length of argv.
argv is the command line arguments, including the executable's name (aguments' values). argc is the amount of elements in argv (argument count).
In your first loop, you are printing the contents of argv, therefore you get the arguments that were passed to your program.

Find the output of a C program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have been trying solve this question but unable to understand.
If the following program (myprog) is run from the command line as:
myprog friday tuesday sunday
What would be the output?
#include<stdio.h>
int main(int argc, char *argv[]){
while(sizeof argv)
printf("%s",argv[--sizeof argv]);
return 0;
}
The output is-
sunday tuesday friday myprog
Please explain me the output.
Thanx :-)
I am guessing you really what this. It just prints the command line argument out backwards.
#include<stdio.h>
int main(int argc, char *argv[])
{
while (argc)
printf("%s ", argv[--argc]);
printf("\n");
return 0;
}
Disregarding the --sizeof issue, I'm assuming you want to know about the elements of argv.
It contains the arguments and the name of the program. Read any manual or document describing argv.
There are two issues in your code:
--sizeof argv is illegal. It would result in an error.
while(sizeof argv) will result in an infinite loop as the condition will always be true
So in short the code will not compile and will result in an error.
You probably want to understand command line argument processing in C. When you are given some list of program arguments, for example,
myprog friday tuesday sunday
The C language provides arguments to the main() function which provide the number of arguments (4 in this case), and an array of char* (pointers) to these arguments.
Note that sizeof argv is computed at compile time, and the value is the size of a pointer on your system (4 or 8).
First, we explain the arguments to the main function,
int main(
int argc, //an integer count of the number of arguments provided to the program
char* argv[] //an array of pointers to character arguments
)
Your main function is then defined to (apparently) print out the arguments starting at the rightmost argument and working back to the zero-th argument,
{
int argv_sizeof = argc; //you cannot use sizeof argv the way you specified
//argv_sizeof = 4 in your example, but argv[4] is not valid
//argv_sizeof has a value that is one past the rightmost element of argv[]
while( argv_sizeof ) //use argv_sizeof > 0 here; argv_sizeof is 4,3,2,1,0
//when argv_sizeof reaches 0, the while loop terminates
{
printf("%s",argv[--argv_sizeof]); //here you pre-decrement argv_sizeof (3,2,1,0)
//then use argv_sizeof to index into argv[]
//then you print the string at argv[3], argv[2], argv[1], argv[0]
}
//argv_sizeof = 0 here
return 0; //you return the value 0 from the main function
}
Error:
lvalue required as decrement operand
It does not compile.
sizeof is an operator just like + or % and it gives you the size of an object. So, you can't decrement it. Just as somehting like this wouldn't make any sense: --%
The gist of the question is:
What happens if the input is: myprog friday tuesday sunday
When the code does:
index = lastIndex
while(index) // note: while(0) == false
print(array[--index])
So, the output would be the reversal of the elements:
sunday tuesday friday

Resources