netbeans Command Line Argument using C: - c

Hi all on stackoverflow
I've changed from codeblocks to netbeans 12.6 writing a simple code:
int main (int argc, char* argv[])
{
int numberOfArguments = argc;
char * Argument1 = argv[0];
char * Argument2 = argv[1];
printf("Number of arguments: %d\n", numberOfArguments);
printf("Argument1 is the program name: %s\n", Argument1);
printf("Argument2 is the command line argument: %s\n", Argument2);
return 0;
}
I know to highlight project, click properties and goto run and open what do I do after that? Everthing I try nothing happens
Thank you in advanvce Frank

Related

Running C program with textfile as a parameter doesn't work

I have a simple program "prog.c" in C:
#include<stdio.h>
int main(int argc, char *argv[]) {
printf("Count: %d\n", argc);
printf("Text: %s\n", argv[1]);
return 0;
}
When I compile the program and run with a .txt file as parameter, the program does not recognize this parameter at all.
I compile with: gcc prog.c
which creates an "a.exe" file.
Then I run the .exe program with .txt file as argument (tried all options below):
a < text.txt
a.exe < text.txt
The output is always
Count: 1
Text: (null)
I am running everything on Win10 and using MinGW for compilation.
You want to check that argv[1] is set before referencing it with:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Count: %d\n", argc);
if(argc > 1) {
printf("Text: %s\n", argv[1]);
}
}
Then you would run it like this:
./your_program argument
Count: 2
Text: argument
When you use < test.txt the file test.txt is copied to standard input of the program. You would use, for example, read() or fread() to read said input.

Using environmental variable to execute a different file

I am a little bit stuck. I have a C program which includes the environmental variable $USER. The goal is to use the environmental variable to execute a different file using command injection.
I already tried different ways of declaring USER.
e.g.: Declaring USER as
env USER=">/dev/null ; cat /home/Steve/Public/file2.txt".
Unfortunately, that did not work.
C program:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
// Execute any shell command
void execute(char *cmd)
{
execl("/bin/bash", "bash", "-p", "-c", cmd, NULL);
}
void sanitise(char *password)
{
int i,j;
char tmp[15];
// remove non-alphabet characters from passwords
j=0;
for(i=0; i < 15; ++i)
if(password[i] >= 'a' && password[i] <= 'z') {
tmp[j]=password[i];
++j;
} else break;
tmp[j] = '\0';
strcpy(password, tmp);
}
int authenticate(char *str)
{
char stored_password[15]="";
char pass[15];
char path[128] = "/home/Steve/private/password";
int i;
FILE *fpp;
int auth=0;
fpp = fopen(path, "r");
if(fpp == NULL)
{
printf("Password file %s not found\n", path);
exit(1);
}
fgets(stored_password, 15, fpp);
sanitise(stored_password);
strcpy(pass, str);
sanitise(pass);
if(strcmp(stored_password,pass) == 0)
auth=1;
else {
auth=0;
}
fclose(fpp);
return auth;
}
int main(int argc, char* argv[], char *envp[])
{
char error[256] = "/home/Steve/Public/errormessage.sh $USER ";
char pass[15];
if(argc < 2)
{
printf("Usage: %s password\n", argv[0]);
return 0;
}
// copy only 15 characters from user input, to prevent stack smashing
strncpy(pass, argv[1], 15);
pass[14]='\0';
if(!authenticate(pass)) {
// Log all failed attempts
printf("Wrong password. This incident has been logged.\n");
strcat(error, pass);
execute(error); // Execute script to log events
return 0;
}
// Display 'secret-file2'
execute("cat /home/Steve/Public/file2.txt");
return 0;
}
The goal would be to make the program output the file from the variable USER and not the initial file path declared in the error char. Ideally, without changing the C program at all.
Can anyone please tell me what I am missing here?
If you wish to use your C program with a fresh seted ${USER} variable "env -i" is what you should use.
Example: env -i USER='injected code' name_of_c_program
If that's what the program is doing, it will execute the following command:
/home/Steve/Public/errormessage.sh $USER <provided-password>
Since you control the provided password (that is, you can pass what you want in argv[1] as long as it's max 14 characters), you can just execute another bash shell as well, and then use it to run any command you want. There really is no need to tamper with $USER.
$ ./program '; bash'
Wrong password. This incident has been logged.
$ <--- another bash shell has now started
$ cat /home/Steve/Public/file2.txt
...contents of file2.txt...
If you want to run it with a single command, this should work:
echo 'cat /home/Steve/Public/file2.txt' | ./program '; bash'
Use 'getenv' library function to get the value of environment variable(USER). And then run the command using 'system(path)'.
#include <stdlib.h>
...
char path[256] = "/home/Steve/Public/file1.sh ";
strcat( path, getenv("USER") );
//char path[256];
//sprintf(file,"%s%s","/home/Steve/Public/file1.sh ",getenv("USER"));
...

How to run a parameter while executing a C file?

I have a working program that computes the cube of a number in a compute_cube.c file. It compiles into compute_cube.
Now I would like to run it through the terminal like this:
./compute_cube 3
And then the terminal would show my program's result (27).
How would I go about doing this? What should I be reading up on?
Use C language's argc and argv:
int main(int argc, char **argv)
{
if (argc > 1)
printf("%s", argv[1]);
}
I know its already been answered but... argc of course = 0-based number of command line args including program name at index 0 and argv contains actual command line text.
#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 1) {
int n = atoi(argv[1]);
printf ("%d^3 = %d\n", n, n*n*n);
return 0;
}
else printf("Usage: %s <num>\n", argv[0]);
return 1;
}

