ARGC/ARGV Strangeness - Code is working

Chris F.A. Johnson cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Wed Sep 21 04:20:03 UTC 2005


On Wed, 21 Sep 2005, William Park wrote:

> On Tue, Sep 20, 2005 at 11:45:50PM -0400, pking123-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org wrote:
>> Below is the code that finally works. I have by now heavily modified
>> this program to, first of all, go forward in the list rather than
>> backward; as well as to accept a range of numbers rather than a list.
>> So, I christen this first effort "backascii.c" :-)
>>
>> The new, properly-working program is completed and is the one called
>> "ascii.c". Thanks to all who made suggestions. Biggest thanks goes to
>> Peter Fletcher, who made valuable suggestions, especially regarding
>> debugging.
>>
>> Paul King
>>
>> ----------8<--------snip----------8<--------snip---------------
>>
>> #include <stdio.h>
>> #include <strings.h>
>>
>> int main (int argc, char *argv[]) {
>>    /* this only takes a number as an argument */
>>    int num = 0;
>>    int count;
>>    char p1[5];
>>    for (count = argc - 1; (count > 0); count--) {
>>       strcpy(p1, argv[count]);
>
> Bad habit.  What happens if your argument is longer than 4 chars?  Try
> using strncpy(3).

    But why copy it at all?

        num = atoi(argv[count]);

>>       num = atoi(p1);
>>       printf ("%d\t'%c'\n", num, num);
>>    }
>>    return 0;
>> }
>
> Of course, in shell script, it becomes
>    ascii()
>    {
> 	for i; do
> 	    echo "$i	`tostring $i`"

    "tostring"? Is that part of your bash extension, William?

> 	done
>    }

    In awk, it's:

awk 'BEGIN { sq = "\047" }
           { for (i = 1; i <= NF; ++i )
                printf "%d\t%s%c%s\n", $i, sq, $i,sq
           }'


-- 
     Chris F.A. Johnson                     <http://cfaj.freeshell.org>
     ==================================================================
     Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
     <http://www.torfree.net/~chris/books/cfaj/ssr.html>
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list