aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/utils.c
blob: 078471125d0578cecbcb7d5d7b2eca7f07fe94b5 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/* utils.c
 *
 */

/*
    fics - An internet chess server.
    Copyright (C) 1993  Richard V. Nash

    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.
*/

/* Revision history:
   name		email		yy/mm/dd	Change
   Richard Nash			93/10/22	Created
   Markus Uhlin			23/12/10	Fixed compiler warnings (plus more)
   Markus Uhlin			23/12/10	Deleted check_emailaddr()
   Markus Uhlin			23/12/17	Reformatted functions
   Markus Uhlin			23/12/25	Revised
   Markus Uhlin			24/03/16	Replaced unbounded string
						handling functions.
   Markus Uhlin			24/07/05	Fixed unusual struct allocations
						and replaced strcpy() with strlcpy().
   Markus Uhlin			24/07/07	Made certain functions private
   Markus Uhlin			24/07/07	Added parameter 'size' to
						psprintf_highlight().
   Markus Uhlin			24/08/11	Improved fix_time().
*/

#include "stdinclude.h"
#include "common.h"

#include <err.h>

#include "config.h"
#include "network.h"
#include "playerdb.h"
#include "rmalloc.h"
#include "utils.h"

#if __linux__
#include <bsd/string.h>
#endif

struct t_tree {
	struct t_tree	*left, *right;
	char		*name;
};

struct t_dirs {
	struct t_dirs	*left, *right;
	time_t		 mtime;
	struct t_tree	*files;
	char		*name;
};

PRIVATE char**	t_buffer = NULL;
PRIVATE int	t_buffersize = 0;

PUBLIC int
count_lines(FILE *fp)
{
	int c, nl = 0;

	while ((c = fgetc(fp)) != EOF)
		if (c == '\n')
			++nl;
	return nl;
}

PUBLIC int
iswhitespace(int c)
{
	if (c < ' ' || c == '\b' || c == '\n' || c == '\t' || c == ' ')
		return 1;
	return 0;
}

PUBLIC char *
getword(char *str)
{
	int i;
	static char word[MAX_WORD_SIZE];

	i = 0;

	while (*str && !iswhitespace(*str)) {
		word[i] = *str;
		str++;
		i++;

		if (i == MAX_WORD_SIZE) {
			i = (i - 1);
			break;
		}
	}

	word[i] = '\0';
	return word;
}

/*
 * Returns a pointer to the first whitespace in the argument.
 */
PUBLIC char *
eatword(char *str)
{
	while (*str && !iswhitespace(*str))
		str++;
	return str;
}

/*
 * Returns a pointer to the first non-whitespace char in the argument.
 */
PUBLIC char *
eatwhite(char *str)
{
	while (*str && iswhitespace(*str))
		str++;
	return str;
}

/*
 * Returns a pointer to the same string with trailing spaces removed.
 */
PUBLIC char *
eattailwhite(char *str)
{
	int len;

	if (str == NULL)
		return NULL;

	len = strlen(str);

	while (len > 0 && iswhitespace(str[len - 1]))
		len--;

	str[len] = '\0';
	return str;
}

/*
 * Returns the next word in a given string.
 */
PUBLIC char *
nextword(char *str)
{
	return eatwhite(eatword(str));
}

PUBLIC int
mail_string_to_address(char *addr, char *subj, char *str)
{
	FILE	*fp;
	char	 com[1000];

#ifdef SENDMAILPROG
	snprintf(com, sizeof com, "%s\n", SENDMAILPROG);
#else
	snprintf(com, sizeof com, "%s -s \"%s\" %s", MAILPROGRAM, subj, addr);
#endif

	fp = popen(com, "w");
	if (!fp)
		return -1;

#ifdef SENDMAILPROG
	fprintf(fp, "To: %s\nSubject: %s\n%s", addr, subj, str);
#else
	fprintf(fp, "%s", str);
#endif

	pclose(fp);
	return 0;
}

