getting trouble with running program in c on linux - c

why I am not succeed
int main(char* name,int arg0,int arg1)
{
name = "/u/e2014/Desktop/os/Prog.c";
arg0 = 0;
arg1 = 1;
char my_args[3];
my_args[0] = arg0;
my_args[1] = arg1;
my_args[2] = NULL;
execl(name,m_args);
return(0);
}
I want that my program will execute the program in the path "name".
Right now its do nothing.
I am not understand where is my mistake?
I program in C on linux, and compile it with gcc
Thanks a lot!!

gcc has 3 different signature for main function
int main(void);
int main(int argc, char* argv[]);
int main(int argc, char *argv[], char *envp[]);
Your main function doesn't match either of these. therefore compiler error.
For your case you can use the 2nd signature with a small modification.
#include <stdlib.h>
int main(int argc, char **argv)
{
char *path;
int int1, int2;
path = argv[1];
int1 = atoi(argv[2]);
int2 = atoi(argv[3]);
}

First you are passing wrong parameter in int main(). main() has at least 3 args only.
int main(int argc, char*argv[], char *envp[]);
To execute your program you should use execvp() because you passing arrey of char* not command-line arguments via a variable-argument.
difference between execl and execv?
**L vs V: whether you want to pass the parameters to the exec'ed program as
L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe()
V: as an array of char* execv(), execve(), execvp(), and execvpe()**
#include <stdio.h>
#include <unistd.h>
int main(int argc,char*argv[])
{
char *name = "/root/a.out";
char *arg0 = "0";
char *arg1 = "1";
char *my_args[4];
my_args[0] = name;
my_args[1] = arg0;
my_args[2] = arg1;
my_args[3] = NULL;
execvp(my_args[0],my_args);
return(0);
}

Related

How to call another function that utilizes argc, char *argv[] in main without passing things to it?

say there is a function in my code:
int getwords(int argc, char *argv[])
and I want to call this function in main().
How do I call this in main without erroring out?
`void main(void)
getwords();`
If you write main to provide access to argc and argv:
int main(int argv, char **argv) {
// ...
}
Then you can pass those to another function when you call it:
void foo(int argc, char **argv) {
// ...
}
int main(int argc, char **argv) {
foo(argc, argv);
}
argc will be copied, but argv is a pointer, so the pointer is copied
but not the data it points to.
if for some special reason you do not want to use parameters you can use global variables available in all program.
int gargc;
char **gargv;
int main(int argc, char *argv[])
{
gargc = argc;
gargv = argv;
getopt();
}

How to use atoi with an int and malloc? [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
When I try and use atoi with an int and malloc I get a bunch of errors and key is given the wrong value, what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char **argv, struct arguments *settings);
int main(int argc, char **argv) {
argv[1] = 101; //makes testing faster
struct arguments *settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char **argv, struct arguments *settings) {
int *key = malloc(sizeof(argv[1]));
*key = argv[1];
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
You probably want this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arguments {
int key;
};
void argument_handler(int argc, char** argv, struct arguments* settings);
int main(int argc, char** argv) {
argv[1] = "101"; // 101 is a string, therefore you need ""
struct arguments* settings = (struct arguments*)malloc(sizeof(struct arguments));
argument_handler(argc, argv, settings);
free(settings);
return 0;
}
void argument_handler(int argc, char** argv, struct arguments* settings) {
char* key = malloc(strlen(argv[1]) + 1); // you want the length of the string here,
// and you want char* here, not int*
strcpy(key, argv[1]); // string needs to be copied
settings->key = atoi(key);
printf("%d\n", settings->key);
free(key);
}
But this is very awkward, actually the argument_handler can be rewritten like this:
void argument_handler(int argc, char** argv, struct arguments* settings) {
settings->key = atoi(argv[1]);
printf("%d\n", settings->key);
}
Disclaimer: I only corrected what was obviously wrong, there are still checks that need to be done, e.g. if argc is smaller than 2 etc.

main() wrapper to start wmain() program to compile it from commandline

For example i have a program thats "main" function is defined as wmain.
int wmain( int argc, wchar_t *argv[] ) {
wchar_t* lpModulePath = NULL;
wchar_t* lpFunctionName = NULL;
lpModulePath = argv[1];
lpFunctionName = argv[2];
}
and of course uses wchar_t types. How can i write a function
int main( int argc, char *argv[] )
that converts the parameters passed as char to wchar_t and then calls wmain by itself?
Thank you
On Windows you can use GetCommandLineW() and CommandLineToArgvW():
int main(int argc, char* argv[])
{
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
int ret = wmain(argc, wargv);
LocalFree(wargv);
return ret;
}
On Linux I'm afraid you'd have to allocate the array and convert strings in loop.

Writing functions in C, passing arguments

I found a code and I want to use it. When I run it from a terminal by ./code 20181010 0810, it works perfectly.
I was trying to rewrite this code into function. The main code was declared by
int main (int argc, char *inp[]) { //some calculations }
So, I changed it into:
int calc(int argc, char *inp[]) { //some calculations }
and the write main code with additional calculations:
int calc(int argc, char *inp[]);
int main(int argc, char *inp[]) {
char* c_date;
char* c_hour;
time_t timer;
char buffer1[26], buffer2[26];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strftime(buffer1, 26, "%Y%m%d", tm_info);
c_date = buffer1;
strftime(buffer2, 26, "%H%M", tm_info);
puts(buffer2);
c_hour = buffer2;
calc(&c_date, &c_hour);
return 0;
}
And for example, for the time now 20180212 1045 it gives me 201802112355, when it should give me 201802121050.
What can be wrong?
At present you’ve just copied the main prototype. What does the function body of calc do? If you had an exact copy of the main function then...
int calc(int argc, char *inp[]);
argc is the number of arguments being passed into your program from the command line and inp is the array of arguments.
You’re passing in &c_date as argc
But that really depends what’s within the calc function......

Execute external program with specific parameters from windows c/c++ code

I want to call Program1 from Program2 with exact same parameters which I called Program2 with.
In Linux, I can do it like this:
int main(char argc, char* argv[]){
execv("./Program1", argv);
}
In windows, I tried CreateProcess
but as the first post says there is potential issue: "argv[0] Doesn't Contain the Module Name as Expected". I do want to send proper argv[0] to Program1. What should I do?
argv[0] is the name of the program itself.
You should do :
int main(char argc, char **argv)
{
char* argvForProgram1[] = { "./Program1", 0 }
execv(argvForProgram1[0], argvForProgram1);
}
or to keep your previous args :
int main(char argc, char **argv)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execv(argvForProgram1[0], argvForProgram1);
}
Using execve is better too because you keep the environment:
int main(char argc, char **argv, char **envp)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execve(argvForProgram1[0], argvForProgram1, envp);
}

Resources