fix include file case
[reactos.git] / rosapps / net / ncftp / ncftp / log.c
1 /* log.c
2 *
3 * Copyright (c) 1992-2001 by Mike Gleason.
4 * All rights reserved.
5 *
6 */
7
8 #include "syshdrs.h"
9
10 #include "util.h"
11 #include "log.h"
12
13 extern int gMaxLogSize;
14 char gLogFileName[256];
15
16 extern char gOurDirectoryPath[];
17
18
19 void
20 InitLog(void)
21 {
22 OurDirectoryPath(gLogFileName, sizeof(gLogFileName), kLogFileName);
23 } /* InitLog */
24
25
26
27 void
28 LogXfer(const char *const mode, const char *const url)
29 {
30 FILE *fp;
31
32 if (gMaxLogSize == 0)
33 return; /* Don't log */
34
35 fp = fopen(gLogFileName, FOPEN_APPEND_TEXT);
36 if (fp != NULL) {
37 (void) fprintf(fp, " %s %s\n", mode, url);
38 (void) fclose(fp);
39 }
40 } /* LogOpen */
41
42
43
44 void
45 LogOpen(const char *const host)
46 {
47 time_t now;
48 FILE *fp;
49
50 if (gMaxLogSize == 0)
51 return; /* Don't log */
52
53 time(&now);
54 fp = fopen(gLogFileName, FOPEN_APPEND_TEXT);
55 if (fp != NULL) {
56 (void) fprintf(fp, "%s at %s", host, ctime(&now));
57 (void) fclose(fp);
58 }
59 } /* LogOpen */
60
61
62
63
64 void
65 EndLog(void)
66 {
67 FILE *new, *old;
68 struct Stat st;
69 long fat;
70 char str[512];
71 char tmpLog[256];
72
73 if (gOurDirectoryPath[0] == '\0')
74 return; /* Don't create in root directory. */
75
76 /* If the user wants to, s/he can specify the maximum size of the log file,
77 * so it doesn't waste too much disk space. If the log is too fat, trim the
78 * older lines (at the top) until we're under the limit.
79 */
80 if ((gMaxLogSize <= 0) || (Stat(gLogFileName, &st) < 0))
81 return; /* Never trim, or no log. */
82
83 if ((size_t)st.st_size < (size_t)gMaxLogSize)
84 return; /* Log size not over limit yet. */
85
86 if ((old = fopen(gLogFileName, FOPEN_READ_TEXT)) == NULL)
87 return;
88
89 /* Want to make it so we're about 30% below capacity.
90 * That way we won't trim the log each time we run the program.
91 */
92 fat = (long) st.st_size - (long) gMaxLogSize + (long) (0.30 * gMaxLogSize);
93 while (fat > 0L) {
94 if (fgets(str, (int) sizeof(str), old) == NULL)
95 return;
96 fat -= (long) strlen(str);
97 }
98 /* skip lines until a new site was opened */
99 for (;;) {
100 if (fgets(str, (int) sizeof(str), old) == NULL) {
101 (void) fclose(old);
102 (void) remove(gLogFileName);
103 return; /* Nothing left, start anew next time. */
104 }
105 if (! isspace(*str))
106 break;
107 }
108
109 /* Copy the remaining lines in "old" to "new" */
110 OurDirectoryPath(tmpLog, sizeof(tmpLog), "log.tmp");
111 if ((new = fopen(tmpLog, FOPEN_WRITE_TEXT)) == NULL) {
112 (void) fclose(old);
113 return;
114 }
115 (void) fputs(str, new);
116 while (fgets(str, (int) sizeof(str), old) != NULL)
117 (void) fputs(str, new);
118 (void) fclose(old);
119 (void) fclose(new);
120 if (remove(gLogFileName) < 0)
121 return;
122 if (rename(tmpLog, gLogFileName) < 0)
123 return;
124 } /* EndLog */