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