PUBLIC int
mail_string_to_user(int p, char *subj, char *str)
{
	if (parray[p].emailAddress &&
	    parray[p].emailAddress[0] &&
	    safestring(parray[p].emailAddress)) {
		return mail_string_to_address(parray[p].emailAddress, subj,
		    str);
	} else {
		return -1;
	}
}

PUBLIC int
mail_file_to_address(char *addr, char *subj, char *fname)
{
	FILE	*fp1, *fp2;
	char	 com[1000];
	char	 tmp[MAX_LINE_SIZE];

	/* maybe unused */
	(void) fp2;
	(void) tmp;

#ifdef SENDMAILPROG
	snprintf(com, sizeof com, "%s\n", SENDMAILPROG);
#else
	snprintf(com, sizeof com, "%s -s \"%s\" %s < %s&", MAILPROGRAM, subj,
	    addr, fname);
#endif
	if ((fp1 = popen(com, "w")) == NULL)
		return -1;
#ifdef SENDMAILPROG
	fprintf(fp1, "To: %s\nSubject: %s\n", addr, subj);
	if ((fp2 = fopen(fname, "r")) == NULL)
		return -1;
	while (!feof(fp2)) {
		fgets(tmp, MAX_LINE_SIZE - 1, fp2);
		if (!feof(fp2)) {
			fputs(tmp, fp1);
		}
	}
	fclose(fp2);
#endif
	pclose(fp1);
	return 0;
}

PUBLIC int
mail_file_to_user(int p, char *subj, char *fname)
{
	if (parray[p].emailAddress &&
	    parray[p].emailAddress[0] &&
	    safestring(parray[p].emailAddress)) {
		return mail_file_to_address(parray[p].emailAddress, subj,
		    fname);
	} else {
		return -1;
	}
}

/*
 * Process a command for a user
 */
PUBLIC int
pcommand(int p, char *comstr, ...)
{
	char tmp[MAX_LINE_SIZE];
	int current_socket = parray[p].socket;
	int retval;
	va_list ap;

	va_start(ap, comstr);
	vsnprintf(tmp, sizeof tmp, comstr, ap);
	va_end(ap);

	retval = process_input(current_socket, tmp);

	if (retval == COM_LOGOUT) {
		process_disconnection(current_socket);
		net_close_connection(current_socket);
	}

	return retval;
}

PUBLIC int
pprintf(int p, const char *format, ...)
{
	char tmp[10 * MAX_LINE_SIZE];
	int retval;
	va_list ap;

	va_start(ap, format);
	retval = vsnprintf(tmp, sizeof tmp, format, ap);
	va_end(ap);

	net_send_string(parray[p].socket, tmp, 1);
	return retval;
}

PRIVATE void
pprintf_dohightlight(int p)
{
	if (parray[p].highlight & 0x01)
		pprintf(p, "\033[7m");
	if (parray[p].highlight & 0x02)
		pprintf(p, "\033[1m");
	if (parray[p].highlight & 0x04)
		pprintf(p, "\033[4m");
	if (parray[p].highlight & 0x08)
		pprintf(p, "\033[2m");
}

PUBLIC int
pprintf_highlight(int p, char *format, ...)
{
	char tmp[10 * MAX_LINE_SIZE];
	int retval;
	va_list ap;

	pprintf_dohightlight(p);

	va_start(ap, format);
	retval = vsnprintf(tmp, sizeof tmp, format, ap);
	va_end(ap);

	net_send_string(parray[p].socket, tmp, 1);

	if (parray[p].highlight)
		pprintf(p, "\033[0m");
	return retval;
}

PRIVATE void
sprintf_dohightlight(int p, char *s, size_t size)
{
	if (parray[p].highlight & 0x01)
		strlcat(s, "\033[7m", size);
	if (parray[p].highlight & 0x02)
		strlcat(s, "\033[1m", size);
	if (parray[p].highlight & 0x04)
		strlcat(s, "\033[4m", size);
	if (parray[p].highlight & 0x08)
		strlcat(s, "\033[2m", size);
}

