This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Variations on strcpy(), strcat(), strstr(), strtok()


Hi,

Functions in <string.h> expect the size of source string.  But, I usually need to specify the size of destination string, to make sure that I don't overrun the buffer.  So, I started with

        snprintf (to, sizeof (to), "%s", from)
and
        n = strlen (to);
        if (n < sizeof (to))
                snprintf (to + n, sizeof (to) - n, "%s", from);

Then, I wrote 4 functions, which are variation on strcpy(), strcat(), strncpy(), strncat(), namely

        
char *snstrcpy  (char *to, size_t size, char *from);
        
char *snstrcat  (char *to, size_t size, char *from);
         char *snstrncpy (char *to, size_t size, char *from, int n);
        
char *snstrncat (char *to, size_t size, char *from, int n);

They will not read or write beyond the destination size.  Of course, there is nothing I can do about buffer overrun on the source string.  I used "int n" instead of "size_t n", because if "n" is negative, it will count from the end of source string.  This may be contamination from Bash/Python, but it is very useful in extracting from the end of string.


Also, I know that strtok() is "deprecated", but I use it a lot in the form,

        for (s = strtok (str, "/"); s; s = strtok (NULL, "/"))

knowing that as long as "s" is not NULL, it will always point to non-empty token.  But, I often need to use regex as delimiter, not just chars.

So, I wrote 4 functions, which are variation on strstr() and strtok(), namely

        char *strregex (char *str, char *regex, int *n);

                -- similar to strstr(), except you use "regex" instead of substring pattern.  It will return non-empty match (its size is returned in "n"); or, it will return NULL if no match is found.  If "str" is NULL, then it will continue with the previous "str", and search for the next "regex" match.

        char *strregextok (char *str, char *regex);

                -- similar to strtok(), except you give "regex" instead of delimiting chars. 

        char *strstrtok (char *str, char *pattern);

                -- similar to strtok(), except you give "substring" instead of delimiting chars.

        char *strcsvtok (char *str);

                -- similar to strtok(), except the delimiting char is hardcoded to ',' (comma) for CSV format.  So, it will ignore comma in "...", and it can return empty string.


I'm attaching the source code with hope that it may be useful to others.

--William



      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/

Attachment: snstring.c
Description: Binary data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]