michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #include michael@0: michael@0: /* michael@0: ** fprintf to a PRFileDesc michael@0: */ michael@0: PR_IMPLEMENT(PRUint32) PR_fprintf(PRFileDesc* fd, const char *fmt, ...) michael@0: { michael@0: va_list ap; michael@0: PRUint32 rv; michael@0: michael@0: va_start(ap, fmt); michael@0: rv = PR_vfprintf(fd, fmt, ap); michael@0: va_end(ap); michael@0: return rv; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRUint32) PR_vfprintf(PRFileDesc* fd, const char *fmt, va_list ap) michael@0: { michael@0: /* XXX this could be better */ michael@0: PRUint32 rv, len; michael@0: char* msg = PR_vsmprintf(fmt, ap); michael@0: if (NULL == msg) { michael@0: return -1; michael@0: } michael@0: len = strlen(msg); michael@0: #ifdef XP_OS2 michael@0: /* michael@0: * OS/2 really needs a \r for every \n. michael@0: * In the future we should try to use scatter-gather instead of a michael@0: * succession of PR_Write. michael@0: */ michael@0: if (isatty(PR_FileDesc2NativeHandle(fd))) { michael@0: PRUint32 last = 0, idx; michael@0: PRInt32 tmp; michael@0: rv = 0; michael@0: for (idx = 0; idx < len+1; idx++) { michael@0: if ((idx - last > 0) && (('\n' == msg[idx]) || (idx == len))) { michael@0: tmp = PR_Write(fd, msg + last, idx - last); michael@0: if (tmp >= 0) { michael@0: rv += tmp; michael@0: } michael@0: last = idx; michael@0: } michael@0: /* michael@0: * if current character is \n, and michael@0: * previous character isn't \r, and michael@0: * next character isn't \r michael@0: */ michael@0: if (('\n' == msg[idx]) && michael@0: ((0 == idx) || ('\r' != msg[idx-1])) && michael@0: ('\r' != msg[idx+1])) { michael@0: /* add extra \r */ michael@0: tmp = PR_Write(fd, "\r", 1); michael@0: if (tmp >= 0) { michael@0: rv += tmp; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: rv = PR_Write(fd, msg, len); michael@0: } michael@0: #else michael@0: rv = PR_Write(fd, msg, len); michael@0: #endif michael@0: PR_DELETE(msg); michael@0: return rv; michael@0: }