Current BE-LFS (but w/ GCC 3.3.3) merged -> 6.0 branch
[linux_from_scratch.git] / lfscmd / src / string.c
bloba38c4247d0587bd9685d32f0ecb4a7b8e254b002
1 /*
2 * Copyright (C) 2003 Timothy Bauscher <timothy@linuxfromscratch.org>
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <regex.h>
10 #include "string.h"
12 void error (const char *fmt, ...) {
13 va_list args;
14 char msg[512];
16 va_start(args, fmt);
17 vsnprintf(msg, sizeof msg, fmt, args);
18 va_end(args);
20 fprintf(stderr, "Error: %s\n", msg);
22 exit(1);
25 int string_cat (char *str1, const char *str2) {
26 int c, d;
27 c = string_len(str1);
28 d = 0;
30 while ('\0' != str2[d])
31 str1[c++] = str2[d++];
33 str1[c] = '\0';
34 return(d);
37 int string_comp (const char *str1, const char *str2) {
38 int len;
39 len = string_len(str1);
41 if (len != string_len(str2)) return(0);
42 for (; len >= 0; len--)
43 if (str1[len] != str2[len]) return(0);
45 return(1);
48 char *string_cpy (const char *str1) {
49 int c;
50 char *dest;
52 /* Allocate space for new string */
53 c = 0;
54 dest = malloc((string_len(str1)+1) * sizeof(char));
56 while (*str1) {
57 dest[c] = *str1++;
58 c++;
61 dest[c] = '\0';
62 return(dest);
65 int string_len (const char *string) {
66 int c;
67 for (c=0; '\0' != string[c]; c++);
68 return(c);
71 char *string_snip (const char *string, const int start, const int end) {
72 int c, d;
73 char *str;
75 /* Allocate space for new string */
76 str = malloc((end-start+1) * sizeof(char));
77 d = 0;
79 /* Record text from start to stop */
80 for (c=start; c < end; c++)
81 str[d++] = string[c];
83 str[d] = '\0';
84 return(str);
87 /* TODO: This function is nasty; recode. */
88 char *string_strip (const char *string, const char *strip) {
89 char *str, *tmp;
90 int len_stp, len_str;
91 int c, s;
93 len_stp = string_len(strip);
94 len_str = string_len(string);
95 str = malloc((len_str + 1) * sizeof(char));
96 c = 0;
98 for (s=0; '\0' != string[s];)
100 tmp = string_snip(string, s, s + len_stp);
102 if (string_comp(strip, tmp))
103 s += len_stp;
104 else
105 str[c++] = string[s++];
107 free(tmp);
110 str[c] = '\0';
111 return(str);