okay, maybe i am missing the point totally with structures. here is my code, let
me know whats wrong with it:

struct utsname *host_uname;

host_uname = (struct utsname *)malloc(sizeof(struct utsname));
uname(host_uname);

system_os = malloc(strlen(host_uname->sysname)+1);
strncpy(system_os, (host_uname->sysname), sizeof(system_os));

system_os is type char *. i cant see anything wrong with this code, but then
again ive never worked extensively with structs.

thanks for any help

Benjamin Scott wrote:

> On Thu, 22 Feb 2001, Tony Lambiris wrote:
> > when I set a pointer to the structure, and say, get the OS version:
> >
> > struct utsname *host_uname;
>
>   Are you allocating a memory buffer and pointing host_uname to it before
> invoking uname:
>
>         struct utsname buf;
>         uname(&buf);
>
> > os_version = malloc(strlen(host_uname->release));
>
>   You do not declare os_version in the code sample you give.  I will assume
> you mean:
>
>         char * os_version;
>
>   If you intend to copy the string pointed to by host_uname->release, then you
> need to malloc one extra byte for the terminating NUL byte:
>
>         os_version = malloc( strlen(buf->release) + 1 );
>
> > os_version = (host_uname->release);
>
>   Be aware that this does *NOT* copy the string pointed to by
> host_uname->release to the buffer pointed to by os_version.  Instead, it
> copies the *pointer* contained in host_uname to os_version (which, hopefully,
> is also a pointer).  This is fine, but it means that if you destroy
> host_uname, os_version will no longer be valid.
>
>   If you intended to copy the string, you need to use:
>
>         strcpy (os_version, host_uname->release);
>
> But you should really use:
>
>         strncpy (os_version, host_uname->release, SIZE_OF_OS_VERSION_BUFFER);
>
> That will prevent buffer overflow bugs.
>
> > or perhaps I just suck at coding??
>
>   I would not go that far, but it is apparent you do not fully understand
> string handling in C.  That is pretty common; C string handling is notoriously
> hard to master for new users, especially those used to other languages with a
> built-in "string" type.  I recommend _The C Programming Language_, Second
> Edition, by Kernighan and Ritchie.  Actually, I recommend two copies:  One to
> use, and one to place in a shrine.  ;-)
>
> --
> Ben Scott <[EMAIL PROTECTED]>
> Net Technologies, Inc. <http://www.ntisys.com>
> Voice: (800)905-3049 x18   Fax: (978)499-7839
>
> **********************************************************
> To unsubscribe from this list, send mail to
> [EMAIL PROTECTED] with the following text in the
> *body* (*not* the subject line) of the letter:
> unsubscribe gnhlug
> **********************************************************


**********************************************************
To unsubscribe from this list, send mail to
[EMAIL PROTECTED] with the following text in the
*body* (*not* the subject line) of the letter:
unsubscribe gnhlug
**********************************************************

Reply via email to