Subversion Repositories HelenOS

Rev

Rev 3345 | Rev 3376 | 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. #include <stdarg.h>
  50.  
  51. #include "config.h"
  52. #include "errors.h"
  53. #include "util.h"
  54.  
  55. /* some platforms do not have strdup, implement it here.
  56.  * Returns a pointer to an allocated string or NULL on failure */
  57. char * cli_strdup(const char *s1)
  58. {
  59.     size_t len = strlen(s1) + 1;
  60.     void *ret = malloc(len);
  61.  
  62.     if (ret == NULL)
  63.         return (char *) NULL;
  64.  
  65.     return (char *) memcpy(ret, s1, len);
  66. }
  67.  
  68. /*
  69.  * Take a previously allocated string (s1), re-size it to accept s2 and copy
  70.  * the contents of s2 into s1.
  71.  * Return -1 on failure, or the length of the copied string on success.
  72.  */
  73. int cli_redup(char **s1, const char *s2)
  74. {
  75.     size_t len = strlen(s2) + 1;
  76.  
  77.     if (! len)
  78.         return -1;
  79.  
  80.     *s1 = realloc(*s1, len);
  81.  
  82.     if (*s1 == NULL)
  83.         return -1;
  84.  
  85.     memset(*s1, 0, sizeof(*s1));
  86.     memcpy(*s1, s2, len);
  87.     return (int) len;
  88. }
  89.  
  90. /* An asprintf() for concantenating paths. Allocates the system PATH_MAX value,
  91.  * expands the formatted string and re-sizes the block s1 points to accordingly.
  92.  *
  93.  * Returns the length of the new s1 on success, -1 on failure. On failure, an
  94.  * attempt is made to return s1 unmodified for sanity, in this case 0 is returned.
  95.  * to indicate that s1 was not modified.
  96.  *
  97.  * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */
  98. int cli_psprintf(char **s1, const char *fmt, ...)
  99. {
  100.     va_list ap;
  101.     size_t needed, base = PATH_MAX + 1;
  102.     char *tmp = (char *) malloc(base);
  103.  
  104.     if (NULL == tmp)
  105.         return -1;
  106.  
  107.     char *orig = *s1;
  108.  
  109.     memset(tmp, 0, sizeof(tmp));
  110.     va_start(ap, fmt);
  111.     vsnprintf(tmp, base, fmt, ap);
  112.     va_end(ap);
  113.     needed = strlen(tmp) + 1;
  114.     *s1 = realloc(*s1, needed);
  115.     if (NULL == *s1) {
  116.         *s1 = realloc(*s1, strlen(orig) + 1);
  117.         if (NULL == *s1) {
  118.             free(tmp);
  119.             return -1;
  120.         }
  121.         memset(*s1, 0, sizeof(*s1));
  122.         memcpy(*s1, orig, strlen(orig) + 1);
  123.         free(tmp);
  124.         return 0;
  125.     }
  126.     memset(*s1, 0, sizeof(*s1));
  127.     memcpy(*s1, tmp, needed);
  128.     free(tmp);
  129.  
  130.     return (int) needed;
  131. }
  132.    
  133. /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
  134. char * cli_strtok_r(char *s, const char *delim, char **last)
  135. {
  136.     char *spanp, *tok;
  137.     int c, sc;
  138.  
  139.     if (s == NULL && (s = *last) == NULL)
  140.         return (NULL);
  141.  
  142. cont:
  143.     c = *s++;
  144.     for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  145.         if (c == sc)
  146.             goto cont;
  147.     }
  148.  
  149.     if (c == 0) {       /* no non-delimiter characters */
  150.         *last = NULL;
  151.         return (NULL);
  152.     }
  153.  
  154.     tok = s - 1;
  155.  
  156.     for (;;) {
  157.         c = *s++;
  158.         spanp = (char *)delim;
  159.         do {
  160.             if ((sc = *spanp++) == c) {
  161.                 if (c == 0)
  162.                     s = NULL;
  163.                 else
  164.                     s[-1] = '\0';
  165.                 *last = s;
  166.                 return (tok);
  167.             }
  168.         } while (sc != 0);
  169.     }
  170. }
  171.  
  172. /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
  173. char * cli_strtok(char *s, const char *delim)
  174. {
  175.     static char *last;
  176.  
  177.     return (cli_strtok_r(s, delim, &last));
  178. }
  179.