Merge branch 'less_closed'
[unleashed.git] / usr / src / cmd / bnu / logent.c
blobb0a6022ff813a96f5581739b4f2d59107d0194b3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1998 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include "uucp.h"
35 static FILE *_Lf = NULL;
36 static FILE *_Cf = NULL;
37 static int _Sf = -1;
38 static int CurRole = MASTER; /* Uucico's current role. */
41 * Make log entry
42 * text -> ptr to text string
43 * status -> ptr to status string
44 * Returns:
45 * none
47 void
48 logent(text, status)
49 register char *text, *status;
51 static char logfile[MAXFULLNAME];
52 char *prev;
54 if (*Rmtname == NULLCHAR) /* ignore logging if Rmtname is not yet set */
55 return;
57 prev = _uu_setlocale(LC_ALL, "C");
58 if (Nstat.t_pid == 0)
59 Nstat.t_pid = getpid();
61 if (_Lf != NULL
62 && strncmp(Rmtname, BASENAME(logfile, '/'), MAXBASENAME) != 0) {
63 fclose(_Lf);
64 _Lf = NULL;
67 if (_Lf == NULL) {
68 sprintf(logfile, "%s/%s", Logfile, Rmtname);
69 _Lf = fopen(logfile, "a");
70 (void) chmod(logfile, LOGFILEMODE);
71 if (_Lf == NULL) {
72 (void) _uu_resetlocale(LC_ALL, prev);
73 return;
75 setbuf(_Lf, CNULL);
77 (void) fseek(_Lf, 0L, 2);
78 (void) fprintf(_Lf, "%s %s %s ", User, Rmtname, Jobid);
79 (void) fprintf(_Lf, "(%s,%ld,%d) ", timeStamp(), (long) Nstat.t_pid, Seqn);
80 (void) fprintf(_Lf, "%s (%s)\n", status, text);
81 (void) _uu_resetlocale(LC_ALL, prev);
82 return;
87 * Make entry for a conversation (uucico only)
88 * text -> pointer to message string
89 * Returns:
90 * none
92 void
93 usyslog(text)
94 register char *text;
96 int sbuflen;
97 char sysbuf[BUFSIZ];
98 char *prev = _uu_setlocale(LC_ALL, "C");
100 (void) sprintf(sysbuf, "%s!%s %s (%s) (%c,%ld,%d) [%s] %s\n",
101 Rmtname, User, CurRole == SLAVE ? "S" : "M", timeStamp(),
102 Pchar, (long) getpid(), Seqn, Dc, text);
103 sbuflen = strlen(sysbuf);
104 if (_Sf < 0) {
105 errno = 0;
106 _Sf = open(SYSLOG, 1);
107 if (errno == ENOENT) {
108 _Sf = creat(SYSLOG, LOGFILEMODE);
109 (void) chmod(SYSLOG, LOGFILEMODE);
111 if (_Sf < 0) {
112 (void) _uu_resetlocale(LC_ALL, prev);
113 return;
116 (void) lseek(_Sf, 0L, 2);
117 (void) write(_Sf, sysbuf, sbuflen);
118 (void) _uu_resetlocale(LC_ALL, prev);
119 return;
123 * Make entry for a command
124 * argc -> number of command arguments
125 * argv -> pointer array to command arguments
126 * Returns:
127 * none
129 void
130 commandlog(argc,argv)
131 int argc;
132 char **argv;
134 int fd;
135 char *prev = _uu_setlocale(LC_ALL, "C");
137 if (_Cf == NULL) {
138 errno = 0;
139 fd = open(CMDLOG, O_WRONLY | O_APPEND);
140 if (errno == ENOENT) {
141 fd = creat(CMDLOG, LOGFILEMODE);
142 (void) chmod(CMDLOG, LOGFILEMODE);
144 if (fd < 0 || (_Cf = fdopen(fd, "a")) == NULL) {
145 (void) _uu_resetlocale(LC_ALL, prev);
146 return;
149 (void) fprintf(_Cf, "%s (%s) ",User, timeStamp() );
150 while (argc-- > 0) {
151 (void) fprintf(_Cf, "%s%c", *argv++, (argc > 0)?' ':'\n');
153 (void) fflush(_Cf);
154 (void) _uu_resetlocale(LC_ALL, prev);
155 return;
159 * Close log files before a fork
161 void
162 ucloselog()
164 if (_Sf >= 0) {
165 (void) close(_Sf);
166 _Sf = -1;
168 if (_Lf) {
169 (void) fclose(_Lf);
170 _Lf = NULL;
172 if (_Cf) {
173 (void) fclose(_Cf);
174 _Cf = NULL;
176 return;
180 * millitick()
182 * return msec since last time called
184 #ifdef ATTSV
185 #include <values.h>
187 time_t
188 millitick()
190 struct tms tbuf;
191 time_t now, rval;
192 static time_t past; /* guaranteed 0 first time called */
194 if (past == 0) {
195 past = times(&tbuf);
196 rval = 0;
197 } else {
198 now = times(&tbuf);
199 if (now - past > MAXLONG / 1000) /* would overflow */
200 rval = (now - past) / HZ * 1000;
201 else
202 rval = (now - past) * 1000 / HZ;
203 past = now;
205 return(rval);
208 #else /* !ATTSV */
209 time_t
210 millitick()
212 struct timeb tbuf;
213 static struct timeb tbuf1;
214 static past; /* guaranteed 0 first time called */
215 time_t rval;
217 if (past == 0) {
218 past++;
219 rval = 0;
220 ftime(&tbuf1);
221 } else {
222 ftime(&tbuf);
223 rval = (tbuf.time - tbuf1.time) * 1000
224 + tbuf.millitm - tbuf1.millitm;
225 tbuf1 = tbuf;
227 return(rval);
229 #endif /* ATTSV */