File: str.c

package info (click to toggle)
fitsh 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,768 kB
  • ctags: 4,050
  • sloc: ansic: 53,352; makefile: 1,120; sh: 25
file content (87 lines) | stat: -rw-r--r-- 1,900 bytes parent folder | download
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
/*****************************************************************************/
/* str.c								     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Some functions related to (dynamic) string handling.			     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* (c) 2006; Pal, A. (apal@szofi.elte.hu)				     */
/*****************************************************************************/

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

#include "str.h"

/*****************************************************************************/

char * strkcpy(char *out,char *in,int size)
{
 strncpy(out,in,size);
 out[size-1]=0;
 return(out);
}

/*****************************************************************************/

int strappend(char **str,char *cat)
{
 int	l1,l2;

 if ( str==NULL )
	return(-1);
 else if ( *str==NULL )
  {	*str=strdup(cat);
	return(0);
  }
 else
  {	l1=strlen(*str);
	l2=strlen(cat);
	*str=realloc(*str,l1+l2+1);
	strcpy((*str)+l1,cat);
	return(0);
  }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

int vstrappendf(char **str,char *fmt,va_list ap)
{
 int		n,l,size;

 if ( str==NULL )	return(0);

 if ( *str==NULL )	l=0;
 else			l=strlen(*str);
 size=128;

 *str=realloc(*str,l+size);
 if ( *str==NULL )	return(-1);
 while ( 1 )
  {	n=vsnprintf((*str)+l,size,fmt,ap);
	if ( n>-1 && n<size )
		return(0);
	else if ( n>-1 )
		size=n+1;	
	else
		size=size*2;	
	if ( (*str=realloc(*str,l+size))==NULL )
		return(-1);
  };
 return(0);	
}

int strappendf(char **str,char *fmt,...)
{
 int		ret;
 va_list	ap;
 
 va_start(ap,fmt);
 ret=vstrappendf(str,fmt,ap);
 va_end(ap);

 return(ret);
}

/*****************************************************************************/