PUBLIC int
psprintf_highlight(int p, char *s, size_t size, char *format, ...)
{
	int retval;
	va_list ap;

	if (parray[p].highlight) {
		char tmp[1000] = { '\0' };

		va_start(ap, format);
		retval = vsnprintf(tmp, sizeof tmp, format, ap);
		va_end(ap);

		sprintf_dohightlight(p, s, size);
		strlcat(s, tmp, size);
		strlcat(s, "\033[0m", size);
	} else {
		va_start(ap, format);
		retval = vsnprintf(s, size, format, ap);
		va_end(ap);
	}

	return retval;
}

PUBLIC int
pprintf_prompt(int p, char *format, ...)
{
	char tmp[10 * MAX_LINE_SIZE];
	int retval;
	va_list ap;

	va_start(ap, format);
	retval = vsnprintf(tmp, sizeof tmp, format, ap);
	va_end(ap);

	net_send_string(parray[p].socket, tmp, 1);
	net_send_string(parray[p].socket, parray[p].prompt, 1);
	return retval;
}

PUBLIC int
pprintf_noformat(int p, char *format, ...)
{
	char tmp[10 * MAX_LINE_SIZE];
	int retval;
	va_list ap;

	va_start(ap, format);
	retval = vsnprintf(tmp, sizeof tmp, format, ap);
	va_end(ap);

	net_send_string(parray[p].socket, tmp, 0);
	return retval;
}

PUBLIC int
psend_raw_file(int p, char *dir, char *file)
{
	FILE	*fp;
	char	 fname[MAX_FILENAME_SIZE] = { '\0' };
	char	 tmp[MAX_LINE_SIZE] = { '\0' };
	int	 num;

	if (dir)
		snprintf(fname, sizeof fname, "%s/%s", dir, file);
	else
		strlcpy(fname, file, sizeof fname);

	if ((fp = fopen(fname, "r")) == NULL)
		return -1;

	while ((num = fread(tmp, sizeof(char), MAX_LINE_SIZE - 1, fp)) > 0) {
		tmp[num] = '\0';
		net_send_string(parray[p].socket, tmp, 1);
	}

	fclose(fp);
	return 0;
}

PUBLIC int
psend_file(int p, char *dir, char *file)
{
	FILE	*fp;
	char	 fname[MAX_FILENAME_SIZE] = { '\0' };
	char	 tmp[MAX_LINE_SIZE] = { '\0' };
	int	 lcount = (parray[p].d_height - 1);

	if (parray[p].last_file)
		rfree(parray[p].last_file);
	parray[p].last_file = NULL;
	parray[p].last_file_byte = 0L;

	if (dir)
		snprintf(fname, sizeof fname, "%s/%s", dir, file);
	else
		strlcpy(fname, file, sizeof fname);

	if ((fp = fopen(fname, "r")) == NULL)
		return -1;

	while (!feof(fp) && --lcount > 0) {
		fgets(tmp, MAX_LINE_SIZE - 1, fp);

		if (!feof(fp))
			net_send_string(parray[p].socket, tmp, 1);
	}

	if (!feof(fp)) {
		parray[p].last_file = xstrdup(fname);
		parray[p].last_file_byte = ftell(fp);
		pprintf(p, "Type [next] to see next page.\n");
	}

	fclose(fp);
	return 0;
}

/*
 * Marsalis added on 8/27/95 so that [next] does not appear in the
 * logout process for those that have a short screen height.
 */
PUBLIC int
psend_logoutfile(int p, char *dir, char *file)
{
	FILE	*fp;
	char	 fname[MAX_FILENAME_SIZE] = { '\0' };
	char	 tmp[MAX_LINE_SIZE] = { '\0' };

	if (parray[p].last_file)
		rfree(parray[p].last_file);
	parray[p].last_file = NULL;
	parray[p].last_file_byte = 0L;

	if (dir)
		snprintf(fname, sizeof fname, "%s/%s", dir, file);
	else
		strlcpy(fname, file, sizeof fname);

	if ((fp = fopen(fname, "r")) == NULL)
		return -1;

	while (!feof(fp)) {
		fgets(tmp, MAX_LINE_SIZE - 1, fp);

		if (!feof(fp))
			net_send_string(parray[p].socket, tmp, 1);
	}

	fclose(fp);
	return 0;
}

