| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/os.h" | 5 #include "vm/os.h" |
| 6 | 6 |
| 7 #include <time.h> | 7 #include <time.h> |
| 8 | 8 |
| 9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 10 | 10 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 va_list args; | 148 va_list args; |
| 149 va_start(args, format); | 149 va_start(args, format); |
| 150 int retval = VSNPrint(str, size, format, args); | 150 int retval = VSNPrint(str, size, format, args); |
| 151 va_end(args); | 151 va_end(args); |
| 152 return retval; | 152 return retval; |
| 153 } | 153 } |
| 154 | 154 |
| 155 | 155 |
| 156 int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) { | 156 int OS::VSNPrint(char* str, size_t size, const char* format, va_list args) { |
| 157 if (str == NULL || size == 0) { | 157 if (str == NULL || size == 0) { |
| 158 return _vscprintf(format, args); | 158 int retval = _vscprintf(format, args); |
| 159 if (retval < 0) { |
| 160 FATAL1("Fatal error in OS::VSNPrint with format '%s'", format); |
| 161 } |
| 162 return retval; |
| 159 } | 163 } |
| 160 va_list args_copy; | 164 va_list args_copy; |
| 161 va_copy(args_copy, args); | 165 va_copy(args_copy, args); |
| 162 int written =_vsnprintf(str, size, format, args_copy); | 166 int written =_vsnprintf(str, size, format, args_copy); |
| 163 va_end(args_copy); | 167 va_end(args_copy); |
| 164 if (written < 0) { | 168 if (written < 0) { |
| 165 // _vsnprintf returns -1 if the number of characters to be written is | 169 // _vsnprintf returns -1 if the number of characters to be written is |
| 166 // larger than 'size', so we call _vscprintf which returns the number | 170 // larger than 'size', so we call _vscprintf which returns the number |
| 167 // of characters that would have been written. | 171 // of characters that would have been written. |
| 168 va_list args_retry; | 172 va_list args_retry; |
| 169 va_copy(args_retry, args); | 173 va_copy(args_retry, args); |
| 170 written = _vscprintf(format, args_retry); | 174 written = _vscprintf(format, args_retry); |
| 175 if (written < 0) { |
| 176 FATAL1("Fatal error in OS::VSNPrint with format '%s'", format); |
| 177 } |
| 171 va_end(args_retry); | 178 va_end(args_retry); |
| 172 } | 179 } |
| 173 // Make sure to zero-terminate the string if the output was | 180 // Make sure to zero-terminate the string if the output was |
| 174 // truncated or if there was an error. | 181 // truncated or if there was an error. |
| 175 if (written >= size) { | 182 if (written >= size) { |
| 176 str[size - 1] = '\0'; | 183 str[size - 1] = '\0'; |
| 177 } | 184 } |
| 178 return written; | 185 return written; |
| 179 } | 186 } |
| 180 | 187 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 206 void OS::Abort() { | 213 void OS::Abort() { |
| 207 abort(); | 214 abort(); |
| 208 } | 215 } |
| 209 | 216 |
| 210 | 217 |
| 211 void OS::Exit(int code) { | 218 void OS::Exit(int code) { |
| 212 exit(code); | 219 exit(code); |
| 213 } | 220 } |
| 214 | 221 |
| 215 } // namespace dart | 222 } // namespace dart |
| OLD | NEW |