File: util.c

package info (click to toggle)
trn4 4.0-test77-12
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 3,644 kB
  • sloc: ansic: 48,331; sh: 6,817; tcl: 1,696; yacc: 660; perl: 108; makefile: 24
file content (1077 lines) | stat: -rw-r--r-- 21,727 bytes parent folder | download | duplicates (12)
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
/* util.c
 */
/* This software is copyrighted as detailed in the LICENSE file. */


#include "EXTERN.h"
#include "common.h"
#include "final.h"
#include "term.h"
#include "list.h"
#include "hash.h"
#include "ngdata.h"
#include "nntpclient.h"
#include "datasrc.h"
#include "nntp.h"
#include "nntpauth.h"
#include "intrp.h"
#include "env.h"
#include "util2.h"
#include "only.h"
#include "search.h"
#ifdef I_SYS_WAIT
#include <sys/wait.h>
#endif
#ifdef MSDOS
#include <process.h>
#endif
#ifdef SCAN
#include "scan.h"
#include "smisc.h"	/* s_default_cmd */
#endif
#include "univ.h"
#include "INTERN.h"
#include "util.ih"
#include "util.h"

#ifdef UNION_WAIT
typedef union wait WAIT_STATUS;
#else
typedef int WAIT_STATUS;
#endif

#ifndef USE_DEBUGGING_MALLOC
static char nomem[] = "trn: out of memory!\n";
#endif

static char null_export[] = "_=X";/* Just in case doshell precedes util_init */

static char* newsactive_export = null_export + 2;
static char* grpdesc_export = null_export + 2;
static char* quotechars_export = null_export + 2;
#ifdef SUPPORT_NNTP
static char* nntpserver_export = null_export + 2;
static char* nntpfds_export = null_export + 2;
#ifdef USE_GENAUTH
static char* nntpauth_export = null_export + 2;
#endif
static char* nntpforce_export = null_export + 2;
#endif

void
util_init()
{
    extern char patchlevel[];
    char* cp;
    int i;
    for (i = 0, cp = buf; i < 512; i++)
	*cp++ = 'X';
    *cp = '\0';
    newsactive_export = export("NEWSACTIVE", buf);
    grpdesc_export = export("NEWSDESCRIPTIONS", buf);
#ifdef SUPPORT_NNTP
    nntpserver_export = export("NNTPSERVER", buf);
#endif
    buf[64] = '\0';
    quotechars_export = export("QUOTECHARS",buf);
#ifdef SUPPORT_NNTP
    nntpfds_export = export("NNTPFDS", buf);
#ifdef USE_GENAUTH
    nntpauth_export = export("NNTP_AUTH_FDS", buf);
#endif
    buf[3] = '\0';
    nntpforce_export = export("NNTP_FORCE_AUTH", buf);
#endif

    for (cp = patchlevel; isspace(*cp); cp++) ;
    export("TRN_VERSION", cp);
}
    
/* fork and exec a shell command */

