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
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Module: pkgstr.c
* Synopsis: general string services
* Taxonomy: project private
* Debug Flag: str
* Description:
*
* This module implements general string utility services
*
* Public Methods:
*
* pkgstrAddToken - Add a token to a string
* pkgstrContainsToken - Determine if a string contains a specified token
* pkgstrConvertPathToBasename - Return copy of base name in path string
* pkgstrConvertPathToDirname - Return copy of directory name in path string
* pkgstrConvertUllToTimeString_r - convert unsigned long long to time string
* pkgstrExpandTokens - Expand tokens from string appending tokens to another
* pkgstrGetToken - Get a token from a string
* pkgstrGetToken_r - Get a token from a string into a fixed buffer
* pkgstrLocatePathBasename - Locate position of base name in path string
* pkgstrNumTokens - Determine number of tokens in string
* pkgstrPrintf - Create a string from a printf style format and arguments
* pkgstrPrintf_r - Create a string from a printf style format and arguments
* into a fixed buffer
* pkgstrRemoveToken - Remove a token from a string
* pkgstrRemoveLeadingWhitespace - remove leading whitespace from string
* pkgstrScaleNumericString - Convert unsigned long long to human
* readable form
*/
/*
* Unix Includes
*/
#define __EXTENSIONS__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <limits.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include <ctype.h>
#include <unistd.h>
#include <strings.h>
#include <stdarg.h>
/*
* pkglib Includes
*/
#include "pkglib.h"
#include "pkgstrct.h"
#include "libintl.h"
#include "pkglocale.h"
/*
* External definitions
*/
/*
* Public methods
*/
/*
* Name: pkgstrRemoveLeadingWhitespace
* Synopsis: Remove leading whitespace from string
* Description: Remove all leading whitespace characters from a string
* Arguments: a_str - [RO, *RW] - (char **)
* Pointer to handle to string (in allocated storage) to
* remove all leading whitespace from
* Returns: void
* The input string is modified as follows:
* == (char *)NULL:
* - input string was (char *)NULL
* - input string is all whitespace
* != (char *)NULL:
* - copy of input string with leading
* whitespace removed
* CAUTION: The input string must be allocated space (via mem* or
* pkgstr* methods) - it must not be a static or inline
* character string
* NOTE: The input string a_str will be freed with 'free'
* if it is all whitespace, or if it contains any leading
* whitespace characters
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
void
pkgstrRemoveLeadingWhitespace(char **a_str)
{
char *o_str;
/* entry assertions */
assert(a_str != (char **)NULL);
/* if string is null, just return */
if (*a_str == (char *)NULL) {
return;
}
o_str = *a_str;
/* if string is empty, deallocate and return NULL */
if (*o_str == '\0') {
/* free string - handle is reset to NULL by free */
free(*a_str);
*a_str = (char *)NULL;
return;
}
/* if first character is not a space, just return */
if (!isspace(*o_str)) {
return;
}
/* advance past all space characters */
while ((*o_str != '\0') && (isspace(*o_str))) {
o_str++;
}
/* if string was all space characters, deallocate and return NULL */
if (*o_str == '\0') {
/* free string - *a_str is reset to NULL by free */
free(*a_str);
*a_str = (char *)NULL;
return;
}
/* have non-space/null byte, return dup, deallocate original */
o_str = strdup(o_str);
assert(o_str != (char *)NULL);
if (o_str != (char *)NULL) {
free(*a_str);
*a_str = o_str;
}
}
unsigned long
pkgstrNumTokens(char *a_string, char *a_separators)
{
int index;
if (a_string == (char *)NULL) {
return (0);
}
if (*a_string == '\0') {
return (0);
}
for (index = 0 ; ; index ++) {
char *p;
p = pkgstrGetToken((char *)NULL, a_string, index, a_separators);
if (p == (char *)NULL) {
return (index);
}
free(p);
}
}
/*
* Name: pkgstrPrintf_r
* Synopsis: Create string from printf style format and arguments
* Description: Call to convert a printf style format and arguments into a
* string of characters placed in allocated storage
* Arguments: a_buf - [RO, *RW] - (char *)
* - Pointer to buffer used as storage space for the
* returned string created
* a_bufLen - [RO, *RO] - (int)
* - Size of 'a_buf' in bytes - a maximum of 'a_bufLen-1'
* bytes will be placed in 'a_buf' - the returned
* string is always null terminated
* a_format - [RO, RO*] (char *)
* printf-style format for string to be formatted
* VARG_LIST - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: void
*/
/*PRINTFLIKE3*/
void
pkgstrPrintf_r(char *a_buf, int a_bufLen, char *a_format, ...)
{
va_list ap;
size_t vres = 0;
/* entry assertions */
assert(a_format != (char *)NULL);
assert(*a_format != '\0');
assert(a_buf != (char *)NULL);
assert(a_bufLen > 1);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(a_buf, a_bufLen-1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(vres < a_bufLen);
a_buf[a_bufLen-1] = '\0';
}
/*
* Name: pkgstrPrintf
* Synopsis: Create string from printf style format and arguments
* Description: Call to convert a printf style format and arguments into a
* string of characters placed in allocated storage
* Arguments: format - [RO, RO*] (char *)
* printf-style format for string to be formatted
* VARG_LIST - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: char *
* A string representing the printf conversion results
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
/*PRINTFLIKE1*/
char *
pkgstrPrintf(char *a_format, ...)
{
va_list ap;
size_t vres = 0;
char bfr[1];
char *rstr = (char *)NULL;
/* entry assertions */
assert(a_format != (char *)NULL);
assert(*a_format != '\0');
/* determine size of the message in bytes */
va_start(ap, a_format);
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(vres < LINE_MAX);
/* allocate storage to hold the message */
rstr = (char *)calloc(1, vres+2);
assert(rstr != (char *)NULL);
if (rstr == (char *)NULL) {
return ((char *)NULL);
}
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(rstr, vres+1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(vres < LINE_MAX);
assert(*rstr != '\0');
/* return the results */
return (rstr);
}
/*
* Name: pkgstrExpandTokens
* Synopsis: Expand tokens from string appending tokens to another
* Description: Given a string and a list of one or more separators,
* expand each token from the string and append those tokens
* to a string that is in allocated space - create new string
* if no string to append to exists.
* Arguments: a_old - [RO, *RW] - (char **)
* - Pointer to handle to string to append token to
* == (char *)NULL - new string is created
* a_separator - [RO, *RO] - (char *)
* - separator to end tokens returned
* a_separators - [RO, *RO] - (char *)
* - String containing one or more characters that
* can separate one "token" from a_string from another
* Returns: void
* NOTE: Any token string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the token string is no longer needed.
*/
void
pkgstrExpandTokens(char **a_old, char *a_string, char a_separator,
char *a_separators)
{
int i;
char sep[2] = {'\0', '\0'};
/* convert single separator character into character string */
sep[0] = a_separator;
/*
* iterate extracting tokens from the source string and adding
* those tokens to the target string when the tokens are not
* already present in the target string
*/
for (i = 0; ; i++) {
char *p;
/* extract the next matching token from the source string */
p = pkgstrGetToken((char *)NULL, a_string, i, a_separators);
/* return if no token is available */
if (p == (char *)NULL) {
return;
}
/*
* obtained token from source string: if the token is not
* in the target string, add the token to the target string
*/
if (pkgstrContainsToken(*a_old, p, sep) == B_FALSE) {
pkgstrAddToken(a_old, p, *sep);
}
/* free up temporary storage used by token from source string */
free(p);
}
/*NOTREACHED*/
}
/*
* Name: pkgstrGetToken
* Synopsis: Get a separator delimited token from a string
* Description: Given a string and a list of one or more separators,
* return the position specified token (sequence of one or
* more characters that do not include any of the separators)
* Arguments: r_sep - [*RW] - (char *)
* - separator that ended the token returned
* - NOTE: this is a pointer to a "char", e.g.:
* - char a;
* - pkgstrGetToken(&a, ...)
* a_string - [RO, *RO] - (char *)
* - pointer to string to extract token from
* a_index - [RO, *RO] - (int)
* - Index of token to return; '0' is first matching
* token, '1' is second matching token, etc.
* a_separators - [RO, *RO] - (char *)
* - String containing one or more characters that
* can separate one "token" from another
* Returns: char *
* == (char *)NULL - no token matching criteria found
* != (char *)NULL - token matching criteria
* NOTE: Any token string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the token string is no longer needed.
*/
char *
pkgstrGetToken(char *r_sep, char *a_string, int a_index, char *a_separators)
{
char *p;
char *q;
char *lasts;
/* entry assertions */
assert(a_string != (char *)NULL);
assert(a_index >= 0);
assert(a_separators != (char *)NULL);
assert(*a_separators != '\0');
/* if returned separator requested, reset to null until token found */
if (r_sep != (char *)NULL) {
*r_sep = '\0';
}
/* duplicate original string before breaking down into tokens */
p = strdup(a_string);
assert(p != (char *)NULL);
if (p == (char *)NULL) {
return ((char *)NULL);
}
lasts = p;
/* scan for separators and return 'index'th token found */
while (q = strtok_r((char *)NULL, a_separators, &lasts)) {
/* retrieve separator if requested */
if (r_sep != (char *)NULL) {
char *x;
x = strpbrk(a_string, a_separators);
if (x) {
*r_sep = *x;
}
}
/* if this is the 'index'th token requested return it */
if (a_index-- == 0) {
char *tmp;
/* duplicate token into its own storage */
tmp = strdup(q);
assert(tmp != (char *)NULL);
if (tmp == (char *)NULL) {
return ((char *)NULL);
}
/* free up copy of original input string */
free(p);
/* return token found */
return (tmp);
}
}
/*
* token not found
*/
/* free up copy of original input string */
free(p);
/* return NULL pointer (token not found) */
return ((char *)NULL);
}
/*
* Name: pkgstrGetToken
* Synopsis: Get separator delimited token from a string into a fixed buffer
* Description: Given a string and a list of one or more separators,
* return the position specified token (sequence of one or
* more characters that do not include any of the separators)
* into a specified buffer of a fixed maximum size
* Arguments: r_sep - [*RW] - (char *)
* - separator that ended the token returned
* - NOTE: this is a pointer to a "char", e.g.:
* - char a;
* - pkgstrGetToken(&a, ...)
* a_string - [RO, *RO] - (char *)
* - pointer to string to extract token from
* a_index - [RO, *RO] - (int)
* - Index of token to return; '0' is first matching
* token, '1' is second matching token, etc.
* a_separators - [RO, *RO] - (char *)
* - String containing one or more characters that
* can separate one "token" from another
* a_buf - [RO, *RW] - (char *)
* - Pointer to buffer used as storage space for the
* returned token - the returned token is always
* null terminated
* a_buf[0] == '\0' - no token meeting criteria found
* a_buf[0] != '\0' - token meeting criteria returned
* a_bufLen - [RO, *RO] - (int)
* - Size of 'a_buf' in bytes - a maximum of 'a_bufLen-1'
* bytes will be placed in 'a_buf' - the returned
* token is always null terminated
* Returns: void
*/
void
pkgstrGetToken_r(char *r_sep, char *a_string, int a_index,
char *a_separators, char *a_buf, int a_bufLen)
{
char *p;
char *q;
char *lasts;
/* entry assertions */
assert(a_string != (char *)NULL);
assert(a_index >= 0);
assert(a_separators != (char *)NULL);
assert(*a_separators != '\0');
assert(a_buf != (char *)NULL);
assert(a_bufLen > 0);
/* reset returned separator */
if (r_sep != (char *)NULL) {
*r_sep = '\0';
}
/* zero out contents of return buffer */
bzero(a_buf, a_bufLen);
/* duplicate original string before breaking down into tokens */
p = strdup(a_string);
assert(p != (char *)NULL);
if (p == (char *)NULL) {
return;
}
lasts = p;
/* scan for separators and return 'index'th token found */
while (q = strtok_r((char *)NULL, a_separators, &lasts)) {
/* retrieve separator if requested */
if (r_sep != (char *)NULL) {
char *x;
x = strpbrk(a_string, a_separators);
if (x) {
*r_sep = *x;
}
}
/* if this is the 'index'th token requested return it */
if (a_index-- == 0) {
/* copy as many characters as possible to return buf */
(void) strncpy(a_buf, q, a_bufLen-1);
break;
}
}
/* free up copy of original input string */
free(p);
}
/*
* Name: pkgstrAddToken
* Synopsis: Add a token to a string
* Description: Append a token (sequence of one or more characters) to a
* string that is in allocated space - create new string if
* no string to append to exists
* Arguments: a_old - [RO, *RW] - (char **)
* - Pointer to handle to string to append token to
* == (char *)NULL - new string is created
* a_new - [RO, *RO] - (char *)
* - Pointer to string representing token to append
* to the end of the "a_old" string
* == (char *)NULL - no action is performed
* a_new[0] == '\0' - no action is performed
* a_separator - [RO, *RO] - (char)
* - One character placed between the old (existing)
* string and the new token to be added IF the old
* string exists and is not empty (zero length)
* Returns: void
* CAUTION: The old (existing) string must be allocated space (via lu_mem*
* or pkgstr* methods) - it must not be a static or inline
* character string
* NOTE: The old (existing) string may be freed with 'free'
* if a token is appended to it
* NOTE: Any string returned in 'a_old' is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the token string is no longer needed.
*/
void
pkgstrAddToken(char **a_old, char *a_new, char a_separator)
{
/* entry assertions */
assert(a_old != (char **)NULL);
assert(a_separator != '\0');
/* if token to add is null, just return */
if (a_new == (char *)NULL) {
return;
}
/* if token to add is empty (zero length), just return */
if (*a_new == '\0') {
return;
}
/* make sure that new token does not contain the separator */
assert(strchr(a_new, (int)a_separator) == (char *)NULL);
/* if old string is empty (zero length), deallocate */
if ((*a_old != (char *)NULL) && ((*a_old)[0] == '\0')) {
/* *a_old is set to NULL by free */
free(*a_old);
*a_old = (char *)NULL;
}
/* if old string is exists, append separator and token */
if (*a_old != (char *)NULL) {
char *p;
p = pkgstrPrintf("%s%c%s", *a_old, a_separator, a_new);
free(*a_old);
*a_old = p;
return;
}
/* old string does not exist - return duplicate of token */
assert(*a_old == (char *)NULL);
*a_old = strdup(a_new);
assert(*a_old != (char *)NULL);
}
/*
* Name: pkgstrContainsToken
* Synopsis: Does a given string contain a specified substring
* Description: Determine if a given substring exists in a larger string
* Arguments: a_string - [RO, *RO] - (char *)
* Pointer to string to look for substring in
* a_token - [RO, *RO] - (char *)
* Pointer to substring to look for in larger string
* Results: boolean_t
* B_TRUE - substring exists in larger string
* B_FALSE - substring does NOT exist in larger string
* NOTE: The substring must match on a "token" basis; that is, the
* substring must exist in the larger string delineated with
* either spaces or tabs to match.
*/
boolean_t
pkgstrContainsToken(char *a_string, char *a_token, char *a_separators)
{
char *lasts;
char *current;
char *p;
/* entry assertions */
assert(a_separators != (char *)NULL);
assert(*a_separators != '\0');
/* if token is not supplied, return false */
if (a_token == (char *)NULL) {
return (B_FALSE);
}
/* if no string provided, return false */
if (a_string == (char *)NULL) {
return (B_FALSE);
}
/* if string empty (zero length), return false */
if (*a_string == '\0') {
return (B_FALSE);
}
/* duplicate larger string because strtok_r changes it */
p = strdup(a_string);
assert(p != (char *)NULL);
if (p == (char *)NULL) {
return (B_FALSE);
}
lasts = p;
/* scan each token looking for a match */
while ((current = strtok_r((char *)NULL, a_separators, &lasts)) !=
(char *)NULL) {
if (streq(current, a_token)) {
free(p);
return (B_TRUE);
}
}
/* free up temporary storage */
free(p);
/* not found */
return (B_FALSE);
}
/*
* Name: pkgstrRemoveToken
* Synopsis: Remove a token from a string
* Description: Remove a token (sequence of one or more characters) from a
* string that is in allocated space
* Arguments: r_string - [RO, *RW] - (char **)
* - Pointer to handle to string to remove token from
* a_token - [RO, *RO] - (char *)
* Pointer to token (substring) to look for and remove
* from r_string provided
* a_separators - [RO, *RO] - (char *)
* - String containing one or more characters that
* separate one "token" from another in r_string
* a_index - [RO, *RO] - (int)
* - Index of token to remove; '0' is first matching
* token, '1' is second matching token, etc.
* Returns: void
* CAUTION: The input string must be allocated space (via lu_mem* or
* pkgstr* methods) - it must not be a static or inline
* character string
* NOTE: The input string r_string will be freed with 'free'
* if the token to be removed is found
* NOTE: Any token string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the token string is no longer needed.
* Errors: If the new token string cannot be created, the process exits
*/
void
pkgstrRemoveToken(char **r_string, char *a_token, char *a_separators,
int a_index)
{
char *a_string;
char *copyString;
char sep = 0;
int copyLength;
int i;
/* entry assertions */
assert(r_string != (char **)NULL);
assert(a_token != (char *)NULL);
assert(*a_token != '\0');
assert(a_separators != (char *)NULL);
assert(*a_separators != '\0');
/* simple case: input string is null; return empty string */
a_string = *r_string;
if (*a_string == '\0') {
return;
}
/* simple case: token == input string; return empty string */
if (streq(a_string, a_token)) {
/* deallocate input string; free sets *r_string to NULL */
free(*r_string);
*r_string = (char *)NULL;
return;
}
/* simple case: token not in input string: return */
if (!pkgstrContainsToken(a_string, a_token, a_separators)) {
return;
}
/*
* Pick apart the old string building the new one as we go along
* removing the first occurance of the token provided
*/
copyLength = (strlen(a_string)-strlen(a_token))+2;
copyString = calloc(1, copyLength);
assert(copyString != (char *)NULL);
if (copyString == (char *)NULL) {
return;
}
for (i = 0; ; i++) {
char *p;
p = pkgstrGetToken(&sep, a_string, i, a_separators);
if (p == (char *)NULL) {
break;
}
if (streq(p, a_token) && (a_index-- == 0)) {
continue;
}
if (*copyString) {
assert(sep != '\0');
(void) strncat(copyString, &sep, 1);
}
(void) strcat(copyString, p);
}
free(*r_string);
assert(*copyString);
*r_string = copyString;
}
/*
* Name: pkgstrScaleNumericString
* Synopsis: Convert unsigned long long to human readable form
* Description: Convert a string containing an unsigned long long representation
* and convert it into a human readable numeric string. The number
* is scaled down until it is small enough to be in a good human
* readable format i.e. in the range 0 thru scale-1.
* Arguments: a_buf - [RO, *RW] - (char *)
* Pointer to buffer containing string representation
* of unsigned long long to convert
* scale - [RO, *RO] - (unsigned long long)
* Value to scale the number into
* Returns: a_buf - contains human readable scaled representation of
* original value contained in the buffer
* Note: The value "(unsigned long long)-1" is a special case and
* is always converted to "-1".
* Errors: If the string cannot be created, the process exits
*/
void
pkgstrScaleNumericString(char *a_buf, unsigned long long scale)
{
static char *M = " KMGTPE"; /* Measurement: */
/* kilo, mega, giga, tera, peta, exa */
unsigned long long number = 0; /* convert this number */
unsigned long long save = 0;
char *uom = M; /* unit of measurement, initially ' ' (=M[0]) */
/* entry assertions */
assert(scale > (unsigned long long)0);
assert(scale <= (unsigned long long)1048576);
/*
* Get the number - if no number of empty number, just return
*/
if (a_buf == (char *)NULL) {
return;
}
if (*a_buf == '\0') {
(void) strcpy(a_buf, "0");
return;
}
/* convert out the number from the input buffer */
number = strtoull(a_buf, (char **)NULL, 10);
/* if conversion error, return "-1" */
if ((long long)number == (long long)-1) {
(void) strcpy(a_buf, "-1");
return;
}
/*
* Now have number as a count of scale units.
* Stop scaling when we reached exa-bytes, then something is
* probably wrong with our number (it is improbably large)
*/
while ((number >= scale) && (*uom != 'E')) {
uom++; /* next unit of measurement */
save = number;
number = (number + (scale / 2)) / scale;
}
/* check if we should output a decimal place after the point */
if (save && ((save / scale) < 10)) {
/* sprintf() will round for us */
float fnum = (float)save / scale;
(void) sprintf(a_buf, "%4.1f%c", fnum, *uom);
} else {
(void) sprintf(a_buf, "%4llu%c", number, *uom);
}
}
/*
* Name: pkgstrLocatePathBasename
* Synopsis: Locate position of base name in path string
* Description: Locate the base name (last path item) in a path and
* return a pointer to the first byte of the base name
* within the given path
* Arguments: a_path - [RO, *RO] - (char *)
* - Pointer to string representing path to scan
* Returns: char *
* - Pointer into string of first byte of path base name
* - == (char *)NULL - input path is (char *)NULL
*/
char *
pkgstrLocatePathBasename(char *a_path)
{
char *p;
/* if path is NULL, return NULL */
if (!a_path) {
return (a_path);
}
/* locate last occurance of '/' in path */
p = strrchr(a_path, '/');
if (p != (char *)NULL) {
/* base name located - return -> first byte */
return (p+1);
}
/* no occurance of '/' - entry path must be basename */
return (a_path);
}
/*
* Name: pkgstrConvertPathToBasename
* Synopsis: Return copy of base name in path string
* Description: Locate the base name (last path item) in a path and
* return a copy of the base name in allocated storage
* Arguments: a_path - [RO, *RO] - (char *)
* - Pointer to string representing path to scan
* Returns: char *
* - String containing path base name
* - == (char *)NULL - input path is (char *)NULL
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
char *
pkgstrConvertPathToBasename(char *a_path)
{
char *p;
/* if path is NULL, return NULL */
if (a_path == (char *)NULL) {
return ((char *)NULL);
}
/* if path is empty (zero length), return NULL */
if (*a_path == '\0') {
return ((char *)NULL);
}
/* locate last occurance of '/' in path */
p = strrchr(a_path, '/');
if (p == (char *)NULL) {
/* no occurance of '/' - entry path must be basename */
return (strdup(a_path));
}
/* base name located - return string from -> first byte */
return (strdup(p+1));
}
/*
* Name: pkgstrConvertPathToDirname
* Synopsis: Return copy of directory in path string
* Description: Locate the directory name (everything but last path item) in a
* path and return a copy of the dir name in allocated storage
* Arguments: a_path - [RO, *RO] - (char *)
* - Pointer to string representing path to scan
* Returns: char *
* - String containing path directory name
* - == (char *)NULL - input path is (char *)NULL,
* or a_path is empty (*a_path == '\0'), or the
* a_path has no directory name in it.
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
char *
pkgstrConvertPathToDirname(char *a_path)
{
char *p;
char *retPath;
/* if path is NULL, return NULL */
if (a_path == (char *)NULL) {
return ((char *)NULL);
}
/* if path is empty (zero length), return NULL */
if (*a_path == '\0') {
return ((char *)NULL);
}
/* locate last occurance of '/' in path */
p = strrchr(a_path, '/');
if (p == (char *)NULL) {
/* no occurance of '/' - entire path must be basename */
return ((char *)NULL);
}
/* duplicate original path */
retPath = strdup(a_path);
assert(retPath != (char *)NULL);
if (retPath == (char *)NULL) {
return ((char *)NULL);
}
/* remove all trailing '/'s from copy of path */
for (p = strrchr(retPath, '/'); (p > retPath) && (*p == '/'); p--) {
*p = '\0';
}
/* if entire path was '/'s, return null string - no directory present */
if (*retPath == '\0') {
free(retPath);
return ((char *)NULL);
}
/* path has at least one non-'/' in it - return -> directory portion */
return (retPath);
}
/*
* Name: pkgstrConvertUllToTimeString_r
* Synopsis: Convert an unsigned long long into a "time string"
* Description: Given an unsigned long long, return a "time string" which is a
* conversion of the unsigned long long interpreted as a number of
* nanoseconds into a "hour:minute:second.ns" ascii string
* Arguments: a_time - [RO, *RO] - (unsigned long long)n
* - value to convert
* a_buf - [RO, *RW] - (char *)
* - Pointer to buffer used as storage space for the
* returned string
* a_bufLen - [RO, *RO] - (int)
* - Size of 'a_buf' in bytes - a maximum of 'a_bufLen-1'
* bytes will be placed in 'a_buf'
* Returns: char *
* - String containing converted value
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
void
pkgstrConvertUllToTimeString_r(unsigned long long a_time,
char *a_buf, int a_bufLen)
{
unsigned long long seconds;
unsigned long long minutes;
unsigned long long hours;
unsigned long long ns;
/* entry assertions */
assert(a_buf != (char *)NULL);
assert(a_bufLen > 0);
/* if time is 0, return immediate result */
if (a_time == 0) {
pkgstrPrintf_r(a_buf, a_bufLen, "%s", "0:00:00.000000000");
return;
}
/* break out individual time components */
ns = a_time % 1000000000ll; /* nanoseconds left over from seconds */
seconds = a_time / 1000000000ll; /* total seconds */
minutes = seconds / 60ll; /* total minutes */
seconds = seconds % 60ll; /* seconds left over from minutes */
hours = minutes / 60ll; /* total hours */
minutes = minutes % 60ll; /* minutes left over from hours */
/* return a converted string */
pkgstrPrintf_r(a_buf, a_bufLen, "%llu:%02llu:%02llu.%09llu",
hours, minutes, seconds, ns);
}
|