Re: Fear
Re: Fear
- Subject: Re: Fear
- From: "Phill Kelley" <email@hidden>
- Date: Thu, 7 Jun 2001 10:06:23 +1000
>
Date: Wed, 06 Jun 2001 12:29:26 -0700
>
Subject: Re: Fear
>
From: Scott <email@hidden>
>
To: <email@hidden>
>
CC: Brian Howard <email@hidden>
[snip]
>
I value Brian's original question (about main(), etc.) and don't mind taking
>
a step back to help someone out.
>
>
Brian, I've found nothing but contempt from some of these people, so I'd
>
ignore them and keep asking your questions. No one's obligated to answer if
>
they don't want to.
I totally agree with Scott. In order to assist Brian, here's a short
excerpt from my 1978 edition of Kernighan & Ritchie. This is a book I tend
to reach for precisely because it *is* so terse:
---Begin Quote---
In environments that support C, there is a way to pass command-line
arguments or parameters to a program when it begins executing. When 'main'
is called to begin execution, it is called with two arguments. the first
(conventionally called 'argc') is the number of command-line arguments the
program was invoked with; the second ('argv') is a pointer to an array of
character strings that contain the arguments, one per string.
The simplest illustration of the necessary declarations and use is the
program echo, which simply echoes its command-line arguments on a single
line, separated by blanks. That is, if the command:
echo hello, world
is given, the output is:
hello, world
By convention, argv[0] is the name by which the program was invoked, so
argc is at least 1. In the example above, argc is 3, and argv[0], argv[1]
and argv[2] are 'echo', 'hello,' and 'world' respectively.
---End Quote---
Merging Kernighan & Ritchie's example with the "Hello World" example from
the Learning Cocoa book produces something like this:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int i;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
for (i = 1; i < argc; i++)
printf("%s%c", argv[i], (i<argc-1) ? ' ' : '\n');
[pool release];
return 0;
}
Build that as a Foundation Tool and give it a name like "myecho". Switch to
a terminal window, change into your ~/myecho/build subdirectory and enter
the command:
./myecho hello, world
and what comes back is:
hello, world
Someone else on this list suggested that there probably wasn't much of a
place for Foundation Tools in Mac OS X but I disagree. I think there are
often situations when it makes a lot of sense to forgo the beauty and
elegance of an Aqua app in favour of a quick-and-dirty tool that solves the
immediate problem. For me, that is part of the attraction of having Unix
under the hood.
Regards, PK
- Follow-Ups:
- Re: Fear
- From: Rob Rix <email@hidden>
- Re: Fear
- From: David P Henderson <email@hidden>