PUBLIC int
pmore_file(int p)
{
	FILE	*fp;
	char	 tmp[MAX_LINE_SIZE];
	int	 lcount = (parray[p].d_height - 1);

	if (!parray[p].last_file) {
		pprintf(p, "There is no more.\n");
		return -1;
	}

	if ((fp = fopen(parray[p].last_file, "r")) == NULL) {
		pprintf(p, "File not found!\n");
		return -1;
	}

	fseek(fp, parray[p].last_file_byte, SEEK_SET);

	while (!feof(fp) && --lcount > 0) {
		fgets(tmp, MAX_LINE_SIZE, fp);

		if (!feof(fp))
			net_send_string(parray[p].socket, tmp, 1);
	}

	if (!feof(fp)) {
		parray[p].last_file_byte = ftell(fp);
		pprintf(p, "Type [next] to see next page.\n");
	} else {
		rfree(parray[p].last_file);
		parray[p].last_file = NULL;
		parray[p].last_file_byte = 0L;
	}

	fclose(fp);
	return 0;
}

PUBLIC int
psend_command(int p, char *command, char *input)
{
	FILE	*fp;
	char	 tmp[MAX_LINE_SIZE];
	int	 num;

	if (input)
		fp = popen(command, "w");
	else
		fp = popen(command, "r");
	if (!fp)
		return -1;

	if (input) {
		fwrite(input, sizeof(char), strlen(input), fp);
	} else {
		while (!feof(fp)) {
			num = fread(tmp, sizeof(char), MAX_LINE_SIZE - 1, fp);
			tmp[num] = '\0';
			net_send_string(parray[p].socket, tmp, 1);
		}
	}

	pclose(fp);
	return 0;
}

PUBLIC char *
stolower(char *str)
{
	if (!str)
		return NULL;
	for (int i = 0; str[i]; i++) {
		if (isupper(str[i]))
			str[i] = tolower(str[i]);
	}
	return str;
}

PUBLIC int
safechar(int c)
{
	if (c == '>' || c == '!' || c == '&' || c == '*' || c == '?' ||
	    c == '/' || c == '<' || c == '|' || c == '`' || c == '$')
		return 0;
	return 1;
}

PUBLIC int
safestring(char *str)
{
	if (!str)
		return 1;
	for (int i = 0; str[i]; i++) {
		if (!safechar(str[i]))
			return 0;
	}
	return 1;
}

PUBLIC int
alphastring(char *str)
{
	if (!str)
		return 1;
	for (int i = 0; str[i]; i++) {
		if (!isalpha(str[i]))
			return 0;
	}
	return 1;
}

PUBLIC int
printablestring(char *str)
{
	if (!str)
		return 1;
	for (int i = 0; str[i]; i++) {
		if (!isprint(str[i]) && str[i] != '\t' && str[i] != '\n')
			return 0;
	}
	return 1;
}

PUBLIC char *
xstrdup(const char *str)
{
	char *out;

	if (str == NULL)
		return NULL;

	const size_t size = strlen(str) + 1;

	out = rmalloc(size);
	return memcpy(out, str, size);
}

PUBLIC char *
hms_desc(int t)
{
	int		days, hours, mins, secs;
	static char	tstr[80];

	days	= t / (60 * 60 * 24);
	hours	= (t % (60 * 60 * 24)) / (60 * 60);
	mins	= ((t % (60 * 60 * 24)) % (60 * 60)) / 60;
	secs	= ((t % (60 * 60 * 24)) % (60 * 60)) % 60;

	if (days == 0 && hours == 0 && mins == 0) {
		snprintf(tstr, sizeof tstr, "%d sec%s", secs, (secs == 1 ? "" :
		    "s"));
	} else if (days == 0 && hours == 0) {
		snprintf(tstr, sizeof tstr, "%d min%s", mins, (mins == 1 ? "" :
		    "s"));
	} else if (days == 0) {
		snprintf(tstr, sizeof tstr, "%d hr%s, %d min%s, %d sec%s",
		    hours, (hours == 1 ? "" : "s"),
		    mins, (mins == 1 ? "" : "s"),
		    secs, (secs == 1 ? "" : "s"));
	} else {
		snprintf(tstr, sizeof tstr, "%d day%s, %d hour%s, %d minute%s "
		    "and %d second%s",
		    days, (days == 1 ? "" : "s"),
		    hours, (hours == 1 ? "" : "s"),
		    mins, (mins == 1 ? "" : "s"),
		    secs, (secs == 1 ? "" : "s"));
	}

	return tstr;
}

