public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc and string.h
@ 2001-11-07  9:30 Jonathan Mortimer
  2001-11-07 11:18 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Mortimer @ 2001-11-07  9:30 UTC (permalink / raw)
  To: help-gcc

Hi!

I have been trying to compile a piece of software which was originally
written on Mandrake Linux (or Mandrake GNU, whatever it may be!) but am
having difficulty with the functions strdupa and strsep which both are
missing from my string.h  (I assume they are string functions)  Are they
new additions or specific to Linux?  I wonder if they might be custom
functions which someone has written into their own string.h file. Hmm.

I am running Irix 6.5.9m and (I think) successfully converted the
makefile so it compiles up to the point where it cannot resolve these
two functions, because it can't find them!

Do you know what is going on?  Is my gcc too old? (version 2.95.2)

Please help!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: gcc and string.h
  2001-11-07  9:30 gcc and string.h Jonathan Mortimer
@ 2001-11-07 11:18 ` Andrea 'Fyre Wyzard' Bocci
  0 siblings, 0 replies; 3+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2001-11-07 11:18 UTC (permalink / raw)
  To: Jonathan Mortimer; +Cc: gcc-help


>I have been trying to compile a piece of software which was originally
>written on Mandrake Linux (or Mandrake GNU, whatever it may be!) but am
>having difficulty with the functions strdupa and strsep which both are
>missing from my string.h  (I assume they are string functions)  Are they
>new additions or specific to Linux?  I wonder if they might be custom
>functions which someone has written into their own string.h file. Hmm.

I've just had a look at the man pages and my glibc's string.h file:
strsep() is not ANSI, so it may not be available in your libraries. Try 
using strtok() ?
strdupa() is clearly a GNU extension (at least it looks so, from the 
__extension__ surround its definition).

I think you can work around this functions, but if you really need the (eg. 
they're heavilt used in your code) - and don't have licensing problems with 
LGPL for your work - you can just grab their source from the GNU library.
Otherwise, you might consider using GNU libc on your IRIX machine.

Hope this helps,
Fyre Wyzard

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: gcc and string.h
@ 2001-11-07 16:35 Paolo Carlini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Carlini @ 2001-11-07 16:35 UTC (permalink / raw)
  Cc: gcc-help

Hi,

strictly speaking, your question has not much to do with GCC, but
instead with the specific LIBC you are using on your system...

Anyway, as regards strdup(a) and and strsep, I think that they are not
part of the Standard ISO C Library, but, basically, BSD extensions
provided by the GNU LIBC commonly used on the Linux systems. You may
confer to the GLIBC docs and info pages for details. I'm reporting here
for your convenience the most relevant parts.

Cheers,
Paolo.


 - Function: char * strdup (const char *S)
     This function copies the null-terminated string S into a newly
     allocated string.  The string is allocated using `malloc'; see
     *Note Unconstrained Allocation::.  If `malloc' cannot allocate
     space for the new string, `strdup' returns a null pointer.
     Otherwise it returns a pointer to the new string.

 - Macro: char * strdupa (const char *S)
     This macro is similar to `strdup' but allocates the new string
     using `alloca' instead of `malloc' (*note Variable Size
     Automatic::).  This means of course the returned string has the
     same limitations as any block of memory allocated using `alloca'.

     For obvious reasons `strdupa' is implemented only as a macro; you
     cannot get the address of this function.  Despite this limitation
     it is a useful function.  The following code shows a situation
     where using `malloc' would be a lot more expensive.

          #include <paths.h>
          #include <string.h>
          #include <stdio.h>

          const char path[] = _PATH_STDPATH;

          int
          main (void)
          {
            char *wr_path = strdupa (path);
            char *cp = strtok (wr_path, ":");

            while (cp != NULL)
              {
                puts (cp);
                cp = strtok (NULL, ":");
              }
            return 0;
          }

     Please note that calling `strtok' using PATH directly is invalid.
     It is also not allowed to call `strdupa' in the argument list of
     `strtok' since `strdupa' uses `alloca' (*note Variable Size
     Automatic::) can interfere with the parameter passing.

     This function is only available if GNU CC is used.

//////////////////////////

   Here is a simple example showing the use of `strtok'.

     #include <string.h>
     #include <stddef.h>

     ...

     const char string[] = "words separated by spaces -- and,
punctuation!";
     const char delimiters[] = " .,;:!-";
     char *token, *cp;

     ...

     cp = strdupa (string);                /* Make writable copy.  */
     token = strtok (cp, delimiters);      /* token => "words" */
     token = strtok (NULL, delimiters);    /* token => "separated" */
     token = strtok (NULL, delimiters);    /* token => "by" */
     token = strtok (NULL, delimiters);    /* token => "spaces" */
     token = strtok (NULL, delimiters);    /* token => "and" */
     token = strtok (NULL, delimiters);    /* token => "punctuation" */
     token = strtok (NULL, delimiters);    /* token => NULL */

   The GNU C library contains two more functions for tokenizing a string

which overcome the limitation of non-reentrancy.  They are only
available for multibyte character strings.

 - Function: char * strtok_r (char *NEWSTRING, const char *DELIMITERS,
          char **SAVE_PTR)
     Just like `strtok', this function splits the string into several
     tokens which can be accessed by successive calls to `strtok_r'.
     The difference is that the information about the next token is
     stored in the space pointed to by the third argument, SAVE_PTR,
     which is a pointer to a string pointer.  Calling `strtok_r' with a
     null pointer for NEWSTRING and leaving SAVE_PTR between the calls
     unchanged does the job without hindering reentrancy.

     This function is defined in POSIX.1 and can be found on many
     systems which support multi-threading.

- Function: char * strsep (char **STRING_PTR, const char *DELIMITER)
     This function has a similar functionality as `strtok_r' with the
     NEWSTRING argument replaced by the SAVE_PTR argument.  The
     initialization of the moving pointer has to be done by the user.
     Successive calls to `strsep' move the pointer along the tokens
     separated by DELIMITER, returning the address of the next token
     and updating STRING_PTR to point to the beginning of the next
     token.

     One difference between `strsep' and `strtok_r' is that if the
     input string contains more than one character from DELIMITER in a
     row `strsep' returns an empty string for each pair of characters
     from DELIMITER.  This means that a program normally should test
     for `strsep' returning an empty string before processing it.

     This function was introduced in 4.3BSD and therefore is widely
     available.

   Here is how the above example looks like when `strsep' is used.

     #include <string.h>
     #include <stddef.h>

     ...

     const char string[] = "words separated by spaces -- and,
punctuation!";
     const char delimiters[] = " .,;:!-";
     char *running;
     char *token;

     ...

     running = strdupa (string);
     token = strsep (&running, delimiters);    /* token => "words" */
     token = strsep (&running, delimiters);    /* token => "separated"
*/
     token = strsep (&running, delimiters);    /* token => "by" */
     token = strsep (&running, delimiters);    /* token => "spaces" */
     token = strsep (&running, delimiters);    /* token => "" */
     token = strsep (&running, delimiters);    /* token => "" */
     token = strsep (&running, delimiters);    /* token => "" */
     token = strsep (&running, delimiters);    /* token => "and" */
     token = strsep (&running, delimiters);    /* token => "" */
     token = strsep (&running, delimiters);    /* token => "punctuation"
*/
     token = strsep (&running, delimiters);    /* token => "" */
     token = strsep (&running, delimiters);    /* token => NULL */



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-11-19  8:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-07  9:30 gcc and string.h Jonathan Mortimer
2001-11-07 11:18 ` Andrea 'Fyre Wyzard' Bocci
2001-11-07 16:35 Paolo Carlini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).