www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/07/10:48:13

Date: Wed, 7 May 1997 10:30:36 -0400 (EDT)
From: "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com>
To: Piotr Sieklucki <pas AT avanti DOT orient DOT uw DOT edu DOT pl>
Cc: djgpp AT delorie DOT com
Subject: Re: Help needed with command line params.
In-Reply-To: <336E468C.35@avanti.orient.uw.edu.pl>
Message-Id: <Pine.D-G.3.91.970507095612.15633B-100000@dg1>
Mime-Version: 1.0

On Mon, 5 May 1997, Piotr Sieklucki wrote:

> Hello fellow DJGPPers,
> 
> I would like to ask for some advice. How do you read the command-line 
> parameters given to a program at runtime, and copy them to strings?
> Basically, I want to get the 2nd and 4th (given that the 1st is the
> full pathname to the program) parameters into strings <2nd> and <4th>.
> I realise that this is bloody simple, but I just seem to be bloody
> stupid. Anyone out there who can give me a helping hand, plz? TIA.
> 
> 						Piotr Sieklucki
> 					<pas AT avanti DOT orient DOT uw DOT edu DOT pl>
> 
> 

Simple question, long answer.  I will give you two answers by example.
The first is the basic command line parsing.  The second more advanced
and MUCH easier is to allow getopt() to do most of the work.  In the 
first example I will not deal with parsing flags and flag arguments 
because the possible permutations of multiple flags combined and flag 
args following flags immediately or after other flags are just too 
complicated for this answer, besides getopt does it all for you ( and if 
you get the GNU getopt() function it even does long flags (like 
--BackupFile as a synonym for -b) and argument reordering.


1) Basic command line:

int main( int argc, char **argv )
{
    char *ProgramName, FirstArgCopy[100];
    long SecondArg;
......

    ProgramName = argv[0];  /* Just copy the pointer here. */
    if (argc < NumExpectedArgs) {
        usage( ProgramName );
        exit(1);
    }

    strncpy( FirstArgCopy, argv[1], 99 );  /* Copy the string itself. */
    FirstArgCopy[99] = (char)0;  /* In case the argument is longer than
                                   99 characters strncpy doen't add
                                   null */
    SecondArg = atol( argv[2] );

.....
}


2) Using getopt()

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
    char *ProgramName, FirstArgCopy[100];
    long SecondArg;
    int AppendFlag;
    char BackUpFile[1025],FileToBackup[1025];
......

    ProgramName = argv[0];  /* Just copy the pointer here. */
    if (argc < MinimumExpectedArgs) {
        usage( ProgramName );
        exit(1);
    }

    AppendFlag = 0;
    BackUpFile[0] = (char)0;

    /* Process all flags and flags taking arguments. */
    while ((ch = getopt( argc, argv, "ab:" )) != EOF)
        switch(ch) {
          case 'a':
            AppendFlag = 1;
            break;
          case 'b':
            strncpy( BackUpFile, optarg, 1024 );  /* Copy the string itself. */
            BackUpFile[1024] = (char)0;
        }

    /* Process any non-flag arguments following the flags using optind
      as index to next argument as in example 1) above. */

    /* getopt() sets the global variable optind set to the next
      argument to process.  After the loop above it is pointing to the
      first command line argument following all flags and their args. */

    for( ; optind < argc; optind++ ) {
	strncpy( FileToBackup, argv[optind], 1024 );
        FileToBackup[1024] = (char)0;
        .....
    }

Art S. Kagel, kagel AT bloomberg DOT com


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019