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