-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckclib.c
3011 lines (2761 loc) · 81.2 KB
/
ckclib.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
char * cklibv = "C-Kermit library, 8.0.040, 24 Dec 2005";
#define CKCLIB_C
/* C K C L I B . C -- C-Kermit Library routines. */
/*
Author: Frank da Cruz <[email protected]>,
Columbia University Academic Information Systems, New York City.
Copyright (C) 1999, 2005,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
*/
/*
General-purpose, system/platform/compiler-independent routines for use
by all modules. Many are replacements for commonly used C library
functions that are not found on every platform, and/or that lack needed
functionality (e.g. caseless string search/compare) or safety features.
ckstrncpy() - Similar to strncpy() but different (see comments).
ckstrncat() - Similar to strncat() but different (see comments).
chartostr() - Converts a char to a string (self or ctrl char name).
ckstrchr() - Portable strchr().
ckstrpbrk() - Portable strpbrk().
cklower() - Lowercase a string (in place).
ckupper() - Uppercase a string (in place).
ckindex() - Left or right index.
ckstrstr() - Portable strstr().
ckitoa() - Converts int to string.
ckuitoa() - Converts unsigned int to string.
ckltoa() - Converts long to string.
ckultoa() - Converts unsigned long to string.
ckfstoa() - Converts off_t-type integer (long or long long) to string.
ckatofs() - Converts a numeric string to an off_t-type integer.
ckctoa() - Converts char to string.
ckmakmsg() - Constructs a message from 4 source strings.
ckmakxmsg() - Constructs a message from 12 source strings.
ckmatch() - Pattern matching.
ckmemcpy() - Portable memcpy().
ckrchar() - Rightmost character of a string.
ckstrcmp() - Possibly caseless string comparison.
ckstrpre() - Caseless string prefix comparison.
sh_sort() - Sorts an array of strings, many options.
brstrip() - Strips enclosing braces (and doublequotes).
makelist() - Splits "{{item}{item}...}" into an array.
makestr() - Careful malloc() front end.
xmakestr() - ditto (see comments).
ckradix() - Convert number radix (2-36).
b8tob64() - Convert data to base 64.
b64tob8() - Convert base 64 to data.
chknum() - Checks if string is a (possibly signed) integer.
rdigits() - Checks if string is composed only of decimal digits.
isfloat() - Checks if string is a valid floating-point number.
parnam() - Returns parity name string.
hhmmss() - Converts seconds to hh:mm:ss string.
lset() - Write fixed-length field left-adjusted into a record.
rset() - Write fixed-length field right-adjusted into a record.
ulongtohex() - Converts an unsigned long to a hex string.
hextoulong() - Converts a hex string to an unsigned long.
cksplit() - Splits a string into an array of words.
Prototypes are in ckclib.h.
Note: This module should not contain any extern declarations.
*/
#include "ckcsym.h"
#include "ckcdeb.h"
#include "ckcasc.h"
/* Public variables */
int dblquo = 1; /* Nonzero if doublequotes can be used for grouping */
char *
ccntab[] = { /* Names of ASCII (C0) control characters 0-31 */
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};
char *
c1tab[] = { /* Names of ISO 6429 (C1) control characters 0-32 */
"XXX", "XXX", "BPH", "NBH", "IND", "NEL", "SSA", "ESA",
"HTS", "HTJ", "VTS", "PLD", "PLU", "RI", "SS2", "SS3",
"DCS", "PU1", "PU2", "STS", "CCH", "MW", "SPA", "EPA",
"SOS", "XXX", "SCI", "CSI", "ST", "OSC", "PM", "APC", "NBS"
};
#define RXRESULT 127
static char rxdigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char rxresult[RXRESULT+1];
/* C K S T R N C P Y */
/*
Copies a NUL-terminated string into a buffer whose total length is given,
ensuring that the result is NUL-terminated even if it has to be truncated.
Call with:
dest = pointer to destination buffer
src = pointer to source string
len = length of destination buffer (the actual length, not one less).
Returns:
int, The number of bytes copied, 0 or more.
NOTE: This is NOT a replacement for strncpy():
. strncpy() does not require its source string to be NUL-terminated.
. strncpy() does not necessarily NUL-terminate its result.
. strncpy() right-pads dest with NULs if it is longer than src.
. strncpy() treats the length argument as the number of bytes to copy.
. ckstrncpy() treats the length argument as the size of the dest buffer.
. ckstrncpy() doesn't dump core if given NULL string pointers.
. ckstrncpy() returns a number.
Use ckstrncpy() when you want to:
. Copy an entire string into a buffer without overrun.
. Get the length of the string back.
Use strncpy() when you want to:
. Copy a piece of a string.
*/
int
#ifdef CK_ANSIC
ckstrncpy(char * dest, const char * src, int len)
#else
ckstrncpy(dest,src,len) char * dest, * src; int len;
#endif /* CK_ANSIC */
{
int i;
if (len < 1 || !src || !dest) { /* Nothing or nowhere to copy */
if (dest) *dest = NUL;
return(0);
}
#ifndef NOCKSTRNCPY
for (i = 0; src[i] && (i < len-1); i++) /* Args OK, copy */
dest[i] = src[i];
dest[i] = NUL;
#else
i = strlen(src);
if (i > len) i = len;
strncpy(dest,src,i);
dest[len] = NUL;
#endif /* NOCKSTRNCPY */
return(i);
}
/* C K S T R N C A T */
/*
Appends a NUL-terminated string to a buffer whose total length is given,
ensuring that the result is NUL-terminated even if it had to be truncated.
Call with:
dest = pointer to destination buffer containing a null-terminated string
src = pointer to null-terminated source string
len = length of destination buffer (the actual length, not one less).
Returns:
int, The number of bytes copied, 0 or more.
*/
int
#ifdef CK_ANSIC
ckstrncat(char * dest, const char * src, int len)
#else
ckstrncat(dest,src,len) char * dest, * src; int len;
#endif /* CK_ANSIC */
{
register int i, j;
#ifdef NOCKSTRNCPY
register char * s1, * s2;
#endif /* NOCKSTRNCPY */
if (len < 1 || !src || !dest) { /* Nothing or nowhere to copy */
if (dest) *dest = NUL;
return(0);
}
#ifndef NOCKSTRNCPY
/* Args OK, copy */
for (i = 0, j = strlen(dest); src[i] && (i < len-j-1); i++)
dest[i+j] = src[i];
dest[i+j] = NUL;
#else
j = 0;
s1 = dest;
while (*s1++) j++; /* j = strlen(dest); */
s1--; /* (back up over NUL) */
i = 0;
s2 = src;
while (*s2++) i++; /* i = strlen(src); */
if (i > (len-j))
i = len - j;
if (i <= 0)
return(0);
#ifdef COMMENT
strncpy(&dest[j],src,i);
#else
j = i; /* This should be a bit faster... */
s2 = src; /* depends on strcpy implementation; */
while ((*s1++ = *s2++) && j--) /* at least it shouldn't be slower. */
;
dest[len-1] = NUL; /* In case of early exit. */
#endif /* COMMENT */
#endif /* NOCKSTRNCPY */
return(i);
}
/* C K M A K M S G */
/*
Constructs a message from up to 4 pieces with length checking.
Result is always NUL terminated. Call with:
buf: Pointer to buffer for constructing message.
len: Length of buffer.
s1-s4: String pointers (can be NULL).
Returns:
0: Nothing was copied.
n: (positive number) n bytes copied, all args copied successfully.
-n: n bytes were copied, destination buffer not big enough for all.
Also see:
ckmakxmsg() -- accepts 12 string args.
ckitoa(), ckltoa(), ckctoa(), ckitox(), etc.
Use ckmak[x]msg() plus ck?to?() as a safe replacement for sprintf().
*/
int
#ifdef CK_ANSIC
ckmakmsg(char * buf, int len, char *s1, char *s2, char *s3, char *s4)
#else /* CK_ANSIC */
ckmakmsg(buf,len,s1,s2,s3,s4) char *buf, *s1, *s2, *s3, *s4; int len;
#endif /* CK_ANSIC */
{
int i, n = 0, m = 0;
char *s;
char *p, *a[4];
if (!buf) return(n); /* No destination */
if (len < 1) return(n); /* No size */
s = buf; /* Point to destination */
a[0] = s1; a[1] = s2; a[2] = s3; a[3] = s4; /* Array of source strings */
for (i = 0; i < 4; i++) { /* Loop thru array */
p = a[i]; /* Point to this element */
if (p) { /* If pointer not null */
n = ckstrncpy(s,p,len); /* Copy safely */
m += n; /* Accumulate total */
if (p[n]) /* Didn't get whole thing? */
return(-m); /* return indicating buffer full */
len -= n; /* Deduct from space left */
s += n; /* Otherwise advance dest pointer */
}
}
return(m); /* Return total bytes copied */
}
/* C K M A K X M S G */
/* Exactly like ckmakmsg(), but accepts 12 string arguments. */
int
#ifdef CK_ANSIC
ckmakxmsg(char * buf, int len,
char *s1, char *s2, char *s3, char *s4, char *s5, char *s6,
char *s7, char *s8, char *s9, char *s10, char *s11, char *s12)
#else /* CK_ANSIC */
ckmakxmsg(buf,len,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12)
char *buf, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9, *s10, *s11, *s12;
int len;
#endif /* CK_ANSIC */
{
int i, n = 0, m = 0;
char *s;
char *p, *a[12];
if (!buf) return(n); /* No destination */
if (len < 1) return(n); /* No size */
s = buf; /* Point to destination */
a[0] = s1; a[1] = s2; a[2] = s3; a[3] = s4; /* Source-string array */
a[4] = s5; a[5] = s6; a[6] = s7; a[7] = s8;
a[8] = s9; a[9] = s10; a[10] = s11; a[11] = s12;
for (i = 0; i < 12; i++) { /* Loop thru array */
p = a[i]; /* Point to this element */
if (p) { /* If pointer not null */
n = ckstrncpy(s,p,len); /* Copy safely */
m += n; /* Accumulate total */
if (p[n]) /* Didn't get whole thing? */
return(-m); /* return indicating buffer full */
len -= n; /* Deduct from space left */
s += n; /* Otherwise advance dest pointer */
}
}
return(m); /* Return total bytes copied */
}
/* C H A R T O S T R */
/* Converts a character to a string, interpreting controls. */
char *
chartostr(x) int x; { /* Call with char x */
static char buf[2]; /* Returns string pointer. */
if (x < 32)
return(ccntab[x]);
if (x == 127)
return("DEL");
if (x > 127 && x < 161)
return(c1tab[x - 128]);
if (x == 0xAD)
return("SHY");
buf[1] = NUL;
buf[0] = (unsigned)(x & 0xff);
return((char *)buf);
}
/* C K R C H A R */
/* Returns the rightmost character of the given null-terminated string */
int
ckrchar(s) char * s; {
register CHAR c = '\0', *p;
p = (CHAR *)s;
if (!p) p = (CHAR *)""; /* Null pointer == empty string */
if (!*p) return(0);
while (*p) /* Crawl to end of string */
c = *p++;
return((unsigned)(c & 0xff)); /* Return final character */
}
/* C K S T R C H R */
/* Replacement for strchr(), which is not universal. */
/* Call with:
s = pointer to string to look in.
c = character to look for.
Returns:
NULL if c not found in s or upon any kind of error, or:
pointer to first occurrence of c in s, searching from left to right.
*/
char *
#ifdef CK_ANSIC
ckstrchr(char * s, char c)
#else
ckstrchr(s,c) char *s, c;
#endif /* CK_ANSIC */
/* ckstrchr */ {
if (!s)
return(NULL);
while (*s && *s != c)
s++;
return((*s == c) ? s : NULL);
}
/* C K S T R R C H R */
/* Replacement for strrchr(), which is not universal. */
/* Call with:
s = pointer to string to look in.
c = character to look for.
Returns:
NULL if c not found in s or upon any kind of error, or:
pointer to first occurrence of c in s, searching from right to left.
*/
char *
#ifdef CK_ANSIC
ckstrrchr(char * s, char c)
#else
ckstrrchr(s,c) char *s, c;
#endif /* CK_ANSIC */
/* ckstrchr */ {
char * s2 = NULL;
if (!s)
return(NULL);
while (*s) {
if (*s == c)
s2 = s;
s++;
}
return(s2);
}
/* C K S T R P B R K -- Portable replacement for strpbrk() */
/* Returns pointer to first char in s1 that is also in s2, or NULL */
char *
ckstrpbrk(s1, s2) char * s1, * s2; {
char c1, c2, * s3;
if (!s1 || !s2) return(NULL);
if (!*s1 || !*s2) return(NULL);
while ((c1 = *s1++)) {
s3 = s2;
while ((c2 = *s3++)) {
if (c2 == c1)
return(s1-1);
}
}
return(NULL);
}
/* C K L O W E R -- Lowercase a string IN PLACE */
/* Returns the length of the string */
int
cklower(s) char *s; {
int n = 0;
if (!s) return(0);
while (*s) {
if (isupper(*s)) *s = (char) tolower(*s);
s++, n++;
}
return(n);
}
/* C K U P P E R -- Uppercase a string IN PLACE */
/* Returns the length of the string */
int
ckupper(s) char *s; {
int n = 0;
if (!s) return(0);
while (*s) {
if (islower(*s)) *s = (char) toupper(*s);
s++, n++;
}
return(n);
}
/* C K L T O A -- Long to string -- FOR DISCIPLINED USE ONLY */
#define NUMBUF 1024
static char numbuf[NUMBUF+32] = { NUL, NUL };
static int numbp = 0;
/*
ckltoa() and ckitoa() are like atol() and atoi() in the reverse direction,
returning a pointer to the string representation of the given number without
the caller having to worry about allocating or defining a buffer first.
They manage their own internal buffer, so successive calls return different
pointers. However, to keep memory consumption from growing without bound,
the buffer recycles itself. So after several hundred calls (depending on
the size of the numbers), some of the earlier pointers might well find
themselves referencing something different. Moral: You can't win in C.
Therefore, these routines are intended mainly for generating numeric strings
for short-term use, e.g. for passing numbers in string form as parameters to
functions. For long-term use, the result must be copied to a safe place.
*/
char *
#ifdef CK_ANSIC
ckltoa(long n)
#else
ckltoa(n) long n;
#endif /* CK_ANSIC */
/* ckltoa */ {
char buf[32]; /* Internal working buffer */
char * p, * s, * q;
int k, x, len = 0, sign = 0;
if (n < 0L) { /* Sign */
n = 0L - n;
sign = 1;
}
buf[31] = NUL;
for (k = 30; k > 0; k--) { /* Convert number to string */
x = n % 10L;
buf[k] = x + '0';
n = n / 10L;
if (!n)
break;
}
if (sign) buf[--k] = '-'; /* Add sign if necessary */
len = 31 - k;
if (len + numbp > NUMBUF)
numbp = 0;
p = numbuf + numbp;
q = p;
s = buf + k;
while ((*p++ = *s++)) ; /* Copy */
*p++ = NUL;
numbp += len+1;
return(q); /* Return pointer */
}
/* C K U L T O A -- Unsigned long to string */
char *
#ifdef CK_ANSIC
ckultoa(unsigned long n)
#else
ckultoa(n) unsigned long n;
#endif /* CK_ANSIC */
/* ckultoa */ {
char buf[32]; /* Internal working buffer */
char * p, * s, * q;
int k, x, len = 0;
buf[31] = NUL;
for (k = 30; k > 0; k--) { /* Convert number to string */
x = n % 10L;
buf[k] = x + '0';
n = n / 10L;
if (!n)
break;
}
len = 31 - k;
if (len + numbp > NUMBUF)
numbp = 0;
p = numbuf + numbp;
q = p;
s = buf + k;
while ((*p++ = *s++)) ; /* Copy */
numbp += len+1;
return(q); /* Return pointer */
}
char *
#ifdef CK_ANSIC
ckltox(long n) /* Long int to "0x.." hex string */
#else
ckltox(n) long n;
#endif /* CK_ANSIC */
/* ckltox */ {
char buf[32]; /* Internal working buffer */
char *p, *q, *s, *bp = buf + 2;
int k;
buf[0] = '0';
buf[1] = 'x';
sprintf(bp, "%lx", n);
k = strlen(bp);
if (k&1) {
sprintf(bp, "0%lx", n);
k++;
}
k += 2; /* "0x" */
if (numbp + k >= NUMBUF)
numbp = 0;
p = numbuf + numbp;
q = p;
s = buf;
while ((*p++ = *s++)) ; /* Copy */
*p++ = NUL;
numbp += k+1;
return(q); /* Return pointer */
}
/* C K F S T O A -- File Size (or offset) to string */
/* This is just like ckltoa() except for the data type of the argument. */
/* It's mainly for printing file sizes without having to know their data */
/* type, so we don't have to hardware "%ld" or "%lld" into printf()s. */
/* Works for 32 or 64 bits, according to CK_OFF_T definition. */
char *
#ifdef CK_ANSIC
ckfstoa(CK_OFF_T n)
#else
ckfstoa(n) CK_OFF_T n;
#endif /* CK_ANSIC */
/* ckfstoa */ {
char buf[32]; /* Internal working buffer */
char * p, * s, * q;
int k, x, len = 0, sign = 0;
if (n < (CK_OFF_T)0) { /* Sign */
n = (CK_OFF_T)0 - n;
sign = 1;
}
buf[31] = NUL; /* 2^63-1 is about 20 decimal digits */
for (k = 30; k > 0; k--) { /* Convert number to string */
x = n % (CK_OFF_T)10;
if (x < 0) {
/* x += 10; */
ckstrncpy(&buf[23],"OVERFLOW",32);
sign = 0;
k = 23;
break;
}
buf[k] = x + '0';
n = n / (CK_OFF_T)10;
if (!n)
break;
}
if (sign) buf[--k] = '-'; /* Add sign if necessary */
len = 31 - k;
if (len + numbp > NUMBUF)
numbp = 0;
p = numbuf + numbp;
q = p;
s = buf + k;
while ((*p++ = *s++)) ; /* Copy */
*p++ = NUL;
numbp += len+1;
return(q); /* Return pointer */
}
/* C K A T O F S -- String to File Size (or offset) */
/* This is the inverse of ckfstoa(), a replacement for atol() that works */
/* for either 32-bit or 64-bit arguments, according to CK_OFF_T definition. */
/* Like atol(), there is no error indication. */
CK_OFF_T
#ifdef CK_ANSIC
ckatofs(char * s)
#else
ckatofs(s) char * s;
#endif /* CK_ANSIC */
/* ckatofs */ {
CK_OFF_T result = (CK_OFF_T)0;
int minus = 0;
while (*s && (*s == SP || *s == HT)) s++;
if (*s == '+') s++;
if (*s == '-') {
minus = 1;
s++;
}
while (isdigit(*s)) {
result = (result * (CK_OFF_T)10) + (CK_OFF_T)(*s - '0');
s++;
}
return(minus ? -result : result);
}
/* C K I T O A -- Int to string -- FOR DISCIPLINED USE ONLY */
char *
ckitoa(n) int n; { /* See comments with ckltoa(). */
long nn;
nn = n;
return(ckltoa(nn));
}
char * /* Unsigned int to string */
ckuitoa(n) unsigned int n; {
unsigned long nn;
nn = n;
return(ckultoa(nn));
}
char *
ckitox(n) int n; { /* Int to hex */
long nn;
nn = n;
return(ckltox(nn));
}
char *
#ifdef CK_ANSIC
ckctoa(char c) /* Char to string */
#else
ckctoa(c) char c;
#endif
/* ckctoa */ {
static char buf[32];
static int current = 0;
if (current >= 30)
current = 0;
buf[current++] = c;
buf[current++] = '\0';
return((char *)(buf + current - 2));
}
char *
#ifdef CK_ANSIC
ckctox(CHAR c, int flag) /* Unsigned char to hex */
#else
ckctox(c, flag) CHAR c; int flag;
#endif
/* ckctox */ {
static char buf[48];
static int current = 0;
int x;
char h;
if (current > 45)
current = 0;
x = (c >> 4) & 0x0f;
h = rxdigits[x];
if (!flag && isupper(rxdigits[x]))
h = tolower(rxdigits[x]);
buf[current++] = h;
x = c & 0x0f;
h = rxdigits[x];
if (!flag && isupper(rxdigits[x]))
h = tolower(rxdigits[x]);
buf[current++] = h;
buf[current++] = '\0';
return((char *)(buf + current - 3));
}
/* C K I N D E X -- C-Kermit's index function */
/*
We can't depend on C libraries to have one, so here is our own.
Call with:
s1 - String to look for.
s2 - String to look in.
t - Offset from right or left of s2, 0 based; -1 for rightmost char in s2.
r - 0 for left-to-right search, non-0 for right-to-left.
icase 0 for case independence, non-0 if alphabetic case matters.
Returns 0 if string not found, otherwise a 1-based result.
Also returns 0 on any kind of error, e.g. junk parameters.
*/
int
ckindex(s1,s2,t,r,icase) char *s1, *s2; int t, r, icase; {
int len1 = 0, len2 = 0, i, j, x, ot = t; /* ot = original t */
char * s;
if (!s1 || !s2) return(0);
s = s1;
while (*s++) len1++; /* length of string to look for */
s = s2;
while (*s++) len2++; /* length of string to look in */
s = s2;
if (t < 0) t = len2 - 1;
j = len2 - len1; /* length difference */
if (j < 0 || (r == 0 && t > j)) /* search string is longer */
return(0);
if (r == 0) { /* Index */
s = s2 + t; /* Point to beginning of target */
for (i = 0; i <= (j - t); i++) { /* Now compare */
x = ckstrcmp(s1,s++,len1,icase);
if (!x)
return(i+1+t);
}
} else { /* Reverse Index */
i = len2 - len1; /* Where to start looking */
if (ot > 0) /* Figure in offset if any */
i -= t;
for (j = i; j > -1; j--) {
if (!ckstrcmp(s1,&s2[j],len1,icase))
return(j+1);
}
}
return(0);
}
/* C K S T R S T R -- Portable replacement for strstr() */
/* Returns pointer to first occurrence of s1 in s2, or NULL */
char *
ckstrstr(s1, s2) char * s1, * s2; {
int k;
k = ckindex(s2,s1,0,0,1);
return((k < 1) ? NULL : &s1[k-1]);
}
/* B R S T R I P -- Strip enclosing braces from arg string, in place. */
/*
Call with:
Pointer to string that can be poked.
Returns:
Pointer to string without enclosing braces.
If original string was not braced, this is the arg pointer;
otherwise it is 1 + the arg pointer, with the matching closing
brace zero'd out. If the string starts with a brace but does
not end with a matching brace, the original pointer to the original
string is returned. If the arg pointer is NULL, a pointer to an
empty string is returned.
*/
#ifdef COMMENT
/* This is the original version, handling only braces */
char *
brstrip(p) char *p; {
if (!p) return("");
if (*p == '{') {
int x;
x = (int)strlen(p) - 1;
if (p[x] == '}') {
p[x] = NUL;
p++;
}
}
return(p);
}
#else
/* New version handles braces and doublequotes */
char *
brstrip(p) char *p; {
if (!p) return("");
if (*p == '{' || (*p == '"' && dblquo)) {
int x;
x = (int)strlen(p) - 1;
if (x > 0) {
if ((*p == '{' && p[x] == '}') ||
(*p == '"' && p[x] == '"')) {
if (x > 0 && p[x-1] != CMDQ) {
p[x] = NUL;
p++;
}
}
}
}
return(p);
}
#endif /* COMMENT */
#ifdef COMMENT
/* Even newer experimental version -- breaks many things */
char *
fnstrip(p) char *p; {
int i, j, k, n, len;
extern int cmd_quoting; /* Bad - no externs allowed! */
if (!p)
return("");
if (*p == '{') {
len = strlen(p);
n = 0;
for (j = 0; j < len; j++ ) {
if (p[j] == '{' &&
(!cmd_quoting || j == 0 || p[j-1] != CMDQ)) {
for (n = 1, i = j+1; i < len; i++ ) {
if (p[i] == '{' && (!cmd_quoting || p[i-1] != CMDQ))
n++;
else if (p[i] == '}' && (!cmd_quoting || p[i-1] != CMDQ)) {
if (--n == 0) {
for (k = j; k < i - 1; k++)
p[k] = p[k+1];
for (; i < len; i++ )
p[i-1] = p[i+1];
len -= 2;
j = i - 1;
}
}
}
}
}
if (n == 1) { /* Implied right brace at end of field */
for (k = j; k < len; k++)
p[k] = p[k+1];
len -= 1;
}
} else if (*p == '"') {
len = strlen(p);
n = 0;
for (j = 0; j < len; j++) {
if (p[j] == '"' &&
(!cmd_quoting || j == 0 || p[j-1] != CMDQ)) {
n++;
for (i = j + 1; i < len; i++) {
if (p[i] == '"' && (!cmd_quoting || p[i-1] != CMDQ)) {
n--;
for (k = j; k < i - 1; k++)
p[k] = p[k+1];
for (; i < len; i++)
p[i-1] = p[i+1];
len -= 2;
j = i - 1;
}
}
}
}
if (n == 1) { /* Implied double quote at end of field */
for (k = j; k < len; k++ )
p[k] = p[k+1];
len -= 1;
}
}
return(p);
}
#endif /* COMMENT */
#ifdef COMMENT
/*
Not used -- Note: these not only write into their arg, but write past
past the end.
*/
char *
brace(fn) char *fn; {
int spaces = 0;
char * p, ch, ch2;
for (p = fn; *p; p++) {
if (*p == SP) {
spaces = 1;
break;
}
}
if (spaces) {
p = fn;
ch = *p;
*p = '{';
p++;
while (*p) {
ch2 = *p;
*p = ch;
ch = ch2;
p++;
}
*p = ch;
p++;
*p = '}';
p++;
*p = '\0';
}
return(fn);
}
#endif /* COMMENT */
/* d q u o t e -- Puts doublequotes around arg in place. */
/*
Call with:
Pointer to buffer and its total length and flag = 0 to use
doublequotes, 1 to use braces.
Returns:
Number: length of result.
*/
int
dquote(fn, len, flag) char *fn; int len; int flag; {
int spaces = 0, k = 0;
char * p, ch, ch2;
if (!fn)
return(0);
k = strlen(fn);
for (p = fn; *p; p++) {
if (*p == SP) {
spaces = 1;
break;
}
}
if (spaces) {
if (k + 2 >= len)
return(k);
p = fn;
ch = *p;
*p = flag ? '{' : '"';
p++;
while (*p) {
ch2 = *p;
*p = ch;
ch = ch2;
p++;
}
*p = ch;
p++;
*p = flag ? '}' : '"';
p++;
*p = '\0';
}
return(k+2);
}
/* U N T A B I F Y --- Untabify s1 into s2, assuming tabs every 8 space */
int
untabify(s1,s2,max) char * s1, * s2; int max; {
int i, j, k, x, z;
x = strlen(s1);
for (i = 0, k = 0; k < x; k++) {
if (s1[k] != '\t') {
if (i >= max-1) {
s2[max-1] = '\0';
return(-1);
}
s2[i++] = s1[k];
continue;
}
z = 8 - i%8;
if (z == 0) z = 8;
for (j = 0; j < z && i < max; j++)
s2[i++] = ' ';
}
s2[i] = '\0';
return(0);
}
/* M A K E L I S T --- Breaks {{s1}{s2}..{sn}} into an array of strings */
/*
Call with:
s = pointer to string to break up.
list = array of string pointers.
len = number of elements in array.