Quote:
|
To be honest, I don't really understand your question.
|
What I was inteersted in, was, what if the program was called like:
Quote:
|
> ./ifc pete john henry -n 4 alex karen
|
in what part of the main() function would your create_file() function be called from ?
Just to see if you understood the checking of optind values agains how the program initialy had been executed.
Quote:
|
I believe optind is the argv[] index of the first command line argument that isn't a switch option?
|
Both yes and no.. optind points to where in argv[] the last occurance of the given possible arguments has been found, but first I want to explain getopt() for you.
getopt()'s last argument, in this case "n:h" tells it which switches to look for, in this case -n and -h, since theres a ':' emidiately following the 'n' this tells getopt() to place the next item in argv[] in the internal optarg variable.
Your program is a very simple program, when dealing with getopt(), since it only takes these two flags, and since the program emidiately terminates, if the -h switch is met, it means the only flag that is of interrest further down through your main() is the -n.
This can be good, and it can be bad...
I would considder this good, as with ls, where
Quote:
> ls -l file1 file2 file3
> ls file1 -l file2 file3
> ls file1 file2 file3 -l
|
is essentialy the same thing.
Since this provides the user with the abillety to parse your "-n <number>" flag anywhere in the list they want, altho if they were to call it like:
Quote:
|
> ./ifc -n 2 pete brian -n 4 john
|
It would create 4 instances of the filenames in question, and ommit the first -n flag, this would by the logic I've shown you, also make the program try to create files named "-n" and "2" hence the bad part.
When providing the user with this sort of scalability, you need to extend your checking throughout your handling of the parsed arguments, so the program tries to capture all possible ways the the execution could have went.
So the
Will be met, when the execution was performed with
Quote:
|
> ./ifc -n 3 pete john betty
|
Since optind has the value 3, it is pointing at index 3 of argv[], which is where we find "pete" so we know the execution was performed
correct in teh sence as you default would expect the user to use your program.
So we just run through the rest of the arguments and take them as beeing the filenames that should be created.
If forinstance the user had written a whole bunch of filenames, then suddently realizes "
-hey I need 3 of all of them, damned I dont want to cyckle through them all again, and add the '-n 3' flag at the beginning, I'll just add it at the end" Then you get a call to your program which is like
Quote:
|
> ./ifc pete mona john ursula -n 3
|
In this case your
Code:
if (optind == argc || optind > argc)
part of the main() function will handle it..
This however needs you to only cyckle through all given arguments untill it reaches the one just befor '-n', hence the
i < (argc -2)
check in the for() loop, since we know at this stage the two remaining can only be '-n' and '3'.
Should there be some sneaky little prick, who wants to see if he can realy screw up the program execution, perhaps to find any exploitable weaknesses, he might try and fool it by calling it with
Quote:
|
> ./ifc pete joe -n 4 alex karen
|
This is where optind would point somewhere in teh mittle of argv[] so the main function will catch it at
Code:
if (optind > 1 && optind < argc)
But in this case we need to cyckle through all the arguments befor the encountered '-n' aswell as all teh arguments after it, thus it requires two for() loops like
Code:
for(i=1; i < (optind - 2); ++i)
create_file(argv[i], count);
for(i+=2; i < argc; ++i)
create_file(argv[i], count); And the very last else will capture anything which dosn't fit in, like if the program had been executed with
where theres no intention on creating a multiple of the files, but only the single file.
Now one last part hasn't been covered, if a mallicius user realy wanted to screw it up, they would try and call it like:
Quote:
|
> ./ifc -n -1 pete karen brian
|
But since every for() loop which check against the given
count as long as the files created dosn't voershoot the count value, we have it covered, since it's pretty hard to create less than one file.
When veturing into teh world of C, I would suggest to read the
The C book it's a nice intro to the structure of the language, which I guess you've discovered ressembles PHP quite alot.
Since I've guessed you're using a *nix environment, theres quite alot of info to get from the man pages aswell ie
is the same as the online man-pages I've referenced to..
The next big thing, once you get comfortable with C, is to use
autoconf,
automake and
libtool so your programs can be ported to any system.
But to be honest, lerning C in 3 - 4 days would require quite alot of programming experiences within a number of different languages..