From: Mark Hobley on
In comp.os.linux.development.apps Mark Hobley <markhobley(a)hotpop.donottypethisbit.com> wrote:

> extern char *progname;

Hmmm. It's not external now though. I changed this to:

char *progname;

When I compile, I get an error:

getname.c: In function 'main':
getname.c:96: error: 'optind' undeclared (first use in this function)
getname.c:96: error: (Each undeclared identifier is reported only once
getname.c:96: error: for each function it appears in.)

That error looks right to me: 'optind' is undeclared. I wonder if this is an
error in the original bsd code, or whether optind becomes defined in the
bsd versions of the header files, or the optind becomes defined when the bsd
compiler is used.

The headers are:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>

The specification says:

SYNOPSIS

#include <unistd.h>

int getopt(int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;

Maybe optind is defined in unistd.h

I added the declaration to the top of the file, and now I get an error:

getname.c:(.text+0x304): undefined reference to `strlcat'

There is a string.h file, so I don't know what is going on there.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

From: Ian Collins on
Mark Hobley wrote:
> In comp.os.linux.development.apps Mark Hobley <markhobley(a)hotpop.donottypethisbit.com> wrote:
>
>> extern char *progname;
>
> Hmmm. It's not external now though. I changed this to:
>
> char *progname;
>
> When I compile, I get an error:
>
> getname.c: In function 'main':
> getname.c:96: error: 'optind' undeclared (first use in this function)

Then you haven't included the header where it is declared.

> getname.c:96: error: (Each undeclared identifier is reported only once
> getname.c:96: error: for each function it appears in.)
>
> That error looks right to me: 'optind' is undeclared. I wonder if this is an
> error in the original bsd code, or whether optind becomes defined in the
> bsd versions of the header files, or the optind becomes defined when the bsd
> compiler is used.
>
> The headers are:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <libgen.h>

Well given the synopsis below, you are missing <unistd.h>.

> The specification says:
>
> SYNOPSIS
>
> #include <unistd.h>
>
> int getopt(int argc, char * const argv[], const char *optstring);
> extern char *optarg;
> extern int optind, opterr, optopt;
>
> Maybe optind is defined in unistd.h

No, it may be declared there, but it will be defined in a library source
file.

> I added the declaration to the top of the file, and now I get an error:
>
> getname.c:(.text+0x304): undefined reference to `strlcat'
>
> There is a string.h file, so I don't know what is going on there.

It will probably be declared in <strings.h>, see the man page.

--
Ian Collins
From: Seebs on
On 2010-02-28, Ian Collins <ian-news(a)hotmail.com> wrote:
>(someone else)
>> getname.c:(.text+0x304): undefined reference to `strlcat'

>> There is a string.h file, so I don't know what is going on there.

> It will probably be declared in <strings.h>, see the man page.

More likely, it was in <string.h> on the system the OP was porting from,
but isn't in it on most other systems, since it's a BSD extension.
(strlcat/strlcpy are what strncat/strncpy probably should have been).

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Mark Hobley on
In comp.unix.programmer Seebs <usenet-nospam(a)seebs.net> wrote:

> More likely, it was in <string.h> on the system the OP was porting from,
> but isn't in it on most other systems, since it's a BSD extension.
> (strlcat/strlcpy are what strncat/strncpy probably should have been).

Right that is interesting. I am reading the manual pages online, and I have
found something very odd:

The man page for string(3)

http://linux.die.net/man/3/string

(This does not mention strlcat)

From the same website, a man page for strlcat:

http://linux.die.net/man/3/strlcat

Strangely, there is a string.h and a strings.h

I tried included both:

#include <string.h>
#include <strings.h>

That didn't fix it. Maybe it is in unistd.h:

#include <unistd.h>

Nope. That didn't fix it either.

I think you are right. Maybe this is a BSD extension and I am reading the wrong
documents.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

From: santosh on
Mark Hobley <markhobley(a)hotpop.donottypethisbit.com> writes:

> In comp.unix.programmer Seebs <usenet-nospam(a)seebs.net> wrote:
>
>> More likely, it was in <string.h> on the system the OP was porting
>> from, but isn't in it on most other systems, since it's a BSD
>> extension. (strlcat/strlcpy are what strncat/strncpy probably
>> should have been).
>
> Right that is interesting. I am reading the manual pages online,
> and I have found something very odd:
>
> The man page for string(3)
>
> http://linux.die.net/man/3/string
>
> (This does not mention strlcat)
>
> From the same website, a man page for strlcat:
>
> http://linux.die.net/man/3/strlcat
>
> Strangely, there is a string.h and a strings.h
>
> I tried included both:
>
> #include <string.h>
> #include <strings.h>
>
> That didn't fix it. Maybe it is in unistd.h:
>
> #include <unistd.h>
>
> Nope. That didn't fix it either.
>
> I think you are right. Maybe this is a BSD extension and I am
> reading the wrong documents.

It does seem to be a BSD specific function, along with strlcpy().

<http://en.wikipedia.org/wiki/Strlcpy>
<http://www.openbsd.org/cgi-
bin/cvsweb/~checkout~/src/lib/libc/string/strlcat.c?rev=1.13>
<http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy&sektion=3>

Since the source is available, and it's easy to implement your own as
well, with the same semantics, it's not a big portability problem,
AFAICS.