File: string.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (956 lines) | stat: -rw-r--r-- 25,887 bytes parent folder | download | duplicates (6)
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
/* libnih
 *
 * string.c - useful string utility functions
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */


#include <sys/ioctl.h>

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/logging.h>

#include "string.h"


/**
 * nih_sprintf:
 * @parent: parent object for new string,
 * @format: format string.
 *
 * Writes a new string according to @format as sprintf(), except that the
 * string is allocated using nih_alloc().
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
nih_sprintf (const void *parent,
	     const char *format,
	     ...)
{
	char    *str;
	va_list  args;

	nih_assert (format != NULL);

	va_start (args, format);
	str = nih_vsprintf (parent, format, args);
	va_end (args);

	return str;
}

/**
 * nih_vsprintf:
 * @parent: parent object for new string,
 * @format: format string,
 * @args: arguments to format string.
 *
 * Writes a new string according to @format as vsprintf(), except that the
 * string is allocated using nih_alloc().
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
nih_vsprintf (const void *parent,
	      const char *format,
	      va_list     args)
{
	ssize_t   len;
	va_list   args_copy;
	char     *str;

	nih_assert (format != NULL);

	va_copy (args_copy, args);
	len = vsnprintf (NULL, 0, format, args_copy);
	va_end (args_copy);

	nih_assert (len >= 0);

	str = nih_alloc (parent, len + 1);
	if (! str)
		return NULL;

	va_copy (args_copy, args);
	vsnprintf (str, len + 1, format, args_copy);
	va_end (args_copy);

	return str;
}


/**
 * nih_strdup:
 * @parent: parent object for new string,
 * @str: string to duplicate.
 *
 * Allocates enough memory to store a duplicate of @str and writes a
 * copy of the string to it.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: duplicated string or NULL if insufficient memory.
 **/
char *
nih_strdup (const void *parent,
	    const char *str)
{
	size_t len;

	nih_assert (str != NULL);

	len = strlen (str);
	return nih_strndup (parent, str, len);
}

/**
 * nih_strndup:
 * @parent: parent object for new string,
 * @str: string to duplicate,
 * @len: length of string to duplicate.
 *
 * Allocates enough memory to store up to @len bytes of @str, or if @str
 * is shorter, @len bytes.  A copy of the string is written to this
 * block with a NUL byte appended.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: duplicated string or NULL if insufficient memory.
 **/
char *
nih_strndup (const void *parent,
	     const char *str,
	     size_t      len)
{
	char *dup;

	nih_assert (str != NULL);

	dup = nih_alloc (parent, len + 1);
	if (! dup)
		return NULL;

	memset (dup, '\0', len + 1);
	strncpy (dup, str, len);

	return dup;
}


/**
 * nih_strcat:
 * @str: pointer to string to modify,
 * @parent: parent object of new string,
 * @src: string to append to @str.
 *
 * Modifies @str, concatenating the contents of @src to it.  The new string
 * is allocated using nih_alloc(), and @str will be updated to point to the
 * new pointer; use the return value simply to check for success.
 *
 * If the string pointed to by @str is NULL, this is equivalent to
 * nih_strdup() and if @parent is not NULL, it should be a pointer to another
 * object which will be used as a parent for the returned string.  When all
 * parents of the returned string are freed, the returned string will also be
 * freed.
 *
 * When the string pointed to by @str is not NULL, @parent is ignored;
 * though it usual to pass a parent of @str for style reasons.
 *
 * Returns: new string pointer or NULL if insufficient memory.
 **/
char *
nih_strcat (char       **str,
	    const void  *parent,
	    const char  *src)
{
	nih_assert (str != NULL);
	nih_assert (src != NULL);

	return nih_strncat (str, parent, src, strlen (src));
}

/**
 * nih_strncat:
 * @str: pointer to string to modify,
 * @parent: parent object of new string,
 * @src: string to append to @str,
 * @len: length of @src.
 *
 * Modifies @str, concatenating up to @len characters of the contents of @src
 * to it.  The new string is allocated using nih_alloc(), and @str will be
 * updated to point to the new pointer; use the return value simply to check
 * for success.
 *
 * If the string pointed to by @str is NULL, this is equivalent to
 * nih_strdup() and if @parent is not NULL, it should be a pointer to another
 * object which will be used as a parent for the returned string.  When all
 * parents of the returned string are freed, the returned string will also be
 * freed.
 *
 * When the string pointed to by @str is not NULL, @parent is ignored;
 * though it usual to pass a parent of @str for style reasons.
 *
 * Returns: new string pointer or NULL if insufficient memory.
 **/
