From owner-freebsd-questions@FreeBSD.ORG Mon Sep 11 18:57:19 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 80E5F16A47E for ; Mon, 11 Sep 2006 18:57:19 +0000 (UTC) (envelope-from cswiger@mac.com) Received: from smtpout.mac.com (smtpout.mac.com [17.250.248.183]) by mx1.FreeBSD.org (Postfix) with ESMTP id B2BCD43D6B for ; Mon, 11 Sep 2006 18:57:17 +0000 (GMT) (envelope-from cswiger@mac.com) Received: from mac.com (smtpin08-en2 [10.13.10.153]) by smtpout.mac.com (Xserve/8.12.11/smtpout13/MantshX 4.0) with ESMTP id k8BIvHc8028166; Mon, 11 Sep 2006 11:57:17 -0700 (PDT) Received: from [17.214.13.96] (a17-214-13-96.apple.com [17.214.13.96]) (authenticated bits=0) by mac.com (Xserve/smtpin08/MantshX 4.0) with ESMTP id k8BIvFSi024333; Mon, 11 Sep 2006 11:57:16 -0700 (PDT) In-Reply-To: <294439d20609110527m2093cde8r4a93e391826f2b17@mail.gmail.com> References: <294439d20609110527m2093cde8r4a93e391826f2b17@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <7557EBA1-58B4-4EB9-B43E-6FE3416573F7@mac.com> Content-Transfer-Encoding: 7bit From: Chuck Swiger Date: Mon, 11 Sep 2006 11:57:14 -0700 To: Amarendra Godbole X-Mailer: Apple Mail (2.752.2) X-Brightmail-Tracker: AAAAAQAAA+k= X-Language-Identified: TRUE Cc: freebsd-questions@freebsd.org Subject: Re: Sequence of execution of getopt() and usage()... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Sep 2006 18:57:19 -0000 On Sep 11, 2006, at 5:27 AM, Amarendra Godbole wrote: > This is a general FreeBSD source related question, and I am posting it > here, as it did not fit in any other FreeBSD lists... This list is a quite reasonable choice to ask such questions. :-) > While browsing through sources for different userland utilities (cat, > chmod, and so on), I noticed that in main(), first getopt() is called > in a while loop, and then the check for the number of arguments passed > is done. Something like this (from chmod.c): [ ... ] > Can't we check for the number of arguments *before* calling getopt()? [ ... ] > I observe a similar pattern in other utilities too - which might mean > that there was a sound reason as to why it was done this way. Can > someone be kind enough to explain this? Thanks in advance! Sure. The issue is that utilities which require a certain number of arguments do not want to count the option flags being passed in, but argc's count includes these flags and any values being passed to flags which take a value (ie, getopt() options followed by a colon ":"). It's much easier to process the options and then do "argc -= optind", and then determine whether the remaining # of arguments left meet the criteria for the particular program. -- -Chuck