misc.c (e93f27e3aeb7c0778012b7732bc6376e20f80427) | misc.c (fe590ffe40f49fe09d8275fbf29f0d46c5b99dc7) |
---|---|
1/* Copyright 1988,1990,1993,1994 by Paul Vixie 2 * All rights reserved | 1/* Copyright 1988,1990,1993,1994 by Paul Vixie 2 * All rights reserved |
3 */ 4 5/* 6 * Copyright (c) 1997 by Internet Software Consortium |
|
3 * | 7 * |
4 * Distribute freely, except: don't remove my name from the source or 5 * documentation (don't take credit for my work), mark your changes (don't 6 * get me blamed for your possible bugs), don't alter or remove this 7 * notice. May be sold if buildable source is provided to buyer. No 8 * warrantee of any kind, express or implied, is included with this 9 * software; use at your own risk, responsibility for damages (if any) to 10 * anyone resulting from the use of this software rests entirely with the 11 * user. | 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. |
12 * | 11 * |
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and 14 * I'll try to keep a version up to date. I can be reached as follows: 15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul | 12 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 13 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 15 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 18 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 19 * SOFTWARE. |
16 */ 17 18#if !defined(lint) && !defined(LINT) | 20 */ 21 22#if !defined(lint) && !defined(LINT) |
19static const char rcsid[] = 20 "$FreeBSD$"; | 23static const char rcsid[] = "$Id: misc.c,v 1.5 1998/08/14 00:32:40 vixie Exp $"; |
21#endif 22 23/* vix 26jan87 [RCS has the rest of the log] 24 * vix 30dec86 [written] 25 */ 26 27 28#include "cron.h" 29#if SYS_TIME_H 30# include <sys/time.h> 31#else 32# include <time.h> 33#endif 34#include <sys/file.h> 35#include <sys/stat.h> | 24#endif 25 26/* vix 26jan87 [RCS has the rest of the log] 27 * vix 30dec86 [written] 28 */ 29 30 31#include "cron.h" 32#if SYS_TIME_H 33# include <sys/time.h> 34#else 35# include <time.h> 36#endif 37#include <sys/file.h> 38#include <sys/stat.h> |
36#include <err.h> | |
37#include <errno.h> 38#include <string.h> 39#include <fcntl.h> 40#if defined(SYSLOG) 41# include <syslog.h> 42#endif 43 44 | 39#include <errno.h> 40#include <string.h> 41#include <fcntl.h> 42#if defined(SYSLOG) 43# include <syslog.h> 44#endif 45 46 |
47#if defined(LOG_CRON) && defined(LOG_FILE) 48# undef LOG_FILE 49#endif 50 |
|
45#if defined(LOG_DAEMON) && !defined(LOG_CRON) | 51#if defined(LOG_DAEMON) && !defined(LOG_CRON) |
46#define LOG_CRON LOG_DAEMON | 52# define LOG_CRON LOG_DAEMON |
47#endif 48 49 50static int LogFD = ERR; 51 52 53int | 53#endif 54 55 56static int LogFD = ERR; 57 58 59int |
54strcmp_until(char *left, char *right, int until) | 60strcmp_until(const char *left, const char *right, int until) |
55{ | 61{ |
56 register int diff; 57 | |
58 while (*left && *left != until && *left == *right) { 59 left++; 60 right++; 61 } 62 63 if ((*left=='\0' || *left == until) && 64 (*right=='\0' || *right == until)) { | 62 while (*left && *left != until && *left == *right) { 63 left++; 64 right++; 65 } 66 67 if ((*left=='\0' || *left == until) && 68 (*right=='\0' || *right == until)) { |
65 diff = 0; 66 } else { 67 diff = *left - *right; | 69 return (0); |
68 } | 70 } |
69 70 return diff; | 71 return (*left - *right); |
71} 72 73 74/* strdtb(s) - delete trailing blanks in string 's' and return new length 75 */ 76int 77strdtb(char *s) 78{ --- 38 unchanged lines hidden (view full) --- 117 118#else /* DEBUGGING */ 119 120 char *pc = flags; 121 122 DebugFlags = 0; 123 124 while (*pc) { | 72} 73 74 75/* strdtb(s) - delete trailing blanks in string 's' and return new length 76 */ 77int 78strdtb(char *s) 79{ --- 38 unchanged lines hidden (view full) --- 118 119#else /* DEBUGGING */ 120 121 char *pc = flags; 122 123 DebugFlags = 0; 124 125 while (*pc) { |
125 char **test; 126 int mask; | 126 const char **test; 127 int mask; |
127 128 /* try to find debug flag name in our list. 129 */ 130 for ( test = DebugFlagNames, mask = 1; | 128 129 /* try to find debug flag name in our list. 130 */ 131 for ( test = DebugFlagNames, mask = 1; |
131 *test && strcmp_until(*test, pc, ','); | 132 *test != NULL && strcmp_until(*test, pc, ','); |
132 test++, mask <<= 1 133 ) 134 ; 135 136 if (!*test) { 137 fprintf(stderr, 138 "unrecognized debug flag <%s> <%s>\n", 139 flags, pc); --- 226 unchanged lines hidden (view full) --- 366 fclose(allow); 367 if (deny) 368 fclose(deny); 369 return (isallowed); 370} 371 372 373void | 133 test++, mask <<= 1 134 ) 135 ; 136 137 if (!*test) { 138 fprintf(stderr, 139 "unrecognized debug flag <%s> <%s>\n", 140 flags, pc); --- 226 unchanged lines hidden (view full) --- 367 fclose(allow); 368 if (deny) 369 fclose(deny); 370 return (isallowed); 371} 372 373 374void |
374log_it(char *username, int xpid, char *event, const char *detail) | 375log_it(const char *username, int xpid, const char *event, const char *detail) |
375{ 376#if defined(LOG_FILE) || DEBUGGING | 376{ 377#if defined(LOG_FILE) || DEBUGGING |
377 PID_T pid = xpid; | 378 PID_T pid = xpid; |
378#endif 379#if defined(LOG_FILE) | 379#endif 380#if defined(LOG_FILE) |
380 char *msg; 381 TIME_T now = time((TIME_T) 0); 382 register struct tm *t = localtime(&now); | 381 char *msg; 382 TIME_T now = time((TIME_T) 0); 383 struct tm *t = localtime(&now); |
383#endif /*LOG_FILE*/ 384 385#if defined(SYSLOG) | 384#endif /*LOG_FILE*/ 385 386#if defined(SYSLOG) |
386 static int syslog_open = 0; | 387 static int syslog_open = 0; |
387#endif 388 389#if defined(LOG_FILE) 390 /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation. 391 */ 392 msg = malloc(strlen(username) 393 + strlen(event) 394 + strlen(detail) --- 69 unchanged lines hidden (view full) --- 464 LogFD = ERR; 465 } 466} 467 468 469/* two warnings: 470 * (1) this routine is fairly slow 471 * (2) it returns a pointer to static storage | 388#endif 389 390#if defined(LOG_FILE) 391 /* we assume that MAX_TEMPSTR will hold the date, time, &punctuation. 392 */ 393 msg = malloc(strlen(username) 394 + strlen(event) 395 + strlen(detail) --- 69 unchanged lines hidden (view full) --- 465 LogFD = ERR; 466 } 467} 468 469 470/* two warnings: 471 * (1) this routine is fairly slow 472 * (2) it returns a pointer to static storage |
473 * parameters: 474 * s: string we want the first word of 475 * t: terminators, implicitly including \0 |
|
472 */ 473char * 474first_word(char *s, char *t) 475{ 476 static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */ 477 static int retsel = 0; | 476 */ 477char * 478first_word(char *s, char *t) 479{ 480 static char retbuf[2][MAX_TEMPSTR + 1]; /* sure wish C had GC */ 481 static int retsel = 0; |
478 register char *rb, *rp; | 482 char *rb, *rp; |
479 480 /* select a return buffer */ 481 retsel = 1-retsel; 482 rb = &retbuf[retsel][0]; 483 rp = rb; 484 485 /* skip any leading terminators */ 486 while (*s && (NULL != strchr(t, *s))) { --- 10 unchanged lines hidden (view full) --- 497 return rb; 498} 499 500 501/* warning: 502 * heavily ascii-dependent. 503 */ 504static void | 483 484 /* select a return buffer */ 485 retsel = 1-retsel; 486 rb = &retbuf[retsel][0]; 487 rp = rb; 488 489 /* skip any leading terminators */ 490 while (*s && (NULL != strchr(t, *s))) { --- 10 unchanged lines hidden (view full) --- 501 return rb; 502} 503 504 505/* warning: 506 * heavily ascii-dependent. 507 */ 508static void |
505mkprint(register char *dst, register unsigned char *src, register int len) | 509mkprint(char *dst, unsigned char *src, int len) |
506{ | 510{ |
511 /* 512 * XXX 513 * We know this routine can't overflow the dst buffer because mkprints() 514 * allocated enough space for the worst case. 515 */ |
|
507 while (len-- > 0) 508 { | 516 while (len-- > 0) 517 { |
509 register unsigned char ch = *src++; | 518 unsigned char ch = *src++; |
510 511 if (ch < ' ') { /* control character */ 512 *dst++ = '^'; 513 *dst++ = ch + '@'; 514 } else if (ch < 0177) { /* printable */ 515 *dst++ = ch; 516 } else if (ch == 0177) { /* delete/rubout */ 517 *dst++ = '^'; --- 8 unchanged lines hidden (view full) --- 526 527 528/* warning: 529 * returns a pointer to malloc'd storage, you must call free yourself. 530 */ 531char * 532mkprints(unsigned char *src, unsigned int len) 533{ | 519 520 if (ch < ' ') { /* control character */ 521 *dst++ = '^'; 522 *dst++ = ch + '@'; 523 } else if (ch < 0177) { /* printable */ 524 *dst++ = ch; 525 } else if (ch == 0177) { /* delete/rubout */ 526 *dst++ = '^'; --- 8 unchanged lines hidden (view full) --- 535 536 537/* warning: 538 * returns a pointer to malloc'd storage, you must call free yourself. 539 */ 540char * 541mkprints(unsigned char *src, unsigned int len) 542{ |
534 register char *dst = malloc(len*4 + 1); | 543 char *dst = malloc(len*4 + 1); |
535 536 if (dst != NULL) 537 mkprint(dst, src, len); 538 539 return dst; 540} 541 542 543#ifdef MAIL_DATE 544/* Sat, 27 Feb 93 11:44:51 CST 545 * 123456789012345678901234567 546 */ 547char * 548arpadate(time_t *clock) 549{ 550 time_t t = clock ?*clock :time(0L); 551 struct tm *tm = localtime(&t); | 544 545 if (dst != NULL) 546 mkprint(dst, src, len); 547 548 return dst; 549} 550 551 552#ifdef MAIL_DATE 553/* Sat, 27 Feb 93 11:44:51 CST 554 * 123456789012345678901234567 555 */ 556char * 557arpadate(time_t *clock) 558{ 559 time_t t = clock ?*clock :time(0L); 560 struct tm *tm = localtime(&t); |
552 static char ret[32]; /* zone name might be >3 chars */ | 561 static char ret[60]; /* zone name might be >3 chars */ |
553 554 if (tm->tm_year >= 100) 555 tm->tm_year += 1900; 556 557 (void) snprintf(ret, sizeof(ret), "%s, %2d %s %d %02d:%02d:%02d %s", 558 DowNames[tm->tm_wday], 559 tm->tm_mday, 560 MonthNames[tm->tm_mon], --- 18 unchanged lines hidden --- | 562 563 if (tm->tm_year >= 100) 564 tm->tm_year += 1900; 565 566 (void) snprintf(ret, sizeof(ret), "%s, %2d %s %d %02d:%02d:%02d %s", 567 DowNames[tm->tm_wday], 568 tm->tm_mday, 569 MonthNames[tm->tm_mon], --- 18 unchanged lines hidden --- |