int
doshell(shell,s)
char* shell;
char* s;
{
#ifndef MSDOS
    WAIT_STATUS status;
    pid_t pid, w;
#endif
    int ret;

    xmouse_off();

#ifdef SIGTSTP
    sigset(SIGTSTP,SIG_DFL);
    sigset(SIGTTOU,SIG_DFL);
    sigset(SIGTTIN,SIG_DFL);
#endif
#ifdef SUPPORT_NNTP
    if (datasrc && (datasrc->flags & DF_REMOTE)) {
#ifdef USE_GENAUTH
	if (export_nntp_fds) {
	    if (!nntplink.rd_fp) {
		if (nntp_command("DATE") <= 0 || nntp_check() < 0)
		    finalize(1); /*$$*/
	    }
	    sprintf(buf,"%d.%d.%d",(int)fileno(nntplink.rd_fp),
		    (int)fileno(nntplink.wr_fp),nntplink.cookiefd);
	    re_export(nntpauth_export, buf, 512);
	}
	else
	    un_export(nntpauth_export);
#endif
	if (!export_nntp_fds || !nntplink.rd_fp)
	    un_export(nntpfds_export);
	else {
	    sprintf(buf,"%d.%d",(int)fileno(nntplink.rd_fp),
				(int)fileno(nntplink.wr_fp));
	    re_export(nntpfds_export, buf, 64);
	}
	re_export(nntpserver_export,datasrc->newsid,512);
	if (datasrc->nntplink.flags & NNTP_FORCE_AUTH_NEEDED)
	    re_export(nntpforce_export,"yes",3);
	else
	    un_export(nntpforce_export);
	if (datasrc->auth_user) {
	    int fd;
	    if ((fd = open(nntp_auth_file, O_WRONLY|O_CREAT, 0600)) >= 0) {
		write(fd, datasrc->auth_user, strlen(datasrc->auth_user));
		write(fd, "\n", 1);
		if (datasrc->auth_pass) {
		    write(fd, datasrc->auth_pass, strlen(datasrc->auth_pass));
		    write(fd, "\n", 1);
		}
		close(fd);
	    }
	}
	if (nntplink.port_number) {
	    int len = strlen(nntpserver_export);
	    sprintf(buf,";%d",nntplink.port_number);
	    if (len + (int)strlen(buf) < 511)
		strcpy(nntpserver_export+len, buf);
	}
	if (datasrc->act_sf.fp)
	    re_export(newsactive_export, datasrc->extra_name, 512);
	else
	    re_export(newsactive_export, "none", 512);
    } else {
#ifdef SUPPORT_NNTP
	un_export(nntpfds_export);
#ifdef USE_GENAUTH
	un_export(nntpauth_export);
#endif
	un_export(nntpserver_export);
	un_export(nntpforce_export);
#endif
	if (datasrc)
	    re_export(newsactive_export, datasrc->newsid, 512);
	else
	    un_export(newsactive_export);
    }
#else
    if (datasrc)
	re_export(newsactive_export, datasrc->newsid, 512);
    else
	un_export(newsactive_export);
#endif
    if (datasrc)
	re_export(grpdesc_export, datasrc->grpdesc, 512);
    else
	un_export(grpdesc_export);
    interp(buf,64-1+2,"%I");
    buf[strlen(buf)-1] = '\0';
    re_export(quotechars_export, buf+1, 64);
    if (shell == NULL && (shell = getval("SHELL",NULL)) == NULL)
	shell = PREFSHELL;
    termlib_reset();
#ifdef MSDOS
    status = spawnl(P_WAIT, shell, shell, "/c", s, (char*)NULL);
#else
    if ((pid = vfork()) == 0) {
#ifdef SUPPORT_NNTP
	if (datasrc && (datasrc->flags & DF_REMOTE)) {
	    int i;
	    /* This is necessary to keep the bourne shell from puking */
	    for (i = 3; i < 10; ++i) {
		if (nntplink.rd_fp
		 && (i == fileno(nntplink.rd_fp)
		  || i == fileno(nntplink.wr_fp)))
		    continue;
#ifdef USE_GENAUTH
		if (i == nntplink.cookiefd)
		    continue;
#endif
		close(i);
	    }
	}
#endif /* SUPPORT_NNTP */
	if (nowait_fork) {
	    close(1);
	    close(2);
	    dup(open("/dev/null",1));
	}

	if (*s)
	    execl(shell, shell, "-c", s, (char*)NULL);
	else
	    execl(shell, shell, (char*)NULL, (char*)NULL, (char*)NULL);
	_exit(127);
    }
    sigignore(SIGINT);
#ifdef SIGQUIT
    sigignore(SIGQUIT);
#endif 
    waiting = TRUE;
    while ((w = wait(&status)) != pid)
	if (w == -1 && errno != EINTR)
	    break;
    if (w == -1)
	ret = -1;
    else
#ifdef USE_WIFSTAT
	ret = WEXITSTATUS(status);
#else
#ifdef UNION_WAIT
	ret = status.w_status >> 8;
#else
	ret = status;
#endif /* UNION_WAIT */
#endif /* USE_WIFSTAT */
#endif /* !MSDOS */
    termlib_init();
    xmouse_check();
    waiting = FALSE;
    sigset(SIGINT,int_catcher);
#ifdef SIGQUIT
    sigset(SIGQUIT,SIG_DFL);
#endif 
#ifdef SIGTSTP
    sigset(SIGTSTP,stop_catcher);
    sigset(SIGTTOU,stop_catcher);
    sigset(SIGTTIN,stop_catcher);
#endif
#ifdef SUPPORT_NNTP
    if (datasrc && datasrc->auth_user)
	UNLINK(nntp_auth_file);
#endif
    return ret;
}

