asprintf.c (a82bbc730e20fe9dec4abeb1d949b2d02869032a) asprintf.c (e74101e4eff767325553039def89de70b70f36d3)
1/* $OpenBSD: asprintf.c,v 1.4 1998/06/21 22:13:46 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

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

38#include "local.h"
39
40int
41asprintf(char **str, char const *fmt, ...)
42{
43 int ret;
44 va_list ap;
45 FILE f;
1/* $OpenBSD: asprintf.c,v 1.4 1998/06/21 22:13:46 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

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

38#include "local.h"
39
40int
41asprintf(char **str, char const *fmt, ...)
42{
43 int ret;
44 va_list ap;
45 FILE f;
46 struct __sFILEX ext;
46
47 va_start(ap, fmt);
48 f._file = -1;
49 f._flags = __SWR | __SSTR | __SALC;
50 f._bf._base = f._p = (unsigned char *)malloc(128);
51 if (f._bf._base == NULL) {
52 *str = NULL;
53 errno = ENOMEM;
54 return (-1);
55 }
56 f._bf._size = f._w = 127; /* Leave room for the NULL */
47
48 va_start(ap, fmt);
49 f._file = -1;
50 f._flags = __SWR | __SSTR | __SALC;
51 f._bf._base = f._p = (unsigned char *)malloc(128);
52 if (f._bf._base == NULL) {
53 *str = NULL;
54 errno = ENOMEM;
55 return (-1);
56 }
57 f._bf._size = f._w = 127; /* Leave room for the NULL */
58 f._extra = &ext;
59 INITEXTRA(&f);
57 ret = __vfprintf(&f, fmt, ap); /* Use unlocked __vfprintf */
58 *f._p = '\0';
59 va_end(ap);
60 f._bf._base = reallocf(f._bf._base, f._bf._size + 1);
61 if (f._bf._base == NULL) {
62 errno = ENOMEM;
63 ret = -1;
64 }
65 *str = (char *)f._bf._base;
66 return (ret);
67}
60 ret = __vfprintf(&f, fmt, ap); /* Use unlocked __vfprintf */
61 *f._p = '\0';
62 va_end(ap);
63 f._bf._base = reallocf(f._bf._base, f._bf._size + 1);
64 if (f._bf._base == NULL) {
65 errno = ENOMEM;
66 ret = -1;
67 }
68 *str = (char *)f._bf._base;
69 return (ret);
70}