From owner-freebsd-questions@FreeBSD.ORG Mon Sep 11 12:28:01 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 69C4116A403 for ; Mon, 11 Sep 2006 12:28:01 +0000 (UTC) (envelope-from amarendra.godbole@gmail.com) Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181]) by mx1.FreeBSD.org (Postfix) with ESMTP id EF6F843D58 for ; Mon, 11 Sep 2006 12:28:00 +0000 (GMT) (envelope-from amarendra.godbole@gmail.com) Received: by py-out-1112.google.com with SMTP id o67so2005914pye for ; Mon, 11 Sep 2006 05:27:27 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=QJgKwuePaD3UKcrtjwbLaSSp8IHwY10/ErPaYVgWthu9VPxSnLoRi+EXplZq5V/MoID3rkIAb3VPIv5iLC9P6UC60zz8nCMLi+ttU0/Qt0C0ZpyYSGoT1nsSI7wuejUwYeWiEBYCeUxY7nSRIjD3XzGUU0YFUcM/MYylm7aBLVo= Received: by 10.35.39.13 with SMTP id r13mr8766424pyj; Mon, 11 Sep 2006 05:27:27 -0700 (PDT) Received: by 10.35.108.6 with HTTP; Mon, 11 Sep 2006 05:27:25 -0700 (PDT) Message-ID: <294439d20609110527m2093cde8r4a93e391826f2b17@mail.gmail.com> Date: Mon, 11 Sep 2006 17:57:26 +0530 From: "Amarendra Godbole" To: freebsd-questions@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: 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 12:28:01 -0000 Hi, This is a general FreeBSD source related question, and I am posting it here, as it did not fit in any other FreeBSD lists... 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): int main(int argc, char *argv[]) { ... while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1) ... if (argc < 2) usage(); ... } Can't we check for the number of arguments *before* calling getopt()? Something like: int main(int argc, char *argv[]) { ... if (argc < 2) usage(); ... while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1) ... } This might make it a bit more efficient, though I don't have numbers' to prove this. 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! Best, Amarendra