/* paranoid version of malloc */

#ifndef USE_DEBUGGING_MALLOC
char*
safemalloc(size)
MEM_SIZE size;
{
    char* ptr;

    ptr = malloc(size ? size : (MEM_SIZE)1);
    if (!ptr) {
	fputs(nomem,stdout) FLUSH;
	sig_catcher(0);
    }
    return ptr;
}
#endif

/* paranoid version of realloc.  If where is NULL, call malloc */

#ifndef USE_DEBUGGING_MALLOC
char*
saferealloc(where,size)
char* where;
MEM_SIZE size;
{
    char* ptr;

    if (!where)
	ptr = malloc(size ? size : (MEM_SIZE)1);
    else
	ptr = realloc(where, size ? size : (MEM_SIZE)1);
    if (!ptr) {
	fputs(nomem,stdout) FLUSH;
	sig_catcher(0);
    }
    return ptr;
}
#endif /* !USE_DEBUGGING_MALLOC */

/* safe version of string concatenate, with \n deletion and space padding */

char*
safecat(to,from,len)
char* to;
register char* from;
register int len;
{
    register char* dest = to;

    len--;				/* leave room for null */
    if (*dest) {
	while (len && *dest++) len--;
	if (len) {
	    len--;
	    *(dest-1) = ' ';
	}
    }
    if (from)
	while (len && (*dest++ = *from++)) len--;
    if (len)
	dest--;
    if (*(dest-1) == '\n')
	dest--;
    *dest = '\0';
    return to;
}

/* effective access */

#ifdef SETUIDGID
int
eaccess(filename, mod)
char* filename;
int mod;
{
    int protection, euid;
    
    mod &= 7;				/* remove extraneous garbage */
    if (stat(filename, &filestat) < 0)
	return -1;
    euid = geteuid();
    if (euid == ROOTID)
	return 0;
    protection = 7 & ( filestat.st_mode >> (filestat.st_uid == euid ?
			6 : (filestat.st_gid == getegid() ? 3 : 0)) );
    if ((mod & protection) == mod)
	return 0;
    errno = EACCES;
    return -1;
}
#endif

/*
 * Get working directory
 */
char*
trn_getwd(buf, buflen)
char* buf;
int buflen;
{
    char* ret;

#ifdef HAS_GETCWD
    ret = getcwd(buf, buflen);
#else
    ret = trn_getcwd(buf, buflen);
#endif
    if (!ret) {
	printf("Cannot determine current working directory!\n") FLUSH;
	finalize(1);
    }
#ifdef MSDOS
    strlwr(buf);
    while ((buf = index(buf,'\\')) != NULL)
	*buf++ = '/';
#endif
    return ret;
}

#ifndef HAS_GETCWD
static char*
trn_getcwd(buf, len)
char* buf;
int len;
{
    char* ret;
#ifdef HAS_GETWD
    buf[len-1] = 0;
    ret = getwd(buf);
    if (buf[len-1]) {
	/* getwd() overwrote the end of the buffer */
	printf("getwd() buffer overrun!\n") FLUSH;
	finalize(1);
    }
#else
    FILE* popen();
    FILE* pipefp;
    char* nl;

    if ((pipefp = popen("/bin/pwd","r")) == NULL) {
	printf("Can't popen /bin/pwd\n") FLUSH;
	return NULL;
    }
    buf[0] = 0;
    fgets(ret = buf, len, pipefp);
    if (pclose(pipefp) == EOF) {
	printf("Failed to run /bin/pwd\n") FLUSH;
	return NULL;
    }
    if (!buf[0]) {
	printf("/bin/pwd didn't output anything\n") FLUSH;
    	return NULL;
    }
    if ((nl = index(buf, '\n')) != NULL)
	*nl = '\0';
#endif
    return ret;
}
#endif

/* just like fgets but will make bigger buffer as necessary */

