C command line parsing

I am attaching my code and I would like to take the command line argument and put it into buff.  I would like an example on how to get that done.  It seems to me it should not take to many lines of code to do it.  I need to change the line that assigns "Now is the time for all good men" to buff so it will have the info from the command line argument.  Do I just need to change the line in the for statement to assign the command line argument to buff rather than just print it?  Thanks.


    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    int main(int argc, char *argv[])
    {
    int fd;
    int wd=0;
   
    speed_t baud = B9600; /* baud rate */  

    //unsigned char buff[] = { 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x01, 0x21, 0x03};
    unsigned char buff[] = "Now is the time for all good men\n\r";

    int x;
    for (x=0; x<argc; x++)
      printf("%s\n",argv[x]);
   
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
    if (fd == -1)
    {     fprintf(stderr, "open_port: Unable to open /dev/ttyS0 - %s\n",
                            strerror(errno));
    }
    else
    {
     
      struct termios settings;
      tcgetattr(fd, &settings);

      cfsetospeed(&settings, baud); /* baud rate */
      settings.c_cflag &= ~PARENB; /* no parity */
      settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
      settings.c_cflag &= ~CSIZE;
      settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
      settings.c_lflag = ICANON; /* canonical mode */
      settings.c_oflag &= ~OPOST; /* raw output */

      tcsetattr(fd, TCSANOW, &settings); /* apply the settings */
      tcflush(fd, TCOFLUSH);      
     
     
      printf("%d", fd); //i get fd value to be 3
      fcntl(fd, F_SETFL, 0);
      wd=write(fd, buff, strlen(buff));
    }
      printf("%d", wd); //i get wd to be -1, so what is the problem??
      close(fd);
      return 61;
    }
sargent240Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkrConnect With a Mentor Commented:
Hmm, I tried

#include <stdlib.h> // for 'malloc()'

//...

int main (int argc, char** argv)
{

unsigned char* buff; // a pointer,memory will be allocated dynamically

    size_t sz = 0; // to cont the length of all arguments
    int x;
    for (x=1; x<argc; x++)
      sz += strlen(argv[x]) + 1; // plus a separating space

    buff = (unsigned char*) malloc(sz + 1); // plus the NULL terminator
    *buff = '\0';

    for (x=1; x<argc; x++) {
      strcat(buff,argv[x]);
      if (x < argc - 1) strcat(buff," "); // also add a blank seperator to all but the last arg
    }

// rest of your code

    free(buff); // free the memory again

    return 0;
}                                            

Open in new window


and that compiles and runs fine - strange.
0
 
jkrCommented:
I assume you want to put the complete command line (i.e. all arguments instead of the image name itself info 'buff'? that would be like

#include <memory.h> // for 'malloc()'

//...

unsigned char* buff; // a pointer,memory will be allocated dynamically

    size_t sz = 0; // to cont the length of all arguments
    int x;
    for (x=1; x<argc; x++)
      sz += strlen(argv[i]) + 1; // plus a separating space

    buff = (unsigned char*) malloc(sz + 1); // plus the NULL terminator
    *buff = '\0';

    for (x=1; x<argc; x++) {
      strcat(buff,argv[i]);
      if (x < argv - 1) strcat(buff," "); // also add a blank seperator to all but the last arg
    }

// rest of your code

    free(buff); // free the memory again

Open in new window

0
 
n2fcCommented:
see:  http://einstein.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line

char buff[2000]="";

for (int x=1;  x<argc;  x++)
      strcat(buff, argv[x]);

Open in new window


Note:

1. The loop starts at 1 to SKIP the command itself; start at 0 to include it!

2. The arguments will be concatenated with NO separators.

3. Make sure to allocate an appropriate buffer size, or it may overflow!

0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
sargent240Author Commented:
Thank YOU!

This is my present code and when I compile it is get a warning:

al@linux-5t7y:~> gcc serialInC.c
serialInC.c: In function ‘main’:
serialInC.c:32:29: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]                                    
serialInC.c:37:13: warning: comparison between pointer and integer [enabled by default]                                                            


I must have screwed up somewhere along the line
0
 
n2fcCommented:
You will need to post FULL source code to debug the inappropriate use of malloc()...
Probably a typo...

Note that if your command line size is limited, it is unnecessary to use malloc to allocate the buffer!   A buffer slightly larger than the command line is all that is needed!  (See my example above)...
0
 
jkrCommented:
Sorry, typo on my side, that should have been

    for (x=1; x<argc; x++) {
      strcat(buff,argv[i]);

      // argc, not argv as in my 1st comment
      if (x < argc - 1) strcat(buff," "); // also add a blank seperator to all but the last arg
    }

Open in new window

0
 
jkrCommented:
BTW, using a static buffer size here is possible - yet then it's always possible to crash your program.
0
 
sargent240Author Commented:
I posted the source and error message a moment ago.  Did you get It?  Thanks
0
 
sargent240Author Commented:
let me try it this way.  File attached.serialInC.c

al@linux-5t7y:~> gcc serialInC.c
serialInC.c: In function ‘main’:
serialInC.c:32:29: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
serialInC.c:72:7: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
0
 
jkrCommented:
Nope - as you can see, there's nothing ;o)
0
 
sargent240Author Commented:
you should have it on this last post.
0
 
jkrCommented:
Yes, sorry, that was about the same time. Try replacing 'memory.h' with 'stdlib.h'
0
 
sargent240Author Commented:
the compile error is gone but when I execute it I get a Seg fault.
0
 
sargent240Author Commented:
I execute it with this command:

./a.out This works
0
 
sargent240Author Commented:
You mentioned in an earlier post that the line;

char buff[2000]="";

should be added some where but as you can see I added:

char buff[256]="";

but that gave me a compiler error about a duplicate declaration.
0
 
sargent240Author Commented:
did you try to compile and run the code I posted last?
0
 
sargent240Author Commented:
I found the problem.  In our original post you used the variable i in two places where it should have been x.  I'll put my code back in again and let you know.
0
 
jkrCommented:
Well, that I saw also, yet I assumed you had fixed it, since it wouldn't compile otherwise. And no, unfortunately I can't try your original code at the moment.
0
 
sargent240Author Commented:
Good, Good, Good.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.