PUBLIC char *
hms(int t, int showhour, int showseconds, int spaces)
{
	char		tmp[10];
	int		h, m, s;
	static char	tstr[20];

	h = (t / 3600);
	t = (t % 3600);
	m = (t / 60);
	s = (t % 60);

	if (h || showhour) {
		if (spaces)
			snprintf(tstr, sizeof tstr, "%d : %02d", h, m);
		else
			snprintf(tstr, sizeof tstr, "%d:%02d", h, m);
	} else {
		snprintf(tstr, sizeof tstr, "%d", m);
	}
	if (showseconds) {
		if (spaces)
			snprintf(tmp, sizeof tmp, " : %02d", s);
		else
			snprintf(tmp, sizeof tmp, ":%02d", s);
		strlcat(tstr, tmp, sizeof tstr);
	}
	return tstr;
}

PRIVATE char *
strtime(struct tm * stm)
{
	static char tstr[100];
#if defined (SGI)
	strftime(tstr, sizeof(tstr), "%a %b %e, %H:%M %Z", stm);
#else
	strftime(tstr, sizeof(tstr), "%a %b %e, %k:%M %Z", stm);
#endif
	return (tstr);
}

PUBLIC char *
fix_time(char *old_time)
{
	char		 date[5] = { '\0' };
	char		 day[5] = { '\0' };
	char		 i;
	char		 month[5] = { '\0' };
	static char	 new_time[20];

	_Static_assert(4 < ARRAY_SIZE(day), "Array too small");
	_Static_assert(4 < ARRAY_SIZE(month), "Array too small");
	_Static_assert(4 < ARRAY_SIZE(date), "Array too small");

	if (sscanf(old_time, "%4s %4s %4s", day, month, date) != 3)
		warnx("%s: sscanf: too few items (%s)", __func__, old_time);

	if (date[2] != ',') {
		i = date[0];
		date[0] = '0';
		date[1] = i;
	}
	date[2] = '\0';

	snprintf(new_time, sizeof new_time, "%s, %s %s", day, month, date);

	return &new_time[0];
}

PUBLIC char *
strltime(time_t *clock)
{
	struct tm *stm = localtime(clock);

	return strtime(stm);
}

PUBLIC char *
strgtime(time_t *clock)
{
	struct tm *stm = gmtime(clock);

	return strtime(stm);
}

/*
 * This is used only for relative timing since it reports seconds
 * since about 5:00 pm on Feb 16, 1994.
 */
PUBLIC unsigned int
tenth_secs(void)
{
	struct timeval	tp;
	struct timezone	tzp;

	gettimeofday(&tp, &tzp);

	return ((tp.tv_sec - 331939277) * 10L) + (tp.tv_usec / 100000);
}

/*
 * This is to translate tenths-secs time back into 1/1/70 time in full
 * seconds, because vek didn't read utils.c when he programmed new
 * ratings. 1 sec since 1970 fits into a 32 bit int OK.
 */
PUBLIC int
untenths(unsigned int tenths)
{
	return (tenths / 10 + 331939277 + 0xffffffff / 10 + 1);
}

PUBLIC char *
tenth_str(unsigned int t, int spaces)
{
	return hms((t + 5) / 10, 0, 1, spaces);
}

/*
 * XXX: if lines in the file are greater than 1024 bytes in length,
 * this won't work!
 */
