/usr.bin/tip/tip/log.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 92 lines · 51 code · 8 blank · 33 comment · 9 complexity · 006ba234aaf7f1e878cfd25555c6e3fa MD5 · raw file

  1. /* $OpenBSD: log.c,v 1.8 2006/03/16 19:32:46 deraadt Exp $ */
  2. /* $NetBSD: log.c,v 1.4 1994/12/24 17:56:28 cgd Exp $ */
  3. /*
  4. * Copyright (c) 1983, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. #ifndef lint
  34. #if 0
  35. static char sccsid[] = "@(#)log.c 8.1 (Berkeley) 6/6/93";
  36. static const char rcsid[] = "$OpenBSD: log.c,v 1.8 2006/03/16 19:32:46 deraadt Exp $";
  37. #endif
  38. #endif /* not lint */
  39. #include "tip.h"
  40. #ifdef ACULOG
  41. static FILE *flog = NULL;
  42. /*
  43. * Log file maintenance routines
  44. */
  45. void
  46. logent(char *group, char *num, char *acu, char *message)
  47. {
  48. char *user, *timestamp;
  49. struct passwd *pwd;
  50. time_t t;
  51. if (flog == NULL)
  52. return;
  53. if (flock(fileno(flog), LOCK_EX) < 0) {
  54. perror("flock");
  55. return;
  56. }
  57. if ((user = getlogin()) == NOSTR) {
  58. if ((pwd = getpwuid(getuid())) == NOPWD)
  59. user = "???";
  60. else
  61. user = pwd->pw_name;
  62. }
  63. t = time(0);
  64. timestamp = ctime(&t);
  65. timestamp[24] = '\0';
  66. fprintf(flog, "%s (%s) <%s, %s, %s> %s\n",
  67. user, timestamp, group,
  68. #ifdef PRISTINE
  69. "",
  70. #else
  71. num,
  72. #endif
  73. acu, message);
  74. (void) fflush(flog);
  75. (void) flock(fileno(flog), LOCK_UN);
  76. }
  77. void
  78. loginit(void)
  79. {
  80. flog = fopen(value(LOG), "a");
  81. if (flog == NULL)
  82. fprintf(stderr, "can't open log file %s.\r\n", value(LOG));
  83. }
  84. #endif