[go: up one dir, main page]

Menu

[r456]: / tags / start / src / tools.c  Maximize  Restore  History

Download this file

268 lines (219 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* -------------------------------------------------------
* Copyright (C) 2002-2003 Tommi Saviranta <tsaviran@cs.helsinki.fi>
* (C) 2002 Lee Hardy <lee@leeh.co.uk>
* (C) 1998-2002 Sebastian Kienzl <zap@riot.org>
* -------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "miau.h"
#include "tools.h"
#include "irc.h"
#ifdef VSNPRINTF_WORKAROUND
int
vsnprintf(
char *str,
size_t size,
const char *format,
va_list ap
)
{
/*
* TODO:
* Someone must have done this (smaller, better and safer)
* before...
*
* Now we simply ignore the threat and keep going.
*/
return vsprintf(str, format, ap);
}
#endif /* VSNPRINTF_WORKAROUND */
/*
* Convert to upper case.
*/
void
upcase(
char *what
)
{
char *doit;
if (what) {
for (doit = what; doit && *doit; doit++) {
*doit = (char) toupper((int) *doit);
}
}
}
/*
* Generates a random string or new nick out of the old one.
*/
void
randname(
char *randchar,
char *oldnick,
int length
)
{
int i;
int oldlen;
if (randchar == NULL) {
/* No target - nothing to do. */
return;
}
if (oldnick == NULL) {
/* No old nick (or generating salt), just generate a new one. */
for (i = 0; i < length; i++ ) {
randchar[i] = (char)('A' + (rand() % 56));
}
randchar[length] = '\0';
return;
}
/* Try to generate a new nick from the old one. */
oldlen = strlen(oldnick);
if (oldlen < length) {
/*
* Nick is shorter than maximum length, try adding a '^'
* at the end.
*/
xstrncpy(randchar, oldnick, oldlen);
randchar[oldlen] = cfg.nickfillchar;
randchar[oldlen + 1] = '\0';
return;
}
/* Nick is already as long as it can be. Try rotating the nick. */
xstrncpy(randchar + 1, oldnick, length - 1);
xstrncpy(randchar, oldnick + length - 1, 1);
}
/*
* Returns index of what in *str.
*/
int
pos(
const char *str,
const char what
)
{
/* It takes less space to do this than to use strchr. */
int i = 0;
if (str != NULL) {
while (str[i]) {
if (str[i] == what) return i;
i++;
}
}
return -1;
}
int
lastpos(
const char *str,
const char what
)
{
int i;
if (str != NULL) {
i = strlen(str) - 1;
while (i) {
if (str[i] == what) return i;
i--;
}
}
return -1;
}
char *
nextword(
char *string
)
{
int i = pos(string, ' ');
if (i == -1) {
return NULL;
} else {
return (string + i + 1);
}
}
char *
lastword(
char *from
)
{
int i = lastpos(from, ' ');
if (i == -1) {
return from;
} else {
return (from + i + 1);
}
}
/*
* Creates a timestamp in the form:
* Mar 21 11:23:12
*/
char *
gettimestamp(
int oldtime
)
{
time_t t;
struct tm *lt;
static char stamp[100];
if (oldtime == 0) {
time(&t);
} else {
t = (time_t) oldtime;
}
lt = localtime(&t);
strftime(stamp, 99, "%b %d %H:%M:%S", lt);
return stamp;
}
#ifdef UPTIME
void
getuptime(
time_t now,
int *days,
int *hours,
int *minutes,
int *seconds
)
{
*days = now / 86400;
now %= 86400;
*hours = now / 3600;
now %= 3600;
*minutes = now / 60;
*seconds = now % 60;
}
#endif /* UPTIME */
void
report(
char *format,
...
)
{
char buffer[150];
va_list va;
va_start(va, format);
vsnprintf(buffer, 149, format, va);
va_end(va);
fprintf(stdout, "%s + %s\n", gettimestamp(0), buffer);
irc_mnotice(&c_clients, status.nickname, buffer);
}
void
error(
char *format,
...
)
{
char buffer[150];
va_list va;
va_start(va, format);
vsnprintf(buffer, 149, format, va);
va_end(va);
fprintf(stdout, "%s - %s\n", gettimestamp(0), buffer);
irc_mnotice(&c_clients, status.nickname, buffer);
}