[go: up one dir, main page]

Menu

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

Download this file

290 lines (241 with data), 6.2 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* $Id$
* -------------------------------------------------------
* Copyright (C) 2002-2005 Tommi Saviranta <wnd@iki.fi>
* -------------------------------------------------------
* 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* ifdef HAVE_CONFIG_H */
#include "tools.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/**
* Convert to upper case.
* @str: String to convert
*/
void
upcase(char *str)
{
char *ptr;
for (ptr = str; ptr != NULL && *ptr != '\0'; ptr++) {
*ptr = (char) toupper((int) *ptr);
}
} /* void upcade(char *str) */
/**
* Convert to lower case.
* @str: String to convert
*/
void
lowcase(char *str)
{
char *ptr;
for (ptr = str; ptr != NULL && *ptr != '\0'; ptr++) {
*ptr = (char) tolower((int) *ptr);
}
} /* void lowcase(char *str) */
/**
* Generates a random string or new nick out of the old one.
* @target: Target. It *target != '\0', target will be treated as old nick.
* If *target == '\0', completely random string will be created.
* @length: (Maximum) length for target
* @fillchar: Character to use pad undersized target
*
* Target must have space for length + 1 characters.
*
* This function doesn't work with multibyte characters.
*/
void
randname(char *target, const size_t length, const char fillchar)
{
size_t oldlen;
char shift;
/* TODO paranoia */
if (target == NULL) {
/* No target - nothing to do. */
return;
}
/* Genereate from scratch? */
if (target[0] == '\0') {
size_t i;
for (i = 0; i < length; i++) {
/*
* Using rand() here could be unwise due to bad
* randomness of it. We ignore the threat, as
* a) user can always run mkpasswd,
* b) knowing the password _should_ only expose
* your IRC client for public, not your computer
*/
target[i] = (char)('A' + (rand() % 56));
}
target[length] = '\0';
return;
}
/* Try to generate a new nick from the old one. */
oldlen = strlen(target);
if (oldlen < length) {
/*
* Nick is shorter than maximum length, try adding fillchar
* at the end.
*/
target[oldlen] = fillchar;
target[oldlen + 1] = '\0';
return;
}
/* Nick is already as long as it can be. Try rotating the nick. */
shift = target[length - 1];
memmove(target + 1, target, length - 1);
target[0] = shift;
} /* void randname(char *target, const size_t length, const char fillchar) */
/**
* Returns index of first occurance of "c" in "str".
* @str: String to look from
* @c: Character to find
*
* Returns: Index of last occurance of "c" in "str". -1 if "c" was not found.
*
* This function does _not_ work with multibyte characters. We get "int c"
* instead of "char c" so that this could be fixed without major rewriting.
*/
int
pos(const char *s, const int c)
{
/* It takes less space to do this than to use strchr. */
int i;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == c) {
return i;
}
}
return -1;
} /* int pos(const char *s, const int c) */
/**
* Returns index of last occurance of "c" in "str".
* @str: String to look from
* @c: Character to find
*
* Returns: Index of last occurance of "c" in "str". -1 if "c" was not found.
*
* This function does _not_ work with multibyte characters. We get "int c"
* instead of "char c" so that this could be fixed without major rewriting.
*/
int
lastpos(const char *s, const int c)
{
/* It takes less space to do this than to use strchr. */
int i;
if (s == NULL) {
return -1;
}
for (i = strlen(s); i >= 0; i--) {
if (s[i] == c) {
return i;
}
}
return -1;
} /* int lastpos(const char *s, const int c) */
/**
* Finds next word starting from s.
* @s: Where to start searching.
*
* Returns: Pointer to stuff starting from next word, NULL if there are no
* words left.
*/
char *
nextword(char *s)
{
int i;
i = pos(s, ' ');
if (i == -1) {
return NULL;
} else {
return s + i + 1;
}
} /* char *nextword(char *s) */
/**
* Finds last word, starting from s.
* @s: Where to start searching.
*
* Returns: Pointer to last word, NULL if there are no words before s.
*/
char *
lastword(char *s)
{
int i;
i = lastpos(s, ' ');
if (i == -1) {
return s;
} else {
return s + i + 1;
}
} /* char *lastword(char *s) */
#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;
} /* void getuptime(time_t now, int *days, int *hours, int *minutes,
int *seconds) */
#endif /* UPTIME */
/**
* Creates a timestamp.
* @t: Pointer to time, NULL if current time.
* @mode: Timestamp format
*
* Returns: Pointer to timestamp. Allocated memory must not be freed.
*
* Creates timestamps like:
* Sun Jan 02 11:53:33 2002
* or
* Jan 02 11:53:33
*/
const char *
get_timestamp(time_t *t, const timestamp_t mode)
{
struct tm *form;
static char stamp[100];
time_t ts;
if (t == NULL) {
time(&ts);
} else {
ts = *t;
}
form = localtime(&ts);
switch (mode) {
case TIMESTAMP_LONG:
strftime(stamp, 99, "%a %b %d %H:%M:%S %Y", form);
break;
case TIMESTAMP_SHORT:
strftime(stamp, 99, "%b %d %H:%M:%S", form);
}
stamp[99] = '\0';
return stamp;
} /* char *get_timestamp(time_t *t, const timestamp_t mode) */
/**
* Creates a short current timestamp.
*
* Returns: Pointer to timestamp. Allocated memory must not be freed.
*
* Creates a timestamp like: "Jan 02 11:53:33"
*/
const char *
get_short_localtime(void)
{
/* termination guaranteed in get_timestamp() */
return get_timestamp(NULL, TIMESTAMP_SHORT);
} /* const char *get_short_localtime(void) */