-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathircaux.c
1534 lines (1333 loc) · 33.1 KB
/
ircaux.c
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
/*
* ircaux.c: some extra routines... not specific to irc... that I needed
*
* Written By Michael Sandrof
*
* Copyright(c) 1990, 1991
*
* See the COPYRIGHT file, or do a HELP IRCII COPYRIGHT
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "irc.h"
#include "expr.h"
#include "log.h"
#include "misc.h"
#include "vars.h"
#include "screen.h"
#include <pwd.h>
#include <sys/stat.h>
#include "ircaux.h"
#include "output.h"
#include "ircterm.h"
void *n_malloc(size_t size, const char *file, int line)
{
char *ptr;
if (!(ptr = (char *) calloc(1, size))) {
yell("Malloc() failed, giving up!");
term_reset();
exit(1);
}
return ptr;
}
/*
* new_free: Why do this? Why not? Saves me a bit of trouble here and there
*/
void *n_free(char **ptr, const char *file, int line)
{
if (*ptr) {
free(*ptr);
*ptr = NULL;
}
return (*ptr);
}
void *n_realloc(void *ptr, size_t eltsize, int newct, int oldct, const char *file, int line)
{
size_t newsize = eltsize * newct;
size_t oldsize = eltsize * oldct;
void *nptr = realloc(ptr, newsize);
if (!nptr) {
yell("realloc() failed, giving up!");
term_reset();
exit(1);
}
if (newsize > oldsize)
memset(nptr+oldsize, 0, newsize-oldsize);
return nptr;
}
/*
* malloc_strcpy: Mallocs enough space for src to be copied in to where
* ptr points to.
*
* Never call this with ptr pointinng to an uninitialised string, as the
* call to new_free() might crash the client... - phone, jan, 1993.
*/
char *malloc_strcpy(char **ptr, const char *src)
{
if (!src)
return new_free(ptr);
if (ptr && *ptr) {
if (*ptr == src)
return *ptr;
new_free(ptr);
}
*ptr = new_malloc(strlen(src) + 1);
return strcpy(*ptr, src);
}
/* malloc_strcat: Yeah, right */
char *malloc_strcat(char **ptr, const char *src)
{
size_t msize;
if (*ptr) {
if (!src)
return *ptr;
msize = strlen(*ptr) + strlen(src) + 1;
*ptr = new_realloc(*ptr, char, strlen(*ptr), msize);
return strcat(*ptr, src);
}
return (*ptr = m_strdup(src));
}
char *m_3dup(const char *str1, const char *str2, const char *str3)
{
size_t msize = strlen(str1) + strlen(str2) + strlen(str3) + 1;
return strcat(strcat(strcpy(new_malloc(msize), str1), str2), str3);
}
char *m_opendup(const char *str1, ...)
{
va_list args;
int size;
char *this_arg = NULL;
char *retval = NULL;
size = strlen(str1);
va_start(args, str1);
while ((this_arg = va_arg(args, char *)))
size += strlen(this_arg);
retval = new_malloc(size + 1);
strcpy(retval, str1);
va_start(args, str1);
while ((this_arg = va_arg(args, char *)))
strcat(retval, this_arg);
va_end(args);
return retval;
}
char *m_strdup(const char *str)
{
char *ptr;
if (!str)
str = empty_str;
ptr = new_malloc(strlen(str) + 1);
return strcpy(ptr, str);
}
char *m_s3cat(char **one, const char *maybe, const char *definitely)
{
if (*one && **one)
return m_3cat(one, maybe, definitely);
return *one = m_strdup(definitely);
}
char *m_s3cat_s(char **one, const char *maybe, const char *ifthere)
{
if (ifthere && *ifthere)
return m_3cat(one, maybe, ifthere);
return *one;
}
char *m_3cat(char **one, const char *two, const char *three)
{
int len = 0;
char *str;
if (*one)
len = strlen(*one);
if (two)
len += strlen(two);
if (three)
len += strlen(three);
len += 1;
str = new_malloc(len);
if (*one)
strcpy(str, *one);
if (two)
strcat(str, two);
if (three)
strcat(str, three);
new_free(one);
return ((*one = str));
}
char *upper(char *str)
{
register char *ptr = NULL;
if (str) {
ptr = str;
for (; *str; str++) {
if (islower(*str))
*str = toupper(*str);
}
}
return (ptr);
}
char *lower(char *str)
{
register char *ptr = NULL;
if (str) {
ptr = str;
for (; *str; str++) {
if (isupper(*str))
*str = tolower(*str);
}
}
return (ptr);
}
char *malloc_sprintf(char **to, const char *pattern, ...)
{
char booya[BIG_BUFFER_SIZE * 10 + 1];
*booya = 0;
if (pattern) {
va_list args;
va_start(args, pattern);
vsnprintf(booya, BIG_BUFFER_SIZE * 10, pattern, args);
va_end(args);
}
malloc_strcpy(to, booya);
return *to;
}
/* same thing, different variation */
char *m_sprintf(const char *pattern, ...)
{
char booya[BIG_BUFFER_SIZE * 10 + 1];
*booya = 0;
if (pattern) {
va_list args;
va_start(args, pattern);
vsnprintf(booya, BIG_BUFFER_SIZE * 10, pattern, args);
va_end(args);
}
return m_strdup(booya);
}
/* case insensitive string searching */
char *stristr(char *source, char *search)
{
int x = 0;
if (!source || !*source || !search || !*search || strlen(source) < strlen(search))
return NULL;
while (*source) {
if (source[x] && toupper(source[x]) == toupper(search[x]))
x++;
else if (search[x])
source++, x = 0;
else
return source;
}
return NULL;
}
/*
* word_count: Efficient way to find out how many words are in
* a given string. Relies on isspace() not being broken.
*/
extern int word_count(char *str)
{
int cocs = 0;
int isv = 1;
register char *foo = str;
if (!foo)
return 0;
while (*foo) {
if (*foo == '"' && isv) {
while (*(foo + 1) && *++foo != '"') ;
isv = 0;
cocs++;
}
if (!my_isspace(*foo) != !isv) {
isv = my_isspace(*foo);
cocs++;
}
foo++;
}
return (cocs + 1) / 2;
}
char *next_arg(char *str, char **new_ptr)
{
char *ptr;
/* added by Sheik ([email protected]) -- sanity */
if (!str || !*str)
return NULL;
if ((ptr = sindex(str, "^ ")) != NULL) {
if ((str = sindex(ptr, " ")) != NULL)
*str++ = (char) 0;
else
str = empty_str;
} else
str = empty_str;
if (new_ptr)
*new_ptr = str;
return ptr;
}
extern char *remove_trailing_spaces(char *foo)
{
char *end;
if (!*foo)
return foo;
end = foo + strlen(foo) - 1;
while (my_isspace(*end))
end--;
end[1] = 0;
return foo;
}
/*
* yanks off the last word from 'src'
* kinda the opposite of next_arg
*/
char *last_arg(char **src)
{
char *ptr;
if (!src || !*src)
return NULL;
remove_trailing_spaces(*src);
ptr = *src + strlen(*src) - 1;
if (*ptr == '"') {
for (ptr--;; ptr--) {
if (*ptr == '"') {
if (ptr == *src)
break;
if (ptr[-1] == ' ') {
ptr--;
break;
}
}
if (ptr == *src)
break;
}
} else {
for (;; ptr--) {
if (*ptr == ' ')
break;
if (ptr == *src)
break;
}
}
if (ptr == *src) {
ptr = *src;
*src = empty_str;
} else {
*ptr++ = 0;
remove_trailing_spaces(*src);
}
return ptr;
}
char *new_next_arg(char *str, char **new_ptr)
{
char *ptr, *start;
if (!str || !*str)
return NULL;
if ((ptr = sindex(str, "^ \t")) != NULL) {
if (*ptr == '"') {
start = ++ptr;
while ((str = sindex(ptr, "\"\\")) != NULL) {
switch (*str) {
case '"':
*str++ = '\0';
if (*str == ' ')
str++;
if (new_ptr)
*new_ptr = str;
return (start);
case '\\':
if (*(str + 1) == '"')
strcpy(str, str + 1);
ptr = str + 1;
}
}
str = empty_str;
} else {
if ((str = sindex(ptr, " \t")) != NULL)
*str++ = '\0';
else
str = empty_str;
}
} else
str = empty_str;
if (new_ptr)
*new_ptr = str;
return ptr;
}
char *new_new_next_arg(char *str, char **new_ptr, char *type)
{
char *ptr, *start;
if (!str || !*str)
return NULL;
if ((ptr = sindex(str, "^ \t")) != NULL) {
if ((*ptr == '"') || (*ptr == '\'')) {
char blah[3];
blah[0] = *ptr;
blah[1] = '\\';
blah[2] = '\0';
*type = *ptr;
start = ++ptr;
while ((str = sindex(ptr, blah)) != NULL) {
switch (*str) {
case '\'':
case '"':
*str++ = '\0';
if (*str == ' ')
str++;
if (new_ptr)
*new_ptr = str;
return (start);
case '\\':
if (str[1] == *type)
strcpy(str, str + 1);
ptr = str + 1;
}
}
str = empty_str;
} else {
*type = '\"';
if ((str = sindex(ptr, " \t")) != NULL)
*str++ = '\0';
else
str = empty_str;
}
} else
str = empty_str;
if (new_ptr)
*new_ptr = str;
return ptr;
}
unsigned char stricmp_table[] = {
0, 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, 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, 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
};
/* my_stricmp: case insensitive version of strcmp */
int my_stricmp(register const char *str1, register const char *str2)
{
while (*str1 && *str2 && (stricmp_table[(unsigned short) *str1] == stricmp_table[(unsigned short) *str2]))
str1++, str2++;
return (*str1 - *str2);
#if 0
int xor;
for (; *str1 || *str2; str1++, str2++) {
if (!*str1 || !*str2)
return (*str1 - *str2);
if (isalpha(*str1) && isalpha(*str2)) {
xor = *str1 ^ *str2;
if (xor != 32 && xor != 0)
return (*str1 - *str2);
} else {
if (*str1 != *str2)
return (*str1 - *str2);
}
}
return 0;
#endif
}
/* my_strnicmp: case insensitive version of strncmp */
int my_strnicmp(register const char *str1, register const char *str2, register int n)
{
while (n && *str1 && *str2 && (stricmp_table[(unsigned short) *str1] == stricmp_table[(unsigned short) *str2]))
str1++, str2++, n--;
return (n) ? (*str1 - *str2) : 0;
}
/* chop -- chops off the last character. capiche? */
char *chop(char *stuff, int nchar)
{
*(stuff + strlen(stuff) - nchar) = 0;
return stuff;
}
/*
* strext: Makes a copy of the string delmited by two char pointers and
* returns it in malloced memory. Useful when you dont want to munge up
* the original string with a null. end must be one place beyond where
* you want to copy, ie, its the first character you dont want to copy.
*/
char *strext(char *start, char *end)
{
char *ptr, *retval;
ptr = retval = new_malloc(end - start + 1);
while (start < end)
*ptr++ = *start++;
*ptr = 0;
return retval;
}
/*
* strmcpy: Well, it's like this, strncpy doesn't append a trailing null if
* strlen(str) == maxlen. strmcpy always makes sure there is a trailing null
*/
char *strmcpy(char *dest, const char *src, int maxlen)
{
strncpy(dest, src, maxlen);
dest[maxlen] = '\0';
return dest;
}
/*
* strmcat: like strcat, but truncs the dest string to maxlen (thus the dest
* should be able to handle maxlen+1 (for the null))
*/
char *strmcat(char *dest, const char *src, int maxlen)
{
int srclen, len;
len = strlen(dest);
srclen = strlen(src);
if ((len + srclen) > maxlen)
strncat(dest, src, maxlen - len);
else
strcat(dest, src);
return dest;
}
/*
* strmcat_ue: like strcat, but truncs the dest string to maxlen (thus the dest
* should be able to handle maxlen + 1 (for the null)). Also unescapes
* backslashes.
*/
char *strmcat_ue(char *dest, const char *src, int maxlen)
{
int dstlen;
dstlen = strlen(dest);
dest += dstlen;
maxlen -= dstlen;
while (*src && maxlen > 0) {
if (*src == '\\') {
if (strchr("npr0", src[1]))
*dest++ = '\020';
else if (*(src + 1))
*dest++ = *++src;
else
*dest++ = '\\';
} else
*dest++ = *src;
src++;
}
*dest = '\0';
return dest;
}
/*
* m_strcat_ues: Given two strings, concatenate the 2nd string to
* the end of the first one, but if the "unescape" argument is 1, do
* unescaping (like in strmcat_ue).
* (Malloc_STRCAT_UnEscape Special, in case you were wondering. ;-))
*
* This uses a cheating, "not-as-efficient-as-possible" algorithm,
* but it works with negligible cpu lossage.
*/
char *m_strcat_ues(char **dest, char *src, int unescape)
{
int total_length;
char *ptr, *ptr2;
int z;
if (!unescape) {
malloc_strcat(dest, src);
return *dest;
}
z = total_length = (*dest) ? strlen(*dest) : 0;
total_length += strlen(src);
*dest = new_realloc(*dest, char, 0, total_length + 2);
if (z == 0)
**dest = 0;
ptr2 = *dest + z;
for (ptr = src; *ptr; ptr++) {
if (*ptr == '\\') {
switch (*++ptr) {
case 'n':
case 'p':
case 'r':
case '0':
*ptr2++ = '\020';
break;
case (char) 0:
*ptr2++ = '\\';
goto end_strcat_ues;
break;
default:
*ptr2++ = *ptr;
}
} else
*ptr2++ = *ptr;
}
end_strcat_ues:
*ptr2 = '\0';
return *dest;
}
/* expand_twiddle: expands ~ in pathnames. */
char *expand_twiddle(char *str)
{
char buffer[BIG_BUFFER_SIZE + 1];
if (*str == '~') {
str++;
if (*str == '/' || !*str) {
strmcpy(buffer, my_path, BIG_BUFFER_SIZE);
strmcat(buffer, str, BIG_BUFFER_SIZE);
} else {
char *rest;
struct passwd *entry;
if ((rest = strchr(str, '/')) != NULL)
*rest++ = '\0';
if ((entry = getpwnam(str)) != NULL) {
strmcpy(buffer, entry->pw_dir, BIG_BUFFER_SIZE);
if (rest) {
strmcat(buffer, "/", BIG_BUFFER_SIZE);
strmcat(buffer, rest, BIG_BUFFER_SIZE);
}
} else
return NULL;
}
} else
strmcpy(buffer, str, BIG_BUFFER_SIZE);
return m_strdup(buffer);
}
/* islegal: true if c is a legal nickname char anywhere but first char */
#define islegal(c) ((((c) >= 'A') && ((c) <= '}')) || \
(((c) >= '0') && ((c) <= '9')) || \
((c) == '-') || ((c) == '_'))
/*
* check_nickname: checks is a nickname is legal. If the first character is
* bad new, null is returned. If the first character is bad, the string is
* truncd down to only legal characters and returned
*
* rewritten, with help from do_nick_name() from the server code (2.8.5),
* phone, april 1993.
*/
char *check_nickname(char *nick)
{
char *s;
if (!nick || *nick == '-' || isdigit(*nick))
return NULL;
for (s = nick; *s && (s - nick) < NICKNAME_LEN; s++)
if (!islegal(*s) || my_isspace(*s))
break;
*s = '\0';
return *nick ? nick : NULL;
}
/*
* sindex: much like index(), but it looks for a match of any character in
* the group, and returns that position. If the first character is a ^, then
* this will match the first occurence not in that group.
*/
char *sindex(char *string, const char *group)
{
const char *ptr;
if (!string || !group)
return NULL;
if (*group == '^') {
group++;
for (; *string; string++) {
for (ptr = group; *ptr; ptr++) {
if (*ptr == *string)
break;
}
if (*ptr == '\0')
return string;
}
} else {
for (; *string; string++) {
for (ptr = group; *ptr; ptr++) {
if (*ptr == *string)
return string;
}
}
}
return NULL;
}
/* is_number: returns true if the given string is a number, false otherwise */
int is_number(char *str)
{
while (*str == ' ')
str++;
if (*str == '-')
str++;
if (*str) {
for (; *str; str++) {
if (!isdigit((*str)))
return (0);
}
return 1;
} else
return 0;
}
/* rfgets: exactly like fgets, cept it works backwards through a file! */
char *rfgets(char *buffer, int size, FILE * file)
{
char *ptr;
off_t pos;
if (fseek(file, -2L, SEEK_CUR))
return NULL;
do {
switch (fgetc(file)) {
case EOF:
return NULL;
case '\n':
pos = ftell(file);
ptr = fgets(buffer, size, file);
fseek(file, pos, 0);
return ptr;
}
}
while (fseek(file, -2L, SEEK_CUR) == 0);
rewind(file);
pos = 0L;
ptr = fgets(buffer, size, file);
fseek(file, pos, 0);
return ptr;
}
/*
* path_search: given a file called name, this will search each element of
* the given path to locate the file. If found in an element of path, the
* full path name of the file is returned in a static string. If not, null
* is returned. Path is a colon separated list of directories
*/
char *path_search(char *name, char *path)
{
static char buffer[BIG_BUFFER_SIZE + 1];
char *ptr, *free_path = NULL;
/* A "relative" path is valid if the file exists */
/* A "relative" path is searched in the path if the filename doesnt really exist from where we are */
if (strchr(name, '/'))
if (!access(name, F_OK))
return name;
/* an absolute path is always checked, never searched */
if (name[0] == '/')
return (access(name, F_OK) ? (char *) 0 : name);
/* This is cheating. >;-) */
free_path = path = m_strdup(path);
while (path) {
if ((ptr = strchr(path, ':')) != NULL)
*(ptr++) = '\0';
strcpy(buffer, empty_str);
if (path[0] == '~') {
strmcat(buffer, my_path, BIG_BUFFER_SIZE);
path++;
}
strmcat(buffer, path, BIG_BUFFER_SIZE);
strmcat(buffer, "/", BIG_BUFFER_SIZE);
strmcat(buffer, name, BIG_BUFFER_SIZE);
if (access(buffer, F_OK) == 0)
break;
path = ptr;
}
new_free(&free_path);
return (path != NULL) ? buffer : NULL;
}
/*
* double_quote: Given a str of text, this will quote any character in the
* set stuff with the QUOTE_CHAR. It returns a malloced quoted, null
* terminated string
*/
char *double_quote(const char *str, const char *stuff, char *buffer)
{
char c;
int pos;
*buffer = 0;
if (!stuff)
return buffer;
for (pos = 0; (c = *str); str++) {
if (strchr(stuff, c)) {
if (c == '$')
buffer[pos++] = '$';
else
buffer[pos++] = '\\';
}
buffer[pos++] = c;
}
buffer[pos] = '\0';
return buffer;
}
void ircpanic(char *format, ...)
{
char buffer[10 * BIG_BUFFER_SIZE + 1];
static int recursion = 0;
if (recursion++)
abort();
if (format) {
va_list arglist;
va_start(arglist, format);
vsnprintf(buffer, BIG_BUFFER_SIZE, format, arglist);
va_end(arglist);
}
yell("An unrecoverable logic error has occured.");
yell("Please email [email protected] with the following message");
yell(" ");
yell("Panic: [%s] %s", PACKAGE_VERSION, buffer);
irc_exit("Xaric panic... Could it possibly be a bug? Nahhhh...", NULL);
}
/* Not very complicated, but very handy function to have */
int end_strcmp(const char *one, const char *two, int bytes)
{
if (bytes <= strlen(one))
return (strcmp(one + strlen(one) - (size_t) bytes, two));
else
return -1;
}
/* beep_em: Not hard to figure this one out */
void beep_em(int beeps)
{
int cnt, i;
for (cnt = beeps, i = 0; i < cnt; i++)
term_beep();
}
FILE *open_compression(char *executable, char *filename)
{
FILE *file_pointer;
int pipes[2];
pipes[0] = -1;
pipes[1] = -1;
if (pipe(pipes) == -1) {
bitchsay("Cannot start decompression: %s\n", strerror(errno));
if (pipes[0] != -1) {
close(pipes[0]);
close(pipes[1]);
}
return NULL;
}
switch (fork()) {
case -1:
{
bitchsay("Cannot start decompression: %s\n", strerror(errno));
return NULL;
}
case 0:
{
dup2(pipes[1], 1);
close(pipes[0]);
close(2); /* we dont want to see errors */
setuid(getuid());
setgid(getgid());
#ifdef ZARGS
execl(executable, executable, "-c", ZARGS, filename, NULL);
#else
execl(executable, executable, "-c", filename, NULL);
#endif
_exit(0);
}
default:
{
close(pipes[1]);
if ((file_pointer = fdopen(pipes[0], "r")) == NULL) {
bitchsay("Cannot start decompression: %s\n", strerror(errno));
return NULL;
}
break;
}
}
return file_pointer;
}
/* Front end to fopen() that will open ANY file, compressed or not, and
* is relatively smart about looking for the possibilities, and even
* searches a path for you! ;-)
*/
FILE *uzfopen(char **filename, char *path)
{
static int setup = 0;
int ok_to_decompress = 0;
char *filename_path;
char filename_trying[256];
char *filename_blah;
static char path_to_gunzip[513];
static char path_to_uncompress[513];
FILE *doh = NULL;
if (setup == 0) {
char *gzip = path_search("gunzip", getenv("PATH"));
char *compress = path_search("uncompress", getenv("PATH"));
if (gzip)
strcpy(path_to_gunzip, path_search("gunzip", getenv("PATH")));
if (compress)
strcpy(path_to_uncompress, path_search("uncompress", getenv("PATH")));
setup = 1;
}
/* It is allowed to pass to this function either a true filename with the compression extention, or to pass it the base name of
* the filename, and this will look to see if there is a compressed file that matches the base name */
/* Start with what we were given as an initial guess */
/* kev asked me to call expand_twiddle here */
filename_blah = expand_twiddle(*filename);
strcpy(filename_trying, filename_blah ? filename_blah : *filename);
new_free(&filename_blah);
/* Look to see if the passed filename is a full compressed filename */