char*
get_a_line(buffer,buffer_length,realloc_ok,fp)
char* buffer;
register int buffer_length;
bool_int realloc_ok;
FILE* fp;
{
    register int bufix = 0;
    register int nextch;

    do {
	if (bufix >= buffer_length) {
	    buffer_length *= 2;
	    if (realloc_ok) {		/* just grow in place, if possible */
		buffer = saferealloc(buffer,(MEM_SIZE)buffer_length+1);
	    }
	    else {
		char* tmp = safemalloc((MEM_SIZE)buffer_length+1);
		strncpy(tmp,buffer,buffer_length/2);
		buffer = tmp;
		realloc_ok = TRUE;
	    }
	}
	if ((nextch = getc(fp)) == EOF) {
	    if (!bufix)
		return NULL;
	    break;
	}
	buffer[bufix++] = (char)nextch;
    } while (nextch && nextch != '\n');
    buffer[bufix] = '\0';
    len_last_line_got = bufix;
    buflen_last_line_got = buffer_length;
    return buffer;
}

int
makedir(dirname,nametype)
register char* dirname;
int nametype;
{
#ifdef MAKEDIR
    register char* end;
    register char* s;
# ifdef HAS_MKDIR
    int status = 0;
# else
    char tmpbuf[1024];
    register char* tbptr = tmpbuf+5;
# endif

    for (end = dirname; *end; end++) ;	/* find the end */
    if (nametype == MD_FILE) {		/* not to create last component? */
	for (--end; end != dirname && *end != '/'; --end) ;
	if (*end != '/')
	    return 0;			/* nothing to make */
	*end = '\0';			/* isolate file name */
    }
# ifndef HAS_MKDIR
    strcpy(tmpbuf,"mkdir");
# endif

    s = end;
    for (;;) {
	if (stat(dirname,&filestat) >= 0 && S_ISDIR(filestat.st_mode)) {
					/* does this much exist as a dir? */
	    *s = '/';			/* mark this as existing */
	    break;
	}
	s = rindex(dirname,'/');	/* shorten name */
	if (!s)				/* relative path! */
	    break;			/* hope they know what they are doing */
	*s = '\0';			/* mark as not existing */
    }
    
    for (s=dirname; s <= end; s++) {	/* this is grody but efficient */
	if (!*s) {			/* something to make? */
# ifdef HAS_MKDIR
	    status = status || mkdir(dirname,0777);
# else
	    sprintf(tbptr," %s",dirname);
	    tbptr += strlen(tbptr);	/* make it, sort of */
# endif
	    *s = '/';			/* mark it made */
	}
    }
    if (nametype == MD_DIR)		/* don't need final slash unless */
	*end = '\0';			/*  a filename follows the dir name */

# ifdef HAS_MKDIR
    return status;
# else
    return (tbptr==tmpbuf+5 ? 0 : doshell(sh,tmpbuf));/* exercise our faith */
# endif
#else
    sprintf(cmd_buf,"%s %s %d", filexp(DIRMAKER), dirname, nametype);
    return doshell(sh,cmd_buf);
#endif
}

void
notincl(feature)
char* feature;
{
    printf("\nNo room for feature \"%s\" on this machine.\n",feature) FLUSH;
}

/* grow a static string to at least a certain length */

void
growstr(strptr,curlen,newlen)
char** strptr;
int* curlen;
int newlen;
{
    if (newlen > *curlen) {		/* need more room? */
	if (*curlen)
	    *strptr = saferealloc(*strptr,(MEM_SIZE)newlen);
	else
	    *strptr = safemalloc((MEM_SIZE)newlen);
	*curlen = newlen;
    }
}

void
setdef(buffer,dflt)
char* buffer;
char* dflt;
{
#ifdef SCAN
    s_default_cmd = FALSE;
#endif
    univ_default_cmd = FALSE;
    if (*buffer == ' '
#ifndef STRICTCR
     || *buffer == '\n' || *buffer == '\r'
#endif
    ) {
#ifdef SCAN
	s_default_cmd = TRUE;
#endif
	univ_default_cmd = TRUE;
	if (*dflt == '^' && isupper(dflt[1]))
	    pushchar(Ctl(dflt[1]));
	else
	    pushchar(*dflt);
	getcmd(buffer);
    }
}

