vasprintf.c (3383deca89b096faaebf065ba1a4be55ea1b46f4) | vasprintf.c (27a29543f3a991b58e859b7c01c24c07bcbe8d29) |
---|---|
1/* $OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $ */ | 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 9 * are met: --- 29 unchanged lines hidden (view full) --- 39vasprintf(str, fmt, ap) 40 char **str; 41 const char *fmt; 42 __va_list ap; 43{ 44 int ret; 45 FILE f; 46 struct __sFILEX ext; | 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 9 * are met: --- 29 unchanged lines hidden (view full) --- 39vasprintf(str, fmt, ap) 40 char **str; 41 const char *fmt; 42 __va_list ap; 43{ 44 int ret; 45 FILE f; 46 struct __sFILEX ext; |
47 unsigned char *_base; | |
48 49 f._file = -1; 50 f._flags = __SWR | __SSTR | __SALC; 51 f._bf._base = f._p = (unsigned char *)malloc(128); | 47 48 f._file = -1; 49 f._flags = __SWR | __SSTR | __SALC; 50 f._bf._base = f._p = (unsigned char *)malloc(128); |
52 if (f._bf._base == NULL) 53 goto err; | 51 if (f._bf._base == NULL) { 52 *str = NULL; 53 errno = ENOMEM; 54 return (-1); 55 } |
54 f._bf._size = f._w = 127; /* Leave room for the NUL */ 55 f._extra = &ext; 56 INITEXTRA(&f); 57 ret = __vfprintf(&f, fmt, ap); | 56 f._bf._size = f._w = 127; /* Leave room for the NUL */ 57 f._extra = &ext; 58 INITEXTRA(&f); 59 ret = __vfprintf(&f, fmt, ap); |
58 if (ret == -1) 59 goto err; 60 *f._p = '\0'; 61 _base = realloc(f._bf._base, ret + 1); 62 if (_base == NULL) 63 goto err; 64 *str = (char *)_base; 65 return (ret); 66 67err: 68 if (f._bf._base != NULL) { | 60 if (ret < 0) { |
69 free(f._bf._base); | 61 free(f._bf._base); |
70 f._bf._base = NULL; | 62 *str = NULL; 63 errno = ENOMEM; 64 return (-1); |
71 } | 65 } |
72 *str = NULL; 73 errno = ENOMEM; 74 return (-1); | 66 *f._p = '\0'; 67 *str = (char *)f._bf._base; 68 return (ret); |
75} | 69} |