Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
so i'm trying to run a simple code for trial to see how i run a C code in terminal but getting this error:
main.c: line 3: syntax error near unexpected token `('
main.c: line 3: `int main(int argc, const char * argv[]) {'
any ideas why this is happening? runs fine on xcode?
The Code that i ran:
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}
command to run file:
bash main.c
This error is returned from the shell that tries to parse your c file as a shell script, since you try to run your C code directly.
sh-3.2$ cat > mycode.c
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}
sh-3.2$ chmod +x mycode.c
sh-3.2$ ./mycode.c
./mycode.c: line 3: syntax error near unexpected token `('
./mycode.c: line 3: `int main(int argc, const char * argv[]) {'
You can't run C code directly, first you need to compile it into an executable, and then execute the compiled executable.
for example:
$ gcc -o myexe main.c
$ ./myexe
Related
This question already has answers here:
Parsing command-line arguments in C
(14 answers)
Closed 9 months ago.
I'm a bit confused as to how I could add certain parameters in the Ubuntu terminal when using the GNU C compiler to compile the program. For example:
gcc -o question question.c
./question -e -f someFile.txt
where -f would open this specific file 'someFile.txt' (any file) and -e would let me access a specific function inside my code.
I tried this with void main(int argc, char* argv[]) but with that I would have to specify the number of arguments I would have to pass i.e. ./question 3 -e -f resources.txt, which I would not like to do.
Is there any other way I could attempt this?
Thank you in advance!!!
#include <stdio.h>
int main(int argc, char **argv) {
printf("program was supplied %d arguments.\n", argc - 1);
for (int k = 0; k < argc; k++) printf("argv[%d] is %s\n", k, argv[k]);
if (!strcmp(argv[1], "-e")) printf("The first argument provided is -e\n");
}
For advanced usage you may want to read about getopt
I am trying AFL for the first time, and for that reason i found a very simple vulnerable C code that i could use to test AFL.
The C code in questions is
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[]){
char name[10];
if ( argc > 1 ){
strcpy(name, argv[1]);
printf("HELLO %s\n", name);
}
return 0;
}
I compile that code by running afl-gcc test.c -o test and i tested it just to make sure it crashes when it was suppose to (running ./test $(python3 -c "print('A'*26)") will give a segmentation fault as expected)
The problem here is, i created a testcase echo -en "test\x00" > input/testcase and run AFL afl-fuzz -i afl_in -o afl_out -- ./test but after a day it still hasn't found any crashes.
I also tried to create a test case that would force it crash python3 -c "print('A'*26)" > input/testcase but it still runs and does not find anything.
This was suppose to be the easiest example so i could get to know AFL a bit better but it is proving to be a challege. Can anyone help?
Just as Nick ODell post it in the comments
Seems like AFL expects the program under test to read from STDIN rather than an argument. github.com/google/AFL#6-fuzzing-binaries
Following that URL shows an experimental module that allows for AFL to read from an argument, and for that to work i just had to add 2 lines to my existing code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "argv-fuzz-inl.h" // <-- Argv fuzz module
int main(int argc, char * argv[]){
AFL_INIT_ARGV(); // <-- needed near the very beginning of main().
char name[10];
if ( argc > 1 ){
strcpy(name, argv[1]);
printf("HELLO %s\n", name);
}
return 0;
}
After that i just compiled it again and everything worked as expected.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Note: This is not a question to ask for a program, It asks about some tech details, see the question bellow first.
I need to write a wrapper program in C/C++ for an existing program. I know we need to use exec/fork/system and pass through the parameters then return the result of the program.
The question is, how to ensure that both the invoker program(that invoke the wrapper) and the wrapped program work exactly like before (ignore timing differences). There maybe subtle things like environment parameters to deal with. fork/system/exec, which to use? Are they enough? Are there other factors to consider?
Let's say you have the following original program:
foo.sh
#!/bin/bash
echo "Called with: ${#}"
exit 23
Make it executable:
$ chmod +x foo.sh
Now the wrapper in C:
wrapper.c
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
printf("Executing wrapper code\n");
/* do something ... */
printf("Executing original program\n");
if(execv("./foo.sh", argv) == -1) {
printf("Failed to execute original program: %s\n", strerror(errno));
return -1;
}
}
Run it:
$ gcc wrapper.c
$ ./a.out --foo -b "ar"
Executing wrapper code
Executing original program
Called with: --foo -b ar
$ echo $?
23
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my code:
/* backtrace_foo1.c */
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#define BACKTRACE() \
do {\
void *array[20];\
size_t size;\
char **strings;\
size_t i;\
size = backtrace(array, 20);\
strings = backtrace_symbols(array, size);\
for (i = 0; i < size; i++) {\
printf ("%s\n", strings[i]);\
}\
free (strings);\
} while(0)
void func1()
{
BACKTRACE();
}
void func()
{
func1();
}
int main(int argc, char **argv)
{
func();
return 0;
}
I compiled it by gcc -g -rdynamic and got
./a.out(func1+0x1f) [0x400905]
./a.out(func+0xe) [0x40097a]
./a.out(main+0x19) [0x400996]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x318ae1ecdd]
./a.out() [0x4007f9]
Then i use addr2line -e ./a.out -f 0x4007f9, i got
_start
??:0
This is my platform
gcc version 5.3.0 (GCC)
Linux 3.10.0_1-0-0-8
I shouldn't really answer this, since you don't really have a question or a stated problem. But sometimes I'm feeling nice...
While the code you write will start execution with the main function, the actual starting point is somewhere before that. There is startup code that will initialize the stdio system (stdin, stdout etc.) and initialize other things. This startup code then calls your main function like any other function.
The "problem" is that the startup code is not really part of your code, it's often a precompiled object file that the frontend program links your program with. And that object file probably doesn't have any kind of debug information, so you can't get any location information about it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have this very simple c program:
#include <stdio.h>
int main (int argc, char ** argv){
printf ("%s\n",argv[1]);
}
When running it on Linux/bash like so:
./a.out *
I get the following output:
a.c
why?
Because * is a glob character that expands to the list of files in the current directory.
If you want to pass a literal * you will need to quote or escape it:
./a.out '*'
I didn't know that, but when running a command line that has a glob character , such as * or ?, the command line interpreter first expands the character and only then run the program.
For example, if your program is:
#include <stdio.h>
int main (int argc, char ** argv){
int i;
printf ("argc=%d\n",argc);
for (i=0;i<argc;i++){
printf("%d: %s\n",i,argv[i]);
}
}
and you run it like so:
./a.out *
, then the output will be:
argc=4
0: ./a.out
1: a.c
2: a.c~
3: a.out
Of course, the output will depend on the content of the current directory.