#ifndef NO_FILELINKS
void
safelink(old, new)
char* old;
char* new;
{
#if 0
    extern int sys_nerr;
    extern char* sys_errlist[];
#endif

    if (link(old,new)) {
	printf("Can't link backup (%s) to .newsrc (%s)\n", old, new) FLUSH;
#if 0
	if (errno>0 && errno<sys_nerr)
	    printf("%s\n", sys_errlist[errno]);
#endif
	finalize(1);
    }
}
#endif

#ifndef HAS_STRSTR
char*
trn_strstr(s1, s2)
char* s1;
char* s2;
{
    register char* p = s1;
    register int len = strlen(s2);

    for ( ; (p = index(p, *s2)) != NULL; p++)
	if (strnEQ(p, s2, len))
	    return p;
    return NULL;
}
#endif /* !HAS_STRSTR */

/* attempts to verify a cryptographic signature. */
void
verify_sig()
{
    int i;

    printf("\n");
    /* RIPEM */
    i = doshell(sh,filexp("grep -s \"BEGIN PRIVACY-ENHANCED MESSAGE\" %A"));
    if (!i) {	/* found RIPEM */
	i = doshell(sh,filexp(getval("VERIFY_RIPEM",VERIFY_RIPEM)));
	printf("\nReturned value: %d\n",i) FLUSH;
	return;
    }
    /* PGP */
    i = doshell(sh,filexp("grep -s \"BEGIN PGP\" %A"));
    if (!i) {	/* found PGP */
	i = doshell(sh,filexp(getval("VERIFY_PGP",VERIFY_PGP)));
	printf("\nReturned value: %d\n",i) FLUSH;
	return;
    }
    printf("No PGP/RIPEM signatures detected.\n") FLUSH;
}

double
current_time()
{
#ifdef HAS_GETTIMEOFDAY
    Timeval t;
    (void) gettimeofday(&t, (struct timezone*)NULL);
    return (double)t.tv_usec / 1000000. + t.tv_sec;
#else
# ifdef HAS_FTIME
    Timeval t;
    ftime(&t);
    return (double)t.millitm / 1000. + t.time;
# else
    return (double)time((time_t*)NULL);
# endif
#endif
}

time_t
text2secs(s, defSecs)
char* s;
time_t defSecs;
{
    time_t secs = 0;
    time_t item;

    if (!isdigit(*s)) {
	if (*s == 'm' || *s == 'M')	/* "missing" */
	    return 2;
	if (*s == 'y' || *s == 'Y')	/* "yes" */
	    return defSecs;
	return secs;			/* "never" */
    }
    do {
	item = atol(s);
	while (isdigit(*s)) s++;
	while (isspace(*s)) s++;
	if (isalpha(*s)) {
	    switch (*s) {
	      case 'd': case 'D':
		item *= 24 * 60L;
		break;
	      case 'h': case 'H':
		item *= 60L;
		break;
	      case 'm': case 'M':
		break;
	      default:
		item = 0;
		break;
	    }
	    while (isalpha(*s)) s++;
	    if (*s == ',') s++;
	    while (isspace(*s)) s++;
	}
	secs += item;
    } while (isdigit(*s));

    return secs * 60;
}

char*
secs2text(secs)
time_t secs;
{
    char* s = buf;
    int items;

    if (!secs || (secs & 1))
	return "never";
    if (secs & 2)
	return "missing";

    secs /= 60;
    if (secs >= 24L * 60) {
	items = (int)(secs / (24*60));
	secs = secs % (24*60);
	sprintf(s, "%d day%s, ", items, PLURAL(items));
	s += strlen(s);
    }
    if (secs >= 60L) {
	items = (int)(secs / 60);
	secs = secs % 60;
	sprintf(s, "%d hour%s, ", items, PLURAL(items));
	s += strlen(s);
    }
    if (secs) {
	sprintf(s, "%d minute%s, ", (int)secs, PLURAL(items));
	s += strlen(s);
    }
    s[-2] = '\0';
    return buf;
}

