1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Netscape Portable Runtime (NSPR).
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "primpl.h"
39 :
40 : #include <string.h>
41 :
42 : /*
43 : ** fprintf to a PRFileDesc
44 : */
45 1319 : PR_IMPLEMENT(PRUint32) PR_fprintf(PRFileDesc* fd, const char *fmt, ...)
46 : {
47 : va_list ap;
48 : PRUint32 rv;
49 :
50 1319 : va_start(ap, fmt);
51 1319 : rv = PR_vfprintf(fd, fmt, ap);
52 1319 : va_end(ap);
53 1319 : return rv;
54 : }
55 :
56 1319 : PR_IMPLEMENT(PRUint32) PR_vfprintf(PRFileDesc* fd, const char *fmt, va_list ap)
57 : {
58 : /* XXX this could be better */
59 : PRUint32 rv, len;
60 1319 : char* msg = PR_vsmprintf(fmt, ap);
61 1319 : if (NULL == msg) {
62 0 : return -1;
63 : }
64 1319 : len = strlen(msg);
65 : #ifdef XP_OS2
66 : /*
67 : * OS/2 really needs a \r for every \n.
68 : * In the future we should try to use scatter-gather instead of a
69 : * succession of PR_Write.
70 : */
71 : if (isatty(PR_FileDesc2NativeHandle(fd))) {
72 : PRUint32 last = 0, idx;
73 : PRInt32 tmp;
74 : rv = 0;
75 : for (idx = 0; idx < len+1; idx++) {
76 : if ((idx - last > 0) && (('\n' == msg[idx]) || (idx == len))) {
77 : tmp = PR_Write(fd, msg + last, idx - last);
78 : if (tmp >= 0) {
79 : rv += tmp;
80 : }
81 : last = idx;
82 : }
83 : /*
84 : * if current character is \n, and
85 : * previous character isn't \r, and
86 : * next character isn't \r
87 : */
88 : if (('\n' == msg[idx]) &&
89 : ((0 == idx) || ('\r' != msg[idx-1])) &&
90 : ('\r' != msg[idx+1])) {
91 : /* add extra \r */
92 : tmp = PR_Write(fd, "\r", 1);
93 : if (tmp >= 0) {
94 : rv += tmp;
95 : }
96 : }
97 : }
98 : } else {
99 : rv = PR_Write(fd, msg, len);
100 : }
101 : #else
102 1319 : rv = PR_Write(fd, msg, len);
103 : #endif
104 1319 : PR_DELETE(msg);
105 1319 : return rv;
106 : }
|