char *
nih_strncat (char       **str,
	     const void  *parent,
	     const char  *src,
	     size_t       len)
{
	char *ret;

	nih_assert (str != NULL);
	nih_assert (src != NULL);

	if (! *str) {
		*str = nih_strndup (parent, src, len);
		return *str;
	}


	ret = nih_realloc (*str, parent, strlen (*str) + len + 1);
	if (! ret)
		return NULL;

	*str = ret;

	strncat (*str, src, len);

	return ret;
}


/**
 * nih_strcat_sprintf:
 * @str: pointer to string to modify,
 * @parent: parent object of new string,
 * @format: format string to append to @str.
 *
 * Modifies @str, concatenating according to @format as sprintf().  The new
 * string is allocated using nih_alloc(), and @str will be updated to point
 * to the new pointer; use the return value simply to check for success.
 *
 * If the string pointed to by @str is NULL, this is equivalent to
 * nih_sprintf() and if @parent is not NULL, it should be a pointer to another
 * object which will be used as a parent for the returned string.  When all
 * parents of the returned string are freed, the returned string will also be
 * freed.
 *
 * When the string pointed to by @str is not NULL, @parent is ignored;
 * though it usual to pass a parent of @str for style reasons.
 *
 * Returns: new string pointer or NULL if insufficient memory.
 **/
char *
nih_strcat_sprintf (char       **str,
		    const void  *parent,
		    const char  *format,
		    ...)
{
	char    *ret;
	va_list  args;

	nih_assert (str != NULL);
	nih_assert (format != NULL);

	va_start (args, format);
	ret = nih_strcat_vsprintf (str, parent, format, args);
	va_end (args);

	return ret;
}

/**
 * nih_strcat_vsprintf:
 * @str: pointer to string to modify,
 * @parent: parent object of new string,
 * @format: format string to append to @str,
 * @args: arguments to format string.
 *
 * Modifies @str, concatenating according to @format as vsprintf().  The new
 * string is allocated using nih_alloc(), and @str will be updated to point
 * to the new pointer; use the return value simply to check for success.
 *
 * If the string pointed to by @str is NULL, this is equivalent to
 * nih_vsprintf() and if @parent is not NULL, it should be a pointer to another
 * object which will be used as a parent for the returned string.  When all
 * parents of the returned string are freed, the returned string will also be
 * freed.
 *
 * When the string pointed to by @str is not NULL, @parent is ignored;
 * though it usual to pass a parent of @str for style reasons.
 *
 * Returns: new string pointer or NULL if insufficient memory.
 **/
char *
nih_strcat_vsprintf (char       **str,
		     const void  *parent,
		     const char  *format,
		     va_list      args)
{
	ssize_t   len, str_len;
	va_list   args_copy;
	char     *ret;

	nih_assert (str != NULL);
	nih_assert (format != NULL);

	str_len = *str ? strlen (*str) : 0;

	va_copy (args_copy, args);
	len = vsnprintf (NULL, 0, format, args_copy);
	va_end (args_copy);

	nih_assert (len >= 0);

	ret = nih_realloc (*str, parent, str_len + len + 1);
	if (! ret)
		return NULL;

	*str = ret;

	va_copy (args_copy, args);
	vsnprintf (*str + str_len, len + 1, format, args_copy);
	va_end (args_copy);

	return ret;
}


/**
 * nih_str_split:
 * @parent: parent object of new array,
 * @str: string to split,
 * @delim: characters to split on,
 * @repeat: allow repeated characters.
 *
 * Splits @str into an array of strings by separating on any character in
 * @delim; if @repeat is true then sequences of @delim are ignored, otherwise
 * they result in empty strings in the returned array.
 *
 * The last element in the array is always NULL.
 *
 * The individual strings are allocated using nih_alloc() so you may just use
 * nih_free() on the returned array.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned array.  When all parents
 * of the returned string are freed, the returned array will also be
 * freed.
 *
 * Returns: allocated array or NULL if insufficient memory.
 **/
char **
nih_str_split (const void *parent,
	       const char *str,
	       const char *delim,
	       int         repeat)
{
	char   **array;
	size_t   len;

	nih_assert (str != NULL);
	nih_assert (delim != NULL);

	len = 0;
	array = nih_str_array_new (parent);
	if (! array)
		return NULL;

	while (*str) {
		const char  *ptr;

		/* Skip initial delimiters */
		while (repeat && strchr (delim, *str))
			str++;

		/* Find the end of the token */
		ptr = str;
		while (*str && (! strchr (delim, *str)))
			str++;

		if (! nih_str_array_addn (&array, parent, &len,
					  ptr, str - ptr)) {
			nih_free (array);
			return NULL;
		}

		/* Skip over the delimiter */
		if (*str)
			str++;
	}

	return array;
}