/* returns a saved string representing a unique temporary filename */
char*
temp_filename()
{
    static int tmpfile_num = 0;
    char tmpbuf[CBUFLEN];
    extern long our_pid;
    sprintf(tmpbuf,"%s/trn%d.%ld",tmpdir,tmpfile_num++,our_pid);
    return savestr(tmpbuf);
}

#ifdef SUPPORT_NNTP
char*
get_auth_user()
{
    return datasrc->auth_user;
}
#endif

#ifdef SUPPORT_NNTP
char*
get_auth_pass()
{
    return datasrc->auth_pass;
}
#endif

#if defined(USE_GENAUTH) && defined(SUPPORT_NNTP)
char*
get_auth_command()
{
    return datasrc->auth_command;
}
#endif

char**
prep_ini_words(words)
INI_WORDS words[];
{
    register int checksum;
    char* cp = (char*)INI_VALUES(words);
    if (!cp) {
	int i;
	for (i = 1; words[i].item != NULL; i++) {
	    if (*words[i].item == '*') {
		words[i].checksum = -1;
		continue;
	    }
	    checksum = 0;
	    for (cp = words[i].item; *cp; cp++)
		checksum += (isupper(*cp)? tolower(*cp) : *cp);
	    words[i].checksum = (checksum << 8) + (cp - words[i].item);
	}
	words[0].checksum = i;
	words[0].help_str = cp = safemalloc(i * sizeof (char*));
    }
    bzero(cp, INI_LEN(words) * sizeof (char*));
    return (char**)cp;
}

void
unprep_ini_words(words)
INI_WORDS words[];
{
    free((char*)INI_VALUES(words));
    words[0].checksum = 0;
    words[0].help_str = NULL;
}

void
prep_ini_data(cp,filename)
char* cp;
char* filename;
{
    char* t = cp;

#ifdef DEBUG
    if (debug & DEB_RCFILES)
	printf("Read %d bytes from %s\n",strlen(cp),filename);
#endif

    while (*cp) {
	while (isspace(*cp)) cp++;

	if (*cp == '[') {
	    char* s = t;
	    do {
		*t++ = *cp++;
	    } while (*cp && *cp != ']' && *cp != '\n');
	    if (*cp == ']' && t != s) {
		*t++ = '\0';
		cp++;
		if (parse_string(&t, &cp))
		    cp++;

		while (*cp) {
		    while (isspace(*cp)) cp++;
		    if (*cp == '[')
			break;
		    if (*cp == '#')
			s = cp;
		    else {
			s = t;
			while (*cp && *cp != '\n') {
			    if (*cp == '=')
				break;
			    if (isspace(*cp)) {
				if (s == t || t[-1] != ' ')
				    *t++ = ' ';
				cp++;
			    }
			    else
				*t++ = *cp++;
			}
			if (*cp == '=' && t != s) {
			    while (t != s && isspace(t[-1])) t--;
			    *t++ = '\0';
			    cp++;
			    if (parse_string(&t, &cp))
				s = NULL;
			    else
				s = cp;
			}
			else
			    s = cp;
		    }
		    cp++;
		    if (s)
			for (cp = s; *cp && *cp++ != '\n'; ) ;
		}
	    }
	    else {
		*t = '\0';
		printf("Invalid section in %s: %s\n", filename, s);
		t = s;
		while (*cp && *cp++ != '\n') ;
	    }
	}
	else
	    while (*cp && *cp++ != '\n') ;
    }
    *t = '\0';
}

bool
parse_string(to, from)
char** to;
char** from;
{
    char inquote = 0;
    char* t = *to;
    char* f = *from;
    char* s;

    while (isspace(*f) && *f != '\n') f++;

    for (s = t; *f; f++) {
	if (inquote) {
	    if (*f == inquote) {
		inquote = 0;
		s = t;
		continue;
	    }
	}
	else if (*f == '\n')
	    break;
	else if (*f == '\'' || *f == '"') {
	    inquote = *f;
	    continue;
	}
	else if (*f == '#') {
	    while (*++f && *f != '\n') ;
	    break;
	}
	if (*f == '\\') {
	    if (*++f == '\n')
		continue;
	    f = interp_backslash(t, f);
	    t++;
	}
	else
	    *t++ = *f;
    }
#if 0
    if (inquote)
	printf("Unbalanced quotes.\n");
#endif
    inquote = (*f != '\0');

    while (t != s && isspace(t[-1])) t--;
    *t++ = '\0';

    *to = t;
    *from = f;

    return inquote;	/* return TRUE if the string ended with a newline */
}