PUBLIC int
truncate_file(char *file, int lines)
{
#define MAX_TRUNC_SIZE 100
	FILE	*fp;
	char	 tBuf[MAX_TRUNC_SIZE][MAX_LINE_SIZE];
	int	 bptr = 0, trunc = 0, i;

	if (lines > MAX_TRUNC_SIZE)
		lines = MAX_TRUNC_SIZE;

	if ((fp = fopen(file, "r")) == NULL)
		return 1;

	while (!feof(fp)) {
		fgets(tBuf[bptr], MAX_LINE_SIZE, fp);

		if (feof(fp))
			break;

		if (tBuf[bptr][strlen(tBuf[bptr]) - 1] != '\n') {
			// Line too long
			fclose(fp);
			return -1;
		}

		if (++bptr == lines) {
			trunc = 1;
			bptr = 0;
		}
	}

	fclose(fp);

	if (trunc) {
		fp = fopen(file, "w");

		for (i = 0; i < lines; i++) {
			fputs(tBuf[bptr], fp);

			if (++bptr == lines)
				bptr = 0;
		}

		fclose(fp);
	}

	return 0;
}

/*
 * XXX: If lines in the file are greater than 1024 bytes in length,
 * this won't work!
 */
PUBLIC int
lines_file(char *file)
{
	FILE	*fp;
	char	 tmp[MAX_LINE_SIZE];
	int	 lcount = 0;

	if ((fp = fopen(file, "r")) == NULL)
		return 0;

	while (!feof(fp)) {
		if (fgets(tmp, MAX_LINE_SIZE, fp))
			lcount++;
	}

	fclose(fp);
	return lcount;
}

PUBLIC int
file_has_pname(char *fname, char *plogin)
{
	if (!strcmp(file_wplayer(fname), plogin))
		return 1;
	if (!strcmp(file_bplayer(fname), plogin))
		return 1;
	return 0;
}

PUBLIC char *
file_wplayer(char *fname)
{
	char		*ptr;
	static char	 tmp[MAX_FILENAME_SIZE];

	strlcpy(tmp, fname, sizeof tmp);

	if ((ptr = strrchr(tmp, '-')) == NULL)
		return "";
	*ptr = '\0';
	return tmp;
}

PUBLIC char *
file_bplayer(char *fname)
{
	char *ptr;

	if ((ptr = strrchr(fname, '-')) == NULL)
		return "";
	return ptr + 1;
}

/*
 * Hey, leave this code alone. 'a' is always in network byte order,
 * which is big-endian, even on a little-endian machine. I fixed this
 * code to handle that correctly a while ago and someone gratuitously
 * changed it so that it would work correctly only on a big-endian
 * machine (like a Sun). I have now changed it back.  --mann
 */
PUBLIC char *
dotQuad(unsigned int a)
{
	static char	 tmp[20];
	unsigned char	*aa = (unsigned char *) &a;

	snprintf(tmp, sizeof tmp, "%d.%d.%d.%d", aa[0], aa[1], aa[2], aa[3]);
	return tmp;
}

PUBLIC int
available_space(void)
{
	return 100000000; /* Infinite space */
}

PUBLIC int
file_exists(char *fname)
{
	FILE *fp;

	if ((fp = fopen(fname, "r")) == NULL)
		return 0;
	fclose(fp);
	return 1;
}

PUBLIC char *
ratstr(int rat)
{
	static char	tmp[20][10];
	static int	on = 0;

	if (on == 20)
		on = 0;
	if (rat) {
		snprintf(tmp[on], sizeof tmp[on], "%4d", rat);
	} else {
		snprintf(tmp[on], sizeof tmp[on], "----");
	}

	on++;
	return tmp[on - 1];
}

PUBLIC char *
ratstrii(int rat, int reg)
{
	static char	tmp[20][10];
	static int	on = 0;

	if (on == 20)
		on = 0;
	if (rat) {
		snprintf(tmp[on], sizeof tmp[on], "%4d", rat);
	} else {
		if (reg) {
			snprintf(tmp[on], sizeof tmp[on], "----");
		} else {
			snprintf(tmp[on], sizeof tmp[on], "++++");
		}
	}

	on++;
	return tmp[on - 1];
}

/*
 * Fill 't_buffer' with anything matching "want*" in file tree
 */