/**
 * nih_str_array_new:
 * @parent: parent object of new array.
 *
 * Allocates a new NULL-terminated array of strings with zero elements;
 * use nih_str_array_add() to append new strings to the array.  Because
 * each array element will be allocated using nih_alloc() as a child of
 * the array itself, the entire array can be freed with nih_free().
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned array.  When all parents
 * of the returned object are freed, the returned array will also be
 * freed.
 *
 * Returns: newly allocated array or NULL if insufficient memory.
 **/
char **
nih_str_array_new (const void *parent)
{
	char **array;

	array = nih_alloc (parent, sizeof (char *));
	if (! array)
		return NULL;

	array[0] = NULL;

	return array;
}

/**
 * nih_str_array_add:
 * @array: array of strings,
 * @parent: parent object of new array,
 * @len: length of @array,
 * @str: string to add.
 *
 * Extend the NULL-terminated string @array (which has @len elements,
 * excluding the final NULL element), appending a copy of @str to it.
 * Both the array and the new string are allocated using nih_alloc(),
 *
 * @len will be updated to contain the new array length and @array will
 * be updated to point to the new array pointer; use the return value
 * simply to check for success.
 *
 * If you don't know or care about the length, @len may be set to NULL;
 * this is less efficient as it necessates counting the length on each
 * invocation.
 *
 * If the array pointed to by @array is NULL, the array will be allocated
 * and @ptr the first element, and if @parent is not NULL, it should be a
 * pointer to another object which will be used as a parent for the returned
 * array.  When all parents of the returned array are freed, the returned
 * array will also be freed.
 *
 * When the array pointed to by @array is not NULL, @parent is ignored;
 * though it usual to pass a parent of @array for style reasons.
 *
 * Returns: new array pointer or NULL if insufficient memory.
 **/
char **
nih_str_array_add (char       ***array,
		   const void   *parent,
		   size_t       *len,
		   const char   *str)
{
	nih_local char *new_str;

	nih_assert (array != NULL);
	nih_assert (str != NULL);

	new_str = nih_strdup (NULL, str);
	if (! new_str)
		return NULL;

	return nih_str_array_addp (array, parent, len, new_str);
}

/**
 * nih_str_array_addn:
 * @array: array of strings,
 * @parent: parent object of new array,
 * @len: length of @array,
 * @str: string to add,
 * @strlen: length of @str.
 *
 * Extend the NULL-terminated string @array (which has @len elements,
 * excluding the final NULL element), appending a copy of the first
 * @strlen bytes of @str to it.
 *
 * Both the array and the new string are allocated using nih_alloc(),
 *
 * @len will be updated to contain the new array length and @array will
 * be updated to point to the new array pointer; use the return value
 * simply to check for success.
 *
 * If you don't know or care about the length, @len may be set to NULL;
 * this is less efficient as it necessates counting the length on each
 * invocation.
 *
 * If the array pointed to by @array is NULL, the array will be allocated
 * and @ptr the first element, and if @parent is not NULL, it should be a
 * pointer to another object which will be used as a parent for the returned
 * array.  When all parents of the returned array are freed, the returned
 * array will also be freed.
 *
 * When the array pointed to by @array is not NULL, @parent is ignored;
 * though it usual to pass a parent of @array for style reasons.
 *
 * Returns: new array pointer or NULL if insufficient memory.
 **/
char **
nih_str_array_addn (char       ***array,
		    const void   *parent,
		    size_t       *len,
		    const char   *str,
		    size_t        strlen)
{
	nih_local char *new_str;

	nih_assert (array != NULL);
	nih_assert (str != NULL);

	new_str = nih_strndup (NULL, str, strlen);
	if (! new_str)
		return NULL;

	return nih_str_array_addp (array, parent, len, new_str);
}

/**
 * nih_str_array_addp:
 * @array: array of strings,
 * @parent: parent object of new array,
 * @len: length of @array,
 * @ptr: pointer to add.
 *
 * Extend the NULL-terminated string @array (which has @len elements,
 * excluding the final NULL element), appending the nih_alloc() allocated
 * object @ptr to it.
 *
 * The array is allocated using nih_alloc(), and @ptr will be referenced
 * by the new array.  After calling this function, you should never use
 * nih_free() to free @ptr and instead use nih_unref() or nih_discard()
 * if you no longer need to use it.
 *
 * @len will be updated to contain the new array length and @array will
 * be updated to point to the new array pointer; use the return value
 * simply to check for success.
 *
 * If you don't know or care about the length, @len may be set to NULL;
 * this is less efficient as it necessates counting the length on each
 * invocation.
 *
 * If the array pointed to by @array is NULL, the array will be allocated
 * and @ptr the first element, and if @parent is not NULL, it should be a
 * pointer to another object which will be used as a parent for the returned
 * array.  When all parents of the returned array are freed, the returned
 * array will also be freed.
 *
 * When the array pointed to by @array is not NULL, @parent is ignored;
 * though it usual to pass a parent of @array for style reasons.
 *
 * Returns: new array pointer or NULL if insufficient memory.
 **/