char*
next_ini_section(cp,section,cond)
char* cp;
char** section;
char** cond;
{
    while (*cp != '[') {
	if (!*cp)
	    return NULL;
	cp += strlen(cp) + 1;
	cp += strlen(cp) + 1;
    }
    *section = cp+1;
    cp += strlen(cp) + 1;
    *cond = cp;
    cp += strlen(cp) + 1;
#ifdef DEBUG
    if (debug & DEB_RCFILES)
	printf("Section [%s] (condition: %s)\n",*section,
	       **cond? *cond : "<none>");
#endif
    return cp;
}

char*
parse_ini_section(cp, words)
char* cp;
INI_WORDS words[];
{
    register int checksum;
    register char* s;
    char** values = prep_ini_words(words);
    int i;

    if (!*cp)
	return NULL;

    while (*cp && *cp != '[') {
	checksum = 0;
	for (s = cp; *s; s++) {
	    if (isupper(*s))
		*s = tolower(*s);
	    checksum += *s;
	}
	checksum = (checksum << 8) + (s++ - cp);
	if (*s) {
	    for (i = 1; words[i].checksum; i++) {
		if (words[i].checksum == checksum
		 && strcaseEQ(cp,words[i].item)) {
		    values[i] = s;
		    break;
		}
	    }
	    if (!words[i].checksum)
		printf("Unknown option: `%s'.\n",cp);
	    cp = s + strlen(s) + 1;
	}
	else
	    cp = s + 1;
    }

#ifdef DEBUG
    if (debug & DEB_RCFILES) {
	printf("Ini_words: %s\n", words[0].item);
	for (i = 1; words[i].checksum; i++)
	    if (values[i])
		printf("%s=%s\n",words[i].item,values[i]);
    }
#endif

    return cp;
}

bool
check_ini_cond(cond)
char* cond;
{
    int not, equal, upordown, num;
    char* s;
    cond = dointerp(buf,sizeof buf,cond,"!=<>",(char*)NULL);
    s = buf + strlen(buf);
    while (s != buf && isspace(s[-1])) s--;
    *s = '\0';
    if ((not = (*cond == '!')) != 0)
	cond++;
    if ((upordown = (*cond=='<'? -1: (*cond=='>'? 1:0))) != 0)
	cond++;
    if ((equal = (*cond == '=')) != 0)
	cond++;
    while (isspace(*cond)) cond++;
    if (upordown) {
	num = atoi(cond) - atoi(buf);
	if (!((equal && !num) || (upordown * num < 0)) ^ not)
	    return FALSE;
    }
    else if (equal) {
	COMPEX condcompex;
	init_compex(&condcompex);
	if ((s = compile(&condcompex,cond,TRUE,TRUE)) != NULL) {
	    /*warning(s)*/;
	    equal = FALSE;
	}
	else
	    equal = execute(&condcompex,buf) != NULL;
	free_compex(&condcompex);
	return equal;
    }
    else
	return FALSE;
    return TRUE;
}

/* $$ might get replaced soonish... */
/* Ask for a single character (improve the prompt?) */
char
menu_get_char()
{
    printf("Enter your choice: ");
    fflush(stdout);
    eat_typeahead();
    getcmd(buf);
    printf("%c\n",*buf) FLUSH;
    return(*buf);
}

/* NOTE: kfile.c uses its own editor function */
/* used in a few places, now centralized */
int
edit_file(fname)
char* fname;
{
    int r = -1;

    if (!fname || !*fname)
	return r;

    /* XXX paranoia check on length */
    sprintf(cmd_buf,"%s ",
	    filexp(getval("VISUAL",getval("EDITOR",defeditor))));
    strcat(cmd_buf, filexp(fname));
    termdown(3);
    resetty();			/* make sure tty is friendly */
    r = doshell(sh,cmd_buf);/* invoke the shell */
    noecho();			/* and make terminal */
    crmode();			/*   unfriendly again */
    return r;
}

/* Consider a trn_pushdir, trn_popdir pair of functions */