# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / ftpd / mystring.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 int pos(char *haystack, char *needle)
6 {
7         if (strstr(haystack, needle)) {
8                 return (int) strstr(haystack, needle) - (int) haystack;
9         } else {
10                 return -1;
11         }
12 }
13
14 void cutto(char *str, int len)
15 {
16         memmove(str, str + len, strlen(str) - len + 1);
17 }
18
19 void mystrncpy(char *dest, char *src, int len)
20 {
21         strncpy(dest, src, len);
22         *(dest + len) = 0;
23 }
24
25 int replace(char *str, char *what, char *by)
26 {
27         char *foo, *bar = str;
28     int i = 0;
29         while ((foo = strstr(bar, what))) {
30         bar = foo + strlen(by);
31                 memmove(bar,
32                                 foo + strlen(what), strlen(foo + strlen(what)) + 1);
33                 memcpy(foo, by, strlen(by));
34         i++;
35         }
36     return i;
37 }
38 //brcm not used
39 #if 0
40 /* int_from_list(char *list, int n) 
41  * returns the n'th integer element from a string like '2,5,12-15,20-23'
42  * if n is out of range or *list is out of range, -1 is returned.
43  */
44
45 int int_from_list(char *list, int n)
46 {
47     char *str, *tok;
48     int count = -1, firstrun = 1;
49     
50     str = (char *) malloc(sizeof(char) * (strlen(list) + 2));
51     if (!str)
52         return -1;
53     
54     memset(str, 0, strlen(list) + 2);
55     strncpy(str, list, strlen(list));
56     
57     /* apppend ',' to the string so we can always use strtok() */
58         str[strlen(list)] = ',';
59     
60     for (;;) {
61         if (firstrun)
62             tok = strtok(str, ",");
63         else
64             tok = strtok(NULL, ",");
65         
66         if (!tok || *tok == '\0') {
67                         free(str);
68             return -1;
69                 }
70         
71         if (strchr(tok, '-')) {
72             char *start, *end;
73             int s, e;
74             start = tok;
75             end = strchr(tok, '-');
76             *end = '\0';
77             end++;
78             if (!*start || !*end) {
79                                 free(str);
80                 return -1;
81                         }
82             s = atoi(start);
83             e = atoi(end);
84             if (s < 1 || e < 1 || e < s) {
85                                 free(str);
86                 return -1;
87                         }
88             count += e - s + 1;
89             if (count >= n) {
90                                 free(str);
91                 return (e - (count - n));
92                         }
93         } else {
94             count++;
95             if (count == n) {
96                                 int val = atoi(tok);
97                                 free(str);
98                 return val;
99                         }
100         }
101         firstrun = 0;
102     }
103 }
104 #endif //brcm