char **
nih_str_array_addp (char       ***array,
		    const void   *parent,
		    size_t       *len,
		    void         *ptr)
{
	char   **new_array;
	size_t   c_len;

	nih_assert (array != NULL);
	nih_assert (ptr != NULL);

	if (! len) {
		len = &c_len;
		c_len = 0;

		for (new_array = *array; new_array && *new_array; new_array++)
			c_len++;
	}

	new_array = nih_realloc (*array, parent, sizeof (char *) * (*len + 2));
	if (! new_array)
		return NULL;

	*array = new_array;

	nih_ref (ptr, *array);

	(*array)[(*len)++] = ptr;
	(*array)[*len] = NULL;

	return *array;
}

/**
 * nih_str_array_copy:
 * @parent: parent object of new array.
 * @len: length of new array,
 * @array: array of strings to copy.
 *
 * Allocates a new NULL-terminated array of strings with elements copied
 * from the existing @array given.
 *
 * Because each array element will be allocated using nih_alloc() as a child
 * of the array itself, the entire array can be freed with nih_free().
 * This will not affect the array copied.
 *
 * @len will be updated to contain the new array length.  If you don't care
 * about the length, @len may be set to NULL; this is less efficient as it
 * necessates counting the length on each future add operation.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned array.  When all parents
 * of the returned array are freed, the returned array will also be
 * freed.
 *
 * Returns: newly allocated array or NULL if insufficient memory.
 **/
char **
nih_str_array_copy (const void   *parent,
		    size_t       *len,
		    char * const *array)
{
	char **new_array;

	nih_assert (array != NULL);

	new_array = nih_str_array_new (parent);
	if (! new_array)
		return NULL;

	if (! nih_str_array_append (&new_array, parent, len, array)) {
		nih_free (new_array);
		return NULL;
	}

	return new_array;
}

/**
 * nih_str_array_append:
 * @array: array of strings,
 * @parent: parent object of new array,
 * @len: length of @array,
 * @args: array of strings to add.
 *
 * Extend the NULL-terminated string @array (which has @len elements,
 * excluding the final NULL element), appending a copy of each element in
 * the additional NULL-terminated string array @args to it.
 * Both the array and the new strings are allocated using nih_alloc(),
 *
 * @len will be updated to contain the new array length and @array will
 * be updated to point to the new array pointer; use the return value
 * simply to check for success.
 *
 * If you don't know or care about the length, @len may be set to NULL;
 * this is less efficient as it necessates counting the length on each
 * operation.
 *
 * If the array pointed to by @array is NULL, this is equivalent to
 * nih_str_array_copy() and if @parent is not NULL, it should be a pointer to
 * another object which will be used as a parent for the returned array.
 * When all parents of the returned array are freed, the returned array will
 * also be freed.
 *
 * When the array pointed to by @array is not NULL, @parent is ignored;
 * though it usual to pass a parent of @array for style reasons.
 *
 * Returns: new array pointer or NULL if insufficient memory.
 **/
char **
nih_str_array_append (char         ***array,
		      const void     *parent,
		      size_t         *len,
		      char * const   *args)
{
	size_t        c_len, o_len;
	int           free_on_error = FALSE;
	char * const *arg;

	nih_assert (array != NULL);
	nih_assert (args != NULL);

	if (! *array)
		free_on_error = TRUE;

	if (! len) {
		c_len = 0;

		for (arg = *array; arg && *arg; arg++)
			c_len++;
	} else {
		c_len = *len;
	}

	o_len = c_len;

	for (arg = args; *arg; arg++) {
		if (! nih_str_array_add (array, parent, &c_len, *arg)) {
			if (*array) {
				for (; c_len > o_len; c_len--)
					nih_free ((*array)[c_len - 1]);

				(*array)[o_len] = NULL;

				if (free_on_error) {
					nih_free (*array);
					*array = NULL;
				}
			}

			return NULL;
		}
	}

	if (len)
		*len = c_len;

	return *array;
}


