Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "primpl.h" |
michael@0 | 7 | |
michael@0 | 8 | #include <string.h> |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | ** fprintf to a PRFileDesc |
michael@0 | 12 | */ |
michael@0 | 13 | PR_IMPLEMENT(PRUint32) PR_fprintf(PRFileDesc* fd, const char *fmt, ...) |
michael@0 | 14 | { |
michael@0 | 15 | va_list ap; |
michael@0 | 16 | PRUint32 rv; |
michael@0 | 17 | |
michael@0 | 18 | va_start(ap, fmt); |
michael@0 | 19 | rv = PR_vfprintf(fd, fmt, ap); |
michael@0 | 20 | va_end(ap); |
michael@0 | 21 | return rv; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | PR_IMPLEMENT(PRUint32) PR_vfprintf(PRFileDesc* fd, const char *fmt, va_list ap) |
michael@0 | 25 | { |
michael@0 | 26 | /* XXX this could be better */ |
michael@0 | 27 | PRUint32 rv, len; |
michael@0 | 28 | char* msg = PR_vsmprintf(fmt, ap); |
michael@0 | 29 | if (NULL == msg) { |
michael@0 | 30 | return -1; |
michael@0 | 31 | } |
michael@0 | 32 | len = strlen(msg); |
michael@0 | 33 | #ifdef XP_OS2 |
michael@0 | 34 | /* |
michael@0 | 35 | * OS/2 really needs a \r for every \n. |
michael@0 | 36 | * In the future we should try to use scatter-gather instead of a |
michael@0 | 37 | * succession of PR_Write. |
michael@0 | 38 | */ |
michael@0 | 39 | if (isatty(PR_FileDesc2NativeHandle(fd))) { |
michael@0 | 40 | PRUint32 last = 0, idx; |
michael@0 | 41 | PRInt32 tmp; |
michael@0 | 42 | rv = 0; |
michael@0 | 43 | for (idx = 0; idx < len+1; idx++) { |
michael@0 | 44 | if ((idx - last > 0) && (('\n' == msg[idx]) || (idx == len))) { |
michael@0 | 45 | tmp = PR_Write(fd, msg + last, idx - last); |
michael@0 | 46 | if (tmp >= 0) { |
michael@0 | 47 | rv += tmp; |
michael@0 | 48 | } |
michael@0 | 49 | last = idx; |
michael@0 | 50 | } |
michael@0 | 51 | /* |
michael@0 | 52 | * if current character is \n, and |
michael@0 | 53 | * previous character isn't \r, and |
michael@0 | 54 | * next character isn't \r |
michael@0 | 55 | */ |
michael@0 | 56 | if (('\n' == msg[idx]) && |
michael@0 | 57 | ((0 == idx) || ('\r' != msg[idx-1])) && |
michael@0 | 58 | ('\r' != msg[idx+1])) { |
michael@0 | 59 | /* add extra \r */ |
michael@0 | 60 | tmp = PR_Write(fd, "\r", 1); |
michael@0 | 61 | if (tmp >= 0) { |
michael@0 | 62 | rv += tmp; |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | } else { |
michael@0 | 67 | rv = PR_Write(fd, msg, len); |
michael@0 | 68 | } |
michael@0 | 69 | #else |
michael@0 | 70 | rv = PR_Write(fd, msg, len); |
michael@0 | 71 | #endif |
michael@0 | 72 | PR_DELETE(msg); |
michael@0 | 73 | return rv; |
michael@0 | 74 | } |