Hi I am writing a simple client-server program. In this program I have to use getopt() to get the port number and ip address like this:
server -i 127.0.0.1 -p 10001
I do not know how can I get values from optarg, to use later in the program.
You use a while loop to move through all the arguments and process them like so ...
#include <unistd.h>
int main(int argc, char *argv[])
{
int option = -1;
char *addr, *port;
while ((option = getopt (argc, argv, "i:p:")) != -1)
{
switch (option)
{
case 'i':
addr = strdup(optarg);
break;
case 'p':
port = strdup(optarg);
break;
default:
/* unrecognised option ... add your error condition */
break;
}
}
/* rest of program */
return 0;
}
How about like this:
char buf[BUFSIZE+1];
snprintf(buf,BUFSIZE,"%s",optarg);
Or in a more complete example:
#include <stdio.h>
#include <unistd.h>
#define BUFSIZE 16
int main( int argc, char **argv )
{
char c;
char port[BUFSIZE+1];
char addr[BUFSIZE+1];
while(( c = getopt( argc, argv, "i:p:" )) != -1 )
switch ( c )
{
case 'i':
snprintf( addr, BUFSIZE, "%s", optarg );
break;
case 'p':
snprintf( port, BUFSIZE, "%s", optarg );
break;
case '?':
fprintf( stderr, "Unrecognized option!\n" );
break;
}
return 0;
}
For more information see the documentation of Getopt.
It is one of the numerous flaws of the getopt documentation: it does not state clearly that optarg must be copied for later use (using strdup(), for instance) because it may be overwritten by later options or just plain freed by getopt.
In the case of an ip and port you don't need to store the strings. Just parse them and store the values in a sockaddr.
#include <arpa/inet.h> // for inet_ntop, inet_pton
#include <getopt.h> // for getopt, optarg
#include <netinet/in.h> // for sockaddr_in, etc
#include <stdio.h> // for fprintf, printf, stderr
#include <stdlib.h> // for atoi, EXIT_SUCCESS
#include <string.h> // for memset
#include <sys/socket.h> // for AF_INET
int main(int argc, char *argv[])
{
struct sockaddr_in sa;
char c;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = 0;
while ((c = getopt(argc, argv, "i:p:")) != -1)
{
switch (c)
{
case 'p':
sa.sin_port = htons(atoi(optarg));
break;
case 'i':
inet_pton(AF_INET, optarg, &(sa.sin_addr));
break;
case '?':
fprintf(stderr, "Unknown option\n");
break;
} /* ----- end switch ----- */
}
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
printf("%s:%d\n", str, ntohs(sa.sin_port));
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
Related
Why do I keep getting a set fault when I try and pass -H in the command line as a flag? -h (help) is working perfectly but -H(header) messes up every single time.
I have a main function as well which calls parse_command_line by passing the argc & argc.
the bool is defined as bool header = false;
the file is char** file = NULL;
and the reason I have the file+=1; in the code is so that it compiles because I am using a makefile that changes all warnings into errors.
#include "parse.h" /* prototypes for exported functions */
#include "../main/unused.h"
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>
int
parse_command_line (int argc, char **argv, bool *header, char **file)
{
int oc = 0;
file += 1;
bool help = false;
struct option long_options[] =
{
{"header", no_argument, NULL, 'H'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
while ((oc = getopt_long(argc, argv, "+Hh", long_options, NULL)) != -1)
{
printf("The value of oc = %d\n", oc);
switch(oc)
{
case 'h':
help = true;
break;
case 'H':
printf("inside case H");
*header = true;
break;
case '?':
fprintf(stderr, "Unknown flag = -%c, type -h or --help for help!\n", optopt);
exit(EXIT_FAILURE);
break;
default:
break;
}
}
printf("Out of loop"); if (optind+1 != argc)
{
fprintf(stderr, "Uh oh, invalid input! Try again with -h or --help for help!\n");
exit(EXIT_FAILURE);
}
if (help)
{
printf("\nHaving some trouble? Let me show you the ropes!\n\n");
printf("Format: ydi <option(s)> mini-elf-file\n\n");
printf("Here's your options:\n");
printf("-h --help Display usage\n");
printf("-H --header Show the Mini-Elf header\n");
exit(1);
}
if (header)
{
printf("Inside HEader");
FILE *file;
uint16_t nums[6];
file = fopen(argv[optind], "r");
#define STRUCT_ITEMS 7
fread(nums, 16, 6, file);
int cur_print;
for (cur_print = 0; cur_print < STRUCT_ITEMS; cur_print++)
{
printf("%d ", nums[cur_print]);
}
}
return 0;
}
My parse.h file is as follows:
#ifndef __PARSE_COMMAND_LINE__
#define __PARSE_COMMAND_LINE__
#include <stdbool.h>
int parse_command_line (int argc, char **argv, bool *header, char **file);
#endif
There are other files such as elf.h and elf.c which I have not implemented and are not called at all at this point, which leads me to believe they are not going to be the problem and don't need to post the small 2 line files. My main function is as follows:
#include <stdio.h> /* standard I/O */
#include <stdlib.h>
#include "unused.h" /* UNUSED macro */
#include "../cmdline/parse.h" /* command line parser */
#include "../y86/elf.h" /* Mini-ELF format */
int
main (int argc UNUSED, char **argv UNUSED)
{
printf ("Congratulations, you have compiled your source code!\n");
bool header = false;
char **file = NULL;
parse_command_line (argc, argv, &header, file);
return 0;
}
And the file unused.h (because the compiler will make unused variables an error instead of warning) is as follows:
#ifndef __UNUSED__
#define __UNUSED__
#define UNUSED __attribute__ ((unused))
#endif
The code doesn't check the return value of fopen, which will be NULL in case of an error. Dereferencing NULL in the fread call causes a segfault.
I am very new to the C language and have been working on a program that takes either a text file or input from the keyboard and either turns all the letters to capitals, lowercase, or rotates them by 13 places depending on the input given by the user which should go something like: ./tconv -u test.txt
This should, in theory, turn all the letters in test.txt to uppercase letters. If no file is given, ./tconv -u, then it should take input from the keyboard.
I think I am missing something fairly simple, but when I run it with any -r,-u, or -l arguments, it says it cannot read "-r", "-u", or"-l". What am I missing?
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
FILE*
input_from_args(int argc, const char *argv[])
{
if (argc==1){
return stdin;
}else{
return fopen(argv[1],"r");
}
}
int
rot13(c)
{
int e;
int ROT;
ROT = 13;
if(c>='A' && c <='Z'){
if((e=c+ROT)<='Z')
return e;
else{
e = c - ROT;
return e;
}
}
else{
return c;
}
}
int
main(int argc, const char*argv[])
{
FILE *src = input_from_args(argc,argv);
FILE *dest = stdout;
if (src == NULL){
fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
char *rotate = "-r";
char *lower = "-l";
char *upper = "-u";
int i;
i =0;
int ch;
while ((ch = fgetc(src))!=EOF){
if (strcmp(upper,argv[i])==0){
fprintf(dest,"%c",toupper(ch));
}
else if (strcmp(lower,argv[i])==0){
fprintf(dest,"%c",tolower(ch));
}
else if (strcmp(rotate,argv[i])==0){
fprintf(dest,"%c",rot13(ch));
}
else{
fprintf(dest,"%c",ch);
}
}
fclose(src);
return EXIT_SUCCESS;
}
Argv[0] is your program, argv[1] is your flag and argv[2] is the file name if you provided one. You are trying to open argv[1] a file named "-r"
If you are using linux platform you can look into getopt_long to parse the command line options. it is provided by GNU for linux systems and it is system dependent. For system independent command line parsing Google gflags
This will help to reduce the overhead of command line parsing and you can concentrate more on program logic.
Here is an example taken from http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/* Flag set by ‘--verbose’. */
static int verbose_flag;
int
main (int argc, char **argv)
{
int c;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
{"verbose", no_argument, &verbose_flag, 1},
{"brief", no_argument, &verbose_flag, 0},
/* These options don’t set a flag.
We distinguish them by their indices. */
{"add", no_argument, 0, 'a'},
{"append", no_argument, 0, 'b'},
{"delete", required_argument, 0, 'd'},
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "abc:d:f:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'a':
puts ("option -a\n");
break;
case 'b':
puts ("option -b\n");
break;
case 'c':
printf ("option -c with value `%s'\n", optarg);
break;
case 'd':
printf ("option -d with value `%s'\n", optarg);
break;
case 'f':
printf ("option -f with value `%s'\n", optarg);
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
/* Instead of reporting ‘--verbose’
and ‘--brief’ as they are encountered,
we report the final status resulting from them. */
if (verbose_flag)
puts ("verbose flag is set");
/* Print any remaining command line arguments (not options). */
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
putchar ('\n');
}
exit (0);
}
I have to programm a TCP/UDP Server/Client software.
Possible arguments: -u: UDP -t: TCP -l Server -p: [Port] -h [IP]
I wrote a function printflags, to see if everything works fine.
The u-, t-, l- and p-Options work fine. But my IP is everytime NULL.
Where is the problem?
#include <ctype.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
int printflags(int, int, int, char *,char *);
int main(int argc, char *argv[]){
int uflag=0;
int tflag=0;
int lflag=0;
char *pvalue = NULL;
char *hvalue = NULL;
int c;
opterr = 0;
while((c = getopt (argc, argv, "utlhp:")) != -1)
{
switch(c)
{
case 'u':
uflag = 1;
break;
case 't':
tflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'p':
pvalue = optarg;
break;
case 'h':
hvalue = optarg;
break;
case ':':
fprintf(stderr, "case :");
case '?':
if(optopt == 'p' || optopt == 'h')
fprintf(stderr, "Option '-%c' requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option character '-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character '%x'.\n", optopt);
return 1;
default:
abort();
}
}
printflags(uflag, tflag, lflag, pvalue, hvalue);
return 0;
}
int printflags(int uflag, int tflag, int lflag, char* pv, char *hv){
printf("-u UDP: %d\n", uflag);
printf("-t TCP: %d\n", tflag);
printf("-l Listen Socket - Server: %d\n", lflag);
printf("-p Port: %s\n", pv);
printf("-h IP: %s\n", hv);
return 0;
}
Your option string should be "utlh:p:". You need a colon after each letter that takes an optarg.
Your parameter to getopt() needs a colon after the h to signify that -h needs an argument.
while((c = getopt (argc, argv, "utlh:p:")) != -1)
// ^ --- here
Hi following code sample seems to have some problem or either it has to do something with OS internals.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static void usage();
#define MONC_AM_CONF "/tmp/monc.conf"
int main(int argc, char **argv)
{
int ch;
char list[200];
int len;
int fd;
memset(list,0,200);
if (argc < 11) {
usage();
exit(1);
}
while ((ch = getopt(argc, argv, "N:S:P:H:R:")) != -1) {
switch (ch) {
case 'N':
len = strlen(optarg) + 2;
sprintf(list,"%s::",optarg);
break;
case 'S':
sprintf(list+len,"%s::",optarg);
len = len + strlen(optarg) + 2;
break;
case 'P':
sprintf(list+len,"%s::",optarg);
len = len + strlen(optarg) + 2;
break;
case 'H':
sprintf(list+len,"%s::",optarg);
len = len + strlen(optarg) + 2;
break;
case 'R':
sprintf(list+len,"%s ",optarg);
len = len + strlen(optarg);
break;
default:
printf ("You specified a parameter I don't "
"know about.\n");
break;
}
}
argc -= optind;
argv += optind;
printf("Total length of string is %d\n",len);
printf("The string is %s\n",list);
fd = open(MONC_AM_CONF, O_WRONLY|O_CREAT|O_APPEND, 0644);
lseek(fd, 0,SEEK_END);
write(fd,list,len);
close(fd);
return 0;
}
static void usage()
{
printf("Please provide the command in correct format\n");
printf("monc_am_config -N <Comp_Name> -S <Start Script Path> -P <Stop Script Path> -H <HealthCheck Script Path> -R <Recovery Policy>\n");
return ;
}
When I am issuing commands I am getting the output file each time different.
I am expecting execution of this program each time should write the information in the new line but it is writing into the same line in the file.
Plz help.
You should append "\n" to list, i.e
list[len++] = '\n';
So I'm in Linux and I want to have a program accept arguments when you execute it from the command line.
For example,
./myprogram 42 -b -s
So then the program would store that number 42 as an int and execute certain parts of code depending on what arguments it gets like -b or -s.
You could use getopt.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int bflag = 0;
int sflag = 0;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "bs")) != -1)
switch (c)
{
case 'b':
bflag = 1;
break;
case 's':
sflag = 1;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("bflag = %d, sflag = %d\n", bflag, sflag);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
In C, this is done using arguments passed to your main() function:
int main(int argc, char *argv[])
{
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
More information can be found online such as this Arguments to main article.
Consider using getopt_long(). It allows both short and long options in any combination.
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
/* Flag set by `--verbose'. */
static int verbose_flag;
int
main (int argc, char *argv[])
{
while (1)
{
static struct option long_options[] =
{
/* This option set a flag. */
{"verbose", no_argument, &verbose_flag, 1},
/* These options don't set a flag.
We distinguish them by their indices. */
{"blip", no_argument, 0, 'b'},
{"slip", no_argument, 0, 's'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
int c = getopt_long (argc, argv, "bs",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'b':
puts ("option -b\n");
break;
case 's':
puts ("option -s\n");
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
if (verbose_flag)
puts ("verbose flag is set");
/* Print any remaining command line arguments (not options). */
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
putchar ('\n');
}
return 0;
}
Related:
Which command line commands style do you prefer?
What is the general syntax of a Unix shell command?
Take a look at the getopt library; it's pretty much the gold standard for this sort of thing.
Instead of getopt(), you may also consider using argp_parse() (an alternative interface to the same library).
From libc manual:
getopt is more standard (the
short-option only version of it is a
part of the POSIX standard), but using
argp_parse is often easier, both for
very simple and very complex option
structures, because it does more of
the dirty work for you.
But I was always happy with the standard getopt.
N.B. GNU getopt with getopt_long is GNU LGPL.
Other have hit this one on the head:
the standard arguments to main(int argc, char **argv) give you direct access to the command line (after it has been mangled and tokenized by the shell)
there are very standard facility to parse the command line: getopt() and getopt_long()
but as you've seen the code to use them is a bit wordy, and quite idomatic. I generally push it out of view with something like:
typedef
struct options_struct {
int some_flag;
int other_flage;
char *use_file;
} opt_t;
/* Parses the command line and fills the options structure,
* returns non-zero on error */
int parse_options(opt_t *opts, int argc, char **argv);
Then first thing in main:
int main(int argc, char **argv){
opt_t opts;
if (parse_options(&opts,argc,argv)){
...
}
...
}
Or you could use one of the solutions suggested in Argument-parsing helpers for C/UNIX.