PRIVATE void
t_sft(char *want, struct t_tree *t)
{
	if (t) {
		int cmp = strncmp(want, t->name, strlen(want));

		if (cmp <= 0) // If 'want' <= this one, look left
			t_sft(want, t->left);

		if (t_buffersize && cmp == 0) { // If a match, add it to buffer
			t_buffersize--;
			*t_buffer++ = t->name;
		}

		if (cmp >= 0) // If 'want' >= this one, look right
			t_sft(want, t->right);
	}
}

/*
 * Delete file tree
 */
PRIVATE void
t_cft(struct t_tree **t)
{
	if (t != NULL && *t != NULL) {
		t_cft(&(*t)->left);
		t_cft(&(*t)->right);
		rfree((*t)->name);
		rfree(*t);
		*t = NULL;
	}
}

/*
 * Make file tree for dir 'd'
 */
PRIVATE void
t_mft(struct t_dirs *d)
{
	DIR *dirp;
#ifdef USE_DIRENT
	struct dirent *dp;
#else
	struct direct *dp;
#endif
	struct t_tree **t;

	if ((dirp = opendir(d->name)) == NULL) {
		fprintf(stderr, "FICS: %s: couldn't opendir\n", __func__);
		return;
	}
	while ((dp = readdir(dirp))) {
		t = &d->files;

		if (dp->d_name[0] != '.') { // skip anything starting with '.'
			size_t size;

			while (*t) {
				if (strcmp(dp->d_name, (*t)->name) < 0)
					t = &(*t)->left;
				else
					t = &(*t)->right;
			}

			size = strlen(dp->d_name) + 1;
			*t = rmalloc(sizeof(struct t_tree));
			(*t)->right = (*t)->left = NULL;
			(*t)->name = rmalloc(size);

			if (strlcpy((*t)->name, dp->d_name, size) >= size)
				errx(1, "%s: name too long", __func__);
		}
	}
	closedir(dirp);
}

/*
 * dir = directory to search
 * filter = what to search for
 * buffer = where to store pointers to matches
 * buffersize = how many pointers will fit inside buffer
 */
PUBLIC int
search_directory(char *dir, char *filter, char **buffer, int buffersize)
{
	int			 cmp;
	static char		 nullify = '\0';
	static struct t_dirs	*ramdirs = NULL;
	struct stat		 statbuf;
	struct t_dirs**		 i;

	t_buffer	= buffer;
	t_buffersize	= buffersize;

	if (!stat(dir, &statbuf)) {
		if (filter == NULL)	// NULL becomes pointer to null string
			filter = &nullify;

		i = &ramdirs;

		while (*i) {		// Find dir in dir tree
			if ((cmp = strcmp(dir, (*i)->name)) == 0)
				break;
			else if (cmp < 0)
				i = &(*i)->left;
			else
				i = &(*i)->right;
		}

		if (!*i) {			// If dir isn't in dir tree,
			size_t size;		// add him.

			size = strlen(dir) + 1;
			*i = rmalloc(sizeof(struct t_dirs));
			(*i)->left = (*i)->right = NULL;
			(*i)->files = NULL;
			(*i)->name = rmalloc(size);

			if (strlcpy((*i)->name, dir, size) >= size)
				errx(1, "%s: dir too long", __func__);
		}

		if ((*i)->files) {		// Delete any obsolete file
						// tree.
			if ((*i)->mtime != statbuf.st_mtime)
				t_cft(&(*i)->files);
		}

		if ((*i)->files == NULL) {	// If no file tree for him,
						// make one.
			(*i)->mtime = statbuf.st_mtime;
			t_mft(*i);
		}

		t_sft(filter, (*i)->files);	// Finally, search for matches.
	}

	return (buffersize - t_buffersize);
}

PUBLIC int
display_directory(int p, char **buffer, int count)
{ // 'buffer' contains 'count' string pointers.
	int		 i;
	multicol	*m = multicol_start(count);

	for (i = 0; (i < count); i++)
		multicol_store(m, *buffer++);
	multicol_pprint(m, p, 78, 1);
	multicol_end(m);
	return (i);
}