home *** CD-ROM | disk | FTP | other *** search
- /*
- * Miscellaneous string routines.
- */
-
- #include <stdio.h>
-
- /*
- * Do a fancy string copy. If NULL, return null. If pointer to NULL, then
- * return the special "null_ptr" variable. If a normal copy, allocate
- * memory first.
- */
-
- char *
- strdup(str)
- char *str;
- {
- extern char *null_ptr;
- char *ret, *malloc(), *strcpy();
-
- if (str == NULL)
- return(NULL);
- /* if pointer to null */
- if (*str == NULL)
- return(null_ptr);
-
- ret = malloc((unsigned int) strlen(str)+1);
- strcpy(ret, str);
- return(ret);
- }
-
- /*
- * Perform the free(2) function, but check for NULL and the special
- * "null_ptr" variable first.
- */
-
- void
- free_ptr(str)
- char *str;
- {
- extern char *null_ptr;
- void free();
-
- if (str != NULL && str != null_ptr)
- free(str);
- return;
- }
-
- /*
- * This routine is similar to strtok(3). But our version handles null
- * strings and takes a single separator character as an argument.
- * Returns a NULL on end of string or error.
- */
-
- char *
- str_tok(str, c)
- char *str, c;
- {
- extern char *null_ptr;
- char *strchr();
- static char *ptr, *sep;
- /* start at beginning */
- if (str != NULL)
- ptr = str;
- else
- ptr = sep;
- /* at the end? */
- if (*ptr == NULL)
- return(NULL);
- /* no separator? */
- if (!(sep = strchr(ptr, c)))
- return(NULL);
- /* zap the sep, move past it */
- *sep = NULL;
- sep++;
-
- return(ptr);
- }
-