Passing argument to sub-functions in C

A simple problem, but somehow I can't find the solution,
I want to pass basically, in my program one of my arguments is the desired file to send the program output to.
It's not really an issue to set it up in main() however, I'm wondering how can I process argv[] in functions that are not main.
I tried it like this:
void print_ip(struct arp* arp, char* argv[]){
char *myfile = argv[2];
if ( strlen(argv[2]) <= 0 ){
printf("ERROR: Please specify a proper interface \n");
}
else {
strcpy(myfile, argv[2]);
}
f = fopen(myfile, "w");
if (f != NULL){
printf("Found an IP address \n");
fprintf(f, "\t<ipv4>%s, %s</ipv4>", arp->ipv4_destination, arp->ipv4_source);
}
else {
printf("ERROR: Failed to open file \n");
exit(1);
}
}
This function works, only issue is when I try to launch the function from elsewhere
for example
void arp_scan(struct arp*, int bsd_socket){
... code ...
print_ip(&arp, argv);
}
i get the following error:
scanner.c: In function ‘arp_scan’: scanner.c:187:25: error: ‘argv’
undeclared (first use in this function)
print_ip(&arp, argv);
Surely there must be a way other than declaeing char *argv[] in every single function to be able to proccess it from one to another
only main can accept arguments from the command line (argv). if you want to pass these command line args to a function then you need to pass them as a paramater.
#include <stdio.h>
int passingvars(int argc, char **argv)
{
int i = 0;
while (i < argc)
{
printf(argv[i]);
i++;
}
}
int main(int argc, char *argv[])
{
passingvars(argc, argv);
return 0;
}

How to execute a c file on the command line

I am very new to C programming, and have written this C program that takes in an input N, and gives a list of all the numbers up to N that are exactly divisible by 7. The program I have written is as follows;
# include <stdio.h>
int main(){
int c,n,k;
int i=0;
int AnswerList [1000];
printf("Enter the number\n");
scanf("%d", &n);
for (c=1;c<=n;c++){
if(c%7==0){
AnswerList[i]=c;
i++;
}
}
for (k=0;k<=i;k++){
printf("%d\n", AnswerList[k]);
}
return 0;
}
I need my program to run such that if N equals 27, I should be able to type into the command line
./byseven 27
In other words, I need to write code that bypasses the printf line I think. I would appreciate any help.
Thanks a lot.
Use command-line arguments. A simple example:
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s N\n", argv[0]);
return 0;
}
int N = atoi(argv[1]); // atoi is used to convert a string to an int
// your code
}
You should use int main(int argc, char** argv) definition. Then argc will be number of your params (first param is always the name of your program), and argv is array of string which contains that params. And scanf function is not needed therefore.
gcc -o hello hello.c
It will compile and produced an exectuable file called hello. To run program type:
./hello

Resources