vasprintf.c (1e98f88776fc606df245a382685b1ac634a81389) vasprintf.c (1b0181df2f46ef73a41ea8c9b7026718f8eec3a1)
1/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 28 unchanged lines hidden (view full) ---

37
38int
39vasprintf(str, fmt, ap)
40 char **str;
41 const char *fmt;
42 __va_list ap;
43{
44 int ret;
1/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 28 unchanged lines hidden (view full) ---

37
38int
39vasprintf(str, fmt, ap)
40 char **str;
41 const char *fmt;
42 __va_list ap;
43{
44 int ret;
45 FILE f;
45 FILE f = FAKE_FILE;
46
46
47 f._file = -1;
48 f._flags = __SWR | __SSTR | __SALC;
49 f._bf._base = f._p = (unsigned char *)malloc(128);
50 if (f._bf._base == NULL) {
51 *str = NULL;
52 errno = ENOMEM;
53 return (-1);
54 }
55 f._bf._size = f._w = 127; /* Leave room for the NUL */
47 f._flags = __SWR | __SSTR | __SALC;
48 f._bf._base = f._p = (unsigned char *)malloc(128);
49 if (f._bf._base == NULL) {
50 *str = NULL;
51 errno = ENOMEM;
52 return (-1);
53 }
54 f._bf._size = f._w = 127; /* Leave room for the NUL */
56 f._orientation = 0;
57 memset(&f._mbstate, 0, sizeof(mbstate_t));
58 ret = __vfprintf(&f, fmt, ap);
59 if (ret < 0) {
60 free(f._bf._base);
61 *str = NULL;
62 errno = ENOMEM;
63 return (-1);
64 }
65 *f._p = '\0';
66 *str = (char *)f._bf._base;
67 return (ret);
68}
55 ret = __vfprintf(&f, fmt, ap);
56 if (ret < 0) {
57 free(f._bf._base);
58 *str = NULL;
59 errno = ENOMEM;
60 return (-1);
61 }
62 *f._p = '\0';
63 *str = (char *)f._bf._base;
64 return (ret);
65}