xref: /aosp_15_r20/external/obstack/android/obstack_printf.c (revision 6912a2bee9c91e2e65d305b4d8eb5813eb26cd9f)
1 /*
2  * Copyright 2021, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * This is a reimplementation of obstack_printf for use by libcpu, which
19  * uses it to print an error message.
20  */
21 
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <obstack.h>
26 
obstack_printf(struct obstack * obs,const char * format,...)27 int __attribute__((__format__(printf, 2, 3)))  obstack_printf(struct obstack *obs, const char *format, ...) {
28 
29   va_list ap;
30   va_start(ap, format);
31 
32   char* str = NULL;
33   int len = vasprintf(&str, format, ap);
34   if (len < 0) {
35     return len;
36   }
37 
38   obstack_grow(obs, str, len);
39   free(str);
40 
41   return len;
42 }
43