Subversion Repositories HelenOS

Rev

Rev 3280 | Rev 3345 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
  2.  * Copyright (C) 1998 by Wes Peters <wes@softweyr.com>
  3.  * Copyright (c) 1988, 1993 The Regents of the University of California.
  4.  * All rights reserved by all copyright holders.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  * Redistributions of source code must retain the above copyright notice, this
  10.  * list of conditions and the following disclaimer.
  11.  *
  12.  * Redistributions in binary form must reproduce the above copyright notice,
  13.  * this list of conditions and the following disclaimer in the documentation
  14.  * and/or other materials provided with the distribution.
  15.  *
  16.  * Neither the name of the original program's authors nor the names of its
  17.  * contributors may be used to endorse or promote products derived from this
  18.  * software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32.  
  33. /* NOTES:
  34.  * 1 - Various functions were adapted from FreeBSD (copyright holders noted above)
  35.  *     these functions are identified with comments.
  36.  *
  37.  * 2 - Some of these have since appeared in libc. They remain here for various
  38.  *     reasons, such as the eventual integration of garbage collection for things
  39.  *     that allocate memory and don't automatically free it.
  40.  *
  41.  * 3 - Things that expect a pointer to an allocated string do _no_ sanity checking
  42.  *     if developing on a simulator with no debugger, take care :)
  43.  */
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <stdarg.h>
  48. #include <stdlib.h>
  49.  
  50. #include "config.h"
  51. #include "errors.h"
  52. #include "util.h"
  53.  
  54.  
  55. /* some platforms do not have strdup, implement it here */
  56. char * cli_strdup(const char *s1)
  57. {
  58.     size_t len = strlen(s1) + 1;
  59.     void *ret = malloc(len);
  60.  
  61.     if (ret == NULL)
  62.         return (char *) NULL;
  63.  
  64.     return (char *) memcpy(ret, s1, len);
  65. }
  66.  
  67. /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
  68. char * cli_strtok_r(char *s, const char *delim, char **last)
  69. {
  70.     char *spanp, *tok;
  71.     int c, sc;
  72.  
  73.     if (s == NULL && (s = *last) == NULL)
  74.         return (NULL);
  75.  
  76. cont:
  77.     c = *s++;
  78.     for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  79.         if (c == sc)
  80.             goto cont;
  81.     }
  82.  
  83.     if (c == 0) {       /* no non-delimiter characters */
  84.         *last = NULL;
  85.         return (NULL);
  86.     }
  87.  
  88.     tok = s - 1;
  89.  
  90.     for (;;) {
  91.         c = *s++;
  92.         spanp = (char *)delim;
  93.         do {
  94.             if ((sc = *spanp++) == c) {
  95.                 if (c == 0)
  96.                     s = NULL;
  97.                 else
  98.                     s[-1] = '\0';
  99.                 *last = s;
  100.                 return (tok);
  101.             }
  102.         } while (sc != 0);
  103.     }
  104. }
  105.  
  106. /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
  107. char * cli_strtok(char *s, const char *delim)
  108. {
  109.     static char *last;
  110.  
  111.     return (cli_strtok_r(s, delim, &last));
  112. }
  113.