Changeset c2ad500 in mainline


Ignore:
Timestamp:
2008-10-02T09:14:33Z (16 years ago)
Author:
Tim Post <echo@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d364e94
Parents:
e436cfe
Message:

More housecleaning in util.c, get rid of cli_psprintf() in favor of asprintf() before getting attached to the first.

Location:
uspace/app/bdsh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/util.c

    re436cfe rc2ad500  
    7878size_t cli_redup(char **s1, const char *s2)
    7979{
    80         size_t len = strlen(s2) + 1;
     80        size_t len;
    8181
    82         if (! len)
     82        if (s2 == NULL)
    8383                return -1;
     84
     85        len = strlen(s2) + 1;
    8486
    8587        *s1 = realloc(*s1, len);
     
    9092        }
    9193
    92         memset(*s1, 0, sizeof(*s1));
     94        *s1[len] = '\0';
     95
    9396        memcpy(*s1, s2, len);
    9497        cli_errno = CL_EOK;
     98
    9599        return len;
    96100}
    97101
    98 /* An asprintf() for formatting paths, similar to asprintf() but ensures
    99  * the returned allocated string is <= PATH_MAX. On failure, an attempt
    100  * is made to return the original string (if not null) unmodified.
    101  *
    102  * Returns: Length of the new string on success, 0 if the string was handed
    103  * back unmodified, -1 on failure. On failure, cli_errno is set.
    104  *
    105  * We do not use POSIX_PATH_MAX, as it is typically much smaller than the
    106  * PATH_MAX defined by the kernel.
    107  *
    108  * Use this like:
    109  * if (1 > cli_psprintf(&char, "%s/%s", foo, bar)) {
    110  *   cli_error(cli_errno, "Failed to format path");
    111  *   stop_what_your_doing_as_your_out_of_memory();
    112  * }
    113  */
    114 
    115 int cli_psprintf(char **s1, const char *fmt, ...)
    116 {
    117         va_list ap;
    118         size_t needed, base = PATH_MAX + 1;
    119         int skipped = 0;
    120         char *orig = NULL;
    121         char *tmp = (char *) malloc(base);
    122 
    123         /* Don't even touch s1, not enough memory */
    124         if (NULL == tmp) {
    125                 cli_errno = CL_ENOMEM;
    126                 return -1;
    127         }
    128 
    129         /* If re-allocating s1, save a copy in case we fail */
    130         if (NULL != *s1)
    131                 orig = cli_strdup(*s1);
    132 
    133         /* Print the string to tmp so we can determine the size that
    134          * we actually need */
    135         memset(tmp, 0, sizeof(tmp));
    136         va_start(ap, fmt);
    137         /* vsnprintf will return the # of bytes not written */
    138         skipped = vsnprintf(tmp, base, fmt, ap);
    139         va_end(ap);
    140 
    141         /* realloc/alloc s1 to be just the size that we need */
    142         needed = strlen(tmp) + 1;
    143         *s1 = realloc(*s1, needed);
    144 
    145         if (NULL == *s1) {
    146                 /* No string lived here previously, or we failed to
    147                  * make a copy of it, either way there's nothing we
    148                  * can do. */
    149                 if (NULL == *orig) {
    150                         cli_errno = CL_ENOMEM;
    151                         return -1;
    152                 }
    153                 /* We can't even allocate enough size to restore the
    154                  * saved copy, just give up */
    155                 *s1 = realloc(*s1, strlen(orig) + 1);
    156                 if (NULL == *s1) {
    157                         free(tmp);
    158                         free(orig);
    159                         cli_errno = CL_ENOMEM;
    160                         return -1;
    161                 }
    162                 /* Give the string back as we found it */
    163                 memset(*s1, 0, sizeof(*s1));
    164                 memcpy(*s1, orig, strlen(orig) + 1);
    165                 free(tmp);
    166                 free(orig);
    167                 cli_errno = CL_ENOMEM;
    168                 return 0;
    169         }
    170 
    171         /* Ok, great, we have enough room */
    172         memset(*s1, 0, sizeof(*s1));
    173         memcpy(*s1, tmp, needed);
    174         free(tmp);
    175 
    176         /* Free tmp only if s1 was reallocated instead of allocated */
    177         if (NULL != orig)
    178                 free(orig);
    179 
    180         if (skipped) {
    181                 /* s1 was bigger than PATH_MAX when expanded, however part
    182                  * of the string was printed. Tell the caller not to use it */
    183                 cli_errno = CL_ETOOBIG;
    184                 return -1;
    185         }
    186 
    187         /* Success! */
    188         cli_errno = CL_EOK;
    189         return (int) needed;
    190 }
    191        
    192102/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
    193103char * cli_strtok_r(char *s, const char *delim, char **last)
     
    274184                snprintf(usr->cwd, PATH_MAX, "(unknown)");
    275185
    276         if (1 < cli_psprintf(&usr->prompt, "%s # ", usr->cwd)) {
    277                 cli_error(cli_errno, "Failed to set prompt");
    278                 return 1;
    279         }
     186        asprintf(&usr->prompt, "%s # ", usr->cwd);
    280187
    281188        return 0;
  • uspace/app/bdsh/util.h

    re436cfe rc2ad500  
    77extern char * cli_strdup(const char *);
    88extern size_t cli_redup(char **, const char *);
    9 extern int cli_psprintf(char **, const char *, ...);
    109extern char * cli_strtok_r(char *, const char *, char **);
    1110extern char * cli_strtok(char *, const char *);
Note: See TracChangeset for help on using the changeset viewer.