On Sat, Nov 18, 2006 at 06:12:17PM +0100, =?ISO-8859-2?q?Roman =A9ustek?= wrote:
> do anybody know source code of snmpset?
>
> http://www.opensource.apple.com/darwinsource/10.2/net_snmp-7/net-snmp/apps/snmpset.c
>
> I need to adjust it in the way that the main function would copy the
> parameters from the memory and no from command line. From structure, what
> would be exactly the same as a argv structure. So I changed name of main
> function to for example int set (int argc, char *argv[]) /this parametres are
> local, that\'s means that they aren\'t the same sturcture as argv form
> command line/.then created such structure as argv
>
> int myargc = 9;
>
> char *myarvg [9];
>
> myargv [0] = \".....\";
> ..
> ..
> myargv [8];
>
> and called
>
> set (myargc, myargv);
>
> but the result was Segmentation Fauld.
>
>
> Could anybody help me figure out where is the problem ? Thanks
There are several issues here, one of which is an outright bug, but this is
a poor way to solve this problem in any case. The better way to do this is
from a shell script, not modifying the source.
But let's say that you are determined to do it this way, it's much wiser
to let the compiler do the counting for you:
char *myargv[100]; // room for plenty
char **pargv = myargv; // starts at the front
*pargv++ = "program name";
*pargv++ = "arg1";
*pargv++ = ...
*pargv++ = "last arg here";
*pargv = 0; // you MUST include NULL termination
/* argc argv */
set( (int)(pargv - myargv), myargv);
Note #1: there's no way to get the count wrong, because pargv maintains
it for you. Just imagine using fixed indexes with either #ifdef code, or
runtime if/else code - it would be a nightmare. Pointers are your friend.
Note #2: I think your bug is failing to include a final NULL parameter in
the argument list. This is part of the spec, and it's common for this to
be *relied* on by consumers of argc/argv, BUT: the NULL is not included
in the count.
Note #3: string constants are really /const/, but pointed-to argv members
are supposed to be writable. It's bad form to mix them, it *will* fault
on some platforms if the strings are actually modified, so you really should
use writable strings. The ugly way:
char *myargv[100]; // room for plenty
char **pargv = myargv; // starts at the front
*pargv++ = strdup("program name");
*pargv++ = strdup("arg1");
*pargv++ = ...
*pargv++ = strdup("last arg here");
*pargv = 0;
It's cheating and ugly, but then so is modifying snmpset in this way, so it
probably would work fine.
Good luck,
Steve
---
Stephen J Friedl | Security Consultant | UNIX Wizard | +1 714 544-6561
www.unixwiz.net | Tustin, Calif. USA | Microsoft MVP | [EMAIL PROTECTED]
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders