Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   (View Single Post)  
Old 14th March 2013
bsdplus bsdplus is offline
Real Name: Alan Cheng
Port Guard
Join Date: Jun 2009
Location: Shanghai, China
Posts: 21
Default Question on getopt and optind


In getopt(3) man page, after arguments are processed in the while loop, there are two lines:

           argc -= optind;
           argv += optind;
Could somebody help explain the purpose of these two lines?

I played with the example code in getopt man page, "argc -= optind" seems to set argc to 0.
I also understand that "argv += optind" make the argv pointer pass over current argument string. But just could not figure out why we need to do that.


example code in getopt (3) man page:
           int bflag, ch, fd;

           bflag = 0;
           while ((ch = getopt(argc, argv, "bf:")) != -1) {
                   switch (ch) {
                   case 'b':
                           bflag = 1;
                   case 'f':
                           if ((fd = open(optarg, O_RDONLY, 0)) == -1)
                                   err(1, "%s", optarg);
                           /* NOTREACHED */
           argc -= optind;
           argv += optind;
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ports version of getopt fridder FreeBSD Ports and Packages 0 8th July 2011 06:19 PM
PF question bug0r OpenBSD Security 7 23rd November 2009 03:54 PM
external drive partition question + fdisk question gosha OpenBSD General 15 15th June 2009 02:00 PM
DR-DOS question RJPugh Other OS 4 31st May 2009 11:10 AM
Question about DNS. bigb89 FreeBSD General 10 22nd May 2008 06:08 AM

All times are GMT. The time now is 02:02 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick