~xnox/ubuntu/trusty/gcc-arm-linux-androideabi/dima

« back to all changes in this revision

Viewing changes to android/bionic/libc/stdio/vasprintf.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-07-05 10:12:24 UTC
  • Revision ID: package-import@ubuntu.com-20130705101224-6qo3e8jbz8p31aa1
Tags: upstream-0.20130705.1
ImportĀ upstreamĀ versionĀ 0.20130705.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $OpenBSD: vasprintf.c,v 1.13 2006/01/06 18:53:04 millert Exp $  */
 
2
 
 
3
/*
 
4
 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
 
5
 *
 
6
 * Permission to use, copy, modify, and distribute this software for any
 
7
 * purpose with or without fee is hereby granted, provided that the above
 
8
 * copyright notice and this permission notice appear in all copies.
 
9
 *
 
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
17
 */
 
18
 
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <errno.h>
 
23
#include "local.h"
 
24
 
 
25
int
 
26
vasprintf(char **str, const char *fmt, __va_list ap)
 
27
{
 
28
        int ret;
 
29
        FILE f;
 
30
        struct __sfileext fext;
 
31
        unsigned char *_base;
 
32
 
 
33
        _FILEEXT_SETUP(&f, &fext);
 
34
        f._file = -1;
 
35
        f._flags = __SWR | __SSTR | __SALC;
 
36
        f._bf._base = f._p = (unsigned char *)malloc(128);
 
37
        if (f._bf._base == NULL)
 
38
                goto err;
 
39
        f._bf._size = f._w = 127;               /* Leave room for the NUL */
 
40
        ret = __vfprintf(&f, fmt, ap);
 
41
        if (ret == -1)
 
42
                goto err;
 
43
        *f._p = '\0';
 
44
        _base = realloc(f._bf._base, ret + 1);
 
45
        if (_base == NULL)
 
46
                goto err;
 
47
        *str = (char *)_base;
 
48
        return (ret);
 
49
 
 
50
err:
 
51
        free(f._bf._base);
 
52
        *str = NULL;
 
53
        errno = ENOMEM;
 
54
        return (-1);
 
55
}