I'm getting a segfault thrown when using invalid input or the -help flag in the command arguments. It is a re-creation of the Unix expand utility, and its supposed to handle errors in a similar fashion.
int main(int argc, char *argv[]){
char help1[]= "-help";
char help2[]= "--help";
int spaces; //number of spaces to replace tabs
if (argc==1){ //if only one argument in stack
//check if asking for help
if ( (strcmp(argv[1], help1)==0) || (strcmp(argv[1], help2)==0) )
printHelp();
else
printError(); //otherwise, print error message
//right number of tokens are provided, need to validate them
} else if (argc>=2){
spaces= atoi(argv[2]); //assign it to spaces
parse_file(spaces); //open the stream and pass on
}
return 0;
}
My printerror method:
void printError(){
fprintf(stderr, "\nInvalid Input.\n");
fprintf(stderr, "The proper format is myexpand -[OPTION] [NUMBER OF SPACES]\n");
exit(1);
}
When I try invalid input or the help flag, I get a segfault. Why is this, since I'm checking if the first flag is help?
If a single command-line parameter is passed to your program, argc == 2, so you need to replace
if (argc==1){ //if only one argument in stack
with
if (argc==2){
Note that in most systems argv[0] is the program name and in this case argc is at least 1. You can think of argc as the number of elements in argv. If you’re testing for argv[1], you’re expecting argv to have at least two elements (argv[0] and argv[1]), hence argc needs to be at least 2.
argv[0] counts too, so if argc==1 argv[1] is NULL
Your help message should be displayed if there are less than 2 parameter given, hence
if (argc<3)
printHelp();
else if(...)
Upon initialization, the arguments to main will meet the following requirements according to this.
argc is greater than zero.
argv[argc] is a null pointer.
argv[0] through to argv[argc-1] are pointers to strings whose meaning will be determined by the program.
argv[0] will be a string containing the program's name or a null string if that is not available. Remaining elements of argv represent the arguments supplied to the program. In cases where there is only support for single-case characters, the contents of these strings will be supplied to the program in lower-case.
As such, you are passing in argv[argc] (which is a null pointer) to strcmp.
Related
I have a c file such that I pass arguments to as such:
./cfile [-size number]
e.g.
./cfile -size 22
Here is an example code for such a file:
int main(int argc, char** argv) {
if (argv[1] != "-size") {
fprintf(stderr, "Wrong format");
return 1;
}
// get_number is some function that returns int value of number if number, NULL otherwise
if (get_number(argv[2]) == NULL) {
fprintf(stderr, "Wrong format");
return 1;
}
return 0;
}
However, when I write
./cfile '-size' '22'
I cannot find a way of making C determine that the apostrophes should not be there.
I want to throw an error due to the apostrophes on each argument, but c seems to treat them as if they are not there.
Is there any way of doing this?
The quotes are interpreted by the shell in order to separate the arguments. They are removed before your program even sees them.
So your program will only see -size for argv[1], not '-size'.
Also, when comparing strings, you need to use strcmp. Using != or == to compare strings is actually comparing pointer values which is not what you want.
I am writing a program in C for a basic calculator. I am trying to do this using what I have learned so far: printf() and scanf() functions. I am passing arguments into my program through the command line. I am assuming three arguments will be passed at a time which includes: first int, an operator, and the second int. I want to check if the second arg passed is an operator and then check if it's +,-,*... so on. Here is what I came up with:
int main(int argc, char **argv) {
scanf("%d %c %d", &a, &oper, &b);
if (oper != 43) {
printf("Error: Operator is not a +");
return(1);
}
}
So obviously, I have omitted a lot of the code and kept the relevant part. Here I am just checking if the oper is a +. The ASCII key is 43 so I thought this would work but no luck! Any ideas? (I would like to see if I can do this just with printf and scanf if possible)
EDIT: For example if 12 b 13 was entered, it should return the error above. Same goes for '10 +a 10' or '10 ++ 10'.
Firstly I would highly recommend looking at the man-pages for any C library function you come across, they have a lot of useful information. It seems like you are using scanf() improperly as it is not made to be used with command line arguments.
You can check for matches for a single character by comparing the argument like this:
if(argv[2][0] == '+') ...
(argv[0] is the program's file name).
If would would like to compare string you can use strcmp(). But for the operator example you can get away with just checking the first and second characters in the argument like this:
if(argv[2][0] == '+' && argv[2][0] == '\0') ...
What this does is compare the first two characters of the argument. It first checks for the '+' and then checks if that is the end of the string with by checking for the null terminator '\0'.
We can make the assumption that any argument has at least two characters, the visible character and a null terminator. Performing this on other strings has no guarantee of this however.
The other characters, specifically the numbers need to be converted from their respective ASCII values to integers. You can use atoi or strtol to do this, although atoi will most likely be easier for you.
As David C. Rankin pointed out, **argv is a double pointer which at a high level and in most cases you can treat as a double array. In C a string is actually just an array of type char, so what argv[2] is doing above is first accessing the third index of **argv, this is now de-referenced to a type char * where the string (char array) is located. This can then further be de-referenced by the [0] in argv[2][0] to look at the first char of the string.
Code example:
char **my_arrays = argv; // a array of arrays
char *array = *argv; // de-references to index 0 in argv
char *array = *(argv + 1); // de-references to index 1 in argv
char *array = argv[0]; // de-references to index 0 in argv
char *array = argv[1]; // de-references to index 1 in argv
char first_char = *(*argv) // the first char of the first array of argv
char first_char = *(argv[0]) // the same as above
char first_char = argv[0][0] // the same as above
A side note. All strings in C should end in a null terminator which can be represented by NULL, 0, or '\0' values. This will represent the end of the string and many C functions rely on this to know when to stop.
Also NULL is technically a C macro, but you don't need to treat it any differently than 0 because it literally just expands to 0.
It's char **argv. As Some programmer dude said, you should reread your book/tutorial.
scanf doesn't read arguments. It reads from stdin.
Arguments are of type char* and are stored in argv. To convert these arguments to integers, use atoi or strtol (preferably strtol). See this for more info.
If you want to read from stdin using scanf, that is fine, and what you have will work as long as you instead input the data into stdin, and not as command line arguments.
Suppose we are given a task to write a function that add 2 numbers.
#include <stdio.h>
int main(int argc, char ** av) {
int a = atoi(av[1]);
int b = atoi(av[2]);
add_and_print(a, b)
return 0;
}
It works fine untill I pass following code:
./a.out
Just passing empty strings. Then it writes the following:
1495 segmentation fault (core dumped)
Could you please explain what is the problem and how do I deal with it ?
argc contains the number of arguments provided to the program, and if you don't check it then you might get a segfault trying to read from argv. You can display an error message and exit if there aren't enough arguments:
if (argc < 3) {
puts("Please provide 2 numbers as command line arguments.");
return 1;
}
How do I deal with segfault
The segfault happens because of a bug in your code.
So, you prevent it happening in the first place, by not writing buggy code.
In general though, the segfault makes it easy to find out exactly what bug triggered it: just run your program under the debugger, and it will stop exactly where the segfault occurs.
Could you please explain what is the problem
In this code:
int a = atoi(av[1]);
the expression av[1] is only legal if there are at least two elements in array av (since we start indexing at zero). If there is only one element, this code attempts to read beyond the end of the array.
Since the array is based on the command-line arguments, you must check it. You need to do this for all inputs from users, files, even other parts of your own code. Don't just assume the user did what you expected (or the file contained what you expected, or the caller passed the right values). This is a bug.
if (argc >= 2) {
// now it is safe to refer to av[1]
a = atoi(av[1]);
}
You have to do something similar for av[2], for the same reason.
A common solution might be instead
int main(int argc, char **argv) {
if (argc < 3) {
printf("Syntax: %s a b\n"
"\n"
"Two integer arguments are required.", argv[0]);
return -1;
}
int a = atoi(argv[1]);
int b = atoi(argv[2]);
add_and_print(a, b)
}
I'm just assuming that argc is at least 1, and that argv[0] is the name of the program. You can check this too if you want perfectly portable code.
Note that you might also want to check whether the arguments are really integers.
I'm new to C and I'm trying to use an if statement to check for an argument (in this case "jobs") but it doesn't seem to be working...
int builtin_cmd(char **argv)
{
printf("test1\n");
if (!strcmp(argv[0], "quit")) { //quit command
exit(0);
}
if ((!strcmp(argv[0], "fg")) || (!strcmp(argv[0], "bg"))) { //fg or bg command
do_bgfg(argv);
return 1;
}
if (!strcmp(argv[0], "jobs")) { //jobs command
printf("test2\n");
listjobs(jobs);
printf("test3\n");
return 1;
}
printf("test4\n");
return 0; /* not a builtin command */
}
I input "jobs", but based on the test output (1-4-repeat), it's not registering. Does anyone know what might be going wrong?
argv[0] points to the program name, not the first argument. argv[1] points to that, if the argument is present.
C Standard, § 5.1.2.2.1, Program Startup:
If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.
Emphasis mine.
This question already has answers here:
How to properly compare command-line arguments?
(4 answers)
Closed 4 years ago.
So I have to write a program that contains two possible options depending on what the user chooses by entering either -i or -w. I'm very new to command line arguments in general and I have no idea how to do this. So far I have:
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc == -'i') {
puts("Test 1");
}
else if(argc == -'w') {
puts("Test 2");
}
return 0;
}
It doesn't print anything...
Any explanation is greatly appreciated. I'm just trying to understand the logic behind this.
First of all, you are comparing oranges with appels. argc stores the number of
arguments. Second, even if you used argv[1], the comparison would be still
wrong, because it would be comparing pointers, not the contents. Note that in C
a string is a sequence of characters that terminates to '\0'-terminating byte.
A char* variable only points to the start of that sequence. The == operator
checks for equality of values, in case of pointers (and string literals), it
compares whether both pointers point to the same location.
When you want to compare strings, you have to compare the strings themselves,
that means you have to compare the contents where the pointers are pointing to.
For strings you have to use the strcmp function like this:
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
fprintf(stderr, "usage: %s option\n", argv[0]);
return 1;
}
if(strcmp(argv[1], "-i") == 0)
puts("Test 1");
else if(strcmp(argv[1], "-w") == 0)
puts("Test 2");
else {
fprintf(stderr, "Invalid option '%s'\n", argv[1]);
return 1;
}
return 0;
}
Note that it is important to check first that you have enough command line
arguments (the first if of my code). If the user didn't pass any argument,
argc will be 1 and argv[1] will point to NULL, which would yield undefined
behaviour if it is passed to strcmp. That's why I only do strcmp when I'm
100% sure that argv[1] is not NULL.
Also if you are coding for a POSIX system (linux, mac, etc), then I recommend using
getopt for parsing of the command line arguments.
You have to check argv[i] where i is the array number of the command line argument being put in, argv[0] would be the file name called upon and after that argv[1] would be the first statement, argv[2] the next and so on
argc means "argument count". meaning the number of arguments
argv is a 2-dimensional array. Strings are 1-dimensional character arrays in C. The second dimension comes from you having multiple String.
So if you want the first String argument, you would access it as follows:
argv[0]
You are also attempting to compare strings, which are more than 1 character long. You should use strcmp to compare strings in C. See How do I properly compare strings?
and if you want to compare equality, you would not use ==, == is for basic data types such as int or char.
argc represents the number of parameters that were passed in at the command line including the program name itself.
In c, a character, e.g., 'i' is an 8-bit number representing the ASCII code of the letter i. So your conditional statement if(argc == -'i') is actually checking whether -105 (105 is the ascii value of the letter i) is the number of arguments that was passed to your program.
To check whether the arguments passed in are "-i" or "-w" you need to perform string comparison, using the c library functions or your own algorithm, and you need to be comparing those strings to argv[i] (where i is the position of the parameter you're checking in the program invocation)
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("The name of the program is %s\n", argv[0]);
if( strcmp(argv[1], "-i") == 0 ) { puts("Test 1"); }
if( strcmp(argv[1], "-w") == 0 ) { puts("Test 2"); }
return 0;
}