/**
 * nih_str_wrap:
 * @parent: parent object of new string,
 * @str: string to be wrapped,
 * @len: length of line to fit into,
 * @first_indent: indent for first line,
 * @indent: indent for subsequent lines.
 *
 * Returns a newly allocated copy of @str with newlines inserted so no
 * line is longer than @len characters (not including the newline).  Where
 * possible, newlines replace existing whitespace characters so that words
 * are not broken.
 *
 * The first line may be indented by an extra @first_indent characters, and
 * subsequent lines may be intended by an extra @indent characters.  These
 * are added to the string as whitespace characters.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
nih_str_wrap (const void *parent,
	      const char *str,
	      size_t      len,
	      size_t      first_indent,
	      size_t      indent)
{
	char   *txt;
	size_t  txtlen, col, ls, i;

	nih_assert (str != NULL);
	nih_assert (len > 0);

	txtlen = first_indent + strlen (str);
	txt = nih_alloc (parent, txtlen + 1);
	if (! txt)
		return NULL;

	memset (txt, ' ', first_indent);
	memcpy (txt + first_indent, str, strlen (str) + 1);

	col = ls = 0;
	for (i = 0; i < txtlen; i++) {
		int nl = 0;

		if (strchr (" \t\r", txt[i])) {
			/* Character is whitespace; convert to an ordinary
			 * space and remember the position for next time.
			 */
			txt[i] = ' ';
			ls = i;

			/* If this doesn't go over the line length,
			 * continue to the next character
			 */
			if (++col <= len)
				continue;
		} else if (txt[i] != '\n') {
			/* Character is part of a word.  If this doesn't go
			 * over the line length, continue to the next
			 * character
			 */
			if (++col <= len)
				continue;

			/* Filled a line; if we marked a whitespace character
			 * on this line, go back to that, otherwise we'll
			 * need to add a newline to the string after this
			 * character
			 */
			if (ls) {
				i = ls;
			} else {
				nl = 1;
			}
		}

		/* We need to insert a line break at this position, and
		 * any indent that goes along with it
		 */
		if (indent | nl) {
			char *new_txt;

			/* Need to increase the size of the string in memory */
			new_txt = nih_realloc (txt, parent,
					       txtlen + indent + nl + 1);
			if (! new_txt) {
				nih_free (txt);
				return NULL;
			}
			txt = new_txt;

			/* Move up the existing characters, then replace
			 * the gap with the indent
			 */
			memmove (txt + i + indent + 1, txt + i + 1 - nl,
				 txtlen - i + nl);
			memset (txt + i + 1, ' ', indent);
			txtlen += indent + nl;
		}

		/* Replace the current character with a newline */
		txt[i] = '\n';

		/* Reset the current column and last seen whitespace index;
		 * make sure we skip any indent as it's whitespace that doesn't
		 * count
		 */
		i += indent;
		col = indent;
		ls = 0;
	}

	return txt;
}

/**
 * nih_str_screen_width:
 *
 * Checks the COLUMNS environment variable, standard output if it is a
 * terminal or defaults to 80 characters.
 *
 * Returns: the width of the screen.
 **/
size_t
nih_str_screen_width (void)
{
	char   *columns;
	size_t  len = 0;

	/* Look at the columns environment variable */
	columns = getenv ("COLUMNS");
	if ((! len) && columns) {
		char *endptr;

		len = strtoul (columns, &endptr, 10);
		if (*endptr)
			len = 0;
	}

	/* Check whether standard output is a tty */
	if ((! len) && isatty (STDOUT_FILENO)) {
		struct winsize winsize;

		if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &winsize) == 0)
			len = winsize.ws_col;
	}

	/* Fallback to 80 columns */
	if (! len)
		len = 80;

	return len;
}

/**
 * nih_str_screen_wrap:
 * @parent: parent object of new string,
 * @str: string to be wrapped,
 * @first_indent: indent for first line,
 * @indent: indent for subsequent lines.
 *
 * Returns a newly allocated copy of @str with newlines inserted so no
 * line is wider than the screen (not including the newline).  Where
 * possible, newlines replace existing whitespace characters so that words
 * are not broken.
 *
 * If standard output is not a terminal, then 80 characters is assumed.
 * The width can be overriden with the COLUMNS environment variable.
 *
 * The first line may be indented by an extra @first_indent characters, and
 * subsequent lines may be intended by an extra @indent characters.  These
 * are added to the string as whitespace characters.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
nih_str_screen_wrap (const void *parent,
		     const char *str,
		     size_t      first_indent,
		     size_t      indent)
{
	size_t len;

	nih_assert (str != NULL);

	len = nih_str_screen_width () - 1;

	return nih_str_wrap (parent, str, len, first_indent, indent);
}