1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/src/io/prstdio.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "primpl.h" 1.10 + 1.11 +#include <string.h> 1.12 + 1.13 +/* 1.14 +** fprintf to a PRFileDesc 1.15 +*/ 1.16 +PR_IMPLEMENT(PRUint32) PR_fprintf(PRFileDesc* fd, const char *fmt, ...) 1.17 +{ 1.18 + va_list ap; 1.19 + PRUint32 rv; 1.20 + 1.21 + va_start(ap, fmt); 1.22 + rv = PR_vfprintf(fd, fmt, ap); 1.23 + va_end(ap); 1.24 + return rv; 1.25 +} 1.26 + 1.27 +PR_IMPLEMENT(PRUint32) PR_vfprintf(PRFileDesc* fd, const char *fmt, va_list ap) 1.28 +{ 1.29 + /* XXX this could be better */ 1.30 + PRUint32 rv, len; 1.31 + char* msg = PR_vsmprintf(fmt, ap); 1.32 + if (NULL == msg) { 1.33 + return -1; 1.34 + } 1.35 + len = strlen(msg); 1.36 +#ifdef XP_OS2 1.37 + /* 1.38 + * OS/2 really needs a \r for every \n. 1.39 + * In the future we should try to use scatter-gather instead of a 1.40 + * succession of PR_Write. 1.41 + */ 1.42 + if (isatty(PR_FileDesc2NativeHandle(fd))) { 1.43 + PRUint32 last = 0, idx; 1.44 + PRInt32 tmp; 1.45 + rv = 0; 1.46 + for (idx = 0; idx < len+1; idx++) { 1.47 + if ((idx - last > 0) && (('\n' == msg[idx]) || (idx == len))) { 1.48 + tmp = PR_Write(fd, msg + last, idx - last); 1.49 + if (tmp >= 0) { 1.50 + rv += tmp; 1.51 + } 1.52 + last = idx; 1.53 + } 1.54 + /* 1.55 + * if current character is \n, and 1.56 + * previous character isn't \r, and 1.57 + * next character isn't \r 1.58 + */ 1.59 + if (('\n' == msg[idx]) && 1.60 + ((0 == idx) || ('\r' != msg[idx-1])) && 1.61 + ('\r' != msg[idx+1])) { 1.62 + /* add extra \r */ 1.63 + tmp = PR_Write(fd, "\r", 1); 1.64 + if (tmp >= 0) { 1.65 + rv += tmp; 1.66 + } 1.67 + } 1.68 + } 1.69 + } else { 1.70 + rv = PR_Write(fd, msg, len); 1.71 + } 1.72 +#else 1.73 + rv = PR_Write(fd, msg, len); 1.74 +#endif 1.75 + PR_DELETE(msg); 1.76 + return rv; 1.77 +}