source: titan/titan/global.h @ 36063

Last change on this file since 36063 was 36063, checked in by obi, 8 years ago

next scan test

File size: 160.1 KB
Line 
1#ifndef GLOBAL_H
2#define GLOBAL_H
3
4//flag 0: video+gui+freez
5//flag 1: video+gui
6//flag 2: video
7//flag 3: gui
8
9void screenshoot(int flag)
10{
11        char* cmd = NULL;
12        if(flag == 0 || flag == 2)
13        {
14                videofreeze(status.aktservice->videodev);
15                sleep(1);
16        }
17
18        if(flag == 0 || flag == 1)
19                cmd = ostrcat("grab -j 100 -r 960", NULL, 0, 0);
20        else if(flag == 2)
21                cmd = ostrcat("grab -v -j 100 -r 960", NULL, 0, 0);
22        else if(flag == 3)
23                cmd = ostrcat("grab -o -j 100 -r 960", NULL, 0, 0);
24
25        if(cmd != NULL)
26                system(cmd);
27
28        if(flag == 0 || flag == 2)
29                videocontinue(status.aktservice->videodev);
30       
31        free(cmd), cmd = NULL;
32}
33
34//flag 0: playerstart
35//flag 1: playerstop
36void set_player_sound(int flag)
37{
38#ifdef SH4
39        char* vol = NULL, *cmd = NULL;
40
41        if(flag == 0)
42                vol = ostrcat(getconfig("vol_playerstart", NULL), NULL, 0, 0);
43        else if(flag == 1)
44                vol = ostrcat(getconfig("vol_playerstop", NULL), NULL, 0, 0);
45
46        cmd = ostrcat("amixer -c 1 set Analog playback '", vol, 0, 0);
47        if(status.mute == 1)
48                cmd = ostrcat(cmd, "%' mute &", 1, 0);
49        else
50                cmd = ostrcat(cmd, "%' unmute &", 1, 0);
51        system(cmd);
52        free(cmd), cmd = NULL;
53
54        cmd = ostrcat("amixer -c 1 set SPDIF playback '", vol, 0, 0);
55        if(status.mute == 1)
56                cmd = ostrcat(cmd, "%' mute &", 1, 0);
57        else
58                cmd = ostrcat(cmd, "%' unmute &", 1, 0);
59        system(cmd);
60        free(cmd), cmd = NULL;
61
62        cmd = ostrcat("amixer -c 1 set HDMI playback '", vol, 0, 0);
63        if(status.mute == 1)
64                cmd = ostrcat(cmd, "%' mute &", 1, 0);
65        else
66                cmd = ostrcat(cmd, "%' unmute &", 1, 0);
67        system(cmd);
68        free(cmd), cmd = NULL;
69        free(vol), vol = NULL;
70#endif
71}
72
73//flag 0: with wait message
74//flag 1: without wait message
75void waitmsgbar(int sec, int exit, char* text, int flag)
76{
77        if(sec < 1) return;
78        int maxsec = sec, rcret = -1;
79       
80        char* tmpstr = NULL;
81        struct skin* waitmsgbar = getscreen("waitmsgbar");
82        struct skin* load = getscreen("loading");
83       
84        waitmsgbar->progresssize = 0;
85        if(text == NULL)
86                tmpstr = ostrcat("0%", NULL, 0, 0);
87        else
88                tmpstr = ostrcat(text, " (0%)", 0, 0);
89        changetext(waitmsgbar, tmpstr);
90        free(tmpstr); tmpstr = NULL;
91       
92        if(flag == 0) drawscreen(load, 0, 0);
93       
94        while(sec > 0)
95        {
96                drawscreen(waitmsgbar, 0, 0);
97               
98                if(exit == 0)
99                        sleep(1);
100                else
101                {
102                        rcret = waitrc(0, 1000, 0);
103                        if(rcret == getrcconfigint("rcexit", NULL))
104                                break;
105                               
106                        if(rcret != RCTIMEOUT) continue;
107                }
108               
109                sec--;
110                waitmsgbar->progresssize = ((maxsec - sec) * 100) / maxsec;
111               
112                if(text == NULL)
113                        tmpstr = ostrcat(oitoa(waitmsgbar->progresssize), "%", 1, 0);
114                else
115                {
116                        tmpstr = ostrcat(text, " (", 0, 0);
117                        tmpstr = ostrcat(tmpstr, oitoa(waitmsgbar->progresssize), 1, 1);
118                        tmpstr = ostrcat(tmpstr, "%)", 1, 0);
119                }
120                changetext(waitmsgbar, tmpstr);
121                free(tmpstr); tmpstr = NULL;
122        }
123       
124        free(tmpstr); tmpstr = NULL;
125        if(flag == 0) clearscreen(load);
126        clearscreen(waitmsgbar);
127        drawscreen(skin, 0, 0);
128}
129
130//flag 0: get pluginpath an add text
131//flag 1: get pluginpath and change "%pluginpath%/"
132char* createpluginpath(char* text, int flag)
133{
134        char* tmpstr = NULL;
135        if(text == NULL) return NULL;
136       
137        if(flag == 0)
138        {
139          tmpstr = ostrcat(getconfig("pluginpath", NULL), text, 0, 0);
140          if(file_exist(tmpstr) == 1) return tmpstr;
141         
142          free(tmpstr); tmpstr = NULL;
143          tmpstr = ostrcat(getconfig("pluginpath1", NULL), text, 0, 0);
144          if(file_exist(tmpstr) == 1) return tmpstr;
145         
146          free(tmpstr); tmpstr = NULL;
147          tmpstr = ostrcat(getconfig("pluginpath2", NULL), text, 0, 0);
148          if(file_exist(tmpstr) == 1) return tmpstr;
149        }
150        else
151        {
152                if(strlen(text) < 13) return NULL;
153               
154                tmpstr = ostrcat(getconfig("pluginpath", NULL), &text[12], 0, 0);
155                if(file_exist(tmpstr) == 1) return tmpstr;
156               
157                free(tmpstr); tmpstr = NULL;
158                tmpstr = ostrcat(getconfig("pluginpath1", NULL), &text[12], 0, 0);
159                if(file_exist(tmpstr) == 1) return tmpstr;
160               
161                free(tmpstr); tmpstr = NULL;
162                tmpstr = ostrcat(getconfig("pluginpath2", NULL), &text[12], 0, 0);
163                if(file_exist(tmpstr) == 1) return tmpstr;
164        }
165       
166        free(tmpstr); tmpstr = NULL;
167        return NULL;
168}
169
170int osystem(char* cmd, int timeout)
171{
172        int ret = 0;
173        char* tmpstr = NULL;
174
175        tmpstr = ostrcat(tmpstr, "timeout -t ", 1, 0);
176        tmpstr = ostrcat(tmpstr, oitoa(timeout), 1, 1);
177        tmpstr = ostrcat(tmpstr, " -s 9 ", 1, 0);
178        tmpstr = ostrcat(tmpstr, cmd, 1, 0);
179        ret = system(tmpstr);
180        free(tmpstr); tmpstr = NULL;
181
182        return ret;
183}
184
185int wlanlinkquality()
186{
187        char dev[20];
188        int tmp = 0, linkquality = 0;
189        FILE *fd = NULL;
190        char *fileline = NULL;
191
192        struct inetwork* wlandev = getinetworkfirstwlan();
193        if(wlandev == NULL) return 0;
194
195        fileline = malloc(MINMALLOC);
196        if(fileline == NULL)
197        {
198                err("no mem");
199                return 0;
200        }
201
202        fd = fopen("/proc/net/wireless", "r");
203        if(fd == NULL)
204        {
205                perr("can't open /proc/net/wireless");
206                free(fileline);
207                return 0;
208        }
209
210        while(fgets(fileline, MINMALLOC, fd) != NULL)
211        {
212                if(ostrstr(fileline, wlandev->device) != NULL)
213                        sscanf(fileline, "%[^:]: %d %d", dev, &tmp, &linkquality);
214        }
215
216        free(fileline);
217        fclose(fd);
218        return linkquality;
219}
220
221char* getispip()
222{
223        char* tmpstr = NULL;
224        char* ip = NULL;
225        tmpstr = gethttp("checkip.dyndns.org", "", 80, NULL, NULL, 10000, NULL, 0);
226
227        if(oregex(".*<body>Current IP Address: ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})</body>.*", tmpstr))
228        {
229                ip = oregex(".*<body>Current IP Address: ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})</body>.*", tmpstr);
230        }
231        free(tmpstr), tmpstr = NULL;
232        return ip;
233}
234
235//flag 0: leave standby
236//flag 1: set standby
237int setcecstandby(int flag)
238{
239        if(getconfigint("usecec", NULL) == 1 || getconfigint("cec_on", NULL) == 1)
240        {
241#ifndef MIPSEL         
242                if(flag == 0)
243                        writesys("/proc/stb/cec/onetouchplay", "0", 1);
244                else
245                {
246                        char* tmpstr = readsys("/proc/stb/cec/state_cecaddress", 1);
247                        char* tmpstr1 = readsys("/proc/stb/cec/state_activesource", 1);
248
249                        if(ostrcmp(tmpstr, tmpstr1) == 0)
250                                writesys("/proc/stb/cec/systemstandby", "0", 1);
251
252                        free(tmpstr); tmpstr = NULL;
253                        free(tmpstr1); tmpstr1 = NULL;
254                }
255#else
256                if(flag == 0)
257                        cecwakeup();
258                else
259                {
260                        cecstandby();
261                        sleep(1);
262                        cecstandby();
263                }
264#endif
265        }
266
267        return 0;
268}
269
270char* mask(char* input, int count, char* maskchar)
271{
272        char* tmpstr = NULL;
273        int i = 0;
274
275        tmpstr = ostrcat(input, NULL, 1, 0);
276
277        if(tmpstr != NULL)
278        {
279                int len = strlen(tmpstr);
280                for(i = 0; i < count - len; i++)
281                        tmpstr = ostrcat(maskchar, tmpstr, 0, 1);
282        }
283        else
284        {
285                for(i = 0; i < count; i++)
286                        tmpstr = ostrcat(maskchar, tmpstr, 0, 1);       
287        }
288
289        return tmpstr;
290}
291
292int checkinternet()
293{
294        int skip = 0, i = 0;
295        char* tmp = NULL, *cmd = NULL;
296
297        cmd = ostrcat(cmd, "google.de", 1, 0);
298
299        //if(system(cmd) != 0)
300        for(i = 0; i < 3; i++)
301        {
302                free(tmp); tmp = NULL;
303                tmp = gethttp(cmd, "/", 80, NULL, NULL, 5000, NULL, 0);
304                if(tmp != NULL) break;
305        }
306        if(tmp == NULL)
307                skip = 1;
308
309        free(tmp); tmp = NULL;
310        free(cmd), cmd = NULL;
311
312        return skip;
313}
314
315int checkpng(char* filename)
316{
317        FILE* fd = NULL;
318        int ret = 0;
319        png_bytep sig = NULL;
320
321        fd = fopen(filename, "rb");
322        if(fd == NULL)
323        {
324                perr("open file %s", filename);
325                return 0;
326        }
327
328        sig = malloc(8);
329        if(sig == NULL)
330        {
331                err("no mem");
332                fclose(fd);
333                return 0;
334        }
335
336        fread(sig, 1, 8, fd);
337        ret = png_check_sig(sig, 8);
338        if(ret == 0)
339        {
340                free(sig);
341                fclose(fd);
342                return 0;
343        }
344
345        free(sig);
346        fclose(fd);
347        return 1;
348}
349
350char **str_split(char *string, char *delim)
351{
352        char **tokens = NULL;
353        char *working = NULL;
354        char *token = NULL;
355        int idx = 0;
356       
357        if(string == NULL) return NULL;
358
359        tokens = malloc(sizeof(char*) * MAXTOKENS);
360        if(tokens == NULL)
361                return NULL;
362
363        working = malloc(sizeof(char) * strlen(string) + 1);
364        if(working == NULL)
365                return NULL;
366
367         /* to make sure, copy string to a safe place */
368        strcpy(working, string);
369        for(idx = 0; idx < MAXTOKENS; idx++)
370                tokens[idx] = NULL;
371
372        token = strtok(working, delim);
373        idx = 0;
374
375        /* always keep the last entry NULL termindated */
376        while(idx < (MAXTOKENS - 1) && token != NULL)
377        {
378                tokens[idx] = malloc(sizeof(char) * strlen(token) + 1);
379                if(tokens[idx] != NULL)
380                {
381                        strcpy(tokens[idx], token);
382                        idx++;
383                        token = strtok(NULL, delim);
384                }
385        }
386
387        free(working);
388        return tokens;
389}
390
391char* readfromlinetoline(char* str, int start, int end, int flag)
392{
393        if(str == NULL) return str;
394
395        char* tmpstr = NULL;
396        int count = 0;
397        int i = 0;
398        struct splitstr* ret = NULL;
399        ret = strsplit(str, "\n", &count);
400
401        for(i = 0; i < count; i++)
402        {
403                if(i >= start && i <= end)
404                {
405                        tmpstr = ostrcat(tmpstr, (&ret[i])->part, 1, 0);
406                        tmpstr = ostrcat(tmpstr, "\n", 1, 0);
407                }
408        }
409        free(ret), ret = NULL;
410
411        if(flag == 1)
412        {
413                free(str), str = NULL;
414        }
415        return tmpstr; 
416}
417
418char* ltostr(char* str, long val, unsigned base)
419{
420        ldiv_t r;
421
422        if(base > 36 || str == NULL)
423                return NULL;
424
425        if(val < 0) *str++ = '-';
426        r = ldiv(labs(val), base);
427
428        if(r.quot > 0) str = ltostr(str, r.quot, base);
429
430        *str++ = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem];
431        *str = '\0';
432        return str;
433}
434
435char* oltostr(long val, unsigned base)
436{
437        char* str = NULL;
438
439        str = calloc(1, MINMALLOC);
440        if(str == NULL)
441        {
442                err("no mem");
443                return NULL;
444        }
445
446        ltostr(str, val, base);
447
448        return ostrshrink(str);
449}
450
451char* unhexlify(char *hexstr)
452{
453        int len = 0, tmpint = 0;
454        char *p, *q, *binstr = NULL;
455
456        if(hexstr == NULL) return NULL;
457
458        len = strlen(hexstr);
459        if(len == 0 || len % 2 != 0) return NULL;
460
461        binstr = calloc(1, (len / 2) + 1);
462        if(binstr == NULL)
463        {
464                err("no mem");
465                return NULL;
466        }
467
468        for(p = hexstr, q = binstr; *p; p += 2, q++)
469        {
470                sscanf(p, "%2x", &tmpint);
471                *q = tmpint;
472        }
473
474        return binstr;
475}
476
477int getsupermagic(char* filename)
478{
479        struct statfs64 s;
480
481        if(statfs64(filename, &s) >= 0)
482                return s.f_type;
483
484        return 0;
485}
486
487int getrandom(int max)
488{
489        srand(time(NULL));
490        return (rand() % max);
491}
492
493char* delmountpart(char* filename, int free1)
494{
495        struct mntent ent;
496        FILE *fd = NULL;
497        char* ret = NULL, *buf = NULL, *treffer = NULL, *rpath = NULL;
498        int mntdirlen = 0;
499
500        if(filename == NULL) return NULL;
501
502        buf = malloc(MINMALLOC);
503        if(buf == NULL)
504        {
505                err("no mem");
506                return NULL;
507        }
508       
509        rpath = realpath(filename, NULL);
510        if(rpath == NULL) return NULL;
511
512        fd = setmntent("/proc/mounts", "r");
513        if(fd != NULL)
514        {
515                while(getmntent_r(fd, &ent, buf, MINMALLOC) != 0)
516                {
517                        if(ent.mnt_dir != NULL)
518                        {
519                                mntdirlen = strlen(ent.mnt_dir);
520                                if(mntdirlen > 1 && ostrstr(rpath, ent.mnt_dir) == rpath)
521                                {
522                                        if(treffer == NULL || strlen(treffer) < mntdirlen)
523                                        {
524                                                free(treffer); treffer = NULL;
525                                                treffer = ostrcat(ent.mnt_dir, NULL, 0, 0);
526                                        }
527                                }
528                        }
529                }
530        }
531
532        if(treffer != NULL)
533                ret = string_replace(treffer, NULL, rpath, 0);
534
535        endmntent(fd);
536        if(free1 == 1) free(filename);
537        free(buf);
538        free(treffer);
539        free(rpath);
540        return ret;
541}
542
543char* addmountpart(char* filename, int free1)
544{
545        struct mntent ent;
546        FILE *fd = NULL;
547        char* ret = NULL, *buf = NULL;
548
549        if(filename == NULL) return NULL;
550
551        buf = malloc(MINMALLOC);
552        if(buf == NULL)
553        {
554                err("no mem");
555                return NULL;
556        }
557
558        fd = setmntent("/proc/mounts", "r");
559        if(fd != NULL)
560        {
561                while(getmntent_r(fd, &ent, buf, MINMALLOC))
562                {
563                        if(ent.mnt_dir != NULL && strlen(ent.mnt_dir) > 1)
564                        {
565                                ret = ostrcat(ent.mnt_dir, filename, 0, 0);
566                                if(file_exist(ret))
567                                {
568                                        if(free1 == 1) free(filename);
569                                        endmntent(fd);
570                                        free(buf);
571                                        return ret;
572                                }
573                                free(ret); ret = NULL;
574                        }
575                }
576        }
577
578        endmntent(fd);
579        free(buf);
580        return ret;
581}
582
583unsigned int gethash(char* str)
584{
585        unsigned int hash = 0;
586        int c = 0;
587
588        if(str == NULL) return 0;
589
590        while((c = *str++))
591                hash = ((hash << 5) + hash) ^ c;
592
593        return hash;
594}
595
596void freeregexstruct(struct regex* node)
597{
598        if(node == NULL) return;
599
600        free(node->match1); node->match1 = NULL;
601        free(node->match2); node->match2 = NULL;
602        free(node->match3); node->match3 = NULL;
603        free(node->match4); node->match4 = NULL;
604        free(node->match5); node->match5 = NULL;
605        free(node->match6); node->match6 = NULL;
606        free(node->match7); node->match7 = NULL;
607        free(node->match8); node->match8 = NULL;
608        free(node->match9); node->match9 = NULL;
609        free(node->match10); node->match10 = NULL;
610
611        free(node); node = NULL;
612}
613
614struct regex* regexstruct(char* regex, char* str)
615{
616        regex_t preg = {NULL};
617        regmatch_t pmatch[11] = {{0}};
618        size_t rm = 0, i = 0, len = 0;
619        char* ret = NULL;
620        struct regex* node = NULL;
621
622        if(regex == NULL || str == NULL) return NULL;
623
624        rm = regcomp(&preg, regex, REG_EXTENDED);
625        if(rm != 0) //error in regex
626        {
627                regfree(&preg);
628                return NULL;
629        }
630
631        rm = regexec(&preg, str, 11, pmatch, 0);
632        if(rm != 0) //no match
633        {
634                regfree(&preg);
635                return NULL;
636        }
637
638        node = (struct regex*)calloc(1, sizeof(struct regex));
639        if(node == NULL)
640        {
641                err("no mem");
642                regfree(&preg);
643                return NULL;
644        }
645
646        for(i = 1; !rm && i <= preg.re_nsub; i++)
647        {
648                len = pmatch[i].rm_eo - pmatch[i].rm_so;
649                if(len < 1) continue;
650
651                ret = malloc(len + 1);
652                if(ret != NULL)
653                {
654                        memcpy(ret, str + pmatch[i].rm_so, len);
655                        ret[len] = '\0';
656                }
657
658                if(i == 1) node->match1 = ret;
659                else if(i == 2) node->match2 = ret;
660                else if(i == 3) node->match3 = ret;
661                else if(i == 4) node->match4 = ret;
662                else if(i == 5) node->match5 = ret;
663                else if(i == 6) node->match6 = ret;
664                else if(i == 7) node->match7 = ret;
665                else if(i == 8) node->match8 = ret;
666                else if(i == 9) node->match9 = ret;
667                else if(i == 10) node->match10 = ret;
668
669                ret = NULL;
670        }
671
672        regfree(&preg);
673        return node;
674}
675
676char* oregex(char* regex, char* str)
677{
678        regex_t preg = {NULL};
679        regmatch_t pmatch[2] = {{0}};
680        size_t rm = 0, len = 0;
681        char* ret = NULL;
682
683        if(regex == NULL || str == NULL) return NULL;
684
685        rm = regcomp(&preg, regex, REG_EXTENDED);
686        if(rm != 0) //error in regex
687        {
688                regfree(&preg);
689                return NULL;
690        }
691
692        rm = regexec(&preg, str, 2, pmatch, 0);
693        if(rm != 0) //no match
694        {
695                regfree(&preg);
696                return NULL;
697        }
698
699        len = pmatch[1].rm_eo - pmatch[1].rm_so;
700        if(len < 1)
701        {
702                regfree(&preg);
703                return NULL;
704        }
705
706        ret = malloc(len + 1);
707        if(ret != NULL)
708        {
709                memcpy(ret, str + pmatch[1].rm_so, len);
710                ret[len] = '\0';
711        }
712
713        regfree(&preg);
714        return ret;
715}
716
717int oregexint(char* regex, char* str)
718{
719        int iret = 0;
720        char* ret = NULL;
721
722        ret = oregex(regex, str);
723        if(ret != NULL) iret = atoi(ret);
724        free(ret);
725
726        return iret;
727}
728
729struct splitstr* oregexsplit(char* regex, char *str, char *tok, int* count)
730{
731        char *tmpstr = NULL;
732        struct splitstr *array = NULL, *tmparray = NULL;
733        *count = 0;
734
735        if(str == NULL || tok == NULL)
736                return NULL;
737
738        tmpstr = strtok(str, tok);
739        while(tmpstr != NULL)
740        {
741                *count = *count + 1;
742                tmparray = array; array = (struct splitstr*)realloc(array, sizeof(struct splitstr*) * (*count));
743                if(array == NULL)
744                {
745                        err("no mem");
746                        free(tmparray);
747                        return NULL;
748                }
749     
750                (&array[(*count) - 1])->part = oregex(regex, tmpstr);
751                tmpstr = strtok(NULL, tok);
752        }
753
754        return array;
755}
756
757void freeoregexsplit(struct splitstr* tmparray, int len)
758{
759        if(tmparray == NULL) return;
760        int i = 0;
761       
762        for(i = 0; i < len; i++)
763        {
764                free(tmparray[i].part);
765                tmparray[i].part = NULL;
766        }
767       
768        free(tmparray);
769}
770
771int getmaxsat(char* feshortname)
772{
773        char *tmpstr = NULL;
774        int maxsat = 1;
775
776        if(feshortname != NULL)
777        {
778                tmpstr = ostrcat(feshortname, "_maxsat", 0, 0);
779                maxsat = getconfigint(tmpstr, NULL);
780                free(tmpstr); tmpstr = NULL;
781        }
782        if(maxsat < 0) maxsat = 1;
783
784        return maxsat;
785}
786
787void autochangechannelname()
788{
789        uint8_t lastsecnr = 0xff;
790        int secnr = 0;
791        unsigned char* buf = NULL;
792
793        if(status.aktservice->fedev == NULL) return;
794
795        while(secnr <= lastsecnr && secnr <= 256)
796        {
797                buf = dvbgetsdt(status.aktservice->fedev, secnr, 2000000);
798                if(buf != NULL)
799                        findchannel(status.aktservice->fedev, NULL, buf, &lastsecnr, NULL, NULL, 1, 1);
800                else
801                        break;
802                free(buf); buf = NULL;
803                secnr++;
804        }
805}
806
807int isbase64(char c)
808{
809        if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/' || c == '=')
810                return 1;
811        return 0;
812}
813
814char decodebase64(char c)
815{
816        if(c >= 'A' && c <= 'Z') return(c - 'A');
817        if(c >= 'a' && c <= 'z') return(c - 'a' + 26);
818        if(c >= '0' && c <= '9') return(c - '0' + 52);
819        if(c == '+') return(62);
820        return 63;
821}
822
823int b64dec(char* dest, char* src)
824{
825        if(src && *src)
826        {
827                char* p = dest;
828                int k, l = strlen(src) + 1;
829                char* buf = NULL;
830
831                buf = malloc(l);
832                if(buf == NULL) return 0;
833
834                for(k = 0, l = 0; src[k]; k++)
835                {
836                        if(isbase64(src[k]))
837                                buf[l++] = src[k];
838                }
839
840                for(k = 0; k < l; k += 4)
841                {
842                        char c1 = 'A', c2 = 'A', c3 = 'A', c4 = 'A';
843                        char b1 = 0, b2 = 0, b3 = 0, b4 = 0;
844
845                        c1 = buf[k];
846                        if(k + 1 < l)
847                                c2 = buf[k + 1];
848                        if(k + 2 < l)
849                                c3 = buf[k + 2];
850                        if(k + 3 < l)
851                                c4 = buf[k + 3];
852
853                        b1 = decodebase64(c1);
854                        b2 = decodebase64(c2);
855                        b3 = decodebase64(c3);
856                        b4 = decodebase64(c4);
857
858                        *p++ = ((b1 << 2) | (b2 >> 4));
859
860                        if(c3 != '=')
861                                *p++ = (((b2 & 0xf) << 4) | (b3 >> 2));
862                        if(c4 != '=')
863                                *p++ = (((b3 & 0x3) << 6) | b4);
864
865                }
866
867                free(buf);
868                return(p - dest);
869        }
870        return 0;
871}
872
873void changechannellist(struct channel* chnode, char* channellist)
874{
875                char* tmpstr = NULL;
876
877                if(chnode == NULL || status.aktservice->channel == NULL) return;
878
879                if(chnode->servicetype != 99) status.servicetype = chnode->servicetype;
880                if(status.servicetype == 0)
881                {
882                        if(channellist != NULL)
883                        {
884                                tmpstr = ostrcat(channellist, NULL, 0, 0);
885                                free(status.aktservice->channellist);
886                                status.aktservice->channellist = tmpstr;
887                                addconfig("channellist", tmpstr);
888                        }
889                        tmpstr = oitoa(status.aktservice->channel->serviceid);
890                        addconfig("serviceid", tmpstr);
891                        free(tmpstr); tmpstr = NULL;
892                        tmpstr = ollutoa(status.aktservice->channel->transponderid);
893                        addconfig("transponderid", tmpstr);
894                        free(tmpstr); tmpstr = NULL;
895                }
896                else
897                {
898                        if(channellist != NULL)
899                        {
900                                tmpstr = ostrcat(channellist, NULL, 0, 0);
901                                free(status.aktservice->channellist);
902                                status.aktservice->channellist = tmpstr;
903                                addconfig("rchannellist", tmpstr);
904                        }
905                        tmpstr = oitoa(status.aktservice->channel->serviceid);
906                        addconfig("rserviceid", tmpstr);
907                        free(tmpstr); tmpstr = NULL;
908                        tmpstr = ollutoa(status.aktservice->channel->transponderid);
909                        addconfig("rtransponderid", tmpstr);
910                        free(tmpstr); tmpstr = NULL;
911                }
912}
913
914//flag 0: normal picons
915//flag 1: alternate picons
916char* createpiconpath(struct channel* chnode, int flag)
917{
918        char* tmpstr = NULL, *picon = NULL;
919
920        if(chnode != NULL)
921        {
922                picon = ostrcat("/", oitoa(chnode->serviceid), 0, 1);
923                picon = ostrcat(picon, "-", 1, 0);
924                picon = ostrcat(picon, ollutoa(chnode->transponderid), 1, 1);
925                picon = ostrcat(picon, ".png", 1, 0);
926
927                if(flag == 1)
928                        tmpstr = ostrcat("/mnt/swapextensions/usr/local/share/titan/picons/alternate", picon, 0, 0);
929                else
930                        tmpstr = ostrcat("/mnt/swapextensions/usr/local/share/titan/picons", picon, 0, 0);
931                if(isfile(tmpstr))
932                {
933                        free(picon);
934                        return tmpstr;
935                }
936               
937                free(tmpstr); tmpstr = NULL;
938                if(flag == 1)
939                        tmpstr = ostrcat("/var/swap/usr/local/share/titan/picons/alternate", picon, 0, 0);
940                else
941                        tmpstr = ostrcat("/var/swap/usr/local/share/titan/picons", picon, 0, 0);
942                if(isfile(tmpstr))
943                {
944                        free(picon);
945                        return tmpstr;
946                }
947               
948                free(tmpstr); tmpstr = NULL;
949                if(flag == 1)
950                        tmpstr = ostrcat("/var/usr/local/share/titan/picons/alternate", picon, 0, 0);
951                else
952                        tmpstr = ostrcat("/var/usr/local/share/titan/picons", picon, 0, 0);
953                if(isfile(tmpstr))
954                {
955                        free(picon);
956                        return tmpstr;
957                }
958        }
959       
960        free(tmpstr); tmpstr = NULL;
961        tmpstr = ostrcat(tmpstr, getconfig("defskinpath", NULL), 1, 0);
962        tmpstr = ostrcat(tmpstr, "/skin/", 1, 0);
963        tmpstr = ostrcat(tmpstr, "defpicon.png", 1, 0);
964
965        free(picon);
966        return tmpstr;
967}
968
969int checkskin()
970{
971        int ret = 0;
972        char* defskinpath = NULL;
973        char* defskinconfig = NULL;
974        char* defskinfile = NULL;
975
976        if(file_exist(getconfig("skinpath", NULL)) == 0)
977                ret = 1;
978        else if(file_exist(getconfig("skinconfig", NULL)) == 0)
979                ret = 2;
980        else if(file_exist(getconfig("skinfile", NULL)) == 0)
981                ret = 3;
982
983        if(ret != 0)
984        {
985                err("skin not found code=%d, change to default", ret);
986
987                defskinpath = getconfig("defskinpath", NULL);
988                if(defskinpath != NULL) addconfig("skinpath", defskinpath);
989                defskinconfig = getconfig("defskinconfig", NULL);
990                if(defskinconfig != NULL) addconfig("skinconfig", defskinconfig);
991                defskinfile = getconfig("defskinfile", NULL);
992                if(defskinfile != NULL) addconfig("skinfile", defskinfile);
993
994                ret = 0;
995                if(file_exist(getconfig("skinpath", NULL)) == 0)
996                        ret = 1;
997                else if(file_exist(getconfig("skinconfig", NULL)) == 0)
998                        ret = 2;
999                else if(file_exist(getconfig("skinfile", NULL)) == 0)
1000                        ret = 3;
1001        }
1002
1003        if(ret != 0)
1004        {
1005                err("default skin not found code=%d", ret);
1006        }
1007
1008        return ret;
1009}
1010
1011int getaktvideosize()
1012{
1013        //wait 3 sek after zap, befor videsize is valid
1014        //driver takes a little time
1015        if(status.videosizevalid == 1)
1016                return 0;
1017        else if(status.videosizevalid + 3 > time(NULL))
1018                return 1;
1019        else
1020        {
1021#ifndef SIMULATE
1022                if(videoreadqwidth(status.aktservice->videodev) == 0)
1023#endif
1024                {
1025                        status.videosizevalid = 1;
1026                        return 0;
1027                }
1028        }
1029
1030        return 1;
1031}
1032
1033int ostrftime(char* buf, int count, char* format, struct tm* t)
1034{
1035        int ret = -1;
1036
1037        if(buf == NULL || t == NULL) return ret;
1038
1039        if(format != NULL)
1040                ret = strftime(buf, count, format, t);
1041        else
1042                ret = strftime(buf, count, "%d-%m-%Y %H:%M", t);
1043
1044        return ret;
1045}
1046
1047//flag 0 = write all
1048//flag 1 = don't write epg
1049//flag 2 = only write epg
1050//flag 3 = only write config
1051int writeallconfig(int flag)
1052{
1053        char* tmpstr = NULL;
1054        int ret = 0;
1055
1056        if(flag == 0 || flag == 1)
1057        {
1058                if(status.writerectimer == 1)
1059                        if(writerectimer(getconfig("rectimerfile", NULL), 0) != 0)
1060                                ret = 1;
1061                if(status.writesat == 1)
1062                        if(writesat(getconfig("satfile", NULL)) != 0)
1063                                ret = 1;
1064                if(status.writeplaylist == 1)
1065                        if(writeallplaylist() != 0)
1066                                ret = 1;
1067                if(status.writemainplaylist == 1)
1068                        if(writemainplaylist(getconfig("playlistfile", NULL)) != 0)
1069                                ret = 1;
1070                if(status.writebouquet == 1)
1071                        if(writeallbouquet() != 0)
1072                                ret = 1;
1073                if(status.writemainbouquet == 1)
1074                        if(writemainbouquet(getconfig("bouquetfile", NULL)) != 0)
1075                                ret = 1;
1076                if(status.writechannel == 1)
1077                        if(writechannel(getconfig("channelfile", NULL)) != 0)
1078                                ret = 1;
1079                if(status.writetransponder == 1)
1080                        if(writetransponder(getconfig("transponderfile", NULL)) != 0)
1081                                ret = 1;
1082                if(status.writeprovider == 1)
1083                        if(writeprovider(getconfig("providerfile", NULL)) != 0)
1084                                ret = 1;
1085                if(status.writeownconfig == 1)
1086                        if(writeownconfig(getconfig("ownconfig", NULL)) != 0)
1087                                ret = 1;
1088                if(status.writeepgscanlist == 1)
1089                        if(writeepgscanlist(getconfig("epgchannelfile", NULL)) != 0)
1090                                ret = 1;
1091                if(status.writercconfig == 1)
1092                        if(writercconfig(getconfig("rcconfig", NULL)) != 0)
1093                                ret = 1;
1094                if(status.writeskinconfig == 1)
1095                        if(writeskinconfig(getconfig("skinconfig", NULL)) != 0)
1096                                ret = 1;
1097                if(status.writemostzap == 1)
1098                        if(writemostzap(getconfig("mostzapfile", NULL)) != 0)
1099                                ret = 1;
1100                if(status.writelastsubtitle == 1)
1101                        if(writelastsubtitle(getconfig("lastsubtitle", NULL)) != 0)
1102                                ret = 1;
1103                if(status.writechannelslot == 1)
1104                        if(writechannelslot(getconfig("channelslotfile", NULL)) != 0)
1105                                ret = 1;
1106        }
1107        if(flag == 0 || flag == 1 || flag == 3)
1108        {
1109                if(status.writeconfig == 1)
1110                {
1111                        addconfigint("vol", getvol());
1112                        if(writeconfig(status.configfile) != 0)
1113                                ret = 1;
1114                }
1115        }
1116        if((flag == 0 || flag == 2) && time(NULL) > 1072224000) // 01.01.2004
1117        {
1118                tmpstr = createpath(getconfig("epg_path", NULL), "epg.dat");
1119                if(writeepg(tmpstr) != 0)
1120                        ret = 1;
1121                free(tmpstr); tmpstr = NULL;
1122        }
1123
1124        sync();
1125
1126        return ret;
1127}
1128
1129void* writeallconfigthread0()
1130{
1131        writeallconfig(0);
1132        pthread_exit(NULL);
1133}
1134
1135void* writeallconfigthread1()
1136{
1137        writeallconfig(1);
1138        pthread_exit(NULL);
1139}
1140
1141void initmutex(int flag)
1142{
1143        if(flag == 1)
1144        {
1145                pthread_mutex_init(&status.drawingmutex, NULL);
1146                pthread_mutex_init(&status.rectimermutex, NULL);
1147                pthread_mutex_init(&status.servicemutex, NULL);
1148                pthread_mutex_init(&status.epgmutex, NULL);
1149                pthread_mutex_init(&status.vfdmutex, NULL);
1150                pthread_mutex_init(&status.channelmutex, NULL);
1151                pthread_mutex_init(&status.timerthreadmutex, NULL);
1152                pthread_mutex_init(&status.audiotrackmutex, NULL);
1153                pthread_mutex_init(&status.subtitlemutex, NULL);
1154                pthread_mutex_init(&status.dmxdevmutex, NULL);
1155                pthread_mutex_init(&status.rcmutex, NULL);
1156                pthread_mutex_init(&status.queuemutex, NULL);
1157                pthread_mutex_init(&status.clistmutex, NULL);
1158                pthread_mutex_init(&status.hddmutex, NULL);
1159                pthread_mutex_init(&status.linkedchannelmutex, NULL);
1160                pthread_mutex_init(&status.tsseekmutex, NULL);
1161                pthread_mutex_init(&status.accelfbmutex, NULL);
1162                pthread_mutex_init(&status.mediadbmutex, NULL);
1163                pthread_mutex_init(&status.oldentrymutex, NULL);
1164                pthread_mutex_init(&status.newslettermutex, NULL);
1165                pthread_mutex_init(&status.tithekmutex, NULL);
1166                pthread_mutex_init(&status.inetworkmutex, NULL);
1167                pthread_mutex_init(&status.textboxmutex, NULL);
1168                pthread_mutex_init(&status.setaktresmutex, NULL);
1169                pthread_mutex_init(&status.waitrcmutex, NULL);
1170                pthread_mutex_init(&status.oledmutex, NULL);
1171        }
1172        else
1173        {
1174                pthread_mutex_destroy(&status.drawingmutex);
1175                pthread_mutex_destroy(&status.rectimermutex);
1176                pthread_mutex_destroy(&status.servicemutex);
1177                pthread_mutex_destroy(&status.epgmutex);
1178                pthread_mutex_destroy(&status.vfdmutex);
1179                pthread_mutex_destroy(&status.channelmutex);
1180                pthread_mutex_destroy(&status.timerthreadmutex);
1181                pthread_mutex_destroy(&status.audiotrackmutex);
1182                pthread_mutex_destroy(&status.subtitlemutex);
1183                pthread_mutex_destroy(&status.dmxdevmutex);
1184                pthread_mutex_destroy(&status.rcmutex);
1185                pthread_mutex_destroy(&status.queuemutex);
1186                pthread_mutex_destroy(&status.clistmutex);
1187                pthread_mutex_destroy(&status.hddmutex);
1188                pthread_mutex_destroy(&status.linkedchannelmutex);
1189                pthread_mutex_destroy(&status.tsseekmutex);
1190                pthread_mutex_destroy(&status.accelfbmutex);
1191                pthread_mutex_destroy(&status.mediadbmutex);
1192                pthread_mutex_destroy(&status.oldentrymutex);
1193                pthread_mutex_destroy(&status.newslettermutex);
1194                pthread_mutex_destroy(&status.tithekmutex);
1195                pthread_mutex_destroy(&status.inetworkmutex);
1196                pthread_mutex_destroy(&status.textboxmutex);
1197                pthread_mutex_destroy(&status.setaktresmutex);
1198                pthread_mutex_destroy(&status.waitrcmutex);
1199                pthread_mutex_destroy(&status.oledmutex);
1200        }
1201}
1202
1203int hexit(char c)
1204{
1205        if(c >= '0' && c <= '9')
1206                return c - '0';
1207        if(c >= 'a' && c <= 'f')
1208                return c - 'a' + 10;
1209        if(c >= 'A' && c <= 'F')
1210                return c - 'A' + 10;
1211
1212        return 0;
1213}
1214
1215char* htmlencode(char* from)
1216{
1217        int buflen = 0;
1218        char* buf = NULL, *to = NULL;
1219
1220        buf = calloc(1, MINMALLOC);
1221        if(buf == NULL)
1222        {
1223                err("no mem");
1224                return NULL;
1225        }
1226        to = buf;
1227
1228        for(buflen = 0; *from != '\0' && buflen < MINMALLOC; ++from)
1229        {
1230                if(isalnum(*from) || strchr("/_.-~", *from) != NULL)
1231                {
1232                        *to = *from;
1233                        ++to;
1234                        ++buflen;
1235                }
1236                else
1237                {
1238                        sprintf(to, "%%%02x", (int)*from & 0xff);
1239                        to += 3;
1240                        buflen += 3;
1241                }
1242        }
1243        *to = '\0';
1244
1245        buf = ostrshrink(buf);
1246        return buf;
1247}
1248
1249void htmldecode(char* to, char* from)
1250{
1251        if(to == NULL || from == NULL) return;
1252
1253        for(; *from != '\0'; ++to, ++from)
1254        {
1255                if(from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2]))
1256                {
1257                        *to = hexit(from[1]) * 16 + hexit(from[2]);
1258                        from += 2;
1259                }
1260                else
1261                        *to = *from;
1262        }
1263        *to = '\0';
1264}
1265
1266void htmldecode2(char* to, char* from)
1267{
1268        if(to == NULL || from == NULL) return;
1269
1270        for(; *from != '\0'; ++to, ++from)
1271        {
1272                if(from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2]) && isxdigit(from[3]) && isxdigit(from[4]))
1273                {
1274                        *to = hexit(from[1]) * 16 + hexit(from[2]) * 16 + hexit(from[3]) * 16 + hexit(from[4]);
1275                        from += 4;
1276                }
1277                else if(from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2]) && isxdigit(from[3]))
1278                {
1279                        *to = hexit(from[1]) * 16 + hexit(from[2]) * 16 + hexit(from[3]);
1280                        from += 3;
1281                }
1282                else if(from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2]))
1283                {
1284                        *to = hexit(from[1]) * 16 + hexit(from[2]);
1285                        from += 2;
1286                }
1287                else
1288                        *to = *from;
1289        }
1290        *to = '\0';
1291}
1292
1293void htmldecode3(char* to, char* from)
1294{
1295        if(to == NULL || from == NULL) return;
1296
1297        for(; *from != '\0'; ++to, ++from)
1298        {
1299                if(from[0] == '\\' && from[1] == 'u' && isxdigit(from[2]) && isxdigit(from[3]) && isxdigit(from[4]) && isxdigit(from[5]) && isxdigit(from[6]) && isxdigit(from[7]))
1300                {
1301                        *to = hexit(from[2]) * 1048576 + hexit(from[3]) * 65536 + hexit(from[4]) * 4096 + hexit(from[5]) * 256 + hexit(from[6]) * 16 + hexit(from[7]);
1302                        from += 7;
1303                }
1304                if(from[0] == '\\' && from[1] == 'u' && isxdigit(from[2]) && isxdigit(from[3]) && isxdigit(from[4]) && isxdigit(from[5]))
1305                {
1306                        *to = hexit(from[2]) * 4096 + hexit(from[3]) * 256 + hexit(from[4]) * 16 + hexit(from[5]);
1307                        from += 5;
1308                }
1309                else
1310                        *to = *from;
1311        }
1312        *to = '\0';
1313}
1314
1315void setosdtransparent(int value)
1316{
1317        int ret = 255;
1318        if(value < 0) value = 0;
1319        if(value > 100) value = 100;
1320
1321        ret -= value * 2.55;
1322
1323        setfbtransparent(ret);
1324}
1325
1326struct tm* olocaltime(time_t *value)
1327{
1328        struct tm *loctime, *loctime1 = NULL;
1329
1330        loctime1 = (struct tm*)malloc(sizeof(struct tm));
1331        if(loctime1 == NULL)
1332        {
1333                err("no mem");
1334                return NULL;
1335        }
1336        loctime = localtime_r(value, loctime1);
1337
1338        return loctime;
1339}
1340
1341int setwakeuptimer(time_t waketime)
1342{
1343        char* wakeuptimerprog = NULL;
1344        struct tm *loctime = NULL;
1345        char *buf = NULL;
1346        int ret = -1;
1347
1348        wakeuptimerprog = getconfig("wakeuptimerprog", NULL);
1349
1350        if(wakeuptimerprog == NULL)
1351        {
1352                err("NULL detect");
1353                return ret;
1354        }
1355
1356        buf = malloc(MINMALLOC);
1357        if(buf == NULL)
1358        {
1359                err("no mem");
1360                return ret;
1361        }
1362
1363        loctime = olocaltime(&waketime);
1364
1365        if(loctime != NULL)
1366                ostrftime(buf, MINMALLOC, wakeuptimerprog, loctime);
1367
1368        if(buf != NULL)
1369        {
1370                debug(10, "set wake up timer (%s)", buf);
1371                ret = system(buf);
1372        }
1373
1374        free(loctime);
1375        free(buf);
1376        return ret;
1377}
1378
1379int autoresolution()
1380{
1381        char *hotpluginfo = NULL;
1382        char *value = NULL;
1383
1384        if(getconfig("av_videomode", NULL) != NULL || getconfig("av_colorformat", NULL))
1385                return 1;
1386
1387        hotpluginfo = getconfig("hotpluginfo", NULL);
1388
1389        if(hotpluginfo == NULL)
1390        {
1391                err("NULL detect");
1392                return 1;
1393        }
1394
1395        value = readsys(hotpluginfo, 1);
1396        if(value == NULL)
1397        {
1398                err("NULL detect");
1399                return 1;
1400        }
1401
1402        if(value[0] == 'y')
1403        {
1404                setvideomode("720p50", 0);
1405                changefbresolution("720p50", 0);
1406                setcolorformat("hdmi_rgb", 0);
1407        }
1408        else
1409        {
1410                setvideomode("576i50", 0);
1411                changefbresolution("576i50", 0);
1412                setcolorformat("rgb", 1);
1413        }
1414
1415        free(value);
1416        return 0;
1417}
1418
1419//TODO: can remove if new function work
1420/*
1421int writelengthfield(unsigned char *buf, unsigned int len)
1422{
1423        if(len < 127)
1424        {
1425                *buf++ = len;
1426                return 1;
1427        }
1428        else if(len < 256)
1429        {
1430                *buf++ = 0x81;
1431                *buf++ = len;
1432                return 2;
1433        }
1434        else if(len < 65535)
1435        {
1436                *buf++ = 0x82;
1437                *buf++ = len >> 8;
1438                *buf++ = len;
1439                return 3;
1440        }
1441
1442        return 0;
1443}
1444*/
1445
1446size_t writelengthfield(unsigned char * buf, unsigned int len)
1447{
1448        if(buf == NULL) return 0;
1449
1450        if(len < 128)
1451        {
1452                buf[0] = len;
1453                return 1;
1454        }
1455        else
1456        {
1457                unsigned int pos = 0;
1458                unsigned int shiftby = 8;
1459                unsigned char lenfieldsize = 1;
1460
1461                while((len >> shiftby) != 0)
1462                {
1463                        lenfieldsize++;
1464                        shiftby += 8;
1465                }
1466
1467                buf[pos++] = ((1 << 7) | lenfieldsize);
1468
1469                while(shiftby != 0)
1470                {
1471                        shiftby -= 8;
1472                        buf[pos++] = len >> shiftby;
1473                }
1474                return pos;
1475        }
1476}
1477
1478//flag 0: del non alpha/num
1479//flag 1: change all /
1480//flag 2: change all non filename conform chars
1481//flag 3: replace non alpha/num with _
1482void delspezchar(char* text, int flag)
1483{
1484        char* tmpstr = text;
1485
1486        if(text == NULL) return;
1487
1488        while(*tmpstr != '\0')
1489        {
1490                if(flag == 1 || flag == 2)
1491                {
1492                        if(tmpstr[0] == '/') tmpstr[0] = '-';
1493                        if(flag == 2)
1494                        {
1495                                if(tmpstr[0] == '§') tmpstr[0] = '-';
1496                                if(tmpstr[0] == '<') tmpstr[0] = '-';
1497                                if(tmpstr[0] == '>') tmpstr[0] = '-';
1498                                if(tmpstr[0] == ':') tmpstr[0] = '-';
1499                                if(tmpstr[0] == '"') tmpstr[0] = '-';
1500                                if(tmpstr[0] == '\\') tmpstr[0] = '-';
1501                                if(tmpstr[0] == '|') tmpstr[0] = '-';
1502                                if(tmpstr[0] == '*') tmpstr[0] = '-';
1503                                if(tmpstr[0] == '?') tmpstr[0] = '-';
1504                                if(tmpstr[0] == '[') tmpstr[0] = '-';
1505                                if(tmpstr[0] == ']') tmpstr[0] = '-';
1506                                if(tmpstr[0] == '=') tmpstr[0] = '-';
1507                                if(tmpstr[0] == '%') tmpstr[0] = '-';
1508                                if(tmpstr[0] == '$') tmpstr[0] = '-';
1509                                if(tmpstr[0] == ',') tmpstr[0] = '-';
1510                                if(tmpstr[0] == ';') tmpstr[0] = '-';
1511                                if(tmpstr[0] == '~') tmpstr[0] = '-';
1512                                if(tmpstr[0] == '&') tmpstr[0] = '-';
1513                        }
1514                }
1515                else if(flag == 3)
1516                {
1517                        if(!isalnum(tmpstr[0]) && tmpstr[0] != '-')
1518                                tmpstr[0] = '_';
1519                }
1520                else
1521                {
1522                        if(!isalnum(tmpstr[0]) && tmpstr[0] != '-' && tmpstr[0] != '.')
1523                                tmpstr[0] = ' ';
1524                }
1525                tmpstr++;
1526        }
1527}
1528
1529void m_lock(pthread_mutex_t *mutex, int flag)
1530{
1531        switch(flag)
1532        {
1533                case 0: debug(900, "drawingmutex lock"); break;
1534                case 1: debug(900, "rectimermutex lock"); break;
1535                case 2: debug(900, "servicemutex lock"); break;
1536                case 3: debug(900, "vfdmutex lock"); break;
1537                case 4: debug(900, "epgmutex lock"); break;
1538                case 5: debug(900, "channelmutex lock"); break;
1539                case 6: debug(900, "timerthreadmutex lock"); break;
1540                case 7: debug(900, "audiotrackmutex lock"); break;
1541                case 8: debug(900, "subtitlemutex lock"); break;
1542                case 9: debug(900, "dmxdevmutex lock"); break;
1543                case 10: debug(900, "rcmutex lock"); break;
1544                case 11: debug(900, "queuemutex lock"); break;
1545                case 12: debug(900, "clistmutex lock"); break;
1546                case 13: debug(900, "hddmutex lock"); break;
1547                case 14: debug(900, "linkedchannelmutex lock"); break;
1548                case 15: debug(900, "tsseekmutex lock"); break;
1549                case 16: debug(900, "accelfbmutex lock"); break;
1550                case 17: debug(900, "mediadbmutex lock"); break;
1551                case 18: debug(900, "oldentrymutex lock"); break;
1552                case 19: debug(900, "newslettermutex lock"); break;
1553                case 20: debug(900, "tithekmutex lock"); break;
1554                case 21: debug(900, "inetworkmutex lock"); break;
1555                case 22: debug(900, "textboxmutex lock"); break;
1556                case 23: debug(900, "setaktresmutex lock"); break;
1557                case 24: debug(900, "waitrcmutex lock"); break;
1558                case 25: debug(900, "oledmutex lock"); break;
1559                default: debug(900, "unknown mutex lock"); break;
1560        }
1561        pthread_mutex_lock(mutex);
1562}
1563
1564void m_unlock(pthread_mutex_t *mutex, int flag)
1565{
1566        switch(flag)
1567        {
1568                case 0: debug(900, "drawingmutex unlock"); break;
1569                case 1: debug(900, "rectimermutex unlock"); break;
1570                case 2: debug(900, "servicemutex unlock"); break;
1571                case 3: debug(900, "vfdmutex unlock"); break;
1572                case 4: debug(900, "epgmutex unlock"); break;
1573                case 5: debug(900, "channelmutex unlock"); break;
1574                case 6: debug(900, "timerthreadmutex unlock"); break;
1575                case 7: debug(900, "audiotrackmutex unlock"); break;
1576                case 8: debug(900, "subtitlemutex unlock"); break;
1577                case 9: debug(900, "dmxdevmutex unlock"); break;
1578                case 10: debug(900, "rcmutex unlock"); break;
1579                case 11: debug(900, "queuemutex unlock"); break;
1580                case 12: debug(900, "clistmutex unlock"); break;
1581                case 13: debug(900, "hddmutex unlock"); break;
1582                case 14: debug(900, "linkedchannelmutex unlock"); break;
1583                case 15: debug(900, "tsseekmutex unlock"); break;
1584                case 16: debug(900, "accelfbmutex unlock"); break;
1585                case 17: debug(900, "mediadbmutex unlock"); break;
1586                case 18: debug(900, "oldentrymutex unlock"); break;
1587                case 19: debug(900, "newslettermutex unlock"); break;
1588                case 20: debug(900, "tithekmutex unlock"); break;
1589                case 21: debug(900, "inetworkmutex unlock"); break;
1590                case 22: debug(900, "textboxmutex unlock"); break;
1591                case 23: debug(900, "setaktresmutex unlock"); break;
1592                case 24: debug(900, "waitrcmutex unlock"); break;
1593                case 25: debug(900, "oledmutex unlock"); break;
1594                default: debug(900, "unknown mutex unlock"); break;
1595        }
1596        pthread_mutex_unlock(mutex);
1597}
1598
1599void debugstack(int sig, void* address, void* address1)
1600{
1601        Dl_info info, info1;
1602        void* trace[20];
1603        size_t size;
1604        size = backtrace(trace, 20);
1605
1606        FILE* fd = NULL;
1607        void* akttrace[2];
1608        int i = 0;
1609        char **strings;
1610        char **aktstring;
1611        time_t rawtime;
1612#ifdef BETA
1613        int y = 0;
1614        Dl_info info2;
1615#endif
1616
1617        char* imgversion = NULL;
1618        struct stimerthread* tnode = NULL;
1619
1620        if(isfile(getconfig("imagenamefile", NULL))     != 0)
1621                imgversion = readsys(getconfig("imagenamefile", NULL), 1);
1622        else
1623                imgversion = ostrcat("unknown", NULL, 0, 0);
1624
1625        tnode = gettimerbythread(pthread_self());
1626        if(tnode != NULL)
1627                dladdr(tnode->func, &info);
1628
1629        dladdr(address, &info1);
1630
1631        strings = backtrace_symbols(trace, size);
1632        akttrace[0] = (void*)address1;
1633        akttrace[1] = (void*)address;
1634        aktstring = backtrace_symbols(akttrace, 2); //get fault funktion name
1635
1636        fprintf(stderr, "--------------------------------------\n");
1637        fprintf(stderr, "Box: %s\n", getboxtype());
1638        fprintf(stderr, "Image: %s\n", imgversion);
1639        fprintf(stderr, "Signal: %d (%s)\n", sig, strsignal(sig));
1640        if(info1.dli_fname != NULL)
1641                fprintf(stderr, "Error in File: %s\n", info1.dli_fname);
1642        fprintf(stderr, "MainThread: %x\n", (unsigned int)status.mainthread);
1643        if(tnode != NULL && info.dli_sname != NULL)
1644                fprintf(stderr, "Error in Thread: %x (%s)\n", (unsigned int)pthread_self(), info.dli_sname);
1645        else
1646                fprintf(stderr, "Error in Thread: %x\n", (unsigned int)pthread_self());
1647       
1648        fprintf(stderr, "\nObtaining %zd stack frames:\n\n", size);
1649        for(i = 0; i < size; i++)
1650                fprintf(stderr, "%s\n", strings[i]);
1651
1652        fprintf(stderr, "\nLast functions:\n\n");
1653        fprintf(stderr, "%s\n", aktstring[0]);
1654        fprintf(stderr, "%s\n", aktstring[1]);
1655
1656#ifdef BETA     
1657        for(i = 0; i < MAXSTACKTRACE; i++)
1658        {
1659                if(stacktrace[i].pos > 0)
1660                {
1661                        fprintf(stderr, "\nObtaining %d Thread Stack frames [%p]:\n\n", stacktrace[i].pos, (void*)stacktrace[i].thread);
1662                        for(y = stacktrace[i].pos - 1; y >= 0; y--)
1663                        {
1664                                dladdr(stacktrace[i].func[y], &info2);
1665        fprintf(stderr, "%s(%s) [%p]\n", info2.dli_fname, info2.dli_sname, stacktrace[i].func[y]);
1666                        }
1667                }
1668        }
1669#endif
1670
1671        fprintf(stderr, "--------------------------------------\n");
1672
1673        char* logdir = ostrcat(getconfig("tracelog", NULL), NULL, 0, 0);
1674        if(logdir != NULL)
1675        {
1676                char* tmplogdir = dirname(logdir);
1677                if(tmplogdir != NULL)
1678                {
1679                        if(!file_exist(tmplogdir))   
1680                                mkdir(tmplogdir, 0777);
1681                }
1682                free(logdir); logdir = NULL;
1683        }
1684       
1685        //del file if greater 100KB
1686        if(getfilesize(getconfig("tracelog", NULL)) > 102400)
1687                unlink(getconfig("tracelog", NULL));
1688                       
1689        fd = fopen(getconfig("tracelog", NULL), "a");
1690        if(fd != NULL)
1691        {
1692                time(&rawtime);
1693                fprintf(fd, "Date: %s", ctime(&rawtime));
1694                fprintf(fd, "Box: %s\n", getboxtype());
1695                fprintf(fd, "Image: %s\n", imgversion);
1696                fprintf(fd, "Signal: %d (%s)\n", sig, strsignal(sig));
1697                if(info1.dli_fname != NULL)
1698                        fprintf(fd, "Error in File: %s\n", info1.dli_fname);
1699                fprintf(fd, "MainThread: %x\n", (unsigned int)status.mainthread);
1700                if(tnode != NULL && info.dli_sname != NULL)
1701                        fprintf(fd, "Error in Thread: %x (%s)\n", (unsigned int)pthread_self(), info.dli_sname);
1702                else
1703                        fprintf(fd, "Error in Thread: %x\n", (unsigned int)pthread_self());
1704                       
1705                fprintf(fd, "\nObtaining %zd stack frames:\n\n", size);
1706                for(i = 0; i < size; i++)
1707                        fprintf(fd, "%s\n", strings[i]);
1708                       
1709                fprintf(fd, "\nLast functions:\n\n");
1710                fprintf(fd, "%s\n", aktstring[0]);
1711                fprintf(fd, "%s\n", aktstring[1]);
1712
1713#ifdef BETA             
1714                for(i = 0; i < MAXSTACKTRACE; i++)
1715                {
1716                        if(stacktrace[i].pos > 0)
1717                        {
1718                                fprintf(fd, "\nObtaining %d Thread Stack frames [%p]:\n\n", stacktrace[i].pos, (void*)stacktrace[i].thread);
1719                                for(y = stacktrace[i].pos - 1; y >= 0; y--)
1720                                {
1721                                        dladdr(stacktrace[i].func[y], &info2);
1722                fprintf(fd, "%s(%s) [%p]\n", info2.dli_fname, info2.dli_sname, stacktrace[i].func[y]);
1723                                }
1724                        }
1725                }
1726#endif
1727               
1728                fprintf(fd, "--------------------------------------\n\n");
1729                fclose(fd);
1730                sync();
1731        }
1732        else
1733                perr("open %s", getconfig("tracelog", NULL));
1734
1735        free(imgversion);
1736        free(strings);
1737        free(aktstring);
1738}
1739
1740int mountauto(const char *file, const char *dir, unsigned long int flag, const void *data)
1741{
1742        int ret = -1;
1743        char* saveptr = NULL, *filesystems = NULL, *tmpfilesystems = NULL, *token = NULL;
1744
1745        filesystems = getconfig("filesystems", NULL);
1746        tmpfilesystems = ostrcat(tmpfilesystems, filesystems, 0, 0);
1747
1748        token = strtok_r(tmpfilesystems, ";", &saveptr);
1749        while(token != NULL)
1750        {
1751                ret = mount(file, dir, token, flag, data);
1752                if(ret == 0) break;
1753                token = strtok_r(NULL, ";", &saveptr);
1754        }
1755
1756        free(tmpfilesystems);
1757        return ret;
1758}
1759
1760int setwaswakuptimer(int value)
1761{
1762        char *waswakeuptimerdev = NULL, *tmpstr = NULL;
1763        int ret = 0;
1764
1765        waswakeuptimerdev = getconfig("waswakeuptimerdev", NULL);
1766
1767        if(waswakeuptimerdev != NULL)
1768        {
1769                debug(100, "set %s to %d", waswakeuptimerdev, value);
1770                tmpstr = oitoa(value);
1771                ret = writesys(waswakeuptimerdev, tmpstr, 0);
1772                free(tmpstr); tmpstr = NULL;
1773                return ret;
1774        }
1775
1776        return 0;
1777}
1778
1779int getwaswakuptimer()
1780{
1781        int ret = 0;
1782        char *waswakeuptimerdev = NULL;
1783        char *value = NULL;
1784
1785        waswakeuptimerdev = getconfig("waswakeuptimerdev", NULL);
1786
1787        if(waswakeuptimerdev == NULL)
1788        {
1789                err("NULL detect");
1790                return 0;
1791        }
1792
1793        value = readsys(waswakeuptimerdev, 1);
1794        if(value == NULL)
1795        {
1796                err("NULL detect");
1797                return 0;
1798        }
1799
1800        // start from timer
1801        if(atoi(value) == 1) ret = 1;
1802
1803        free(value);
1804        return ret;
1805}
1806
1807int getrtctime()
1808{
1809        int ret = 0;
1810        char *rtctimedev = NULL;
1811        char *value = NULL;
1812
1813        rtctimedev = getconfig("rtctimedev", NULL);
1814
1815        if(rtctimedev == NULL)
1816        {
1817                err("NULL detect");
1818                return 0;
1819        }
1820
1821        value = readsys(rtctimedev, 1);
1822        if(value == NULL)
1823        {
1824                err("NULL detect");
1825                return 0;
1826        }
1827
1828        // start from timer
1829        if(atoi(value) == 1) ret = 1;
1830
1831        free(value);
1832        return ret;
1833}
1834
1835void checkboxstart()
1836{
1837        struct rectimer* node = rectimer;
1838        int timediff = getconfigint("rectimer_timediff", NULL);
1839        int test = 0;
1840       
1841        if(node == NULL)
1842        {
1843                setcecstandby(0);
1844                return;                                                 //no record
1845        }
1846
1847        while(node != NULL)
1848        {
1849                if(node->status < 2 || node->status == 4)
1850                {
1851                        time_t akttime = time(NULL);
1852                        time_t begin = node->begin - getconfigint("wakeuptimerdevdiff", NULL);
1853
1854                        debug(400, "begin=%ld akttime1=%ld akttime2=%ld", begin, akttime - timediff, akttime + timediff);
1855
1856                        if(begin > akttime - timediff && begin < akttime + timediff)
1857                        {
1858                                debug(400, "found rectimer who has start the box");
1859                                setwaswakuptimer(1);
1860                                setcecstandby(1);
1861                                test = 1;
1862                        }
1863                }
1864                node = node->next;
1865        }
1866        if(test == 0)
1867                setcecstandby(0);
1868}
1869
1870int setwakeuptimerdev(time_t value)
1871{
1872        char* wakeuptimerdev, *tmpstr = NULL;
1873        int ret = 0;
1874        int diff = getconfigint("wakeuptimerdevdiff", NULL);
1875
1876        if(value != 0x7FFFFFFF && value - diff > time(NULL))
1877                value -= diff;
1878
1879        wakeuptimerdev = getconfig("wakeuptimerdev", NULL);
1880
1881        if(wakeuptimerdev != NULL && value >= time(NULL))
1882        {
1883                debug(10, "set %s to %ld", wakeuptimerdev, value);
1884                tmpstr = olutoa(value);
1885                ret = writesys(wakeuptimerdev, tmpstr, 0);
1886                free(tmpstr); tmpstr = NULL;
1887                return ret;
1888        }
1889
1890        return 0;
1891}
1892
1893int setrtctime(int value)
1894{
1895        char *rtctimedev = NULL, *tmpstr = NULL;
1896        int ret = 0;
1897
1898        value += 7200;
1899
1900        rtctimedev = getconfig("rtctimedev", NULL);
1901
1902        if(rtctimedev != NULL)
1903        {
1904                debug(10, "set %s to %d", rtctimedev, value);
1905                tmpstr = oitoa(value);
1906                ret = writesys(rtctimedev, tmpstr, 0);
1907                free(tmpstr); tmpstr = NULL;
1908                return ret;
1909        }
1910
1911        return 0;
1912}
1913
1914int changepolicy()
1915{
1916        char *tmppolicy = NULL, *tmpstr = NULL;
1917
1918        tmppolicy = getpolicy();
1919
1920#ifdef MIPSEL
1921/*
1922        char *aspect = NULL;
1923        aspect = getaspect();
1924
1925        if(!ostrncmp("16:9", aspect, 4))
1926        {
1927                if(ostrcmp(_("16:9_set_bestfit_to_policy_show_justscale"), _(tmppolicy)) == 0)
1928                        tmpstr = ostrcat(_("16:9_set_letterbox_to_policy_show_panscan"), NULL, 0, 0);
1929                else if(ostrcmp(_("16:9_set_letterbox_to_policy_show_panscan"), _(tmppolicy)) == 0)
1930                        tmpstr = ostrcat(_("16:9_set_panscan_to_policy_show_pillarbox"), NULL, 0, 0);
1931                else if(ostrcmp(_("16:9_set_panscan_to_policy_show_pillarbox"), _(tmppolicy)) == 0)
1932                        tmpstr = ostrcat(_("16:9_set_bestfit_to_policy_show_justscale"), NULL, 0, 0);
1933
1934        }
1935        else
1936        {
1937                if(ostrcmp(_("4:3_set_bestfit_to_policy2_show_justscale"), _(tmppolicy)) == 0)
1938                        tmpstr = ostrcat(_("4:3_set_letterbox_to_policy2_show_letterbox"), NULL, 0, 0);
1939                else if(ostrcmp(_("4:3_set_letterbox_to_policy2_show_letterbox"), _(tmppolicy)) == 0)
1940                        tmpstr = ostrcat(_("4:3_set_panscan_to_policy2_show_panscan"), NULL, 0, 0);
1941                else if(ostrcmp(_("4:3_set_panscan_to_policy2_show_panscan"), _(tmppolicy)) == 0)
1942                        tmpstr = ostrcat(_("4:3_set_bestfit_to_policy2_show_justscale"), NULL, 0, 0);
1943        }
1944        free(aspect), aspect = NULL;
1945*/
1946        if(ostrcmp(_("16:9_set_bestfit_to_policy_show_justscale"), _(tmppolicy)) == 0)
1947                tmpstr = ostrcat(_("16:9_set_letterbox_to_policy_show_panscan"), NULL, 0, 0);
1948        else if(ostrcmp(_("16:9_set_letterbox_to_policy_show_panscan"), _(tmppolicy)) == 0)
1949                tmpstr = ostrcat(_("16:9_set_panscan_to_policy_show_pillarbox"), NULL, 0, 0);
1950        else if(ostrcmp(_("16:9_set_panscan_to_policy_show_pillarbox"), _(tmppolicy)) == 0)
1951                tmpstr = ostrcat(_("4:3_set_bestfit_to_policy2_show_justscale"), NULL, 0, 0);
1952        else if(ostrcmp(_("4:3_set_bestfit_to_policy2_show_justscale"), _(tmppolicy)) == 0)
1953                tmpstr = ostrcat(_("4:3_set_letterbox_to_policy2_show_letterbox"), NULL, 0, 0);
1954        else if(ostrcmp(_("4:3_set_letterbox_to_policy2_show_letterbox"), _(tmppolicy)) == 0)
1955                tmpstr = ostrcat(_("4:3_set_panscan_to_policy2_show_panscan"), NULL, 0, 0);
1956        else if(ostrcmp(_("4:3_set_panscan_to_policy2_show_panscan"), _(tmppolicy)) == 0)
1957                tmpstr = ostrcat(_("16:9_set_bestfit_to_policy_show_justscale"), NULL, 0, 0);
1958#else
1959        if(!ostrncmp("letterbox", tmppolicy, 8))
1960                tmpstr = ostrcat(tmpstr, "panscan", 1, 0);
1961        else if(!ostrncmp("panscan", tmppolicy, 7))
1962                tmpstr = ostrcat(tmpstr, "non", 1, 0);
1963        else if(!ostrncmp("non", tmppolicy, 3))
1964                tmpstr = ostrcat(tmpstr, "bestfit", 1, 0);
1965        else if(!ostrncmp("bestfit", tmppolicy, 7))
1966                tmpstr = ostrcat(tmpstr, "letterbox", 1, 0);
1967#endif
1968
1969        setpolicy(tmpstr);
1970
1971        free(tmpstr); tmpstr = NULL;
1972        free(tmppolicy); tmppolicy = NULL;
1973        return 0;
1974}
1975
1976char* getmac(char* dev)
1977{
1978        int sock = -1, i = 0;
1979        struct ifreq buf;
1980        char* tmpstr = NULL;
1981        char tmpbuf[4] = {};
1982
1983        if(dev == NULL) return NULL;
1984
1985        sock = socket(PF_INET, SOCK_DGRAM, 0);
1986        if(sock >= 0)
1987        {
1988                memset(&buf, 0x00, sizeof(buf));
1989                strcpy(buf.ifr_name, dev);
1990                ioctl(sock, SIOCGIFHWADDR, &buf);
1991                close(sock);
1992
1993                for(i = 0; i < 6; i++)
1994                {
1995                        snprintf(tmpbuf, 4, "%.2X:", (unsigned char)buf.ifr_hwaddr.sa_data[i]);
1996                        tmpstr = ostrcat(tmpstr, tmpbuf, 1, 0);
1997                }
1998                if(tmpbuf != NULL)
1999                        tmpstr[strlen(tmpstr) - 1] = '\0';
2000        }
2001
2002        return tmpstr;
2003}
2004
2005char* getdefaultgw()
2006{
2007        char* name = NULL;
2008        unsigned long def, gw, m;
2009        int flags, ref, use, metric, mtu, win, ir;
2010        struct in_addr ip;
2011
2012        FILE *fd = fopen("/proc/net/route", "r");
2013        if(fd == NULL)
2014                return NULL;
2015
2016        if(fscanf(fd, "%*[^\n]\n") < 0)
2017        {
2018                fclose(fd);
2019                return NULL;
2020        }
2021
2022        name = malloc(64);
2023        if(name == NULL)
2024        {
2025                err("no mem");
2026                return NULL;
2027        }
2028
2029        while(1)
2030        {
2031                int r = fscanf(fd, "%63s%lx%lx%x%d%d%d%lx%d%d%d\n", name, &def, &gw, &flags, &ref, &use, &metric, &m, &mtu, &win, &ir);
2032                if(def == 0) break;
2033                if(r != 11)
2034                {
2035                        fclose(fd);
2036                        free(name);
2037                        return NULL;
2038                }
2039                if(!(flags & 0x0001)) continue; //is interface down
2040        }
2041
2042        fclose(fd);
2043        free(name);
2044
2045        ip.s_addr = gw;
2046        if(def == 0)
2047                return inet_ntoa(ip);
2048
2049        return NULL;
2050}
2051
2052char* changefilenameext(char* filename, char* ext)
2053{
2054        char* newfilename = NULL;
2055        char* zeichen = NULL;
2056
2057        if(filename == NULL) return NULL;
2058
2059        newfilename = ostrcat(filename, NULL, 0, 0);
2060
2061        zeichen = strrchr(newfilename, '.');
2062        if(zeichen != NULL)
2063        {
2064                zeichen[0] = '\0';
2065                newfilename = ostrcat(newfilename, ext, 1, 0);
2066        }
2067
2068        return newfilename;
2069}
2070
2071int cmpfilenameext(char* filename, char* ext)
2072{
2073        char* zeichen = NULL;
2074
2075        if(filename == NULL) return 1;
2076
2077        zeichen = strrchr(filename, '.');
2078        if(zeichen != NULL)
2079        {
2080                if(ostrcasecmp(zeichen, ext) == 0) return 0;
2081        }
2082
2083        return 1;
2084}
2085
2086char* getfilenameext(char* filename)
2087{
2088        char* zeichen = NULL;
2089
2090        if(filename == NULL) return NULL;
2091
2092        zeichen = strrchr(filename, '.');
2093        if(zeichen != NULL)
2094        {
2095                return ostrcat(zeichen + 1, NULL, 0, 0);
2096        }
2097
2098        return NULL;
2099}
2100
2101char* getfilenamepng(char* filename)
2102{
2103        char* tmpstr = NULL;
2104
2105        if(filename == NULL) return NULL;
2106       
2107        if(!filelistflt(".avi .dat .divx .flv .mkv .m4v .mp4 .mov .mpg .mpeg .mts .m2ts .trp .ts .vdr .vob .wmv .rm", filename))
2108                tmpstr = ostrcat("movie", NULL, 0, 0);
2109        else if(!filelistflt(".mp3 .flac .ogg .wma .ra .wav", filename))
2110                tmpstr = ostrcat("musik", NULL, 0, 0);
2111        else if(!filelistflt(".jpg .png", filename))
2112                tmpstr = ostrcat("picture", NULL, 0, 0);
2113        else
2114                tmpstr = ostrcat(getfilenameext(filename), NULL, 1, 0);
2115
2116        if(tmpstr != NULL)
2117        {
2118                return tmpstr;
2119        }
2120
2121        return NULL;
2122}
2123
2124char* getcurrentdir(char* path)
2125{
2126        char* zeichen = NULL;
2127
2128        if(path == NULL) return NULL;
2129
2130        zeichen = strrchr(path, '/');
2131        if(zeichen != NULL)
2132        {
2133                return ostrcat(zeichen + 1, NULL, 0, 0);
2134        }
2135
2136        return NULL;
2137}
2138
2139char* convert_timesec(int sec)
2140{
2141        int hour = 0, min = 0, seconds = 0;
2142        char* buf = NULL;
2143
2144        buf = malloc(9);
2145        if(buf == NULL)
2146        {
2147                err("no mem");
2148                return NULL;
2149        }
2150
2151        seconds = sec % 60;
2152        min = (sec / 60) % 60;
2153        hour = sec / 3600;
2154
2155        if(seconds < 0) seconds = 0;
2156        if(min < 0) min = 0;
2157        if(hour < 0 || hour > 23)
2158        {
2159                hour = 0;
2160                min = 0;
2161                seconds = 0;
2162        }
2163
2164        snprintf(buf, 9, "%02d:%02d:%02d", hour, min, seconds);
2165
2166        return buf;
2167}
2168
2169char* convert_dtimesec(int sec)
2170{
2171        int hour = 0, min = 0, seconds = 0, days = 0;
2172        char* buf = NULL;
2173
2174        buf = malloc(12);
2175        if(buf == NULL)
2176        {
2177                err("no mem");
2178                return NULL;
2179        }
2180
2181        seconds = sec % 60;
2182        min = (sec / 60) % 60;
2183        hour = sec / 3600;
2184        if(hour > 23)
2185        {
2186                days = hour / 24;
2187                hour = hour % 24;
2188        }
2189
2190        if(seconds < 0) seconds = 0;
2191        if(min < 0) min = 0;
2192        if(hour < 0/* || hour > 23*/)
2193        {
2194                hour = 0;
2195                min = 0;
2196                seconds = 0;
2197        }
2198
2199        snprintf(buf, 12, "%02d_%02d:%02d:%02d",days, hour, min, seconds);
2200
2201        return buf;
2202}
2203
2204int checkdate()
2205{
2206        time_t dvbtime = 0;
2207
2208        if(time(NULL) < 1072224000 || status.timeupdatecount > 3600) // 01.01.2004
2209        {
2210                if(dvbgetdate(&dvbtime, 10000000) == 0) //10 sek
2211                {
2212                        setsystime(&dvbtime);
2213#ifdef MIPSEL
2214                        setrtctimemips();
2215#endif
2216                        status.timeupdatecount = 0;
2217                        return 0;
2218                }
2219                return 1;
2220        }
2221        return 0;
2222}
2223
2224void closeonexec(int fd)
2225{
2226        if(fd > -1)
2227                fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
2228}
2229
2230void startnewsletter(flag)
2231{
2232        if(flag == 1)
2233        {
2234                if(getconfigint("newsletter", NULL) == 1 && status.newsletterthread == NULL)
2235                        status.newsletterthread = addtimer(&newsletterthreadfunc, START, 1000, 1, NULL, NULL, NULL);
2236        }
2237        else if(status.newsletterthread != NULL)
2238        {
2239                status.newsletterthread->aktion = STOP;
2240                status.newsletterthread = NULL;
2241        }
2242}
2243
2244void startthumb(flag)
2245{
2246        if(flag == 1)
2247        {
2248                if(getconfigint("createthumb", NULL) == 1 && status.thumbthread == NULL)
2249                        status.thumbthread = addtimer(&thumbthread, START, 1000, 1, NULL, NULL, NULL);
2250        }
2251        else if(status.thumbthread != NULL)
2252        {
2253                status.thumbthread->aktion = STOP;
2254                status.thumbthread = NULL;
2255        }
2256}
2257
2258void starthttpd(flag)
2259{
2260        if(flag == 1)
2261        {
2262                if(getconfigint("httpdstart", NULL) == 1 && status.httpthread == NULL)
2263                {
2264                        status.httpthread = addtimer(&httpdthreadfunc, START, 10000, -1, NULL, NULL, NULL);
2265                        if(status.httpthread != NULL)
2266                                status.httpthread->flag = setbit(status.httpthread->flag, 0);
2267                }
2268        }
2269        else if(status.httpthread != NULL)
2270        {
2271                status.httpthread->aktion = STOP;
2272                status.httpthread = NULL;
2273        }
2274}
2275
2276void startrguid(flag)
2277{
2278        if(flag == 1)
2279        {
2280                if(getconfigint("rguidstart", NULL) == 1 && status.rguithread == NULL)
2281                {
2282                        status.rguithread = addtimer(&rguidthreadfunc, START, 10000, -1, NULL, NULL, NULL);
2283                        if(status.rguithread != NULL)
2284                                status.rguithread->flag = setbit(status.rguithread->flag, 0);
2285                }
2286        }
2287        else if(status.rguithread != NULL)
2288        {
2289                status.rguithread->aktion = STOP;
2290                status.rguithread = NULL;
2291        }
2292}
2293
2294int setoverclockfreq(int mode)
2295{
2296        // mode 0 = Set Standby Freq
2297        // mode 1 = Set Freq
2298
2299        if(ostrcmp(getownconfig("standby_overclock"), "0") == 0)
2300                return 1;
2301
2302        if(isfile("/proc/cpu_frequ/pll0_ndiv_mdiv") == 0)
2303                return 1;
2304
2305        char* tmpstr = NULL;
2306
2307        if(mode == 0)
2308                tmpstr = getownconfig("standby_freq");
2309        else
2310                tmpstr = getownconfig("pll0_ndiv_mdiv");
2311
2312        return writesys("/proc/cpu_frequ/pll0_ndiv_mdiv", tmpstr, 1);
2313}
2314
2315/*
2316int checkdev(char* dev)
2317{
2318        char* cmd = NULL;
2319        cmd = ostrcat(cmd, "cat ", 1, 0);
2320        cmd = ostrcat(cmd, dev, 1, 0);
2321
2322        char* tmpstr = NULL;
2323        tmpstr = string_newline(command(cmd));
2324        free(cmd), cmd = NULL;
2325
2326        if(tmpstr == NULL)
2327        {
2328                return 0;
2329        }
2330
2331        if(ostrcmp(tmpstr, "Segmentation fault") == 0)
2332        {
2333                return 0;
2334        }
2335       
2336        free(tmpstr), tmpstr = NULL;
2337        return 1;
2338}
2339*/
2340
2341int setsaturation(int value)
2342{
2343        char* saturationdev;
2344
2345        saturationdev = getconfig("saturationdev", NULL);
2346
2347        if(saturationdev != NULL /*&& checkdev(saturationdev)*/)
2348        {
2349                debug(100, "set %s to %d", saturationdev, value);
2350                return writesysint(saturationdev, value, 1);
2351        }
2352
2353        return 0;
2354}
2355
2356int setbrightness(int value)
2357{
2358        char* brightnessdev;
2359
2360        brightnessdev = getconfig("brightnessdev", NULL);
2361
2362        if(brightnessdev != NULL /*&& checkdev(brightnessdev)*/)
2363        {
2364                debug(100, "set %s to %d", brightnessdev, value);
2365                return writesysint(brightnessdev, value, 1);
2366        }
2367
2368        return 0;
2369}
2370
2371int setcontrast(int value)
2372{
2373        char* contrastdev;
2374
2375        contrastdev = getconfig("contrastdev", NULL);
2376
2377        if(contrastdev != NULL /*&& checkdev(contrastdev)*/)
2378        {
2379                debug(100, "set %s to %d", contrastdev, value);
2380                return writesysint(contrastdev, value, 1);
2381        }
2382
2383        return 0;
2384}
2385
2386int settint(int value)
2387{
2388        char* tintdev;
2389
2390        tintdev = getconfig("tintdev", NULL);
2391
2392        if(tintdev != NULL /*&& checkdev(tintdev)*/)
2393        {
2394                debug(100, "set %s to %d", tintdev, value);
2395                return writesysint(tintdev, value, 1);
2396        }
2397
2398        return 0;
2399}
2400
2401int ozip(char* inbuf, int inlen, char** outbuf, int* outlen, int level)
2402{
2403        int ret = 0;
2404        z_stream stream;
2405       
2406        if(inbuf == NULL || outbuf == NULL || inlen == 0) return 1;
2407
2408        stream.zalloc = Z_NULL;
2409        stream.zfree = Z_NULL;
2410        stream.opaque = Z_NULL;
2411        stream.avail_in = 0;
2412        stream.next_in = Z_NULL;
2413        stream.avail_out = 0;
2414        stream.next_out = Z_NULL;
2415
2416        ret = deflateInit(&stream, level);
2417        if(ret != Z_OK)
2418                return 1;
2419
2420        *outbuf = malloc(inlen);
2421        if(*outbuf == NULL)
2422        {
2423                err("no mem");
2424                (void)deflateEnd(&stream);
2425                return 1;
2426        }
2427
2428        stream.avail_in = inlen;
2429        stream.next_in = (void*)inbuf;
2430 
2431        do
2432        {
2433                stream.avail_out = inlen;
2434                stream.next_out = (void*)*outbuf;
2435
2436                ret = deflate(&stream, Z_FINISH);
2437                if(ret == Z_STREAM_ERROR)
2438                {
2439                        free(*outbuf); *outbuf = NULL;
2440                        (void)deflateEnd(&stream);
2441                        return 1;
2442                }
2443        }
2444        while(stream.avail_out == 0);
2445       
2446        char* tmpoutbuf = *outbuf;     
2447        *outlen = inlen - stream.avail_out;
2448        *outbuf = realloc(*outbuf, *outlen);
2449        if(*outbuf == NULL)
2450        {
2451                if(*outlen > 0)
2452                {
2453                        err("no mem"); 
2454                        free(tmpoutbuf);
2455                }
2456                *outlen = 0;
2457                (void)deflateEnd(&stream);
2458                return 1;
2459        }       
2460
2461        (void)deflateEnd(&stream);
2462        return 0;
2463}
2464
2465//flag 0: malloc mem
2466//flag 1: don't malloc mem
2467//flag 2: malloc mem (gzip)
2468//flag 3: don't malloc mem (gzip)
2469int ounzip(char* inbuf, int inlen, char** outbuf, int* outlen, int maxbuf, int flag)
2470{
2471        int ret = 0;
2472        z_stream stream;
2473       
2474        if(inbuf == NULL || outbuf == NULL || maxbuf == 0) return 1;
2475        if(flag == 1 && *outbuf == NULL) return 1;
2476
2477        stream.zalloc = Z_NULL;
2478        stream.zfree = Z_NULL;
2479        stream.opaque = Z_NULL;
2480        stream.avail_in = 0;
2481        stream.next_in = Z_NULL;
2482        stream.avail_out = 0;
2483        stream.next_out = Z_NULL;
2484
2485        if(flag == 2 || flag == 3)
2486        {
2487                ret = inflateInit2(&stream, 16 + MAX_WBITS);
2488                if(flag == 2) flag = 0;
2489                else if(flag == 3) flag = 1;
2490        }
2491        else
2492                ret = inflateInit(&stream);
2493        if(ret != Z_OK)
2494                return 1;
2495
2496        if(flag == 0)
2497        {
2498                *outbuf = malloc(maxbuf);
2499                if(*outbuf == NULL)
2500                {
2501                        err("no mem");
2502                        (void)inflateEnd(&stream);
2503                        return 1;
2504                }
2505        }
2506
2507        stream.avail_in = inlen;
2508        stream.next_in = (void*)inbuf;
2509
2510        int round = 0;
2511        do
2512        {
2513                stream.avail_out = maxbuf;
2514               
2515                if(flag == 0 && round > 0)
2516                {
2517                        char* tmpoutbuf = *outbuf;
2518                        *outbuf = realloc(*outbuf, maxbuf * (round + 1));
2519                        if(*outbuf == NULL)
2520                        {
2521                                err("no mem");
2522                                free(tmpoutbuf);
2523                                (void)inflateEnd(&stream);
2524                                return 1;
2525                        }
2526                        stream.next_out = (void*)(*outbuf) + maxbuf * round;
2527                }
2528                else
2529                        stream.next_out = (void*)*outbuf;
2530
2531                ret = inflate(&stream, Z_NO_FLUSH);
2532                if(ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR)
2533                {
2534                        if(flag == 0)
2535                        {
2536                                free(*outbuf);
2537                                *outbuf = NULL;
2538                        }                               
2539                        (void)inflateEnd(&stream);
2540                        return 1;
2541                }
2542
2543                round++;
2544        }
2545        while(stream.avail_out == 0);
2546       
2547        if(flag == 0)
2548        {
2549                char* tmpoutbuf = *outbuf;
2550                *outlen = (maxbuf * round) - stream.avail_out;
2551                *outbuf = realloc(*outbuf, *outlen);
2552                if(*outbuf == NULL)
2553                {
2554                        err("no mem");
2555                        if(*outlen > 0) free(tmpoutbuf);
2556                        *outlen = 0;
2557                        (void)inflateEnd(&stream);
2558                        return 1;
2559                }       
2560        }
2561        else
2562                *outlen = maxbuf - stream.avail_out;                   
2563
2564        (void)inflateEnd(&stream);
2565        return 0;
2566}
2567
2568void endianswapshort(unsigned short* value)
2569{
2570        *value = (*value >> 8) | (*value << 8);
2571}
2572
2573int rcnumber(int rcret)
2574{
2575        if(rcret == getrcconfigint("rc0", NULL) ||
2576        rcret == getrcconfigint("rc1", NULL) ||
2577        rcret == getrcconfigint("rc2", NULL) ||
2578        rcret == getrcconfigint("rc3", NULL) ||
2579        rcret == getrcconfigint("rc4", NULL) ||
2580        rcret == getrcconfigint("rc5", NULL) ||
2581        rcret == getrcconfigint("rc6", NULL) ||
2582        rcret == getrcconfigint("rc7", NULL) ||
2583        rcret == getrcconfigint("rc8", NULL) ||
2584        rcret == getrcconfigint("rc9", NULL))
2585                return rcret;
2586        else
2587                return -9999;
2588}
2589
2590int checkdirext(char* dir, char* ext)
2591{
2592        struct dirent *dirent = NULL;
2593        DIR *pdir = NULL;
2594
2595        pdir = opendir(dir);
2596        if(pdir != NULL)
2597        {
2598                while((dirent = readdir(pdir)) != NULL)
2599                {
2600                        if(ostrcmp(".", dirent->d_name) != 0 && ostrcmp("..", dirent->d_name) != 0)
2601                        {
2602                                if(ostrstr(dirent->d_name, ext) != NULL)
2603                                {
2604                                        closedir(pdir);
2605                                        return 1;
2606                                }
2607                        }
2608                }
2609
2610                closedir(pdir);
2611        }
2612
2613        return 0;
2614}
2615
2616int delallfiles(char* dir, char* ext)
2617{
2618        struct dirent *dirent = NULL;
2619        DIR *pdir = NULL;
2620        char* tmpstr = NULL;
2621
2622        pdir = opendir(dir);
2623        if(pdir != NULL)
2624        {
2625                while((dirent = readdir(pdir)) != NULL)
2626                {
2627                        if(ostrcmp(".", dirent->d_name) != 0 && ostrcmp("..", dirent->d_name) != 0)
2628                        {
2629                                if(ext == NULL || ostrstr(dirent->d_name, ext) != NULL)
2630                                {
2631                                        tmpstr = ostrcat(dir, "/", 0, 0);
2632                                        tmpstr = ostrcat(tmpstr, dirent->d_name, 1, 0);
2633                                        unlink(tmpstr);
2634                                        free(tmpstr); tmpstr = NULL;
2635                                }
2636                        }
2637                }
2638
2639                closedir(pdir);
2640        }
2641
2642        return 0;
2643}
2644
2645unsigned long getfilecount(char* dir)
2646{
2647        unsigned long count = 0;
2648        struct dirent *dirent = NULL;
2649        DIR *pdir = NULL;
2650
2651        pdir = opendir(dir);
2652        if(pdir != NULL)
2653        {
2654                while((dirent = readdir(pdir)) != NULL)
2655                        count++;
2656
2657                closedir(pdir);
2658        }
2659
2660        return count;
2661}
2662
2663int getfiletype(char* filename)
2664{
2665        struct stat64 s;
2666
2667        if(filename == NULL) return -1;
2668
2669        if(stat64(filename, &s) >= 0)
2670        {
2671                if(S_ISREG(s.st_mode)) return DT_REG;
2672                if(S_ISDIR(s.st_mode)) return DT_DIR;
2673                if(S_ISCHR(s.st_mode)) return DT_CHR;
2674                if(S_ISBLK(s.st_mode)) return DT_BLK;
2675                if(S_ISFIFO(s.st_mode)) return DT_FIFO;
2676                if(S_ISLNK(s.st_mode)) return DT_LNK;
2677        }
2678
2679        return DT_UNKNOWN;
2680}
2681
2682int getlfiletype(char* filename)
2683{
2684        struct stat64 s;
2685
2686        if(filename == NULL) return -1;
2687
2688        if(lstat64(filename, &s) >= 0)
2689        {
2690                if(S_ISREG(s.st_mode)) return DT_REG;
2691                if(S_ISDIR(s.st_mode)) return DT_DIR;
2692                if(S_ISCHR(s.st_mode)) return DT_CHR;
2693                if(S_ISBLK(s.st_mode)) return DT_BLK;
2694                if(S_ISFIFO(s.st_mode)) return DT_FIFO;
2695                if(S_ISLNK(s.st_mode)) return DT_LNK;
2696        }
2697
2698        return DT_UNKNOWN;
2699}
2700
2701unsigned long long getfullspace(char* dir)
2702{
2703        struct statfs64 s;
2704        unsigned long long fullsize = 0;
2705
2706        if(statfs64(dir, &s) >= 0)
2707        {
2708                fullsize = s.f_blocks;
2709                fullsize *= s.f_bsize;
2710        }
2711
2712        return fullsize;
2713}
2714
2715unsigned long long getfreespace(char* dir)
2716{
2717        struct statfs64 s;
2718        unsigned long long freesize = 0;
2719
2720        if(statfs64(dir, &s) >= 0)
2721        {
2722                freesize = s.f_bfree;
2723                freesize *= s.f_bsize;
2724        }
2725
2726        return freesize;
2727}
2728
2729int checkbit(int value, int bitpos)
2730{
2731        int ret = 0;
2732
2733        ret = value & (1 << bitpos) ? 1: 0;
2734        return ret;
2735}
2736
2737int tooglebit(int value, int bitpos)
2738{
2739        value ^= 1 << bitpos;
2740        return value;
2741}
2742
2743int clearbit(int value, int bitpos)
2744{
2745        value &= ~(1 << bitpos);
2746        return value;
2747}
2748
2749int setbit(int value, int bitpos)
2750{
2751        value |= 1 << bitpos;
2752        return value;
2753}
2754
2755int setsystime(time_t* newtime)
2756{
2757        if (stime(newtime))
2758        {
2759                err("can't set system time");
2760                return 1;
2761        }
2762        return 0;
2763}
2764
2765off64_t getfilesize(char* name)
2766{
2767        struct stat64 sbuf;
2768
2769        if(lstat64(name, &sbuf) == -1)
2770                return 0;
2771
2772        return sbuf.st_size;
2773}
2774
2775time_t getfiletime(char* name, int type)
2776{
2777        struct stat64 sbuf;
2778
2779        if(lstat64(name, &sbuf) == -1)
2780                return 0;
2781
2782        switch(type)
2783        {
2784                case 1: return sbuf.st_mtime;
2785                case 2: return sbuf.st_ctime;
2786                default: return sbuf.st_atime;
2787        }
2788}
2789
2790int isfile(char* name)
2791{
2792        struct stat64 sbuf;
2793
2794        if(lstat64(name, &sbuf) == -1)
2795                return 0;
2796
2797        if(S_ISREG(sbuf.st_mode))
2798                return 1;
2799
2800        return 0;
2801}
2802
2803int isdir(char* name)
2804{
2805        struct stat64 sbuf;
2806        char *rpath = NULL;
2807
2808        if(lstat64(name, &sbuf) == -1)
2809                return 0;
2810
2811        if(S_ISDIR(sbuf.st_mode))
2812                return 1;
2813
2814        if(S_ISLNK(sbuf.st_mode))
2815        {
2816                rpath = realpath(name, NULL);
2817                if(lstat64(rpath, &sbuf) == -1)
2818                {
2819                        free(rpath);
2820                        return 0;
2821                }
2822                free(rpath);
2823                if(S_ISDIR(sbuf.st_mode))
2824                        return 1;
2825        }
2826
2827        return 0;
2828}
2829
2830char* getmaxsatstring(int maxsat)
2831{
2832        char* tmpstr = NULL, *tmpnr = NULL;
2833        int i;
2834
2835        tmpstr = ostrcat(tmpstr, "1", 1, 0);
2836        for(i = 2; i <= maxsat; i++)
2837        {
2838                tmpnr = oitoa(i);
2839                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
2840                tmpstr = ostrcat(tmpstr, tmpnr, 1, 1);
2841        }
2842        return tmpstr;
2843}
2844
2845void ostrcatbig(char** value1, char* value2, int* maxlen, int* pos)
2846{
2847        int len = 0;
2848
2849        if(value2 == NULL)
2850                return;
2851
2852        len = strlen(value2) + 1;
2853
2854        if(*value1 != NULL && maxlen == 0)
2855                *maxlen = strlen(*value1);
2856
2857        if(*value1 == NULL || *pos + len > *maxlen)
2858        {
2859                *maxlen = *maxlen + len + (MINMALLOC * 10);
2860                *value1 = realloc(*value1, *maxlen);
2861                if(*value1 == NULL)
2862                {
2863                        err("no mem");
2864                        return;
2865                }
2866        }
2867
2868        memcpy(*value1 + *pos, value2, len);
2869        *pos = *pos + (len - 1);
2870}
2871
2872char* ostrshrink(char* value)
2873{
2874        int len = 0;
2875        char* buf = NULL;
2876
2877        if(value == NULL) return NULL;
2878
2879        len = strlen(value);
2880        if(len == 0) return value;
2881
2882        buf = malloc(len + 1);
2883        if(buf == NULL)
2884                return value;
2885
2886        memcpy(buf, value, len);
2887        free(value);
2888        buf[len] = '\0';
2889
2890        return buf;
2891}
2892
2893char* ostrcat(char* value1, char* value2, int free1, int free2)
2894{
2895        int len = 0, len1 = 0, len2 = 0;
2896        char* buf = NULL;
2897
2898        if(value1 == NULL && value2 == NULL) return NULL;
2899
2900        if(value1 != NULL) len1 = strlen(value1);
2901        if(value2 != NULL) len2 = strlen(value2);
2902
2903        len = len1 + len2 + 1;
2904
2905        if(free1 == 1)
2906                buf = realloc(value1, len);
2907        else
2908                buf = malloc(len);
2909        if(buf == NULL)
2910        {
2911                if(free1 == 1) free(value1);
2912                if(free2 == 1) free(value2);
2913                return NULL;
2914        }
2915
2916        if(free1 == 0 && len1 > 0) memcpy(buf, value1, len1);
2917        if(len2 > 0) memcpy(buf + len1, value2, len2);
2918        buf[len - 1] = '\0';
2919
2920        if(free2 == 1) free(value2);
2921
2922        //helpfull for memleak detect
2923        //if(buf != NULL && strlen(buf) == 0x0b - 0x01)
2924        //      printf("******** memleak string (%s) (%p) ********\n", buf, buf);
2925
2926        return buf;
2927}
2928
2929char* ollutoa(uint64_t value)
2930{
2931        char *buf = NULL;
2932
2933        buf = malloc(MINMALLOC);
2934        if(buf == NULL)
2935        {
2936                err("no mem");
2937                return NULL;
2938        }
2939
2940        sprintf(buf, "%llu", value);
2941        buf = ostrshrink(buf);
2942
2943        return buf;
2944}
2945
2946char* olutoa(unsigned long value)
2947{
2948        char *buf = NULL;
2949
2950        buf = malloc(MINMALLOC);
2951        if(buf == NULL)
2952        {
2953                err("no mem");
2954                return NULL;
2955        }
2956
2957        sprintf(buf, "%lu", value);
2958        buf = ostrshrink(buf);
2959
2960        return buf;
2961}
2962
2963char* oitoax(int value)
2964{
2965        char *buf = NULL;
2966
2967        buf = malloc(MINMALLOC);
2968        if(buf == NULL)
2969        {
2970                err("no mem");
2971                return NULL;
2972        }
2973
2974        sprintf(buf, "%x", value);
2975        buf = ostrshrink(buf);
2976
2977        return buf;
2978}
2979
2980char* oitoa(int value)
2981{
2982        char *buf = NULL;
2983
2984        buf = malloc(MINMALLOC);
2985        if(buf == NULL)
2986        {
2987                err("no mem");
2988                return NULL;
2989        }
2990
2991        sprintf(buf, "%d", value);
2992        buf = ostrshrink(buf);
2993
2994        return buf;
2995}
2996
2997char* oitoa64(off64_t value)
2998{
2999        char *buf = NULL;
3000
3001        buf = malloc(MINMALLOC);
3002        if(buf == NULL)
3003        {
3004                err("no mem");
3005                return NULL;
3006        }
3007
3008        sprintf(buf, "%lld", value);
3009        buf = ostrshrink(buf);
3010
3011        return buf;
3012}
3013
3014char* oftoa64(double value, char* count)
3015{
3016        char *buf = NULL;
3017        char* tmpstr = NULL;
3018
3019        buf = malloc(MINMALLOC);
3020        if(buf == NULL)
3021        {
3022                err("no mem");
3023                return NULL;
3024        }
3025
3026        tmpstr = ostrcat("%.", count, 0, 0);
3027        tmpstr = ostrcat(tmpstr, "f", 1, 0);
3028
3029        sprintf(buf, tmpstr, value);
3030        buf = ostrshrink(buf);
3031
3032        free(tmpstr);
3033        return buf;
3034}
3035
3036int ostrncmp(char* value1, char* value2, int count)
3037{
3038        int ret = 1;
3039
3040        if(value1 != NULL && value2 != NULL)
3041                ret = strncmp(value1, value2, count);
3042
3043        return ret;
3044}
3045
3046int ostrcmp(char* value1, char* value2)
3047{
3048        int ret = 1;
3049
3050        if(value1 != NULL && value2 != NULL)
3051                ret = strcmp(value1, value2);
3052
3053        return ret;
3054}
3055
3056int ostrcasecmp(char* value1, char* value2)
3057{
3058        int ret = 1;
3059
3060        if(value1 != NULL && value2 != NULL)
3061                ret = strcasecmp(value1, value2);
3062
3063        return ret;
3064}
3065
3066char* createpath(char* dir, char* file)
3067{
3068        char *absdir = NULL;
3069        char *pos = NULL;
3070        char* tmpdir = NULL;
3071
3072        if(dir == NULL || file == NULL)
3073        {
3074                err("NULL detect");
3075                return NULL;
3076        }
3077
3078        tmpdir = ostrcat(dir, NULL, 0, 0);
3079        if(tmpdir == NULL)
3080        {
3081                err("NULL detect");
3082                return NULL;
3083        }
3084
3085        while((tmpdir[strlen(tmpdir) - 1] == '.' && tmpdir[strlen(tmpdir) - 2] != '.') || tmpdir[strlen(tmpdir) - 1] == '/')
3086                tmpdir[strlen(tmpdir) - 1] = '\0';
3087
3088        while(tmpdir[strlen(tmpdir) - 1] == '.' && tmpdir[strlen(tmpdir) - 2] == '.')
3089        {
3090                tmpdir[strlen(tmpdir) - 1] = '\0';
3091                tmpdir[strlen(tmpdir) - 1] = '\0';
3092                if(tmpdir[strlen(tmpdir) - 1] == '/')
3093                        tmpdir[strlen(tmpdir) - 1] = '\0';
3094                pos = strrchr(tmpdir, '/');
3095                if(pos != NULL)
3096                        pos[0] = '\0';
3097        }
3098
3099        absdir = malloc(strlen(tmpdir) + strlen(file) + 2);
3100        if(absdir == NULL)
3101        {
3102                free(tmpdir);
3103                err("no mem");
3104                return NULL;
3105        }
3106
3107        if(strlen(file) > 0 || strlen(tmpdir) == 0)
3108                sprintf(absdir, "%s/%s", tmpdir, file);
3109        else
3110                sprintf(absdir, "%s", tmpdir);
3111
3112        free(tmpdir);
3113        return absdir;
3114}
3115
3116int settimezone(char* zone)
3117{
3118        int ret = 0;
3119        char* tmpzone = NULL, *zonepath = NULL;
3120
3121        zonepath = getconfig("zonepath", NULL);
3122        if(zonepath == NULL)
3123        {
3124                err("config zonepath not defined");
3125                return 1;
3126        }
3127
3128        tmpzone = createpath(zonepath, zone);
3129        if(tmpzone == NULL)
3130                return 1;
3131
3132        setenv("TZ", zone, 1);
3133
3134        ret = unlink(getconfig("localtimefile", NULL));
3135        if(ret != 0)
3136                perr("unlink");
3137        ret = symlink(tmpzone, getconfig("localtimefile", NULL));
3138        if(ret != 0)
3139                perr("link");
3140
3141        free(tmpzone);
3142        return ret;
3143}
3144
3145int delchar(char** text, int pos)
3146{
3147        char *tmptext = NULL;
3148        int i = 0, y = 0, len = 0;
3149
3150        if(text == NULL || *text == NULL)
3151                return pos;
3152
3153        len = strlen(*text);
3154        if(len == 0) return pos;
3155
3156        tmptext = malloc(strlen(*text));
3157        if(tmptext == NULL)
3158        {
3159                err("no mem");
3160                return pos;
3161        }
3162
3163        for(i = 0; i < strlen(*text); i++)
3164        {
3165                if(i == pos - 1)
3166                        y++;
3167
3168                tmptext[i] = (*text)[y];
3169                y++;
3170        }
3171
3172        if(i > 0) tmptext[i - 1] = '\0';
3173        if(pos >= strlen(tmptext)) pos = strlen(tmptext);
3174
3175        free(*text);
3176        *text = tmptext;
3177        return pos;
3178}
3179
3180
3181int insertchar(char** text, char zeichen, int pos)
3182{
3183        char *tmptext = NULL;
3184        int i, y = 0;
3185
3186        if(text == NULL || *text == NULL)
3187                return pos;
3188
3189        tmptext = malloc(strlen(*text) + 2);
3190        if(tmptext == NULL)
3191        {
3192                err("no mem");
3193                return pos;
3194        }
3195
3196        if(strlen(*text) == 0)
3197        {
3198                tmptext[0] = zeichen;
3199                i = 1;
3200                pos = 0;
3201        }
3202        else
3203        {
3204                for(i = 0; i < strlen(*text) + 1; i++)
3205                {
3206                        if(i == pos)
3207                                tmptext[i] = zeichen;
3208                        else
3209                        {
3210                                tmptext[i] = (*text)[y];
3211                                y++;
3212                        }
3213                }
3214        }
3215        tmptext[i] = '\0';
3216
3217        free(*text);
3218        *text = tmptext;
3219        return pos + 1;
3220}
3221
3222void calctext(char* buf, char* buf1, unsigned int* linecount, unsigned int* pagecount, unsigned int* poscount, int pagelen, int page)
3223{
3224        unsigned int tmpposcount = 0;
3225        unsigned int pagepagelen = 0;
3226
3227        *poscount = 0;
3228        *linecount = 1;
3229        *pagecount = 1;
3230
3231        page--;
3232        if(page < 0) page = 0;
3233        pagepagelen = pagelen * page;
3234
3235        if(buf != NULL)
3236        {
3237                while(*buf != '\0')
3238                {
3239                        if(*buf == '\n')
3240                        {
3241                                if(*(buf + 1) == '\0') break;
3242                                (*linecount)++;
3243
3244                                if(*linecount - 1 == pagepagelen)
3245                                        *poscount = tmpposcount + 1;
3246                        }
3247                        buf++;
3248                        tmpposcount++;
3249                }
3250        }
3251
3252        if(buf1 != NULL)
3253        {
3254                while(*buf1 != '\0')
3255                {
3256                        if(*buf1 == '\n')
3257                        {
3258                                if(*(buf1 + 1) == '\0') break;
3259                                (*linecount)++;
3260
3261                                if(*linecount - 1 == pagepagelen)
3262                                        *poscount = tmpposcount + 1;
3263                        }
3264                        buf1++;
3265                        tmpposcount++;
3266                }
3267        }
3268
3269        if(pagelen > 0)
3270                *pagecount = (int)ceil(((float)*linecount / pagelen));
3271}
3272
3273int initlocale(char *localepath)
3274{
3275        debug(10, "localepath: %s", localepath);
3276        setlocale(LC_ALL, "");
3277        if(bindtextdomain(PROGNAME, localepath) == NULL)
3278        {
3279                debug(10, "err set bindtextdomain");
3280                err("set bindtextdomain");
3281                return 1;
3282        }
3283        if(textdomain(PROGNAME) == NULL)
3284        {
3285                debug(10, "err set textdomain");
3286                err("set textdomain");
3287                return 1;
3288        }
3289        return 0;
3290}
3291
3292//for langage you must
3293//mkdir /usr/lib/locale
3294//ln -s %titanpath%/po /usr/lib/locale/po
3295//copy SYS_LC_MESSAGES from other language into LC_MESSAGE
3296int setlang(char *lang)
3297{
3298        debug(10, "lang: %s", lang);
3299        char *ret;
3300
3301#ifdef MIPSEL
3302        // old: po/de new: de_DE
3303        char* tmpstr = NULL, *src = NULL, *dest = NULL, *cmd = NULL;
3304        tmpstr = ostrcat(lang, NULL, 0, 0);
3305        int count = 0;
3306        struct splitstr* ret1 = NULL;
3307        ret1 = strsplit(tmpstr, "/", &count);
3308        if(count > 1)
3309        {
3310                lang = ostrcat(ret1[1].part, NULL, 0, 0);
3311                lang = string_replace("de", "de_DE", lang, 1);
3312                lang = string_replace("el", "el_GR", lang, 1);
3313                lang = string_replace("en", "en_EN", lang, 1);
3314                lang = string_replace("es", "es_ES", lang, 1);
3315                lang = string_replace("fr", "fr_FR", lang, 1);
3316                lang = string_replace("it", "it_IT", lang, 1);
3317                lang = string_replace("lt", "lt_LT", lang, 1);
3318                lang = string_replace("nl", "nl_NL", lang, 1);
3319                lang = string_replace("pl", "pl_PL", lang, 1);
3320                lang = string_replace("ru", "ru_RU", lang, 1);
3321                lang = string_replace("vi", "vi_VN", lang, 1);
3322                debug(10, "lang changed: %s", lang);
3323
3324                dest = ostrcat("/var/usr/local/share/titan/po/", ret1[1].part, 0, 0);
3325                src = ostrcat("/var/usr/local/share/titan/po/", lang, 0, 0);
3326
3327                debug(10, "lang link: %s > %s", dest, src);
3328                symlink(dest, src);
3329
3330                free(dest), dest = NULL;
3331                dest = ostrcat("/usr/lib/locale/", lang, 0, 0);
3332                debug(10, "lang check system locale: %s", dest);
3333                if(!file_exist(dest))
3334                {
3335                        cmd = ostrcat("cp -a /usr/lib/locale/en_EN ", dest, 0, 0);
3336                        debug(10, "lang create system locale: %s", dest);
3337                        debug(10, "lang cmd: %s", cmd);
3338                        system(cmd);
3339                }
3340        }
3341        free(ret1), ret1 = NULL;
3342        free(dest), dest = NULL;
3343        free(src), src = NULL;
3344        free(cmd), cmd = NULL;
3345        free(tmpstr), tmpstr = NULL;
3346#endif
3347
3348        setenv("LANG", lang, 1);
3349        setenv("LANGUAGE", lang, 1);
3350        ret = setlocale(LC_MESSAGES, lang);
3351        if(ret == NULL)
3352        {
3353                printf("can't set LC_MESSAGES to %s\n", lang);
3354                err("can't set LC_MESSAGES to %s", lang);
3355                return 1;
3356        }
3357        return 0;
3358}
3359
3360unsigned long readsysul(const char *filename, int line)
3361{
3362        int i = 0, len = 0;
3363        unsigned long ret = 0;
3364        FILE *fd = NULL;
3365        char *fileline = NULL;
3366        char *buf1 = NULL;
3367
3368        fileline = malloc(MINMALLOC);
3369        if(fileline == NULL)
3370        {
3371                err("no mem");
3372                return 0;
3373        }
3374
3375        fd = fopen(filename, "r");
3376        if(fd == NULL)
3377        {
3378                perr("can't open %s", filename);
3379                free(fileline);
3380                return 0;
3381        }
3382
3383        for(i = 0; i < line; i++)
3384        {
3385                memset(fileline, 0, MINMALLOC);
3386                fgets(fileline, MINMALLOC, fd);
3387        }
3388
3389        len = strlen(fileline) - 1;
3390        if(len >= 0 && fileline[len] == '\n')
3391                fileline[len] = '\0';
3392        buf1 = ostrshrink(fileline);
3393
3394        fclose(fd);
3395
3396        if(buf1 != NULL)
3397        {
3398                ret = strtoul(buf1, NULL, 10);
3399                free(buf1); buf1 = NULL;
3400        }
3401
3402        return ret;
3403}
3404
3405unsigned long long readsysull(const char *filename, int line)
3406{
3407        int i = 0, len = 0;
3408        unsigned long long ret = 0;
3409        FILE *fd = NULL;
3410        char *fileline = NULL;
3411        char *buf1 = NULL;
3412
3413        fileline = malloc(MINMALLOC);
3414        if(fileline == NULL)
3415        {
3416                err("no mem");
3417                return 0;
3418        }
3419
3420        fd = fopen(filename, "r");
3421        if(fd == NULL)
3422        {
3423                perr("can't open %s", filename);
3424                free(fileline);
3425                return 0;
3426        }
3427
3428        for(i = 0; i < line; i++)
3429        {
3430                memset(fileline, 0, MINMALLOC);
3431                fgets(fileline, MINMALLOC, fd);
3432        }
3433
3434        len = strlen(fileline) - 1;
3435        if(len >= 0 && fileline[len] == '\n')
3436                fileline[len] = '\0';
3437        buf1 = ostrshrink(fileline);
3438
3439        fclose(fd);
3440
3441        if(buf1 != NULL)
3442        {
3443                ret = strtoull(buf1, NULL, 10);
3444                free(buf1); buf1 = NULL;
3445        }
3446
3447        return ret;
3448}
3449
3450char* readsys(const char *filename, int line)
3451{
3452        int i = 0, len = 0;
3453        FILE *fd = NULL;
3454        char *fileline = NULL;
3455        char *buf1 = NULL;
3456
3457        fileline = malloc(MINMALLOC);
3458        if(fileline == NULL)
3459        {
3460                err("no mem");
3461                return NULL;
3462        }
3463
3464        fd = fopen(filename, "r");
3465        if(fd == NULL)
3466        {
3467                perr("can't open %s", filename);
3468                free(fileline);
3469                return NULL;
3470        }
3471
3472        for(i = 0; i < line; i++)
3473        {
3474                memset(fileline, 0, MINMALLOC);
3475                fgets(fileline, MINMALLOC, fd);
3476        }
3477
3478        len = strlen(fileline) - 1;
3479        if(len >= 0 && fileline[len] == '\n')
3480                fileline[len] = '\0';
3481        buf1 = ostrshrink(fileline);
3482
3483        fclose(fd);
3484        return buf1;
3485}
3486
3487//flag 0: without \n
3488//flag 1: with \n
3489//flag 2: append without \n
3490//flag 3: append with \n
3491int writesys(const char *filename, char *value, int flag)
3492{
3493        FILE *fd = NULL;
3494        char* tmpstr = NULL;
3495        int ret;
3496
3497        if(value == NULL)
3498        {
3499                err("NULL detect");
3500                return 1;
3501        }
3502
3503        if(flag == 2 || flag == 3)
3504                fd = fopen(filename, "a");
3505        else
3506                fd = fopen(filename, "w");
3507        if(fd == NULL)
3508        {
3509                perr("can't open %s", filename);
3510                return 1;
3511        }
3512
3513        if(flag == 1 || flag == 3)
3514                tmpstr = ostrcat(value, "\n", 0, 0);
3515        else
3516                tmpstr = ostrcat(value, NULL, 0, 0);
3517
3518        ret = fwrite(tmpstr, strlen(tmpstr), 1, fd);
3519        if(ret != 1)
3520        {
3521                perr("writting to %s", filename);
3522                free(tmpstr);
3523                fclose(fd);
3524                return 1;
3525        }
3526
3527        free(tmpstr);
3528        fclose(fd);
3529        return 0;
3530}
3531
3532//flag 0: without \n
3533//flag 1: with \n
3534//flag 2: append without \n
3535//flag 3: append with \n
3536int writesysint(const char *filename, int value, int flag)
3537{
3538        char* tmpstr = NULL;
3539        int ret = 0;
3540
3541        tmpstr = oitoa(value);
3542        ret = writesys(filename, tmpstr, flag);
3543
3544        free(tmpstr);
3545        return ret;
3546}
3547
3548char* getdevcontent(char* devconfig)
3549{
3550        char *dev = NULL;
3551        char *value = NULL;
3552
3553        dev = getconfig(devconfig, NULL);
3554
3555        if(dev == NULL)
3556        {
3557                err("NULL detect");
3558                return NULL;
3559        }
3560
3561        value = readsys(dev, 1);
3562        if(value == NULL)
3563        {
3564                err("NULL detect");
3565                return NULL;
3566        }
3567
3568        return value;
3569}
3570
3571char* convertspacetolf(char* value)
3572{
3573        int i = 0;
3574
3575        while(value[i] != '\0')
3576        {
3577                if(value[i] == ' ')
3578                        value[i] = '\n';
3579                i++;
3580        }
3581
3582        return value;
3583}
3584
3585//flag 0: all
3586//flag 1: hdmi
3587//flag 2: not hdmi
3588char* getcolorformatchoices(int flag)
3589{
3590        char *colorformatchoicesdev = NULL;
3591        char *value = NULL;
3592
3593        colorformatchoicesdev = getconfig("colorformatchoicesdev", NULL);
3594
3595        if(colorformatchoicesdev == NULL)
3596        {
3597                err("NULL detect");
3598                return NULL;
3599        }
3600
3601        value = readsys(colorformatchoicesdev, 1);
3602        if(value == NULL)
3603        {
3604                err("NULL detect");
3605                return NULL;
3606        }
3607       
3608        if(flag == 1)
3609        {
3610                value = string_replace("rgb", NULL, value, 1);
3611                value = string_replace("  ", " ", value, 1);
3612                value = string_replace("cvbs", NULL, value, 1);
3613                value = string_replace("  ", " ", value, 1);
3614                value = string_replace("svideo", NULL, value, 1);
3615                value = string_replace("  ", " ", value, 1);
3616                value = string_replace("yuv", NULL, value, 1);
3617                value = string_replace("  ", " ", value, 1);
3618        }
3619        else if(flag == 2)
3620        {
3621                value = string_replace("hdmi_rgb", NULL, value, 1);
3622                value = string_replace("  ", " ", value, 1);
3623                value = string_replace("hdmi_yuv", NULL, value, 1);
3624                value = string_replace("  ", " ", value, 1);
3625                value = string_replace("hdmi_422", NULL, value, 1);
3626                value = string_replace("  ", " ", value, 1);
3627        }
3628
3629        value = strstrip(value);
3630        value = convertspacetolf(value);
3631
3632        return value;
3633}
3634
3635char* getaudiosourcechoices()
3636{
3637        char *audiosourcechoicesdev = NULL;
3638        char *value = NULL;
3639
3640        audiosourcechoicesdev = getconfig("audiosourcechoicesdev", NULL);
3641
3642        if(audiosourcechoicesdev == NULL)
3643        {
3644                err("NULL detect");
3645                return NULL;
3646        }
3647
3648        value = readsys(audiosourcechoicesdev, 1);
3649        if(value == NULL)
3650        {
3651                err("NULL detect");
3652                return NULL;
3653        }
3654
3655        value = convertspacetolf(value);
3656
3657        return value;
3658}
3659
3660char* getac3choices()
3661{
3662        char *ac3choicesdev = NULL;
3663        char *value = NULL;
3664
3665        ac3choicesdev = getconfig("ac3choicesdev", NULL);
3666
3667        if(ac3choicesdev == NULL)
3668        {
3669                err("NULL detect");
3670                return NULL;
3671        }
3672
3673        value = readsys(ac3choicesdev, 1);
3674        if(value == NULL)
3675        {
3676                err("NULL detect");
3677                return NULL;
3678        }
3679
3680        value = convertspacetolf(value);
3681
3682        return value;
3683}
3684
3685char* getaacchoices()
3686{
3687        char *aacchoicesdev = NULL;
3688        char *value = NULL;
3689
3690        aacchoicesdev = getconfig("aacchoicesdev", NULL);
3691
3692        if(aacchoicesdev == NULL)
3693        {
3694                err("NULL detect");
3695                return NULL;
3696        }
3697
3698        value = readsys(aacchoicesdev, 1);
3699        if(value == NULL)
3700        {
3701                err("NULL detect");
3702                return NULL;
3703        }
3704
3705        value = convertspacetolf(value);
3706
3707        return value;
3708}
3709
3710char* getwsschoices()
3711{
3712        char *wsschoicesdev = NULL;
3713        char *value = NULL;
3714
3715        wsschoicesdev = getconfig("wsschoicesdev", NULL);
3716
3717        if(wsschoicesdev == NULL)
3718        {
3719                err("NULL detect");
3720                return NULL;
3721        }
3722
3723        value = readsys(wsschoicesdev, 1);
3724        if(value == NULL)
3725        {
3726                err("NULL detect");
3727                return NULL;
3728        }
3729
3730        value = convertspacetolf(value);
3731
3732        return value;
3733}
3734
3735int setciclock(int slotnr, char* value)
3736{
3737        char* ciclockdev = NULL, *tmpstr = NULL;
3738        int ret = 0;
3739
3740        ciclockdev = getconfig("ciclockdev", NULL);
3741
3742        if(ciclockdev != NULL && value != NULL)
3743        {
3744                tmpstr = malloc(MINMALLOC);
3745                if(tmpstr == NULL)
3746                {
3747                        err("no mem");
3748                        return 1;
3749                }
3750
3751                snprintf(tmpstr, MINMALLOC, ciclockdev, slotnr);
3752                debug(100, "set %s to %s", tmpstr, value);
3753                ret = writesys(tmpstr, value, 0);
3754
3755                free(tmpstr); tmpstr = NULL;
3756                return ret;
3757        }
3758
3759        return 0;
3760}
3761
3762//routes stream from tuner to ci or ci to ci
3763int setciinput(int slotnr, char* value)
3764{
3765        char* ciinputdev = NULL, *tmpstr = NULL;
3766        int ret = 0;
3767
3768        ciinputdev = getconfig("ciinputdev", NULL);
3769
3770        if(ciinputdev != NULL && value != NULL)
3771        {
3772                tmpstr = malloc(MINMALLOC);
3773                if(tmpstr == NULL)
3774                {
3775                        err("no mem");
3776                        return 1;
3777                }
3778
3779                snprintf(tmpstr, MINMALLOC, ciinputdev, slotnr);
3780                debug(100, "set %s to %s", tmpstr, value);
3781                ret = writesys(tmpstr, value, 0);
3782
3783                free(tmpstr); tmpstr = NULL;
3784                return ret;
3785        }
3786
3787        return 0;
3788}
3789
3790//routes stream from tuner or ci to linux
3791int setcisource(int tunernr, char* value)
3792{
3793        char* cisourcedev = NULL, *tmpstr = NULL;
3794        int ret = 0;
3795
3796        cisourcedev = getconfig("cisourcedev", NULL);
3797
3798        if(cisourcedev != NULL && value != NULL)
3799        {
3800                tmpstr = malloc(MINMALLOC);
3801                if(tmpstr == NULL)
3802                {
3803                        err("no mem");
3804                        return 1;
3805                }
3806
3807                snprintf(tmpstr, MINMALLOC, cisourcedev, tunernr);
3808                debug(100, "set %s to %s", tmpstr, value);
3809                ret = writesys(tmpstr, value, 0);
3810
3811                free(tmpstr); tmpstr = NULL;
3812                return ret;
3813        }
3814
3815        return 0;
3816}
3817
3818int setinput(char* value)
3819{
3820        char* inputdev = NULL;
3821        int ret = 0;
3822
3823        inputdev = getconfig("inputdev", NULL);
3824
3825        if(inputdev != NULL && value != NULL)
3826        {
3827                debug(100, "set %s to %s", inputdev, value);
3828                ret = writesys(inputdev, value, 0);
3829                return ret;
3830        }
3831
3832        return 0;
3833}
3834
3835char* getinput()
3836{
3837        char *inputdev = NULL;
3838        char *value = NULL;
3839
3840        inputdev = getconfig("inputdev", NULL);
3841
3842        if(inputdev == NULL)
3843        {
3844                err("NULL detect");
3845                return NULL;
3846        }
3847
3848        value = readsys(inputdev, 1);
3849        if(value == NULL)
3850        {
3851                err("NULL detect");
3852                return NULL;
3853        }
3854
3855        return value;
3856}
3857
3858int setac3(char* value)
3859{
3860        char* ac3dev = NULL;
3861        int ret = 0;
3862
3863        ac3dev = getconfig("ac3dev", NULL);
3864
3865        if(ac3dev != NULL && value != NULL)
3866        {
3867                debug(100, "set %s to %s", ac3dev, value);
3868                ret = writesys(ac3dev, value, 0);
3869                if(ret == 0) addconfig("av_ac3mode", value);
3870                return ret;
3871        }
3872
3873        return 0;
3874}
3875
3876int setaac(char* value)
3877{
3878        char* aacdev = NULL;
3879        int ret = 0;
3880
3881        aacdev = getconfig("aacdev", NULL);
3882
3883        if(aacdev != NULL && value != NULL)
3884        {
3885                debug(100, "set %s to %s", aacdev, value);
3886                ret = writesys(aacdev, value, 0);
3887                if(ret == 0) addconfig("av_aacmode", value);
3888                return ret;
3889        }
3890
3891        return 0;
3892}
3893
3894int setwss(char* value)
3895{
3896        char* wssdev = NULL;
3897        int ret = 0;
3898
3899        wssdev = getconfig("wssdev", NULL);
3900
3901        if(wssdev != NULL && value != NULL)
3902        {
3903                debug(100, "set %s to %s", wssdev, value);
3904                ret = writesys(wssdev, value, 0);
3905                if(ret == 0) addconfig("av_wssmode", value);
3906                return ret;
3907        }
3908        else if(wssdev != NULL && value == NULL)
3909        {
3910                debug(100, "set %s to %s", wssdev, "auto");
3911                ret = writesys(wssdev, "auto", 0);
3912                return ret;
3913        }
3914
3915        return 0;
3916}
3917
3918char* getac3()
3919{
3920        char *ac3dev = NULL;
3921        char *value = NULL;
3922
3923        ac3dev = getconfig("ac3dev", NULL);
3924
3925        if(ac3dev == NULL)
3926        {
3927                err("NULL detect");
3928                return NULL;
3929        }
3930
3931        value = readsys(ac3dev, 1);
3932        if(value == NULL)
3933        {
3934                err("NULL detect");
3935                return NULL;
3936        }
3937
3938        return value;
3939}
3940
3941char* getaac()
3942{
3943        char *aacdev = NULL;
3944        char *value = NULL;
3945
3946        aacdev = getconfig("aacdev", NULL);
3947
3948        if(aacdev == NULL)
3949        {
3950                err("NULL detect");
3951                return NULL;
3952        }
3953
3954        value = readsys(aacdev, 1);
3955        if(value == NULL)
3956        {
3957                err("NULL detect");
3958                return NULL;
3959        }
3960
3961        return value;
3962}
3963
3964char* getwss()
3965{
3966        char *wssdev = NULL;
3967        char *value = NULL;
3968
3969        wssdev = getconfig("wssdev", NULL);
3970
3971        if(wssdev == NULL)
3972        {
3973                err("NULL detect");
3974                return NULL;
3975        }
3976
3977        value = readsys(wssdev, 1);
3978        if(value == NULL)
3979        {
3980                err("NULL detect");
3981                return NULL;
3982        }
3983
3984        return value;
3985}
3986
3987int setaudiodelaybitstream(char* value)
3988{
3989        char* audiodelaybitstreamdev = NULL;
3990        int ret = 0;
3991
3992        audiodelaybitstreamdev = getconfig("audiodelaybitstreamdev", NULL);
3993
3994        if(audiodelaybitstreamdev != NULL && value != NULL)
3995        {
3996                debug(100, "set %s to %s", audiodelaybitstreamdev, value);
3997                ret = writesys(audiodelaybitstreamdev, value, 0);
3998                if(ret == 0) addconfig("audiodelaybitstream", value);
3999                return ret;
4000        }
4001
4002        return 0;
4003}
4004
4005char* getaudiodelaybitstream()
4006{
4007        char *audiodelaybitstreamdev = NULL;
4008        char *value = NULL;
4009
4010        audiodelaybitstreamdev = getconfig("audiodelaybitstreamdev", NULL);
4011
4012        if(audiodelaybitstreamdev == NULL)
4013        {
4014                err("NULL detect");
4015                return NULL;
4016        }
4017
4018        value = readsys(audiodelaybitstreamdev, 1);
4019        if(value == NULL)
4020        {
4021                err("NULL detect");
4022                return NULL;
4023        }
4024
4025        return value;
4026}
4027
4028int setaudiodelaypcm(char* value)
4029{
4030        char* audiodelaypcmdev = NULL;
4031        int ret = 0;
4032
4033        audiodelaypcmdev = getconfig("audiodelaypcmdev", NULL);
4034
4035        if(audiodelaypcmdev != NULL && value != NULL)
4036        {
4037                debug(100, "set %s to %s", audiodelaypcmdev, value);
4038                ret = writesys(audiodelaypcmdev, value, 0);
4039                if(ret == 0) addconfig("audiodelaypcm", value);
4040                return ret;
4041        }
4042
4043        return 0;
4044}
4045
4046char* getaudiodelaypcm()
4047{
4048        char *audiodelaypcmdev = NULL;
4049        char *value = NULL;
4050
4051        audiodelaypcmdev = getconfig("audiodelaypcmdev", NULL);
4052
4053        if(audiodelaypcmdev == NULL)
4054        {
4055                err("NULL detect");
4056                return NULL;
4057        }
4058
4059        value = readsys(audiodelaypcmdev, 1);
4060        if(value == NULL)
4061        {
4062                err("NULL detect");
4063                return NULL;
4064        }
4065
4066        return value;
4067}
4068
4069
4070char* getpolicychoices()
4071{
4072        char *policychoicesdev = NULL;
4073        char *value = NULL;
4074
4075        policychoicesdev = getconfig("policychoicesdev", NULL);
4076
4077        if(policychoicesdev == NULL)
4078        {
4079                err("NULL detect");
4080                return NULL;
4081        }
4082
4083        value = readsys(policychoicesdev, 1);
4084        if(value == NULL)
4085        {
4086                err("NULL detect");
4087                return NULL;
4088        }
4089
4090#ifdef MIPSEL
4091        free(value), value = NULL;
4092//      value = ostrcat("16:9_set_bestfit_to_policy_show_justscale 16:9_set_letterbox_to_policy_show_panscan 16:9_set_panscan_to_policy_show_pillarbox 4:3_set_bestfit_to_policy2_show_justscale 4:3_set_letterbox_to_policy2_show_letterbox 4:3_set_panscan_to_policy2_show_panscan", NULL, 0, 0);
4093        value = ostrcat(_("16:9_set_bestfit_to_policy_show_justscale"), NULL, 0, 0);
4094        value = ostrcat(value, "\n", 1, 0);
4095        value = ostrcat(value, _("16:9_set_letterbox_to_policy_show_panscan"), 1, 0);
4096        value = ostrcat(value, "\n", 1, 0);
4097        value = ostrcat(value, _("16:9_set_panscan_to_policy_show_pillarbox"), 1, 0);
4098        value = ostrcat(value, "\n", 1, 0);
4099        value = ostrcat(value, _("4:3_set_bestfit_to_policy2_show_justscale"), 1, 0);
4100        value = ostrcat(value, "\n", 1, 0);
4101        value = ostrcat(value, _("4:3_set_letterbox_to_policy2_show_letterbox"), 1, 0);
4102        value = ostrcat(value, "\n", 1, 0);
4103        value = ostrcat(value, _("4:3_set_panscan_to_policy2_show_panscan"), 1, 0);
4104#else
4105        value = convertspacetolf(value);
4106#endif
4107
4108        return value;
4109}
4110
4111char* getpolicy()
4112{
4113        char *policydev = NULL;
4114        char *value = NULL;
4115
4116        policydev = getconfig("policydev", NULL);
4117
4118        if(policydev == NULL)
4119        {
4120                err("NULL detect");
4121                return NULL;
4122        }
4123
4124        value = readsys(policydev, 1);
4125
4126#ifdef MIPSEL
4127        char *aspect = NULL;
4128        aspect = getaspect();
4129        if(!ostrncmp("16:9", aspect, 4))
4130        {
4131                if(ostrcmp("bestfit", value) == 0)
4132                {
4133                        free(value), value = NULL;
4134                        value = ostrcat(_("16:9_set_bestfit_to_policy_show_justscale"), NULL, 0, 0);
4135                }
4136                else if(ostrcmp("letterbox", value) == 0)
4137                {
4138                        free(value), value = NULL;
4139                        value = ostrcat(_("16:9_set_letterbox_to_policy_show_panscan"), NULL, 0, 0);
4140                }
4141                else if(ostrcmp("panscan", value) == 0)
4142                {
4143                        free(value), value = NULL;
4144                        value = ostrcat(_("16:9_set_panscan_to_policy_show_pillarbox"), NULL, 0, 0);
4145                }
4146        }
4147        else
4148        {
4149                policydev = getconfig("policy2dev", NULL);
4150                value = readsys(policydev, 1);
4151
4152                if(ostrcmp("bestfit", value) == 0)
4153                {
4154                        free(value), value = NULL;
4155                        value = ostrcat(_("4:3_set_bestfit_to_policy2_show_justscale"), NULL, 0, 0);
4156                }
4157                else if(ostrcmp("letterbox", value) == 0)
4158                {
4159                        free(value), value = NULL;
4160                        value = ostrcat(_("4:3_set_letterbox_to_policy2_show_letterbox"), NULL, 0, 0);
4161                }
4162                else if(ostrcmp("panscan", value) == 0)
4163                {
4164                        free(value), value = NULL;
4165                        value = ostrcat(_("4:3_set_panscan_to_policy2_show_panscan"), NULL, 0, 0);
4166                }
4167        }
4168        free(aspect), aspect = NULL;
4169
4170#endif
4171
4172        if(value == NULL)
4173        {
4174                err("NULL detect");
4175                return NULL;
4176        }
4177
4178        return value;
4179}
4180
4181int setpolicy(char* value)
4182{
4183        char* policydev, *tmpstr = NULL, *tmpstr2 = NULL;
4184        int ret = 0;
4185
4186        policydev = getconfig("policydev", NULL);
4187
4188        if(policydev != NULL && value != NULL)
4189        {
4190                debug(10, "set %s to %s", policydev, value);
4191
4192#ifdef MIPSEL
4193                if(ostrcmp(_("16:9_set_bestfit_to_policy_show_justscale"), _(value)) == 0)
4194                {
4195                        tmpstr = ostrcat("bestfit", NULL, 0, 0);
4196                        tmpstr2 = ostrcat("16:9_set_bestfit_to_policy_show_justscale", NULL, 0, 0);
4197                        setaspect("set_16:9");
4198                }
4199                else if(ostrcmp(_("16:9_set_letterbox_to_policy_show_panscan"), _(value)) == 0)
4200                {
4201                        tmpstr = ostrcat("letterbox", NULL, 0, 0);
4202                        tmpstr2 = ostrcat("16:9_set_letterbox_to_policy_show_panscan", NULL, 0, 0);
4203                        setaspect("set_16:9");
4204                }
4205                else if(ostrcmp(_("16:9_set_panscan_to_policy_show_pillarbox"), _(value)) == 0)
4206                {
4207                        tmpstr = ostrcat("panscan", NULL, 0, 0);
4208                        tmpstr2 = ostrcat("16:9_set_panscan_to_policy_show_pillarbox", NULL, 0, 0);
4209                        setaspect("set_16:9");
4210                }                       
4211                else if(ostrcmp(_("4:3_set_bestfit_to_policy2_show_justscale"), _(value)) == 0)
4212                {
4213                        policydev = getconfig("policy2dev", NULL);
4214                        tmpstr = ostrcat("bestfit", NULL, 0, 0);
4215                        tmpstr2 = ostrcat("4:3_set_bestfit_to_policy2_show_justscale", NULL, 0, 0);
4216                        setaspect("set_4:3");
4217                }
4218                else if(ostrcmp(_("4:3_set_letterbox_to_policy2_show_letterbox"), _(value)) == 0)
4219                {
4220                        policydev = getconfig("policy2dev", NULL);
4221                        tmpstr = ostrcat("letterbox", NULL, 0, 0);
4222                        tmpstr2 = ostrcat("4:3_set_letterbox_to_policy2_show_letterbox", NULL, 0, 0);
4223                        setaspect("set_4:3");
4224                }
4225                else if(ostrcmp(_("4:3_set_panscan_to_policy2_show_panscan"), _(value)) == 0)
4226                {
4227                        policydev = getconfig("policy2dev", NULL);
4228                        tmpstr = ostrcat("panscan", NULL, 0, 0);
4229                        tmpstr2 = ostrcat("4:3_set_panscan_to_policy2_show_panscan", NULL, 0, 0);
4230                        setaspect("set_4:3");
4231                }
4232#else
4233                tmpstr = ostrcat(value, NULL, 0, 0);
4234#endif
4235                debug(10, "set change %s to %s", policydev, tmpstr);
4236                ret = writesys(policydev, tmpstr, 0);
4237
4238#ifdef MIPSEL
4239                if(ret == 0)
4240                {
4241                        debug(10, "write %s to /mnt/config/titan.cfg", tmpstr2);
4242                        addconfig("av_policy", tmpstr2);
4243                }
4244#else
4245                if(ret == 0) addconfig("av_policy", value);
4246                {
4247                        debug(10, "write %s to /mnt/config/titan.cfg", value);
4248                        addconfig("av_policy", value);
4249                }
4250#endif
4251        }
4252
4253        free(tmpstr), tmpstr = NULL;
4254        free(tmpstr2), tmpstr2 = NULL;
4255        return ret;
4256}
4257
4258char* getaspectchoices()
4259{
4260        char *aspectchoicesdev = NULL;
4261        char *value = NULL;
4262
4263        aspectchoicesdev = getconfig("aspectchoicesdev", NULL);
4264
4265        if(aspectchoicesdev == NULL)
4266        {
4267                err("NULL detect");
4268                return NULL;
4269        }
4270
4271        value = readsys(aspectchoicesdev, 1);
4272        if(value == NULL)
4273        {
4274                err("NULL detect");
4275                return NULL;
4276        }
4277
4278        value = convertspacetolf(value);
4279
4280        return value;
4281}
4282
4283char* getaspect()
4284{
4285        char *aspectdev = NULL;
4286        char *value = NULL;
4287
4288        aspectdev = getconfig("aspectdev", NULL);
4289
4290        if(aspectdev == NULL)
4291        {
4292                err("NULL detect");
4293                return NULL;
4294        }
4295
4296        value = readsys(aspectdev, 1);
4297        if(value == NULL)
4298        {
4299                err("NULL detect");
4300                return NULL;
4301        }
4302
4303        return value;
4304}
4305
4306int setaspect(char* value)
4307{
4308        char* aspectdev;
4309        int ret = 0;
4310
4311        aspectdev = getconfig("aspectdev", NULL);
4312
4313        if(aspectdev != NULL && value != NULL)
4314        {
4315                debug(10, "set %s to %s", aspectdev, value);
4316
4317                if(!ostrncmp("set_", value, 4))
4318                {
4319                        char* tmpstr = NULL;
4320                        tmpstr = ostrcat(value, NULL, 0, 0);
4321                        tmpstr = string_replace("set_", "", tmpstr, 1);
4322                        debug(10, "change %s to %s", aspectdev, tmpstr);
4323                        ret = writesys(aspectdev, tmpstr, 0);
4324                        if(ret == 0) addconfig("av_aspect", tmpstr);
4325                        free(tmpstr); tmpstr = NULL;
4326                        return ret;
4327                }
4328                else
4329                {
4330                        ret = writesys(aspectdev, value, 0);
4331
4332                        if(ret == 0)
4333                        {
4334                                addconfig("av_aspect", value);
4335       
4336                                //set policy new after change aspect
4337                                char* tmpstr = getpolicy();
4338                                setpolicy(tmpstr);
4339                                free(tmpstr); tmpstr = NULL;
4340                        }
4341                }
4342                return ret;
4343        }
4344
4345        return 0;
4346}
4347
4348char* getvideomodechoices()
4349{
4350        char *videomodechoicesdev = NULL;
4351        char *value = NULL;
4352
4353        videomodechoicesdev = getconfig("videomodechoicesdev", NULL);
4354
4355        if(videomodechoicesdev == NULL)
4356        {
4357                err("NULL detect");
4358                return NULL;
4359        }
4360
4361        value = readsys(videomodechoicesdev, 1);
4362        if(value == NULL)
4363        {
4364                err("NULL detect");
4365                return NULL;
4366        }
4367
4368        value = string_replace("pal", NULL, value, 1);
4369        value = string_replace("  ", " ", value, 1);
4370
4371        if(status.mcaktiv == 0)
4372        {
4373                value = string_replace("1080p60", NULL, value, 1);
4374                value = string_replace("  ", " ", value, 1);
4375                value = string_replace("1080p59", NULL, value, 1);
4376                value = string_replace("  ", " ", value, 1);
4377                value = string_replace("1080p30", NULL, value, 1);
4378                value = string_replace("  ", " ", value, 1);
4379                value = string_replace("1080p29", NULL, value, 1);
4380                value = string_replace("  ", " ", value, 1);
4381                value = string_replace("1080p25", NULL, value, 1);
4382                value = string_replace("  ", " ", value, 1);
4383                value = string_replace("1080p24", NULL, value, 1);
4384                value = string_replace("  ", " ", value, 1);
4385                value = string_replace("1080p23", NULL, value, 1);
4386                value = string_replace("  ", " ", value, 1);
4387                value = string_replace("1080i60", NULL, value, 1);
4388                value = string_replace("  ", " ", value, 1);
4389                value = string_replace("1080i59", NULL, value, 1);
4390                value = string_replace("  ", " ", value, 1);
4391                value = string_replace("1080i60", NULL, value, 1);
4392                value = string_replace("  ", " ", value, 1);
4393                value = string_replace("720p60", NULL, value, 1);
4394                value = string_replace("  ", " ", value, 1);
4395                value = string_replace("576p50", NULL, value, 1);
4396                value = string_replace("  ", " ", value, 1);
4397                value = string_replace("1280x1024_75", NULL, value, 1);
4398                value = string_replace("  ", " ", value, 1);
4399                value = string_replace("1280x1024_70", NULL, value, 1);
4400                value = string_replace("  ", " ", value, 1);
4401                value = string_replace("1280x1024_60", NULL, value, 1);
4402                value = string_replace("  ", " ", value, 1);
4403                value = string_replace("1600x1200_60", NULL, value, 1);
4404                value = string_replace("  ", " ", value, 1);
4405                value = string_replace("1024x768_100", NULL, value, 1);
4406                value = string_replace("  ", " ", value, 1);
4407                value = string_replace("1024x768_90", NULL, value, 1);
4408                value = string_replace("  ", " ", value, 1);
4409                value = string_replace("1024x768_75", NULL, value, 1);
4410                value = string_replace("  ", " ", value, 1);
4411                value = string_replace("1024x768_70", NULL, value, 1);
4412                value = string_replace("  ", " ", value, 1);
4413                value = string_replace("1024x768_60", NULL, value, 1);
4414                value = string_replace("  ", " ", value, 1);
4415        }
4416
4417        value = strstrip(value);
4418        value = convertspacetolf(value);
4419
4420        return value;
4421}
4422
4423char* getmode3dchoices()
4424{
4425        char *mode3dchoicesdev = NULL;
4426        char *value = NULL;
4427
4428        mode3dchoicesdev = getconfig("mode3dchoicesdev", NULL);
4429
4430        if(mode3dchoicesdev == NULL)
4431        {
4432                return ostrcat("off\nsbs\ntab", "", 0, 0);
4433        }
4434
4435        value = readsys(mode3dchoicesdev, 1);
4436        if(value == NULL)
4437        {
4438                err("NULL detect");
4439                return NULL;
4440        }
4441
4442        value = convertspacetolf(value);
4443
4444        return value;
4445}
4446
4447char* getmode3d()
4448{
4449        char *mode3ddev = NULL;
4450        char *value = NULL;
4451
4452        mode3ddev = getconfig("mode3ddev", NULL);
4453
4454        if(mode3ddev == NULL)
4455        {
4456                err("NULL detect");
4457                return NULL;
4458        }
4459
4460        value = readsys(mode3ddev, 1);
4461        if(value == NULL)
4462        {
4463                err("NULL detect");
4464                return NULL;
4465        }
4466
4467        return value;
4468}
4469
4470int setmode3d(char* value)
4471{
4472        char* mode3ddev;
4473        int ret = 0;
4474
4475        mode3ddev = getconfig("mode3ddev", NULL);
4476
4477        if(mode3ddev != NULL && value != NULL)
4478        {
4479                debug(100, "set %s to %s", mode3ddev, value);
4480                ret = writesys(mode3ddev, value, 0);
4481                if(ret == 0) addconfig("av_mode3d", value);
4482                return ret;
4483        }
4484
4485        return 0;
4486}
4487
4488char* getvideomode()
4489{
4490        char *videomodedev = NULL;
4491        char *value = NULL;
4492
4493        videomodedev = getconfig("videomodedev", NULL);
4494
4495        if(videomodedev == NULL)
4496        {
4497                err("NULL detect");
4498                return NULL;
4499        }
4500
4501        value = readsys(videomodedev, 1);
4502        if(value == NULL)
4503        {
4504                err("NULL detect");
4505                return NULL;
4506        }
4507
4508        return value;
4509}
4510
4511void switchvideomode()
4512{
4513        int rcret = 0;
4514        char* tmpstr = NULL, *tmpstr2 = NULL, *tmpstr3 = NULL;
4515        tmpstr = getvideomode();       
4516        struct skin* playpolicy = getscreen("playpolicy");
4517        tmpstr2 = getcolorformat(2);
4518               
4519        if(tmpstr != NULL)
4520        {
4521                if((ostrcmp("pal", tmpstr) == 0 || ostrncmp("576", tmpstr, 3) == 0) && ostrncmp("yuv", tmpstr2, 3) == 0)
4522                {
4523                        setvideomode("720p50", 0);
4524                        changefbresolution("720p50", 0);
4525                        changetext(playpolicy, "720p50 (rgb)");
4526                        writevfdmenu("720p50 (rgb)");
4527                        setcolorformat("rgb", 1);
4528                        unlink("/var/etc/.scart");
4529                }
4530                else if((ostrcmp("pal", tmpstr) == 0 || ostrncmp("576", tmpstr, 3) == 0) && ostrncmp("rgb", tmpstr2, 3) == 0)
4531                {
4532                        setvideomode("720p50", 0);
4533                        changefbresolution("720p50", 0);
4534                        changetext(playpolicy, "720p50 (yuv)");
4535                        writevfdmenu("720p50 (yuv)");
4536                        setcolorformat("yuv", 1);
4537                        unlink("/var/etc/.scart");
4538                }
4539                else if(ostrncmp("720", tmpstr, 3) == 0)
4540                {
4541                        setvideomode("1080i50", 0);
4542                        changefbresolution("1080i50", 0);
4543                        tmpstr3 = ostrcat("1080i50 (", tmpstr2, 0, 0);
4544                        tmpstr3 = ostrcat(tmpstr3, ")", 1, 0);
4545                        changetext(playpolicy, tmpstr3);
4546                        writevfdmenu(tmpstr3);
4547                        unlink("/var/etc/.scart");                     
4548                }
4549                else if(ostrncmp("1080", tmpstr, 4) == 0)
4550                {
4551                        setvideomode("576i50", 0);
4552                        changefbresolution("576i50", 0);
4553                        changetext(playpolicy, "576i50 (rgb)");
4554                        writevfdmenu("576i50 (rgb)");
4555                        setcolorformat("rgb", 1);
4556                        writesys("/var/etc/.scart", "0", 0);
4557                }
4558                /*
4559                int ret = textbox(_("Message"), _("Is this Videomode ok ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 10, 0);
4560                if(ret == 1 || ret == 2)
4561                {
4562                        setvideomode(tmpstr, 0);
4563                        changefbresolution(tmpstr, 0);
4564                }
4565                */
4566                drawscreen(playpolicy, 0, 0);
4567                while(1)
4568                {
4569                        rcret = waitrc(playpolicy, 5000, 0);
4570                        break;
4571                }
4572                clearscreen(playpolicy);
4573        }
4574        free(tmpstr);
4575        free(tmpstr2);
4576        free(tmpstr3);
4577}
4578
4579//flag 0: write videomode to config
4580//flag 1: don't write videomode to config
4581int setvideomode(char* value, int flag)
4582{
4583        char* videomodedev;
4584        int ret = 0;
4585
4586        videomodedev = getconfig("videomodedev", NULL);
4587
4588        if(videomodedev != NULL && value != NULL)
4589        {
4590                debug(100, "set %s to %s", videomodedev, value);
4591                ret = writesys(videomodedev, value, 0);
4592                if(ret == 0 && flag == 0) addconfig("av_videomode", value);
4593                return ret;
4594        }
4595
4596        return 0;
4597}
4598
4599//flag 0 = hdmi
4600//flag 1 = scart
4601int setcolorformat(char* value, int flag)
4602{
4603        char* colorformatdev;
4604        int ret = 0;
4605
4606        colorformatdev = getconfig("colorformatdev", NULL);
4607
4608        if(colorformatdev != NULL && value != NULL)
4609        {
4610                debug(100, "set %s to %s", colorformatdev, value);
4611                ret = writesys(colorformatdev, value, 0);
4612                if(ret == 0)
4613                {
4614                        if(flag == 0)
4615                                addconfig("av_colorformat", value);
4616                        else
4617                                addconfig("av_colorformatscart", value);
4618                }
4619                return ret;
4620        }
4621
4622        return 0;
4623}
4624
4625char* getcolorformat(int line)
4626{
4627        char *colorformatdev = NULL;
4628        char *value = NULL;
4629
4630        colorformatdev = getconfig("colorformatdev", NULL);
4631
4632        if(colorformatdev == NULL)
4633        {
4634                err("NULL detect");
4635                return NULL;
4636        }
4637
4638        value = readsys(colorformatdev, line);
4639        if(value == NULL)
4640        {
4641                err("NULL detect");
4642                return NULL;
4643        }
4644
4645        return value;
4646}
4647
4648int setaudiosource(char* value)
4649{
4650        char* audiosourcedev;
4651        int ret = 1;
4652
4653        audiosourcedev = getconfig("audiosourcedev", NULL);
4654
4655        if(audiosourcedev != NULL && value != NULL)
4656        {
4657                debug(100, "set %s to %s", audiosourcedev, value);
4658                ret = writesys(audiosourcedev, value, 0);
4659                if(ret == 0) addconfig("av_audiosource", value);
4660                return ret;
4661        }
4662
4663        return 0;
4664}
4665
4666char* getaudiosource()
4667{
4668        char *audiosourcedev = NULL;
4669        char *value = NULL;
4670
4671        audiosourcedev = getconfig("audiosourcedev", NULL);
4672
4673        if(audiosourcedev == NULL)
4674        {
4675                err("NULL detect");
4676                return NULL;
4677        }
4678
4679        value = readsys(audiosourcedev, 1);
4680        if(value == NULL)
4681        {
4682                err("NULL detect");
4683                return NULL;
4684        }
4685
4686        return value;
4687}
4688
4689char* getsataswitch()
4690{
4691        char *sataswitchdev = NULL;
4692        char *value = NULL;
4693
4694        sataswitchdev = getconfig("sataswitchdev", NULL);
4695
4696        if(sataswitchdev == NULL)
4697        {
4698                err("NULL detect");
4699                return NULL;
4700        }
4701
4702        value = readsys(sataswitchdev, 1);
4703        if(value == NULL)
4704        {
4705                err("NULL detect");
4706                return NULL;
4707        }
4708
4709        return value;
4710}
4711
4712int setsataswitch(char* value)
4713{
4714        char* sataswitchdev;
4715        int ret = 1;
4716
4717        sataswitchdev = getconfig("sataswitchdev", NULL);
4718
4719        if(sataswitchdev != NULL && value != NULL)
4720        {
4721                debug(100, "set %s to %s", sataswitchdev, value);
4722                ret = writesys(sataswitchdev, value, 0);
4723                return ret;
4724        }
4725
4726        return 0;
4727}
4728
4729int setprogress(value)
4730{
4731        char *progressdev;
4732
4733        progressdev = getconfig("progressdev", NULL);
4734
4735        if(progressdev != NULL)
4736        {
4737                debug(100, "set %s to %d",progressdev, value);
4738                return writesysint(progressdev, value, 0);
4739        }
4740
4741        return 0;
4742}
4743
4744int setmute(int value)
4745{
4746        char* mutedev;
4747        int tmpvol, ret = 0;
4748
4749        //don't set mute 2x
4750        if(value == 1 && status.mute == 1) return 0;
4751
4752//      printf("mute value: %d\n", value);
4753
4754        if(value == 2)
4755        {
4756                tmpvol = getvol();
4757                tmpvol = tmpvol * 50 / 100;
4758                status.mute = value;
4759                setvol(tmpvol);
4760        }
4761        else
4762        {
4763                mutedev = getconfig("mutedev", NULL);
4764
4765                if(mutedev != NULL)
4766                {
4767                        debug(100, "set %s to %d", mutedev, value);
4768                        //if(status.volautochangevalue != 0 && value == 0) setvol(getvol());
4769#ifdef MIPSEL
4770                        struct dvbdev *audionode = NULL;
4771                        int openaudio = 0;
4772                        if(status.aktservice != NULL)
4773                        {
4774                                if(status.aktservice->audiodev == NULL)
4775                                {
4776                                        audionode = audioopen(0); //we must open the audio device for set mute in external player
4777                                        openaudio = 1;
4778                                }
4779                                else
4780                                        audionode = status.aktservice->audiodev;
4781                        }
4782                        ret = audiosetmute(audionode, value);
4783                        if(openaudio == 1)
4784                                audioclose(audionode, -1);
4785#else
4786                        ret = writesysint(mutedev, value, 0);
4787#endif
4788               
4789                        if(ret == 0) status.mute = value;
4790                        if(status.volautochangevalue != 0 && value == 0) setvol(getvol());
4791//                      if(value == 1)
4792//                      {
4793//                              printf("mute\n");
4794//                              system("amixer -c 1 set HDMI mute &");
4795//                              system("amixer -c 1 set Analog mute &");
4796//                              system("amixer -c 1 set SPDIF mute &");
4797//                              sleep(1);
4798//                      }
4799//                      else
4800//                      {
4801//                              printf("unmute\n");
4802//                              system("amixer -c 1 set HDMI unmute &");
4803//                              system("amixer -c 1 set Analog unmute &");
4804//                              system("amixer -c 1 set SPDIF unmute &");
4805//                      }
4806
4807                        return ret;
4808                }
4809        }
4810        return 0;
4811}
4812
4813int setvol(int value)
4814{
4815        char* voldev;
4816        int ret = 0, tmpvol = value;
4817
4818        voldev = getconfig("voldev", NULL);
4819       
4820        if(voldev != NULL)
4821        {
4822                if(value > 100) value = 100;
4823                if(value < 0) value = 0;
4824                if(status.volautochangevalue != 0 && value != 0)
4825                {
4826                        if(status.volautochange == 0)
4827                                value = value - (status.volautochangevalue * value / 100);
4828                }
4829                value = 63 - value * 63 / 100;
4830                debug(100, "set %s to %d", voldev, value);
4831                ret = 0;
4832                if(status.mute == 1)
4833                        status.volmute = value;
4834                else
4835                {
4836                        status.volmute = -1;
4837                        ret = writesysint(voldev, value, 0);
4838
4839#ifdef MIPSEL
4840                        struct dvbdev *audionode = NULL;
4841                        int openaudio = 0;
4842       
4843                        if(ret == 0 && status.aktservice != NULL)
4844                        {
4845                                if(status.aktservice->audiodev == NULL)
4846                                {
4847                                        audionode = audioopen(0); //we must open the audio device for change volume in external player
4848                                        openaudio = 1;
4849                                }
4850                                else
4851                                        audionode = status.aktservice->audiodev;
4852               
4853                                if(ret == 0 && audionode != NULL)
4854                                        ret = setmixer(audionode, value, value);
4855                               
4856                                if(openaudio == 1)
4857                                        audioclose(audionode, -1);
4858                        }
4859#else
4860                        if(ret == 0 && status.aktservice != NULL)
4861                                ret = setmixer(status.aktservice->audiodev, value, value);
4862#endif
4863                }
4864                if(ret == 0 && status.mute != 2) addconfigint("vol", tmpvol);
4865                return ret;
4866        }
4867
4868        return 0;
4869}
4870
4871int getvol()
4872{
4873        char *voldev = NULL;
4874        char *value = NULL;
4875        int tmpvol = -1;
4876
4877        voldev = getconfig("voldev", NULL);
4878
4879        if(voldev == NULL)
4880        {
4881                err("NULL detect");
4882                return 0;
4883        }
4884        if(status.volmute == -1)
4885                value = readsys(voldev, 1);
4886        else
4887                tmpvol = status.volmute;
4888        if(value == NULL && tmpvol == -1)
4889        {
4890                err("NULL detect");
4891                return 0;
4892        }
4893        if(status.volmute == -1)
4894                tmpvol = atoi(value);
4895        free(value);
4896        tmpvol = 100 - tmpvol * 100 / 63;
4897        if(status.volautochangevalue != 0)
4898        {
4899                if(status.volautochange == 0 && status.volautochangevalue < 100)
4900                        tmpvol = tmpvol + ((tmpvol * status.volautochangevalue) / (100 - status.volautochangevalue));
4901        }
4902        return tmpvol;
4903}
4904
4905void setdebuglevel()
4906{
4907        debug_level = getconfigint("debuglevel", NULL);
4908        debug(0, "set debug level to %d", debug_level);
4909}
4910
4911char* getxmlentry(char *line, char *searchstr)
4912{
4913        char *buf = NULL, *buf1 = NULL, *buf2 = NULL;
4914
4915        buf = ostrstr(line, searchstr);
4916        if(buf == NULL)
4917                return NULL;
4918
4919        buf = buf + strlen(searchstr);
4920        if(buf[0] == '"')
4921        {
4922                buf = buf + 1;
4923                if(buf[0] == '\\')
4924                {       
4925                        buf = buf + 1;
4926                        if(buf[0] == '"')
4927                                buf2 = strchr(buf+1, '"');
4928                        else
4929                                buf2 = strchr(buf, '"');
4930                }
4931                else
4932                        buf2 = strchr(buf, '"');
4933                if(buf2 == NULL)
4934                {
4935                        err("strchr returns NULL");
4936                        return NULL;
4937                }
4938                buf1 = strndup(buf, buf2 - buf);
4939                if(buf1 == NULL)
4940                {
4941                        err("strndup failed");
4942                        return NULL;
4943                }
4944        }
4945        else if(buf[0] == '\'')
4946        {
4947                buf = buf + 1;
4948                buf2 = strchr(buf, '\'');
4949                if(buf2 == NULL)
4950                {
4951                        err("strchr returns NULL");
4952                        return NULL;
4953                }
4954                buf1 = strndup(buf, buf2 - buf);
4955                if(buf1 == NULL)
4956                {
4957                        err("strndup failed");
4958                        return NULL;
4959                }
4960        }
4961        else
4962        {
4963                buf2 = strchr(buf, ' ');
4964                if(buf2 == NULL)
4965                {
4966                        buf2 = ostrstr(buf, "/>");
4967                        if(buf2 == NULL)
4968                        {
4969                                buf2 = strchr(buf, '>');
4970                                if(buf2 == NULL)
4971                                {
4972                                        err("strchr returns NULL");
4973                                        return NULL;
4974                                }
4975                        }
4976                }
4977                buf1 = strndup(buf, buf2 - buf);
4978                if(buf1 == NULL)
4979                {
4980                        err("strndup failed");
4981                        return NULL;
4982                }
4983        }
4984
4985        return buf1;
4986}
4987
4988char* readbintomem(const char* filename, size_t size)
4989{
4990        FILE *fd = NULL;
4991        char *fileline = NULL;
4992
4993        fileline = calloc(1, size + 1);
4994        if(fileline == NULL)
4995        {
4996                err("no mem");
4997                return NULL;
4998        }
4999
5000        fd = fopen(filename, "rb");
5001        if(fd == NULL)
5002        {
5003                perr("can't open %s", filename);
5004                free(fileline);
5005                return NULL;
5006        }
5007
5008        fread(fileline, size, 1, fd);
5009
5010        fclose(fd);
5011        return fileline;
5012}
5013
5014char* readfiletomem(const char* filename, int flag)
5015{
5016        FILE *fd = NULL;
5017        char *fileline = NULL, *buf = NULL, *tmpbuf = NULL;
5018        int bufsize = 0, bufoldsize = 0;
5019
5020        fileline = malloc(MINMALLOC);
5021        if(fileline == NULL)
5022        {
5023                err("no mem");
5024                return NULL;
5025        }
5026
5027        fd = fopen(filename, "r");
5028        if(fd == NULL)
5029        {
5030                perr("can't open %s", filename);
5031                free(fileline);
5032                return NULL;
5033        }
5034
5035        while(fgets(fileline, MINMALLOC, fd) != NULL)
5036        {
5037                if(flag == 1)
5038                        if(fileline[0] == '#' || fileline[0] == '\n')
5039                                continue;
5040
5041                bufoldsize = bufsize;
5042                bufsize += strlen(fileline);
5043                tmpbuf = buf;   buf = realloc(buf, bufsize + 1);
5044                if(buf == NULL)
5045                {
5046                        err("no mem");
5047                        free(fileline);
5048                        free(tmpbuf);
5049                        fclose(fd);
5050                        return NULL;
5051                }
5052
5053                sprintf(buf + bufoldsize, "%s", fileline);
5054        }
5055
5056        free(fileline);
5057        fclose(fd);
5058        return buf;
5059}
5060
5061char* readeittomem(const char* filename)
5062{
5063        unsigned char byte;
5064        FILE *fil = NULL;
5065        char *zeichen = NULL, *buf = NULL, *buf1 = NULL, *tmpbuf1 = NULL;
5066        int buf1size = 0, buf1oldsize = 0;
5067        int Beschreibung;
5068        int len;
5069
5070        zeichen = malloc(255);
5071        if(zeichen == NULL)
5072        {
5073                err("no mem");
5074                return NULL;
5075        }
5076        buf = malloc(255);
5077        if(buf == NULL)
5078        {
5079                free(zeichen);
5080                err("no mem");
5081                return NULL;
5082        }
5083
5084        fil = fopen(filename, "r");
5085        if(fil == NULL)
5086        {
5087                err("can't open %s", filename);
5088                free(zeichen);
5089                free(buf);
5090                return NULL;
5091        }
5092        Beschreibung = 0;
5093        fseek(fil, 12, SEEK_SET); //ersten 12 Byte nicht relevant
5094        while(!feof(fil))
5095        {
5096                byte = fgetc(fil);
5097
5098                if(byte == 0x4D)
5099                {
5100                        fseek(fil, 4,SEEK_CUR);
5101                        byte = fgetc(fil);
5102                        len = byte + 0;
5103                        byte = fgetc(fil);
5104                        fgets(zeichen, len, fil);
5105                        if(byte != 0x05)
5106                                sprintf(buf, "%c%s\n", byte, zeichen);
5107                        else
5108                                sprintf(buf, "%s\n", zeichen);
5109
5110                        buf1oldsize = buf1size;
5111                        buf1size += strlen(buf);
5112                        tmpbuf1 = buf1; buf1 = realloc(buf1, buf1size + 1);
5113                        if(buf1 == NULL)
5114                        {
5115                                err("no mem");
5116                                free(zeichen);
5117                                free(buf);
5118                                free(tmpbuf1);
5119                                fclose(fil);
5120                                return NULL;
5121                        }
5122                        sprintf(buf1 + buf1oldsize, "%s", buf);
5123
5124                        //printf("T %s\n", zeichen);
5125                        byte = fgetc(fil);
5126                        len = byte + 0;
5127                        byte = fgetc(fil);
5128                        fgets(zeichen, len, fil);
5129                        if(byte != 0x05)
5130                                sprintf(buf,"%c%s\n\n", byte, zeichen);
5131                        else
5132                                sprintf(buf,"%s\n\n", zeichen);
5133                               
5134                        buf1oldsize = buf1size;
5135                        buf1size += strlen(buf);
5136                        tmpbuf1 = buf1; buf1 = realloc(buf1, buf1size + 1);
5137                        if(buf1 == NULL)
5138                        {
5139                                err("no mem");
5140                                free(zeichen);
5141                                free(buf);
5142                                free(tmpbuf1);
5143                                fclose(fil);
5144                                return NULL;
5145                        }
5146                        sprintf(buf1 + buf1oldsize, "%s", buf);
5147
5148                }
5149                else if(byte == 0x4E)
5150                {
5151                        fseek(fil, 6, SEEK_CUR);
5152                        byte = fgetc(fil);
5153                        len = byte;
5154                        byte = fgetc(fil);
5155                        fgets(zeichen, len, fil);
5156                        if(Beschreibung == 0)
5157                        {
5158                                if(byte != 0x05)
5159                                        sprintf(buf, "%c%s", byte, zeichen);
5160                                else
5161                                        sprintf(buf, "%s", zeichen);
5162                                Beschreibung = 1;
5163                        }
5164                        else
5165                        {
5166                                if(byte != 0x05)
5167                                        sprintf(buf, "%c%s", byte, zeichen);
5168                                else
5169                                        sprintf(buf, "%s", zeichen);
5170                        }       
5171
5172                        buf1oldsize = buf1size;
5173                        buf1size += strlen(buf);
5174                        tmpbuf1 = buf1; buf1 = realloc(buf1, buf1size + 1);
5175                        if(buf1 == NULL)
5176                        {
5177                                err("no mem");
5178                                free(zeichen);
5179                                free(buf);
5180                                free(tmpbuf1);
5181                                fclose(fil);
5182                                return NULL;
5183                        }
5184                        sprintf(buf1 + buf1oldsize, "%s", buf);     
5185
5186                }
5187                else
5188                {
5189                        byte = fgetc(fil);
5190                        len= byte;
5191                        fgets(zeichen, len + 1, fil);
5192                } 
5193        }
5194        free(zeichen);
5195        free(buf);
5196        fclose(fil);
5197        return buf1;
5198}
5199
5200char* command(char* input)
5201{
5202        int maxlen = 0, pos = 0;
5203        char* tmpstr = NULL, *fileline = NULL;
5204        FILE *iopipe = NULL;
5205       
5206        if(input == NULL) return NULL;
5207
5208        fileline = malloc(MINMALLOC);
5209        if(fileline == NULL)
5210        {
5211                err("no mem");
5212                return NULL;
5213        }
5214
5215        if((iopipe = popen(input, "r")) == NULL)
5216        {
5217                free(fileline);
5218                return NULL;
5219        }
5220
5221        while(!feof(iopipe))
5222        {
5223                if(fgets(fileline, MINMALLOC, iopipe) != NULL)
5224                        ostrcatbig(&tmpstr, fileline, &maxlen, &pos);
5225        }
5226       
5227        if(pos > 0)
5228        {
5229                char* tmp = tmpstr;
5230                tmpstr = realloc(tmpstr, pos + 1);
5231                if(tmpstr == NULL)
5232                        tmpstr = tmp;
5233        }
5234
5235        free(fileline);
5236        pclose(iopipe);
5237        return tmpstr;
5238}
5239
5240char* string_tolower(char *str)
5241{
5242/*
5243        char *p1 = str;
5244
5245        if(str == NULL) return NULL;
5246
5247        while(*p1 != '\0')
5248                *p1++ = tolower(*p1);
5249       
5250        return str;
5251*/
5252        if(str == NULL) return NULL;
5253
5254        int i=0;
5255        while (str[i])
5256        {
5257                str[i] = tolower(str[i]);
5258                i++;
5259        }
5260        return str;
5261}
5262
5263char* string_toupper(char *str)
5264{
5265/*
5266        char *p1 = str;
5267
5268        if(str == NULL) return NULL;
5269
5270        while(*p1 != '\0')
5271                *p1++ = toupper(*p1);
5272       
5273*/
5274        if(str == NULL) return NULL;
5275
5276        int i=0;
5277        while (str[i])
5278        {
5279                str[i] = toupper(str[i]);
5280                i++;
5281        }
5282        return str;
5283}
5284
5285char* stringreplacecharonce(char *str, char c1, char c2)
5286{
5287        char *p1 = str;
5288
5289        if(str == NULL) return NULL;
5290
5291        while(*p1 != '\0')
5292        {
5293                if(*p1 == c1)
5294                {
5295                        *p1 = c2;
5296                        break;                 
5297                }                       
5298                p1++;
5299        }
5300
5301        return str;
5302}
5303
5304char* stringreplacechar(char *str, char c1, char c2)
5305{
5306        char *p1 = str;
5307
5308        if(str == NULL) return NULL;
5309
5310        while(*p1 != '\0')
5311        {
5312                if(*p1 == c1) *p1 = c2;
5313                p1++;
5314        }
5315
5316        return str;
5317}
5318
5319
5320char* string_removechar(char *str)
5321{
5322        char *p1 = str;
5323
5324        if(str == NULL) return NULL;
5325
5326        while(*p1 != '\0')
5327        {
5328                if(*p1 == '.') *p1 = ' ';
5329                else if(*p1 == '-') *p1 = ' ';
5330                else if(*p1 == '_') *p1 = ' ';
5331                else if(*p1 == '/') *p1 = ' ';
5332                p1++;
5333        }
5334
5335        return str;
5336}
5337
5338char* string_withchars2return(char *str)
5339{
5340        char *p1 = str;
5341
5342        if(str == NULL) return NULL;
5343
5344        while(*p1 != '\0')
5345        {
5346                if(*p1 == ' ') *p1 = '\n';
5347                p1++;
5348        }
5349
5350        return str;
5351}
5352
5353char* string_remove_whitechars(char *text)
5354{
5355        char *p1 = text, *p2 = text;
5356
5357        if(text == NULL) return NULL;
5358
5359        while(*p1 != '\0')
5360        {
5361                if(*p1 == ' ')
5362                        ++p1;
5363                else
5364                        *p2++ = *p1++;
5365        }
5366        *p2 = '\0';
5367
5368        return text;
5369}
5370
5371char* strstrip(char *text)
5372{
5373        char* tmpstr = text;
5374
5375        if(text == NULL) return NULL;
5376        int len = strlen(text);
5377
5378        while(isspace(tmpstr[len - 1])) tmpstr[--len] = '\0';
5379        while(*tmpstr && isspace(*tmpstr)) ++tmpstr, --len;
5380
5381        if(text != tmpstr) memmove(text, tmpstr, len + 1);
5382
5383        return text;
5384}
5385
5386char* string_strip_whitechars(char *text)
5387{
5388        char *p1 = text, *p2 = text;
5389
5390        if(text == NULL)
5391                return NULL;
5392
5393        while(*p1 != '\0')
5394        {
5395                if(*p1 == ' ' && *(p1 + 1) == ' ')
5396                        ++p1;
5397                else
5398                        *p2++ = *p1++;
5399        }
5400        *p2 = '\0';
5401
5402        return text;
5403}
5404
5405char* string_replace_all(char *search, char *replace, char *string, int free1)
5406{
5407        char* tmpstr = NULL;
5408        char* searchpos = NULL;
5409
5410        if(string == NULL || search == NULL)
5411        {
5412                tmpstr = ostrcat(tmpstr, string, 1, 0);
5413                if(free1 == 1) free(string);
5414                return tmpstr;
5415        }
5416
5417        searchpos = strstr(string, search);
5418        if(searchpos == NULL)
5419        {
5420                tmpstr = ostrcat(tmpstr, string, 1, 0);
5421                if(free1 == 1) free(string);
5422                return tmpstr;
5423        }
5424
5425        int count = 0;
5426        int stringlen = strlen(string);
5427        int searchlen = strlen(search);
5428        int replacelen = strlen(replace);
5429
5430        while(searchpos != NULL)
5431        {
5432                count++;
5433                searchpos = strstr(searchpos + searchlen, search);
5434        }
5435
5436        int len = stringlen - (searchlen * count) + (replacelen * count);
5437        tmpstr = calloc(1, len + 1);
5438        if(tmpstr == NULL)
5439        {
5440                err("no mem");
5441                tmpstr = ostrcat(tmpstr, string, 1, 0);
5442                if(free1 == 1) free(string);
5443                return tmpstr;
5444        }
5445
5446        len = 0;       
5447        char* str = string;
5448        char* tmp = tmpstr;
5449        searchpos = strstr(str, search);
5450        while(searchpos != NULL)
5451        {
5452                len = searchpos - str;
5453                memcpy(tmp, str, len);
5454                memcpy(tmp + len, replace, replacelen);
5455                tmp += len + replacelen;
5456                str += len + searchlen;
5457                searchpos = strstr(str, search);
5458        }
5459        memcpy(tmp, str, strlen(str));
5460
5461        if(free1 == 1) free(string);
5462
5463        return tmpstr;
5464}
5465
5466char* string_replace_remove_last_chars(char *search, char *replace, char *string, int free1)
5467{
5468        char* searchpos = NULL;
5469        char* tmpstr = NULL;
5470
5471        if(string == NULL || search == NULL)
5472        {
5473                tmpstr = ostrcat(tmpstr, string, 1, 0);
5474                if(free1 == 1) free(string);
5475                return tmpstr;
5476        }
5477
5478        searchpos = ostrstr(string, search);
5479
5480        if(searchpos == NULL)
5481        {
5482                tmpstr = ostrcat(tmpstr, string, 1, 0);
5483                if(free1 == 1) free(string);
5484                return tmpstr;
5485        }
5486
5487        tmpstr = strndup(string, searchpos - string);
5488        if(replace != NULL)
5489                tmpstr = ostrcat(tmpstr, replace, 1, 0);
5490
5491        if(free1 == 1) free(string);
5492
5493        return tmpstr;
5494}
5495
5496char* string_replace(char *search, char *replace, char *string, int free1)
5497{
5498        char* searchpos = NULL;
5499        char* tmpstr = NULL;
5500
5501        if(string == NULL || search == NULL)
5502        {
5503                tmpstr = ostrcat(tmpstr, string, 1, 0);
5504                if(free1 == 1) free(string);
5505                return tmpstr;
5506        }
5507
5508        searchpos = ostrstr(string, search);
5509
5510        if(searchpos == NULL)
5511        {
5512                tmpstr = ostrcat(tmpstr, string, 1, 0);
5513                if(free1 == 1) free(string);
5514                return tmpstr;
5515        }
5516
5517        tmpstr = strndup(string, searchpos - string);
5518        if(replace != NULL)
5519                tmpstr = ostrcat(tmpstr, replace, 1, 0);
5520        tmpstr = ostrcat(tmpstr, string + (searchpos - string) + strlen(search), 1, 0);
5521
5522        if(free1 == 1) free(string);
5523
5524        return tmpstr;
5525}
5526
5527//flag 0: search full str
5528//flag 1: search only end of string
5529char* ostrrstrcase(char* str, char* search, int len, int flag)
5530{
5531        int slen = 0;
5532        char* tmpstr = NULL;
5533
5534        if(str == NULL || search == NULL) return NULL;
5535
5536        if(len == -1) len = strlen(str);
5537        slen = strlen(search);
5538        if(slen > len) return NULL;
5539
5540        for(tmpstr = str + len - slen; tmpstr >= str; tmpstr--)
5541        {
5542                if(strncasecmp(tmpstr, search, slen) == 0)
5543                        return tmpstr;
5544                if(flag == 1) return NULL;
5545        }
5546
5547        return NULL;
5548}
5549
5550//flag 0: search full str
5551//flag 1: search only end of string
5552char* ostrrstr(char* str, char* search, int len, int flag)
5553{
5554        int slen = 0;
5555        char* tmpstr = NULL;
5556
5557        if(str == NULL || search == NULL) return NULL;
5558
5559        if(len == -1) len = strlen(str);
5560        slen = strlen(search);
5561        if(slen > len) return NULL;
5562
5563        for(tmpstr = str + len - slen; tmpstr >= str; tmpstr--)
5564        {
5565                if(strncmp(tmpstr, search, slen) == 0)
5566                        return tmpstr;
5567                if(flag == 1) return NULL;
5568        }
5569
5570        return NULL;
5571}
5572
5573char* ostrstr(char* str, char* search)
5574{
5575        char* ret = NULL;
5576
5577        if(str == NULL || search == NULL) return NULL;
5578        ret = strstr(str, search);
5579
5580        return ret;
5581}
5582
5583int file_exist(char* filename)
5584{
5585        if(access(filename, F_OK) == 0)
5586                return 1;
5587        else
5588                return 0;
5589}
5590
5591char* string_newline(char* str)
5592{
5593        if(str == NULL) return NULL;
5594
5595        int size = strlen(str);
5596
5597        if(str[size - 1] == '\n')
5598                str[size - 1] = '\0';
5599
5600        return str;
5601}
5602
5603char* string_quote(char* str)
5604{
5605        char* tmpstr = NULL;
5606
5607        tmpstr = ostrcat("\"", str, 0, 0);
5608        tmpstr = ostrcat(tmpstr, "\"", 1, 0);
5609
5610        return tmpstr;
5611}
5612
5613struct splitstr* strsplit(char *str, char *tok, int* count)
5614{
5615        char *tmpstr = NULL;
5616        struct splitstr *array = NULL, *tmparray = NULL;
5617        *count = 0;
5618
5619        if(str == NULL || tok == NULL)
5620                return NULL;
5621
5622        tmpstr = strtok(str, tok);
5623        while(tmpstr != NULL)
5624        {
5625                *count = *count + 1;
5626                tmparray = array; array = (struct splitstr*)realloc(array, sizeof(struct splitstr*) * (*count));
5627                if(array == NULL)
5628                {
5629                        err("no mem");
5630                        free(tmparray);
5631                        return NULL;
5632                }
5633               
5634                (&array[(*count) - 1])->part = tmpstr;
5635                tmpstr = strtok(NULL, tok);
5636        }
5637
5638        return array;
5639}
5640
5641char* string_shortname(char *tmpfilename, int mode)
5642{
5643        debug(50, "in %s", tmpfilename);
5644
5645//      char replacelist[] = "avi mkv x264 se disc0 disc1 disc2 disc3 disc4 0disc 1disc 2disc 3disc 4disc season0 season1 season2 season3 season4 season5 season6 season7 season8 season9 hdtv 720p 1080i 1080p uncut cd0 cd1 cd2 cd3 cd4 cd5 cd6 cd7 cd8 cd9 dvd0 dvd1 dvd2 dvd3 dvd4 ac3d ac3 bdrip bluray cam camrip complete custom cut dc directors dl doku dts dvdr dvdrip dvdscr dvdscreener extended french finnish german hd hddvd hddvdrip hdtv int internal int ld limited multi multisubs nordic ntsc pal pl r1 r5 recut remastered repack rip screener se see special.edition sse stv subbed swedish staffel tc telecine telesync ts unrated ws xxx italian";
5646        char* str = NULL;
5647
5648        if(mode==1)
5649        {
5650                char* replacelist = "avi mkv x264 xvid se uncut ac3d ac3hd ac3 bdrip bluray cam camrip complete custom cut dc directors dl doku dts dvdr dvdrip dvdscr dvdscreener ecc extended french finnish german hd hddvd hddvdrip hdtv int internal int ld limited multi multisubs nordic ntsc pal pl r1 r5 recut remastered repack rip screener se see sse stv subbed swedish staffel tc telecine telesync ts unrated ws xxx italian";
5651                str = ostrcat(str, replacelist, 1, 0);
5652        }
5653        else if(mode==2)
5654        {
5655                char* replacelist = "1080i 1080p 720p ac3 ac3d ac3hd avi bdrip bluray cam camrip complete custom cut dat dc directors divx dl doku dts dvdr dvdrip dvdscr dvdscreener ecc ed extended finnish flv french german hd hddvd hddvdrip hdtv int internal ld limited linedubbed m2ts m4v md mkv mov mp4 mpeg mpg mts multi multisubs nordic ntsc pal pl proper r1 r3 r5 recut remastered repack rip rm screener se sse staffel stv subbed svcd swedish tc telecine telesync trp ts uc uncut unrated ur vcd vdr vob wmv ws x264 xvid xxx";
5656                str = ostrcat(str, replacelist, 1, 0);
5657        }
5658        else
5659        {
5660                char* replacelist = "disc0 disc1 disc2 disc3 disc4 0disc 1disc 2disc 3disc 4disc season0 season1 season2 season3 season4 season5 season6 season7 season8 season9 hdtv 720p 1080i 1080p cd0 cd1 cd2 cd3 cd4 cd5 cd6 cd7 cd8 cd9 dvd0 dvd1 dvd2 dvd3 dvd4";
5661                str = ostrcat(str, replacelist, 1, 0);
5662        }
5663
5664        char* replace = NULL;
5665        struct splitstr* ret1 = NULL;
5666        int count = 0;
5667        int i = 0;
5668        ret1 = strsplit(str, " ", &count);
5669        int max = count - 1;
5670        int first = 1;
5671
5672        for(i = 0; i < max; i++)
5673        {
5674                struct splitstr* ret2 = NULL;
5675                int count2 = 0;
5676                int j = 0;
5677                char *tmpstr = NULL;
5678                tmpstr = ostrcat(tmpstr, tmpfilename, 1, 0);
5679                ret2 = strsplit(tmpstr, " ,.-_", &count2);
5680
5681                for(j = 0; j < count2; j++)
5682                {
5683                        if(j > 0)
5684                        {                                       
5685                                if(ostrcmp((&ret1[i])->part, (&ret2[j])->part) == 0)
5686                                {
5687                                        if(mode==1)
5688                                        {
5689                                                tmpfilename = string_replace((&ret2[j])->part, replace, tmpfilename, 1);
5690                                                continue;
5691                                        }
5692                                        else if(mode==2)
5693                                        {
5694                                                tmpfilename = string_replace_remove_last_chars((&ret2[j])->part, replace, tmpfilename, 1);
5695                                                //break;
5696                                                continue;
5697                                        }
5698                                        else
5699                                        {
5700                                                tmpfilename = string_replace((&ret2[j])->part, replace, tmpfilename, 1);
5701                                                continue;
5702                                        }
5703                                }
5704                                else if(first == 1 && mode == 2)
5705                                {
5706//                                      printf("zahl: %s\n", (&ret2[j])->part);
5707                                        int theCharacter = atoi((&ret2[j])->part);
5708                                        if(theCharacter != 0)
5709                                        {
5710//                                              printf("found zahl: %s\n", (&ret2[j])->part);
5711                                                if(theCharacter > 1800 && theCharacter < 2100)
5712                                                {
5713//                                                      printf("found year: %s\n", (&ret2[j])->part);
5714                                                        tmpfilename = string_replace_remove_last_chars((&ret2[j])->part, "", tmpfilename, 1);
5715                                                        break;
5716                                                }
5717                                        }
5718                                }                               
5719                        }
5720                }
5721                free(ret2); ret2 = NULL;
5722                first = 0;
5723                free(tmpstr), tmpstr = NULL;
5724        }
5725
5726        free(ret1); ret1 = NULL;
5727        free(replace); replace = NULL;
5728        free(str); str = NULL;
5729
5730        debug(50, "out %s", tmpfilename);
5731        return tmpfilename;
5732}
5733
5734char* get_uuid(char* device)
5735{
5736        debug(60, "in %s", device);
5737        char* cmd = NULL, *tmpstr = NULL;
5738       
5739        if(device == NULL) return NULL;
5740#ifdef MIPSEL           
5741        cmd = ostrcat(cmd, "blkid -w /dev/null -c /dev/null -s UUID /dev/", 1, 0);
5742#else
5743        cmd = ostrcat(cmd, "/bin/blkid -w /dev/null -c /dev/null -s UUID /dev/", 1, 0);
5744#endif
5745        cmd = ostrcat(cmd, device, 1, 0);
5746        cmd = ostrcat(cmd, " | cut -d'\"' -f2", 1, 0);
5747
5748        tmpstr = string_newline(command(cmd));
5749
5750        if(ostrcmp(string_newline(tmpstr), "") == 0)
5751        {
5752                free(tmpstr); tmpstr = NULL;
5753        }
5754
5755        debug(60, "out %s", cmd);
5756        free(cmd); cmd = NULL;
5757        return tmpstr;
5758}
5759
5760char* get_label(char* device)
5761{
5762        debug(60, "in %s", device);
5763        char* cmd = NULL, *tmpstr = NULL;
5764
5765        if(device == NULL) return NULL;
5766#ifdef MIPSEL
5767        cmd = ostrcat(cmd, "blkid -w /dev/null -c /dev/null -s LABEL /dev/", 1, 0);
5768#else
5769        cmd = ostrcat(cmd, "/bin/blkid -w /dev/null -c /dev/null -s LABEL /dev/", 1, 0);
5770#endif 
5771        cmd = ostrcat(cmd, device, 1, 0);
5772        cmd = ostrcat(cmd, " | cut -d'\"' -f2", 1, 0);
5773
5774        tmpstr = string_newline(command(cmd));
5775
5776        if(tmpstr == NULL)
5777                tmpstr = ostrcat(tmpstr, "NONLABEL", 1, 0);
5778
5779        if(ostrcmp(string_newline(tmpstr), "") == 0)
5780                tmpstr = ostrcat(tmpstr, "NONLABEL", 1, 0);
5781
5782        debug(60, "out %s", cmd);
5783        free(cmd); cmd = NULL;
5784        return tmpstr;
5785}
5786
5787char* get_filesystem(char* device)
5788{
5789        debug(60, "in %s", device);
5790        char* cmd = NULL, *tmpstr = NULL;
5791
5792        if(device == NULL) return NULL;
5793#ifdef MIPSEL
5794        cmd = ostrcat(cmd, "blkid -w /dev/null -c /dev/null -s TYPE /dev/", 1, 0);
5795#else
5796        cmd = ostrcat(cmd, "/bin/blkid -w /dev/null -c /dev/null -s TYPE /dev/", 1, 0);
5797#endif
5798        cmd = ostrcat(cmd, device, 1, 0);
5799        cmd = ostrcat(cmd, " | cut -d'\"' -f2", 1, 0);
5800
5801        tmpstr = string_newline(command(cmd));
5802
5803        if(tmpstr == NULL)
5804                tmpstr = ostrcat(tmpstr, "NONTYPE", 1, 0);
5805
5806        if(ostrcmp(string_newline(tmpstr), "") == 0)
5807                tmpstr = ostrcat(tmpstr, "NONTYPE", 1, 0);
5808
5809        debug(60, "out %s", cmd);
5810        free(cmd); cmd = NULL;
5811        return tmpstr;
5812}
5813
5814//flag 0: convert ip to 000.000.000.000
5815//flag 1: convert ip to 0.0.0.0
5816char* fixip(char* ipinput, int flag)
5817{
5818        debug(60, "in %s", ipinput);
5819        int ret = 0;
5820        char* ipout = NULL;
5821        unsigned char ip[4];
5822       
5823        ip[0] = 0;
5824        ip[1] = 0;
5825        ip[2] = 0;
5826        ip[3] = 0;
5827
5828        if(ipinput == NULL) return NULL;
5829        ret = sscanf(ipinput, "%hhu.%hhu.%hhu.%hhu", &ip[0], &ip[1], &ip[2], &ip[3]);
5830        if(ret != 4) return NULL;
5831
5832        ipout = malloc(16);
5833        if(ipout == NULL)
5834        {
5835                err("no mem");
5836                return NULL;
5837        }
5838
5839        if(flag == 1)
5840                snprintf(ipout, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
5841        else
5842                snprintf(ipout, 16, "%03d.%03d.%03d.%03d", ip[0], ip[1], ip[2], ip[3]);
5843
5844        return ipout;
5845}
5846
5847void setfanspeed(int speed, int aktion)
5848{
5849        char* speedWert = NULL;
5850        char* speedSet = NULL;
5851        int base = 0;
5852
5853        if(checkbox("UFS922") == 1)
5854                base = 100;
5855        else
5856                base = -15;
5857               
5858
5859        if(speed < 0)
5860        {
5861                if(speed == -1)
5862                        speedWert = getconfig("fanspeed", NULL);
5863                else
5864                        speedWert = getconfig("fanspeedstandby", NULL);
5865                if(speedWert == NULL)
5866                        speedSet = ostrcat(speedSet, oitoa(base + 70), 1, 1);
5867                else
5868                        speedSet = ostrcat(speedSet, speedWert, 1, 0);
5869        }
5870        else
5871        {
5872                if(speed == 0)
5873                        speedSet = ostrcat(speedSet, oitoa(base + 15), 1, 1);
5874                else if(speed == 25)
5875                        speedSet = ostrcat(speedSet, oitoa(base + 30), 1, 1);
5876                else if(speed == 50)
5877                        speedSet = ostrcat(speedSet, oitoa(base + 45), 1, 1);
5878                else if(speed == 75)
5879                        speedSet = ostrcat(speedSet, oitoa(base + 55), 1, 1);
5880                else
5881                        speedSet = ostrcat(speedSet, oitoa(base + 70), 1, 1);
5882        }
5883
5884        if(checkbox("UFS922") == 1)
5885                writesys("/proc/stb/fan/fan_ctrl", speedSet, 1);
5886        else
5887                writesys("/proc/stb/fp/fan_pwm", speedSet, 1);
5888
5889        if(aktion == 1)
5890                addconfig("fanspeed", speedSet);
5891        else if(aktion == 2)
5892                addconfig("fanspeedstandby", speedSet);
5893
5894        free(speedSet); speedSet=NULL;
5895}
5896
5897void setaktres()
5898{
5899        int m_width = 0;
5900        char* res = NULL;
5901        char* res_akt = NULL;
5902        char* res_sd = NULL;
5903        int count = 1;
5904        int sec = 0;
5905
5906        m_lock(&status.setaktresmutex, 23);
5907
5908        if(status.restimer == NULL)
5909                goto end;
5910
5911        sec = (int)status.restimer->param1;
5912
5913        if(sec > 0)
5914        {
5915                while(status.restimer->aktion == START && count <= (sec * 10))
5916                {
5917                        usleep(100000);
5918                        if(status.restimer->aktion != START)
5919                                goto end;
5920                        count++;
5921                }
5922        }
5923        if(status.restimer->aktion != START) goto end;
5924
5925        if(videoreadqwidth(status.aktservice->videodev) == 0)
5926        {
5927                if(status.restimer->aktion != START) goto end;
5928                m_width = status.videosize.w;
5929                if(m_width == 720)
5930                {
5931                        res_sd = getconfig("av_videomode_autores_sd", NULL);
5932                        if(res_sd == NULL)
5933                                res = ostrcat(res, "576i50", 1, 0);
5934                        else
5935                                res = ostrcat(res, res_sd, 1, 0);
5936                }
5937                else if (m_width == 1280)
5938                        res = ostrcat(res, "720p50", 1, 0);
5939                else if (m_width == 1920)
5940                        res = ostrcat(res, "1080i50", 1, 0);
5941                else
5942                        m_width = 0;
5943
5944                if(m_width > 0)
5945                {
5946                        if(status.restimer->aktion != START) goto end;
5947                        res_akt = getvideomode();
5948                        if(status.restimer->aktion != START) goto end;
5949                        if(ostrcmp(res_akt, res) != 0)
5950                        {
5951                                if(status.restimer->aktion != START) goto end;
5952                                setvideomode(res, 1);
5953                                changefbresolution(res, 1);
5954/*
5955                                if(status.restimer->aktion != START) goto end;
5956
5957                                count = 0;
5958                                while(status.restimer->aktion == START && count < 10)
5959                                {
5960                                        usleep(100000);
5961                                        if(status.restimer->aktion != START)
5962                                                goto end;
5963                                        count++;
5964                                }
5965
5966                                if(status.restimer->aktion != START) goto end;
5967*/
5968                                screenautores(res, 5, 0);
5969                        }
5970                }
5971        }
5972        else
5973        {
5974                if(status.restimer->aktion != START) goto end;
5975                if(status.servicetype == 0)
5976                        textbox(_("Message"), _("ERROR cant read res"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 5, 0);
5977        }
5978
5979end:
5980        free(res); res = NULL;
5981        status.restimer = NULL;
5982        m_unlock(&status.setaktresmutex, 23);
5983        return;
5984}
5985
5986char* gettimestamp()
5987{
5988        char* timestamp = NULL;
5989        struct timeval numtime;
5990               
5991        gettimeofday(&numtime, 0);
5992        timestamp = ostrcat(timestamp, olutoa(numtime.tv_sec), 1, 1);
5993        timestamp = ostrcat(timestamp, olutoa(numtime.tv_usec), 1, 1);
5994
5995        return timestamp;
5996}
5997
5998char* string_decode(char* input, int flag)
5999{
6000        if(input == NULL) return input;
6001
6002        while(ostrstr(input, "\\u00") != NULL)
6003        {
6004                debug(210, "input: %s", input);
6005                input = string_replace("\\u00", "%", input, 1);
6006                debug(210, "input: %s", input);
6007        }
6008
6009        while(ostrstr(input, "&amp;") != NULL)
6010        {
6011                debug(210, "input: %s", input);
6012                input = string_replace("&amp;", "und", input, 1);
6013                debug(210, "input: %s", input);
6014        }
6015
6016        while(ostrstr(input, "&gt;") != NULL)
6017        {
6018                debug(210, "input: %s", input);
6019                input = string_replace("&gt;", ">", input, 1);
6020                debug(210, "input: %s", input);
6021        }
6022
6023        while(ostrstr(input, "&lt;") != NULL)
6024        {
6025                debug(210, "input: %s", input);
6026                input = string_replace("&lt;", "<", input, 1);
6027                debug(210, "input: %s", input);
6028        }
6029       
6030        while(ostrstr(input, "&quot;") != NULL)
6031        {
6032                debug(210, "input: %s", input);
6033                input = string_replace("&quot;", "\"", input, 1);
6034                debug(210, "input: %s", input);
6035        }
6036
6037        while(ostrstr(input, "&#x") != NULL)
6038        {
6039                debug(210, "out %s", input);
6040                input = string_replace("&#x", "%", input, 1);
6041                debug(210, "input: %s", input);
6042        }
6043       
6044        while(ostrstr(input, "&#") != NULL)
6045        {
6046                debug(210, "input: %s", input);
6047                input = string_replace("&#", "%", input, 1);
6048                debug(210, "input: %s", input);
6049        }
6050       
6051        if(flag == 1)
6052                htmldecode2(input, input);
6053        else
6054                htmldecode(input, input);
6055                       
6056        while(ostrstr(input, ";") != NULL)
6057        {
6058                debug(210, "input: %s", input);
6059                input = string_replace(";", NULL, input, 1);
6060                debug(210, "input: %s", input);
6061        }
6062
6063        return input;
6064}
6065
6066char* string_deltags(char* str)
6067{
6068        int i = 0, y = 0, len = 0;
6069
6070        if(str == NULL) return 0;
6071
6072        len = strlen(str);
6073
6074        int skip = 0;
6075        for(i = 0; i < len; i++)
6076        {
6077                if(str[i] == '<')
6078                        skip = 1;
6079                else if(str[i] == '>')
6080                        skip = 0;
6081
6082                if(skip == 0 && str[i] != '>')
6083                {
6084                        str[y] = str[i];
6085                        y++;
6086                }
6087        }
6088        str[y] = '\0';
6089
6090        return str;
6091}
6092
6093char* string_striptags(char* str)
6094{
6095        int i = 0, len = 0;
6096
6097        if(str == NULL) return 0;
6098       
6099        len = strlen(str);
6100
6101        int skip = 0;
6102        for(i = 0; i < len; i++)
6103        {
6104                if(str[i] == '<')
6105                        skip = 1;
6106                else if(str[i] == '>')
6107                {
6108                        skip = 0;
6109                        str[i] = ' ';
6110                }
6111                if(skip == 1)
6112                        str[i] = ' ';
6113        }       
6114
6115        return strstrip(str);
6116}
6117
6118char* string_resub(char* str, char* str2, char* input, int dir)
6119{
6120        char* tmpstr = NULL, *pos = NULL, *pos2 = NULL;
6121
6122        if(str == NULL || str2 == NULL || input == NULL) return NULL;
6123
6124        if(dir == 0)
6125        {
6126                pos = ostrstr(input, str);
6127                if(pos == NULL) return NULL;
6128                pos += strlen(str);
6129
6130                pos2 = ostrstr(pos, str2);
6131                if(pos2 == NULL) return NULL;
6132        }
6133        else
6134        {
6135                pos2 = ostrstr(input, str2);
6136                if(pos2 == NULL) return NULL;
6137
6138                pos = ostrrstr(input, str, pos2 - input, 0);
6139                if(pos == NULL) return NULL;
6140                pos += strlen(str);
6141        }
6142
6143        tmpstr = strndup(pos, pos2 - pos);
6144
6145        return strstrip(tmpstr);
6146}
6147
6148char* ostrstrcase(char* str, char* sub)
6149{
6150        size_t len = 0;
6151
6152        if(str == NULL || sub == NULL) return NULL;
6153
6154        len = strlen(sub);
6155        while(*str)
6156        {
6157                if(toupper(*str) == toupper(*sub) && strncasecmp(str, sub, len) == 0)
6158                        return str;     
6159                ++str;
6160        }
6161        return NULL;
6162}
6163
6164//flag 0: convert port to 00021
6165//flag 1: convert port to 21
6166char* fixport(char* input, int flag)
6167{
6168        char* tmpstr = NULL;
6169
6170        if(flag == 0)
6171        {
6172                int plen = strlen(input);
6173                if(plen < 5)
6174                {
6175                        int i;
6176                        for(i = 0; i < 5 - plen; i++)
6177                                tmpstr = ostrcat(tmpstr, "0", 1, 0);
6178                }
6179                tmpstr = ostrcat(tmpstr, input, 1, 0);         
6180        }
6181        else
6182        {
6183                while (*input && *input == '0')
6184                input++;
6185       
6186                tmpstr = ostrcat(tmpstr, input, 1, 0);         
6187        }
6188        return tmpstr;
6189}
6190
6191//flag 0: Mon > Montag
6192//flag 1: Mon > Mon
6193//flag 2: Monday > Montag
6194//flag 3: January > Januar
6195char* translate_time(char* input, int flag)
6196{
6197        char* tmpstr = NULL;
6198        if(input == NULL) return tmpstr;
6199        tmpstr = ostrcat(tmpstr, input, 1, 1); 
6200
6201        if(flag == 0)
6202        {
6203                tmpstr = string_replace_all("Mon", _("Monday"), tmpstr, 1);
6204                tmpstr = string_replace_all("Tue", _("Tuesday"), tmpstr, 1);
6205                tmpstr = string_replace_all("Wed", _("Wednesday"), tmpstr, 1);
6206                tmpstr = string_replace_all("Thu", _("Thursday"), tmpstr, 1);
6207                tmpstr = string_replace_all("Fri", _("Friday"), tmpstr, 1);
6208                tmpstr = string_replace_all("Sat", _("Saturday"), tmpstr, 1);
6209                tmpstr = string_replace_all("Sun", _("Sunday"), tmpstr, 1);
6210        }
6211        else if(flag == 2)
6212        {
6213                tmpstr = string_replace_all("Monday", _("Monday"), tmpstr, 1);
6214                tmpstr = string_replace_all("Tuesday", _("Tuesday"), tmpstr, 1);
6215                tmpstr = string_replace_all("Wednesday", _("Wednesday"), tmpstr, 1);
6216                tmpstr = string_replace_all("Thursday", _("Thursday"), tmpstr, 1);
6217                tmpstr = string_replace_all("Friday", _("Friday"), tmpstr, 1);
6218                tmpstr = string_replace_all("Saturday", _("Saturday"), tmpstr, 1);
6219                tmpstr = string_replace_all("Sunday", _("Sunday"), tmpstr, 1);
6220        }
6221        else if(flag == 3)
6222        {
6223                tmpstr = string_replace_all("January", _("January"), tmpstr, 1);
6224                tmpstr = string_replace_all("February", _("February"), tmpstr, 1);
6225                tmpstr = string_replace_all("March", _("March"), tmpstr, 1);
6226                tmpstr = string_replace_all("April", _("April"), tmpstr, 1);
6227                tmpstr = string_replace_all("May", _("May"), tmpstr, 1);
6228                tmpstr = string_replace_all("June", _("June"), tmpstr, 1);
6229                tmpstr = string_replace_all("July", _("July"), tmpstr, 1);
6230                tmpstr = string_replace_all("August", _("August"), tmpstr, 1);
6231                tmpstr = string_replace_all("September", _("September"), tmpstr, 1);
6232                tmpstr = string_replace_all("October", _("October"), tmpstr, 1);
6233                tmpstr = string_replace_all("November", _("November"), tmpstr, 1);
6234                tmpstr = string_replace_all("December", _("December"), tmpstr, 1);
6235        }
6236        else
6237        {
6238                tmpstr = string_replace("Mon", _("Mon"), tmpstr, 1);
6239                tmpstr = string_replace("Tue", _("Tue"), tmpstr, 1);
6240                tmpstr = string_replace("Wed", _("Wed"), tmpstr, 1);
6241                tmpstr = string_replace("Thu", _("Thu"), tmpstr, 1);
6242                tmpstr = string_replace("Fri", _("Fri"), tmpstr, 1);
6243                tmpstr = string_replace("Sat", _("Sat"), tmpstr, 1);
6244                tmpstr = string_replace("Sun", _("Sun"), tmpstr, 1);
6245        }
6246
6247        return tmpstr;
6248}
6249
6250char* gethypridtunerchoices(int dev)
6251{
6252        char *value = NULL;
6253        char *hypridtunerchoices = NULL;
6254        char *tmpstr = NULL;
6255        char *tmpstr1 = NULL;
6256        char* start = NULL;
6257
6258#ifdef MIPSEL
6259        char *tmpstr2 = NULL;
6260        char *tmpstr3 = NULL;
6261#endif
6262
6263
6264        hypridtunerchoices = getconfig("hypridtunerchoices", NULL);
6265
6266        if(hypridtunerchoices == NULL)
6267        {
6268                err("NULL detect");
6269                return NULL;
6270        }
6271
6272        tmpstr = readfiletomem(hypridtunerchoices, 1);
6273        if(tmpstr == NULL)
6274        {
6275                err("NULL detect");
6276                return NULL;
6277        }
6278
6279        start = ostrcat("NIM Socket ", oitoa(dev), 0, 0);
6280        start = ostrcat(start, ":", 1, 0);
6281
6282#ifdef MIPSEL
6283
6284        tmpstr1 = string_resub(start, "I2C_Device", tmpstr, 0);
6285        tmpstr2 = string_resub("Mode ", ":", tmpstr1, 0);
6286        tmpstr3 = oregex(".*Mode ([0-9]{1}):.*", tmpstr1);
6287
6288        if(tmpstr2 != NULL)
6289                value = ostrcat(tmpstr2, "\n", 0, 0);
6290        if(tmpstr3 != NULL)
6291                value = ostrcat(value, tmpstr3, 1, 0);
6292
6293        free(start), start = NULL;
6294        free(tmpstr), tmpstr = NULL;
6295        free(tmpstr1), tmpstr1 = NULL;
6296        free(tmpstr2), tmpstr2 = NULL;
6297        free(tmpstr3), tmpstr3 = NULL;
6298#else
6299        tmpstr1 = string_resub(start, "Frontend_Device", tmpstr, 0);
6300        free(start), start = NULL;
6301
6302        printf("---gethypridtunerchoices--------------------------------\n");
6303        printf("tmpstr1: %s\n", tmpstr1);
6304
6305        if(ostrstr(tmpstr1, "(T/C)") != NULL)
6306        {
6307                value = ostrcat("t", "\n", 0, 0);
6308                value = ostrcat(value, "c", 1, 0);
6309        }
6310        printf("value: %s\n", value);
6311        printf("---gethypridtunerchoices--------------------------------\n");
6312
6313        free(tmpstr1), tmpstr1 = NULL;
6314#endif
6315        return value;
6316}
6317
6318char* gethypridtunerchoicesvalue(int dev)
6319{
6320        char* hypridtunerchoices = NULL, *value = NULL, *tmpstr = NULL, *tmpstr1 = NULL, *start = NULL;
6321
6322#ifdef MIPSEL
6323        char* tmpstr2 = NULL, *hypridlist = NULL;
6324#endif
6325
6326        hypridtunerchoices = getconfig("hypridtunerchoices", NULL);
6327
6328        if(hypridtunerchoices == NULL)
6329        {
6330                err("NULL detect");
6331                return NULL;
6332        }
6333
6334        tmpstr = readfiletomem(hypridtunerchoices, 1);
6335        if(tmpstr == NULL)
6336        {
6337                err("NULL detect");
6338                return NULL;
6339        }
6340
6341        start = ostrcat("NIM Socket ", oitoa(dev), 0, 0);
6342        start = ostrcat(start, ":", 1, 0);
6343#ifdef MIPSEL
6344        tmpstr1 = string_resub(start, "I2C_Device", tmpstr, 0);
6345        free(start), start = NULL;
6346
6347        hypridlist = gethypridtunerchoices(dev);
6348
6349        int count = 0;
6350        int i = 0;
6351        struct splitstr* ret1 = NULL;
6352        ret1 = strsplit(hypridlist, "\n", &count);
6353
6354        if(ret1 != NULL)
6355        {
6356                int max = count;
6357                for(i = 0; i < max; i++)
6358                {
6359                        start = ostrcat("Mode ", ret1[i].part, 0, 0);
6360                        start = ostrcat(start, ":", 1, 0);
6361                        tmpstr2 = string_resub(start, "\n", tmpstr1, 0);
6362               
6363                        if(i > 0)
6364                                value = ostrcat(value, "\n", 1, 0);
6365               
6366                        if(tmpstr2 != NULL)
6367                                value = ostrcat(value, tmpstr2, 1, 0);
6368                               
6369                        free(tmpstr2), tmpstr2 = NULL;
6370                        free(start), start = NULL;
6371                }
6372        }
6373        free(ret1), ret1 = NULL;                       
6374        free(tmpstr1), tmpstr1 = NULL;
6375        free(hypridlist), hypridlist = NULL;
6376#else
6377        tmpstr1 = string_resub(start, "Frontend_Device", tmpstr, 0);
6378        free(start), start = NULL;
6379
6380        printf("---gethypridtunerchoicesvalue--------------------------------\n");
6381        printf("tmpstr1: %s\n", tmpstr1);
6382
6383        if(ostrstr(tmpstr1, "(T/C)") != NULL)
6384        {
6385                value = ostrcat("DVB-T", "\n", 0, 0);
6386                value = ostrcat(value, "DVB-C", 1, 0);
6387        }
6388        printf("value: %s\n", value);
6389        printf("---gethypridtunerchoicesvalue--------------------------------\n");
6390        free(tmpstr1), tmpstr1 = NULL;
6391#endif
6392
6393        return value;
6394}
6395
6396char* gethypridtunerchoicesvaluename(int dev, char* hyprid)
6397{
6398
6399        char* value = NULL;
6400#ifdef MIPSEL
6401        char* hypridtunerchoices = NULL, *tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL, *start = NULL;
6402
6403        hypridtunerchoices = getconfig("hypridtunerchoices", NULL);
6404
6405        if(hypridtunerchoices == NULL)
6406        {
6407                err("NULL detect");
6408                return NULL;
6409        }
6410
6411        tmpstr = readfiletomem(hypridtunerchoices, 1);
6412        if(tmpstr == NULL)
6413        {
6414                err("NULL detect");
6415                return NULL;
6416        }
6417
6418        start = ostrcat("NIM Socket ", oitoa(dev), 0, 0);
6419        start = ostrcat(start, ":", 1, 0);
6420        tmpstr1 = string_resub(start, "I2C_Device", tmpstr, 0);
6421        free(start), start = NULL;
6422
6423        start = ostrcat("Mode ", hyprid, 0, 0);
6424        start = ostrcat(start, ":", 1, 0);
6425
6426        tmpstr2 = string_resub(start, "\n", tmpstr1, 0);
6427       
6428        if(tmpstr2 != NULL)
6429                value = ostrcat(value, tmpstr2, 1, 0);
6430
6431        free(tmpstr1), tmpstr1 = NULL;
6432        free(tmpstr2), tmpstr2 = NULL;
6433        free(start), start = NULL;
6434#else
6435        printf("---gethypridtunerchoicesvalue--------------------------------\n");
6436        if(ostrstr(hyprid, "t") != NULL)
6437                value = ostrcat("DVB-T", NULL, 0, 0);
6438        else if(ostrstr(hyprid, "c") != NULL)
6439                value = ostrcat("DVB-C", NULL, 0, 0);
6440        printf("value: %s\n", value);
6441        printf("---gethypridtunerchoicesvalue--------------------------------\n");
6442
6443#endif
6444        return value;
6445}
6446
6447int sethypridtuner(int dev, char* value)
6448{
6449        int ret = 0;
6450
6451#ifdef MIPSEL
6452        char* buf = NULL, *hypridtuner = NULL, *tmpstr = NULL;
6453
6454        hypridtuner = getconfig("hypridtuner", NULL);
6455       
6456        if(hypridtuner != NULL)
6457        {
6458                buf = malloc(MINMALLOC);
6459                if(buf == NULL)
6460                {
6461                        err("no memory");
6462                        return 0;
6463                }
6464        }
6465
6466        sprintf(buf, hypridtuner, dev);
6467        if(buf != NULL)
6468        {
6469                printf("set %s to %s\n", buf, value);
6470                ret = writesys(buf, value, 0);
6471                free(tmpstr); tmpstr = NULL;
6472                return ret;
6473        }
6474#else
6475        printf("---sethypridtuner--------------------------------\n");
6476
6477        addconfigtmp("hypridtuner", value);
6478        ret = writeconfigtmp();
6479        writeallconfig(1);
6480
6481        printf("value: %s\n", value);
6482        printf("read titan.cfg: hypridtuner=%s\n", getconfig("frontenddev", NULL));
6483        printf("---sethypridtuner--------------------------------\n");
6484
6485        return ret;
6486#endif
6487        return 0;
6488}
6489
6490int phpkit_userauth(char* link, char* user, char* pass)
6491{
6492        debug(99, "phpkit user: %s", user);
6493        debug(99, "phpkit pass: %s", pass);     
6494        debug(99, "phpkit url: %s", link);
6495
6496        int skip = 0;
6497        char* ip = NULL, *tmphost = NULL, *tmppath = NULL, *tmpstr = NULL, *send = NULL, *hash = NULL, *cookie1 = NULL, *cookie2 = NULL, *tmplink = NULL, *pos = NULL, *path = NULL, *hashlen = NULL;
6498
6499        tmplink = ostrcat(link, NULL, 0, 0);
6500
6501        tmphost = string_replace("http://", "", tmplink, 0);
6502        free(tmplink) , tmplink = NULL;
6503
6504        if(tmphost != NULL)
6505                pos = strchr(tmphost, '/');
6506        if(pos != NULL)
6507        {
6508                pos[0] = '\0';
6509                path = pos + 1;
6510        }
6511
6512        tmppath = ostrcat("/", path, 0, 0);
6513
6514        send = ostrcat(send, "GET ", 1, 0);
6515        send = ostrcat(send, tmppath, 1, 0);
6516        send = ostrcat(send, " HTTP/1.1\r\nHost: ", 1, 0);     
6517        send = ostrcat(send, tmphost, 1, 0);
6518        send = ostrcat(send, "\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1\r\nConnection: close\r\nAccept-Encoding: gzip\r\n\r\n", 1, 0);       
6519        debug(99, "#############################################################################################################");
6520        debug(99, "send1: %s", send);
6521        debug(99, "#############################################################################################################");
6522
6523        tmpstr = gethttpreal(tmphost, tmppath, 80, NULL, NULL, NULL, 0, send, NULL, 5000, 1);
6524        debug(99, "tmpstr: %s", tmpstr);
6525
6526        free(send), send = NULL;
6527        free(tmpstr), tmpstr = NULL;
6528
6529        if(user == NULL || pass == NULL || link == NULL) return 1;
6530
6531        hash = ostrcat("login=1&user=", user, 0, 0);
6532        hash = ostrcat(hash, "&userpw=", 1, 0);
6533        hash = ostrcat(hash, pass, 1, 1);
6534        hashlen = oitoa(strlen(hash));
6535       
6536        send = ostrcat(send, "POST ", 1, 0);
6537        send = ostrcat(send, tmppath, 1, 0);
6538        send = ostrcat(send, " HTTP/1.1\r\nContent-Length: ", 1, 0);
6539        send = ostrcat(send, hashlen, 1, 0);
6540        send = ostrcat(send, "\r\nAccept-Encoding: gzip\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1\r\nHost: ", 1, 0);
6541        send = ostrcat(send, tmphost, 1, 0);
6542        send = ostrcat(send, "\r\nCookie: pageredir=", 1, 0);
6543        send = ostrcat(send, cookie1, 1, 0);
6544        send = ostrcat(send, "; PHPSESSID=", 1, 0);     
6545        send = ostrcat(send, cookie2, 1, 0);
6546        send = ostrcat(send, "\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n", 1, 0);     
6547        send = ostrcat(send, hash, 1, 0);
6548        free(hash); hash = NULL;
6549        free(hashlen); hashlen = NULL;
6550
6551        debug(99, "#############################################################################################################");
6552        debug(99, "send1: %s", send);
6553        debug(99, "#############################################################################################################");
6554
6555        tmpstr = gethttpreal(tmphost, tmppath, 80, NULL, NULL, NULL, 0, send, NULL, 5000, 1);
6556        debug(99, "tmpstr: %s", tmpstr);
6557
6558        free(cookie1); cookie1 = NULL;
6559        free(cookie2); cookie2 = NULL;
6560        free(tmphost); tmphost = NULL;
6561        free(send); send = NULL;
6562        free(ip); ip = NULL;
6563        if(tmpstr == NULL) skip = 1;
6564        free(tmpstr); tmpstr = NULL;
6565        if(skip == 1) return 1;
6566        return 0;
6567}
6568
6569void changetunername(struct skin* tunernode, int adapter, int devnr, char* name, char* fehyprid)
6570{
6571        char* tmpnr = NULL, *tmpstr = NULL;
6572        tmpnr = oitoa(adapter);
6573        tmpstr = ostrcat(_("Tuner "), tmpnr, 0, 1);
6574        tmpstr = ostrcat(tmpstr, "/", 1, 0);
6575        tmpnr = oitoa(devnr);
6576        tmpstr = ostrcat(tmpstr, tmpnr, 1, 1);
6577        tmpstr = ostrcat(tmpstr, ": ", 1, 0);
6578        tmpstr = ostrcat(tmpstr, name, 1, 0);
6579        if(fehyprid != NULL)
6580        {
6581                tmpstr = ostrcat(tmpstr, " (", 1, 0);
6582                tmpstr = ostrcat(tmpstr, _("Multituner adjustable"), 1, 0);
6583                tmpstr = ostrcat(tmpstr, ")", 1, 0);
6584        }
6585        changetext(tunernode, tmpstr);
6586        free(tmpstr); tmpstr = NULL;
6587}
6588
6589char* gethypridtunername(int dev, char* hyprid)
6590{
6591        char* hypridtunerchoices = NULL, *value = NULL, *tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL, *start = NULL;
6592
6593        hypridtunerchoices = getconfig("hypridtunerchoices", NULL);
6594
6595        if(hypridtunerchoices == NULL)
6596        {
6597                err("NULL detect");
6598                return NULL;
6599        }
6600
6601        tmpstr = readfiletomem(hypridtunerchoices, 1);
6602        if(tmpstr == NULL)
6603        {
6604                err("NULL detect");
6605                return NULL;
6606        }
6607
6608        start = ostrcat("NIM Socket ", oitoa(dev), 0, 0);
6609        start = ostrcat(start, ":", 1, 0);
6610        tmpstr1 = string_resub(start, "I2C_Device", tmpstr, 0);
6611        free(start), start = NULL;
6612
6613        tmpstr2 = string_resub("Name: ", "\n", tmpstr1, 0);
6614       
6615        if(tmpstr2 != NULL)
6616                value = ostrcat(value, tmpstr2, 1, 0);
6617
6618        free(tmpstr1), tmpstr1 = NULL;
6619        free(tmpstr2), tmpstr2 = NULL;
6620        free(start), start = NULL;
6621
6622        return value;
6623}
6624
6625void convertsettings()
6626{
6627        struct menulist* mlist = NULL, *mbox = NULL;
6628        struct skin* load = getscreen("loading");
6629        int flag = 0, ret = 0;
6630        char* tmpstr = NULL;
6631
6632        addmenulist(&mlist, "Create Transponder (Sat)", _("Create Transponder (Sat)"), NULL, 0, 0);
6633        addmenulist(&mlist, "Create Transponder (Cable)", _("Create Transponder (Cable)"), NULL, 0, 0);
6634        addmenulist(&mlist, "Create Transponder (Terrestrial)", _("Create Transponder (Terrestrial)"), NULL, 0, 0);
6635        addmenulist(&mlist, "Create Transponder (All)", _("Create Transponder (All)"), NULL, 0, 0);
6636
6637        mbox = menulistbox(mlist, "createsettings", _("Select Your Serach Modus"), NULL, NULL, NULL, 1, 0);
6638        if(mbox != NULL) tmpstr = mbox->name;
6639
6640        if(ostrcmp(tmpstr, "Create Transponder (Sat)") == 0)
6641                flag = 0;
6642        else if(ostrcmp(tmpstr, "Create Transponder (Cable)") == 0)
6643                flag = 1;
6644        else if(ostrcmp(tmpstr, "Create Transponder (Terrestrial)") == 0)
6645                flag = 2;
6646        else if(ostrcmp(tmpstr, "Create Transponder (All)") == 0)
6647                flag = 3;
6648        else
6649        {
6650                freemenulist(mlist, 1); mlist = NULL;
6651                return;
6652        }
6653
6654        freemenulist(mlist, 1); mlist = NULL;
6655
6656        drawscreen(load, 0, 0);
6657
6658        if(flag == 0)
6659        {
6660                ret = converte2settings(0);
6661                if(ret == 0) return;
6662                system("cp -a /tmp/satellites.sat /mnt/settings/satellites");
6663                system("cp -a /tmp/transponder.sat /mnt/settings/transponder");
6664        }
6665        else if(flag == 1)
6666        {
6667                ret = converte2settings(1);
6668                if(ret == 0) return;
6669                system("cp -a /tmp/satellites.cable /mnt/settings/satellites");
6670                system("cp -a /tmp/transponder.cable /mnt/settings/transponder");
6671        }
6672        else if(flag == 2)
6673        {
6674                converte2settings(2);
6675                system("cp -a /tmp/satellites.ter /mnt/settings/satellites");
6676                system("cp -a /tmp/transponder.ter /mnt/settings/transponder");
6677        }
6678        else if(flag == 3)
6679        {
6680                ret = converte2settings(0);
6681                if(ret == 0) return;
6682                ret = converte2settings(1);
6683                if(ret == 0) return;
6684                ret = converte2settings(2);
6685                if(ret == 0) return;
6686                system("cp -a /tmp/satellites.sat /mnt/settings/satellites");
6687                system("cp -a /tmp/transponder.sat /mnt/settings/transponder");
6688
6689                system("cat /tmp/satellites.cable >> /mnt/settings/satellites");
6690                system("cat /tmp/transponder.cable >> /mnt/settings/transponder");
6691
6692                system("cat /tmp/satellites.ter >> /mnt/settings/satellites");
6693                system("cat /tmp/transponder.ter >> /mnt/settings/transponder");
6694        }
6695
6696//      free(tmpstr), tmpstr = NULL;
6697        if(textbox(_("Message"), _("Transponder/Satellite Convert done, please use channel search on next Boot.\nYour System will reboot !"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 1000, 200, 0, 0) == 1)
6698        {       
6699                //write only config file
6700                system("sync");
6701                writeallconfig(3);
6702                oshutdown(2,2);
6703                system("init 6");
6704        }
6705}
6706
6707// flag 0 = sat
6708// flag 1 = cable
6709// flag 2 = ter
6710int converte2settings(int flag)
6711{
6712        char* path = NULL, *buf = NULL, *tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL, *line = NULL, *name = NULL, *orbitalpos = NULL, *fetype = NULL, *flags = NULL, *outfile = NULL, *start = NULL, *end = NULL, *filename = NULL, *transponderfile = NULL, *satfile = NULL;
6713        int incount = 0;
6714       
6715        if(flag == 0)
6716        {
6717                system("rm -rf /tmp/transponder.sat");
6718                system("rm -rf /tmp/satellites.sat");
6719                system("cp -a /var/etc/tuxbox/satellites.xml /tmp/satellites.xml");
6720                start = ostrcat("<sat ", NULL, 0, 0);
6721                end = ostrcat("</sat>", NULL, 0, 0);
6722                filename = ostrcat("/tmp/satellites.xml", NULL, 0, 0);
6723                path = ostrcat("/transponder/satellites.xml", NULL, 0, 0);
6724                transponderfile = ostrcat("/tmp/transponder.sat", NULL, 0, 0);
6725                satfile = ostrcat("/tmp/satellites.sat", NULL, 0, 0);
6726                fetype = ostrcat("0", NULL, 0, 0);
6727        }
6728        else if(flag == 1)
6729        {
6730                system("rm -rf /tmp/transponder.cable");
6731                system("rm -rf /tmp/satellites.cable");
6732                system("cp -a /var/etc/tuxbox/cables.xml /tmp/cables.xml");
6733                start = ostrcat("<cable ", NULL, 0, 0);
6734                end = ostrcat("</cable>", NULL, 0, 0);
6735                filename = ostrcat("/tmp/cables.xml", NULL, 0, 0);
6736                path = ostrcat("/transponder/cables.xml", NULL, 0, 0);
6737                transponderfile = ostrcat("/tmp/transponder.cable", NULL, 0, 0);
6738                satfile = ostrcat("/tmp/satellites.cable", NULL, 0, 0);
6739                fetype = ostrcat("1", NULL, 0, 0);
6740                incount = 4999;
6741        }
6742        else if(flag == 2)
6743        {
6744                system("rm -rf /tmp/transponder.ter");
6745                system("rm -rf /tmp/satellites.ter");
6746                system("cp -a /var/etc/tuxbox/terrestrial.xml /tmp/terrestrial.xml");
6747                start = ostrcat("<terrestrial ", NULL, 0, 0);
6748                end = ostrcat("</terrestrial>", NULL, 0, 0);
6749                filename = ostrcat("/tmp/terrestrial.xml", NULL, 0, 0);
6750                path = ostrcat("/transponder/terrestrial.xml", NULL, 0, 0);
6751                transponderfile = ostrcat("/tmp/transponder.ter", NULL, 0, 0);
6752                satfile = ostrcat("/tmp/satellites.ter", NULL, 0, 0);
6753                fetype = ostrcat("2", NULL, 0, 0);
6754                incount = 9999;
6755        }
6756
6757        if(!file_exist(filename))
6758                gethttp("atemio.dyndns.tv", path, 80, filename, HTTPAUTH, 5000, NULL, 0);
6759
6760        char* tmptext = NULL;
6761        tmptext = ostrcat(_("Error: Transponder Source file not found"), "\nfilename: ", 0, 0);
6762        tmptext = ostrcat(tmptext, transponderfile, 1, 0);
6763
6764        if(!file_exist(filename))
6765        {
6766                textbox(_("Message"), tmptext, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 1000, 200, 0, 0);
6767                free(tmptext), tmptext = NULL;
6768                free(path), path = NULL;
6769                free(filename), filename = NULL;
6770                free(satfile), satfile = NULL;
6771                free(fetype), fetype = NULL;
6772                free(start), start = NULL;
6773                free(end), end = NULL;
6774                free(transponderfile), transponderfile = NULL;
6775                return 0;
6776        }
6777        free(tmptext), tmptext = NULL;
6778
6779        buf = readfiletomem(filename, 1);
6780        if(buf == NULL)
6781        {
6782                printf("buf: %s\n", buf);       
6783                tmptext = ostrcat(_("Error: Transponder Source file no data"), "\nfilename: ", 0, 0);
6784                tmptext = ostrcat(tmptext, transponderfile, 1, 0);
6785                textbox(_("Message"), tmptext, _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 1000, 200, 0, 0);
6786                free(tmptext), tmptext = NULL;
6787                free(path), path = NULL;
6788                free(filename), filename = NULL;
6789                free(satfile), satfile = NULL;
6790                free(fetype), fetype = NULL;
6791                free(start), start = NULL;
6792                free(end), end = NULL;
6793                free(transponderfile), transponderfile = NULL;
6794                return 0;
6795        }
6796       
6797//      writesys("/tmp/convert.log", buf, 1);
6798
6799        while(ostrstr(buf, start) != NULL)
6800        {
6801                incount++;
6802                tmpstr = string_resub(start, end, buf, 0);
6803                tmpstr1 = ostrcat(tmpstr, NULL, 0, 0);
6804               
6805                //printf("name: %s\n", getxmlentry(ret1[i].part, "name="));
6806                //printf("position: %s\n", getxmlentry(ret1[i].part, "position="));
6807               
6808                name = getxmlentry(tmpstr, "name=");
6809                if(flag == 0)
6810                        orbitalpos = getxmlentry(tmpstr, "position=");
6811                else
6812                        orbitalpos = ostrcat(oitoa(incount), NULL, 1, 0);
6813                       
6814                flags = getxmlentry(tmpstr, "flags=");
6815                //string_decode(name, 0);
6816                name = string_replace_all("&amp;", "und", name, 1);
6817       
6818                line = ostrcat(line, name, 1, 0); //name
6819                line = ostrcat(line, "#", 1, 0);
6820//              line = ostrcat(line, flags, 1, 0); //flag
6821// 0 for all sat/ter/cab ???
6822                line = ostrcat(line, "0", 1, 0);
6823                line = ostrcat(line, "#", 1, 0);
6824                line = ostrcat(line, orbitalpos, 1, 0); //orbitalpos
6825                line = ostrcat(line, "#", 1, 0);
6826                line = ostrcat(line, fetype, 1, 0); //fetype
6827                printf("%s: %s\n", satfile, line);
6828                writesys(satfile, line, 3);
6829                free(line), line = NULL;
6830                               
6831                int count = 0;
6832
6833                int i = 0;
6834                struct splitstr* ret1 = NULL;
6835                ret1 = strsplit(tmpstr1, "\n", &count);
6836                if(ret1 != NULL)
6837                {
6838                        int max = count;
6839                        for(i = 0; i < max; i++)
6840                        {
6841                                if(i == 0) continue;
6842                                line = ostrcat(line, "0", 1, 0); //id
6843                                line = ostrcat(line, "#", 1, 0);
6844
6845                                line = ostrcat(line, fetype, 1, 0); //fetype
6846                                line = ostrcat(line, "#", 1, 0);
6847
6848                                if(ostrstr((ret1[i]).part, "frequency=") != NULL)
6849                                        line = ostrcat(line, getxmlentry(ret1[i].part, "frequency="), 1, 0); //frequency
6850                                else
6851                                        line = ostrcat(line, "-1", 1, 0);
6852                                line = ostrcat(line, "#", 1, 0);
6853
6854                                if(ostrstr((ret1[i]).part, "polarization=") != NULL)
6855                                        line = ostrcat(line, getxmlentry(ret1[i].part, "polarization="), 1, 0); //polarization
6856                                else
6857                                        line = ostrcat(line, "-1", 1, 0); //polarization
6858
6859                                line = ostrcat(line, "#", 1, 0);
6860                                if(orbitalpos != NULL)
6861                                        line = ostrcat(line, orbitalpos, 1, 0); //orbitalpos
6862                                else
6863                                        line = ostrcat(line, "-1", 1, 0); //orbitalpos
6864                                line = ostrcat(line, "#", 1, 0);
6865
6866                                if(ostrstr((ret1[i]).part, "symbol_rate=") != NULL)
6867                                        line = ostrcat(line, getxmlentry(ret1[i].part, "symbol_rate="), 1, 0); //symbolrate
6868                                else
6869                                        line = ostrcat(line, "-1", 1, 0);
6870                                line = ostrcat(line, "#", 1, 0);
6871
6872                                if(ostrstr((ret1[i]).part, "modulation=") != NULL)
6873                                        line = ostrcat(line, getxmlentry(ret1[i].part, "modulation="), 1, 0); //modulation
6874                                else
6875                                {
6876                                        if(flag == 2)
6877                                                line = ostrcat(line, "3", 1, 0);
6878                                        else
6879                                                line = ostrcat(line, "0", 1, 0);
6880                                }
6881                                line = ostrcat(line, "#", 1, 0);
6882
6883                                if(ostrstr((ret1[i]).part, "fec_inner=") != NULL)
6884                                        line = ostrcat(line, getxmlentry(ret1[i].part, "fec_inner="), 1, 0); //fec
6885                                else
6886                                {
6887                                        if(flag == 2)
6888                                                line = ostrcat(line, "5", 1, 0);
6889                                        else
6890                                                line = ostrcat(line, "0", 1, 0);
6891                                }
6892                                line = ostrcat(line, "#", 1, 0);
6893
6894                                if(checkbox("UFS910") == 1)
6895                                        line = ostrcat(line, "1", 1, 0); //pilot
6896                                else
6897                                        line = ostrcat(line, "2", 1, 0); //pilot
6898                                line = ostrcat(line, "#", 1, 0);
6899
6900                                if(checkbox("UFS910") == 1)
6901                                        line = ostrcat(line, "0", 1, 0); //rolloff
6902                                else
6903                                        line = ostrcat(line, "3", 1, 0); //rolloff
6904                                line = ostrcat(line, "#", 1, 0);
6905
6906                                line = ostrcat(line, "2", 1, 0); //inversion
6907                                line = ostrcat(line, "#", 1, 0);
6908
6909                                if(ostrstr((ret1[i]).part, "system=") != NULL)
6910                                        line = ostrcat(line, getxmlentry(ret1[i].part, "system="), 1, 0); //system
6911                                else
6912                                        line = ostrcat(line, "0", 1, 0); //system
6913                                line = ostrcat(line, "\n", 1, 0);
6914                        }
6915                }
6916       
6917                tmpstr2 = ostrcat(start, tmpstr, 0, 0);
6918       
6919                buf = string_replace(tmpstr2, NULL, buf, 1);
6920       
6921        //      outfile = ostrcat("/tmp/convert.", oitoa(incount), 0, 1);
6922        //      outfile = ostrcat(outfile, ".log", 1, 0);       
6923        //      writesys(outfile, buf, 2);
6924        //      writesys("/tmp/convert.log", buf, 3);
6925       
6926                writesys(transponderfile, line, 2);
6927                free(line), line = NULL;
6928       
6929                free(tmpstr), tmpstr = NULL;   
6930                free(tmpstr1), tmpstr1 = NULL;
6931                free(tmpstr2), tmpstr2 = NULL;
6932                free(ret1), ret1 = NULL;
6933                free(name), name = NULL;
6934                free(orbitalpos), orbitalpos = NULL;
6935                free(flags), flags = NULL;
6936                free(outfile), outfile = NULL; 
6937        }
6938
6939        free(path), path = NULL;
6940        free(filename), filename = NULL;
6941        free(satfile), satfile = NULL;
6942        free(transponderfile), transponderfile = NULL;
6943               
6944        free(buf), buf = NULL;
6945        free(start), start = NULL;
6946        free(end), end = NULL;
6947        free(fetype), fetype = NULL;
6948                       
6949        return 1;
6950}
6951
6952void createfav()
6953{
6954        struct skin* load = getscreen("loading");
6955        drawscreen(load, 0, 0);
6956
6957        system("rm -rf /mnt/settings/bouquets*");
6958        system("cp -a /etc/titan.restore/mnt/settings/bouquets* /mnt/settings");
6959
6960        if(textbox(_("Message"), _("Standard favorites have been successfully applied.\nYour System will reboot !"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 1000, 200, 0, 0) == 1)
6961        {
6962                //write only config file
6963                system("sync");
6964                writeallconfig(3);
6965                oshutdown(2,2);
6966                system("init 6");
6967        }
6968}
6969
6970char* system_infos(int mode)
6971{
6972        char* tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL;
6973
6974        if(mode == 0)
6975        {
6976                tmpstr1 = ostrcat("Date = ", "", 0, 0);
6977                tmpstr1 = ostrcat(tmpstr1, gettime(NULL, "%d %B %Y"), 1, 1);
6978                tmpstr = ostrcat(tmpstr, tmpstr1, 1, 1);
6979
6980                tmpstr1 = ostrcat("\nTime = ", "", 0, 0);
6981                tmpstr1 = ostrcat(tmpstr1, gettime(NULL, "%H:%M:%S"), 1, 1);
6982                tmpstr = ostrcat(tmpstr, tmpstr1, 1, 1);
6983               
6984                tmpstr1 = ostrcat("\nUptime = ", "", 0, 0);
6985                tmpstr2 = command("uptime");
6986                if(tmpstr2 != NULL) tmpstr2 = strtok(tmpstr2, ",");
6987                tmpstr1 = ostrcat(tmpstr1, tmpstr2, 1, 1);
6988                tmpstr = ostrcat(tmpstr, tmpstr1, 1, 1);
6989
6990                tmpstr1 = ostrcat("\nBoxtype = ", "", 0, 0);
6991                tmpstr2 = string_toupper(command("cat /etc/model"));
6992                tmpstr1 = ostrcat(tmpstr1, tmpstr2, 1, 1);
6993                tmpstr = ostrcat(tmpstr, tmpstr1, 1, 1);
6994
6995                tmpstr1 = ostrcat("\nLoad = ", "", 0, 0);
6996                tmpstr2 = command("cat /proc/loadavg");
6997                tmpstr1 = ostrcat(tmpstr1, tmpstr2, 1, 1);
6998                tmpstr = ostrcat(tmpstr, tmpstr1, 1, 1);
6999        }
7000
7001        if(mode == 1)
7002        {
7003                system("ls /media/usb/* >/dev/null");
7004                system("ls /media/net/* >/dev/null");
7005                system("ls /var/swap/* >/dev/null");
7006                system("ls /mnt/swapextension/* >/dev/null");
7007                system("ls /var/backup/* >/dev/null");
7008                system("ls /media/hdd/* >/dev/null");
7009                tmpstr = command("df -h");
7010        }
7011
7012        if(mode == 2)
7013                tmpstr = command("cat /proc/version");
7014
7015        if(mode == 3)
7016        {
7017                system("ls /media/usb/* >/dev/null");
7018                system("ls /media/net/* >/dev/null");
7019                system("ls /var/swap/* >/dev/null");
7020                system("ls /mnt/swapextension/* >/dev/null");
7021                system("ls /var/backup/* >/dev/null");
7022                system("ls /media/hdd/* >/dev/null");
7023                tmpstr = command("mount");
7024        }
7025
7026        if(mode == 4)
7027        {
7028                tmpstr1 = command("ifconfig");
7029                tmpstr2 = command("route -n");
7030                tmpstr = ostrcat(tmpstr1, tmpstr2, 1, 1);
7031        }
7032
7033        if(mode == 5)
7034                tmpstr = command("free");
7035
7036        return tmpstr;
7037}
7038
7039char* system_infos_sysinfo(int mode)
7040{
7041        char* tmpstr = NULL;
7042
7043        if(mode == 0)
7044                tmpstr = command("cat /proc/cpuinfo | sed 's/\t\t/\t/'");
7045        else if(mode == 1)
7046                tmpstr = command("cat /proc/meminfo");
7047        else if(mode == 2)
7048                tmpstr = command("cat /proc/mtd");
7049        else if(mode == 3)
7050                tmpstr = command("cat /proc/modules");
7051        else if(mode == 4)
7052                tmpstr = command("cat /proc/devices");
7053        else if(mode == 5)
7054        {
7055                char* tmpstr1 = NULL, **tmpstr2 = NULL, **tmpstr3 = NULL;
7056                int i = 0, ii = 0;
7057                char* swap[] = {"Name: ", "Type: ", "Size: ", "Used: ", "Prio: "};
7058
7059                tmpstr1 = command("cat /proc/swaps | sed 's/\t/ /g; s/[ ]* / /g'");
7060                tmpstr2 = str_split(tmpstr1, "\n");
7061                if(tmpstr2 != NULL)
7062                {
7063                        free(tmpstr2[0]); tmpstr2[0] = NULL;
7064
7065                        for(i = 1; tmpstr2[i] != NULL; i++)
7066                        {
7067                                tmpstr3 = str_split(tmpstr2[i], " ");
7068                                if(tmpstr3 != NULL)
7069                                {
7070
7071                                        for(ii = 0; tmpstr3[ii] != NULL; ii++)
7072                                        {
7073                                                tmpstr = ostrcat(tmpstr, swap[ii], 1, 0);
7074                                                tmpstr = ostrcat(tmpstr, tmpstr3[ii], 1, 1);
7075                                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
7076                                        }
7077                                }
7078
7079                                tmpstr = ostrcat(tmpstr, "\n", 1, 0);
7080                                free(tmpstr2[i]); tmpstr2[i] = NULL;
7081                                free(tmpstr3); tmpstr3 = NULL;
7082                        }
7083                }
7084
7085                free(tmpstr3); tmpstr3 = NULL;
7086                free(tmpstr2); tmpstr2 = NULL;
7087                free(tmpstr1); tmpstr1 = NULL;
7088        }
7089        else if(mode == 6)
7090                tmpstr = command("top -b -n1");
7091        else if(mode == 7)
7092                tmpstr = command("ps");
7093        else if(mode == 8)
7094        {
7095                tmpstr = command("lsusb");
7096                tmpstr = ostrcat(tmpstr, "\n-------------------------------------------\n", 1, 0);
7097                char* tmpstr2 = NULL;
7098                tmpstr2 = command("cat /proc/bus/usb/devices");
7099                tmpstr = ostrcat(tmpstr, tmpstr2, 1, 1);
7100        }
7101        return tmpstr;
7102}
7103
7104char* system_logs(int mode)
7105{
7106        char* tmpstr = NULL, *tmpstr1 = NULL, *tmpstr2 = NULL, *tmpstr3 = NULL, *path = NULL, *boxversion = NULL;
7107
7108        if(mode == 0)
7109        {
7110                tmpstr1 = readfiletomem("/etc/motd", 0);
7111                if(tmpstr1 != NULL) tmpstr2 = strstr(tmpstr1, "wElc0me");
7112                tmpstr3 = readfiletomem("/etc/imageinfo", 0);
7113                if(tmpstr2 == NULL)
7114                        tmpstr = ostrcat(tmpstr3, NULL, 1, 0);
7115                else
7116                        tmpstr = ostrcat(tmpstr2, tmpstr3, 1, 1);
7117        }
7118        else if(mode == 1)
7119        {
7120                if(isfile("/etc/model") == 0) return NULL;
7121                boxversion = string_tolower(readsys("/etc/model", 1));
7122
7123//              if(file_exist("/etc/.beta"))
7124                        path = ostrcat(path, "/svn/image-beta/changelog.", 1, 0);
7125//              else
7126//                      path = ostrcat(path, "/svn/image/changelog.", 1, 0);
7127
7128                path = ostrcat(path, boxversion, 1, 0);
7129                path = ostrcat(path, ".titan", 1, 0);
7130
7131//              if(file_exist("/etc/.beta"))
7132                        tmpstr1 = gethttp("beta.dyndns.tv", path, 80, NULL, HTTPAUTH, 5000, NULL, 0);
7133//              else
7134//                      tmpstr1 = gethttp("atemio.dyndns.tv", path, 80, NULL, HTTPAUTH, 5000, NULL, 0);
7135
7136                tmpstr = readfromlinetoline(tmpstr1, 37, 537, 1);
7137        }
7138        else if(mode == 2)
7139        {
7140//              if(file_exist("/etc/.beta"))
7141#ifdef MIPSEL
7142                        path = ostrcat(path, "/svn/image-beta/changelog.git.mipsel", 1, 0);
7143#else
7144                        path = ostrcat(path, "/svn/image-beta/changelog.git.sh4", 1, 0);
7145#endif
7146//              else
7147//                      path = ostrcat(path, "/svn/image/changelog.git", 1, 0);
7148
7149//              if(file_exist("/etc/.beta"))
7150                        tmpstr1 = gethttp("beta.dyndns.tv", path, 80, NULL, HTTPAUTH, 5000, NULL, 0);
7151//              else
7152//                      tmpstr1 = gethttp("atemio.dyndns.tv", path, 80, NULL, HTTPAUTH, 5000, NULL, 0);
7153
7154                tmpstr = readfromlinetoline(tmpstr1, 0, 500, 1);
7155        }
7156        else if(mode == 3)
7157        {
7158                tmpstr = readfiletomem(getconfig("tracelog", NULL), 0);
7159        }
7160
7161        free(path), path = NULL;
7162        free(boxversion), boxversion = NULL;
7163//      free(tmpstr1), tmpstr1 = NULL;
7164//      free(tmpstr2), tmpstr2 = NULL;
7165//      free(tmpstr3), tmpstr3 = NULL;
7166
7167        return tmpstr;
7168}
7169
7170char* getimgnamereal()
7171{
7172        char* tmpstr = NULL;
7173
7174        tmpstr = readfiletomem(getconfig("imagenamefile", NULL), 1);
7175
7176        return tmpstr;
7177}
7178
7179char* getmoviedev()
7180{
7181        char* tmpstr = NULL, *buf = NULL;
7182
7183        tmpstr = readfiletomem("/tmp/.moviedev", 1);
7184        printf("tmpstr: %s\n", tmpstr);
7185
7186        buf = oregex("sd([a-z]{1,1}).*", tmpstr);
7187        printf("buf: %s\n", buf);
7188
7189        free(tmpstr), tmpstr = NULL;
7190
7191        return buf;
7192}
7193
7194void wakeup_record_device()
7195{
7196        char* cmd = NULL, *dev = NULL;
7197
7198        dev = getmoviedev();
7199        if(dev == NULL)
7200        {
7201                cmd = ostrcat("ls -al ", getconfig("rec_path", NULL), 0, 0);
7202                cmd = ostrcat(cmd," > /dev/null 2>&1", 1, 0);
7203        }
7204        else
7205                cmd = ostrcat("sdparm -C START /dev/sd", dev, 0, 0);
7206
7207//      cmd = ostrcat(cmd, " >/dev/null", 1, 0);
7208        printf("cmd: %s\n", cmd);
7209        system(cmd);
7210        free(dev), dev = NULL;
7211        free(cmd), cmd = NULL;
7212}
7213
7214void setdefaultbouquets()
7215{
7216        if(textbox(_("Message"), _("Start Restore Default Bouquets ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0) == 1)
7217        {
7218                system("rm -f /mnt/settings/bouquets.* > /dev/null 2>&1");
7219                system("cp -a /etc/titan.restore/mnt/settings/bouquets.cfg /mnt/settings/bouquets.cfg > /dev/null 2>&1");
7220                system("cp -a /etc/titan.restore/mnt/settings/bouquets.*.tv /mnt/settings > /dev/null 2>&1");
7221                system("cp -a /etc/titan.restore/mnt/settings/bouquets.*.radio /mnt/settings > /dev/null 2>&1");
7222                textbox(_("Message"), _("Titan will be restarted!"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 600, 200, 0, 0);
7223                oshutdown(3, 0);
7224        }
7225}
7226
7227void setdefaultsettings()
7228{
7229        if(textbox(_("Message"), _("Start Restore Default Channel Settings ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0) == 1)
7230        {
7231                system("rm -rf /mnt/settings/* > /dev/null 2>&1");
7232                system("cp -a /etc/titan.restore/mnt/settings/* /mnt/settings > /dev/null 2>&1");
7233                textbox(_("Message"), _("Titan will be restarted!"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 600, 200, 0, 0);
7234                oshutdown(3, 0);
7235        }
7236}
7237
7238void setdefaultproviders()
7239{
7240        if(textbox(_("Message"), _("Start Restore Default Providers ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0) == 1)
7241        {
7242                system("rm -f /mnt/settings/provider > /dev/null 2>&1");
7243                system("cp -a /etc/titan.restore/mnt/settings/provider /mnt/settings > /dev/null 2>&1");
7244                textbox(_("Message"), _("Titan will be restarted!"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 600, 200, 0, 0);
7245                oshutdown(3, 0);
7246        }
7247}
7248
7249void setdefaultchannels()
7250{
7251        if(textbox(_("Message"), _("Start Restore Default Channels ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0) == 1)
7252        {
7253                system("rm -f /mnt/settings/channel > /dev/null 2>&1");
7254                system("cp -a /etc/titan.restore/mnt/settings/channel /mnt/settings > /dev/null 2>&1");
7255                textbox(_("Message"), _("Titan will be restarted!"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 600, 200, 0, 0);
7256                oshutdown(3, 0);
7257        }
7258}
7259
7260void setdefaultsats()
7261{
7262        if(textbox(_("Message"), _("Start Restore Default Sats ?"), _("OK"), getrcconfigint("rcok", NULL), _("EXIT"), getrcconfigint("rcexit", NULL), NULL, 0, NULL, 0, 600, 200, 0, 0) == 1)
7263        {
7264                system("rm -f /mnt/settings/satellites > /dev/null 2>&1");
7265                system("cp -a /etc/titan.restore/mnt/settings/satellites /mnt/settings > /dev/null 2>&1");
7266                textbox(_("Message"), _("Titan will be restarted!"), _("OK"), getrcconfigint("rcok", NULL), NULL, 0, NULL, 0, NULL, 0, 600, 200, 0, 0);
7267                oshutdown(3, 0);
7268        }
7269}
7270
7271char* create_backup(char* input, int flag)
7272{
7273        char* infotext = ostrcat(_("The backup image will be saved to /tmp or /var/backup. In case of insufficient internal\nmemory, a USB Stick or HDD must be used. A folder named backup must exist on\nthe target drive."), NULL, 0, 0);
7274        if(flag == 0) return infotext;
7275
7276        char* tmpstr = NULL, *cmd = NULL;
7277        int merksec = status.sec;
7278       
7279        status.sec = 0; //deaktivate spinner
7280        cmd = ostrcat(cmd, "backup.sh ", 1, 0);
7281        cmd = ostrcat(cmd, input, 1, 0);
7282
7283        if(flag == 2)
7284                cmd = ostrcat(cmd, " 1", 1, 0);
7285       
7286        if(file_exist("/var/swap"))
7287        {
7288                if(!file_exist("/var/swap/logs"))
7289                         mkdir("/var/swap/logs", 777);
7290       
7291                if(file_exist("/etc/.beta") && file_exist("/var/swap/logs"))
7292                        cmd = ostrcat(cmd, " > /var/swap/logs/backup_debug.log 2>&1", 1, 0);           
7293        }
7294        else if(checkbox("ATEMIO510") != 1 && checkbox("UFS910") != 1 && checkbox("UFS922") != 1 && checkbox("ATEVIO700") != 1 && checkbox("ATEVIO7000") != 1 && checkbox("IPBOX91") != 1 && checkbox("IPBOX900") != 1 && checkbox("IPBOX910") != 1 && checkbox("IPBOX9000") != 1)
7295        {
7296                if(!file_exist("/mnt/logs"))
7297                         mkdir("/mnt/logs", 777);
7298       
7299                if(file_exist("/etc/.beta") && file_exist("/mnt/logs"))
7300                        cmd = ostrcat(cmd, " > /mnt/logs/backup_debug.log 2>&1", 1, 0);
7301        }
7302        //tmpstr = command(cmd);
7303        if(system(cmd) == 0)
7304        //if(tmpstr == NULL)
7305                tmpstr = ostrcat(tmpstr, _("Backup created successfully"), 1, 0);
7306        else
7307                tmpstr = ostrcat(tmpstr, _("Backup ended with error"), 1, 0);
7308
7309        free(cmd); cmd = NULL;
7310        status.sec = merksec;
7311       
7312        return tmpstr;
7313}
7314
7315char* create_backuprestore()
7316{
7317        char* tmpstr = ostrcat(_("Press -restore- and your saved settings will be restored from your swapstick / recording hdd! The Box will restart automatically for restoring!\n\nPress -backup- to save your actual settings to swapstick / recording hdd.\nWARNING: The old backup will be deleted!"), NULL, 0, 0);
7318        return tmpstr;
7319}
7320char* randomstring(int n)
7321{
7322    int i, wert;
7323    time_t t;
7324    char* tmp = malloc(n+1);
7325
7326    /* Intializes random number generator */
7327    srand((unsigned) time(&t));
7328    for( i = 0 ; i < n ; i++ )
7329    {
7330        wert = rand() % 9;
7331        sprintf(tmp+i, "%d", wert);
7332    }
7333    tmp[i] = '\0';
7334    return tmp;
7335}
7336
7337
7338int putmsgbuffer()
7339{
7340        if(file_exist("/tmp/textbox_standby") != 1)
7341                return 1;
7342       
7343        FILE *fp = fopen("/tmp/textbox_standby" , "r");
7344        if(fp == NULL)
7345        {
7346                printf("Error opening file: /tmp/textbox_standby\n");
7347                return(-1);
7348        }
7349       
7350        char* caption = malloc(255);
7351        char* body = malloc(255);
7352        char* comtext1 = malloc(20);
7353        char* com1 = malloc(10);
7354        char* comtext2 = malloc(20);
7355        char* com2 = malloc(10);
7356        char* comtext3 = malloc(20);
7357        char* com3 = malloc(10);
7358        char* comtext4 = malloc(20);
7359        char* com4 = malloc(10);
7360        char* width = malloc(10);
7361        char* height = malloc(10);
7362        char* timeout = malloc(10);
7363        char* flag = malloc(10);
7364 
7365        while(fgets(caption, 255, fp)!=NULL)
7366        {
7367                string_newline(caption);
7368               
7369                fgets(body, 255, fp);
7370                string_newline(body);
7371                body = string_replace_all("\t", "\n", body, 1);
7372               
7373                if(comtext1 == NULL)
7374                        comtext1 = malloc(20);
7375                fgets(comtext1, 20, fp);
7376                string_newline(comtext1);
7377                fgets(com1, 10, fp);
7378                string_newline(com1);
7379               
7380                if(comtext2 == NULL)
7381                        comtext2 = malloc(20);
7382                fgets(comtext2, 20, fp);
7383                string_newline(comtext2);
7384                fgets(com2, 10, fp);
7385                string_newline(com2);
7386               
7387                if(comtext3 == NULL)
7388                        comtext3 = malloc(20);
7389                fgets(comtext3, 20, fp);
7390                string_newline(comtext3);
7391                fgets(com3, 10, fp);
7392                string_newline(com3);
7393               
7394                if(comtext4 == NULL)
7395                        comtext4 = malloc(20);
7396                fgets(comtext4, 20, fp);
7397                string_newline(comtext4);
7398                fgets(com4, 10, fp);
7399                string_newline(com4);
7400               
7401                fgets(width, 10, fp);
7402                string_newline(width);
7403               
7404                fgets(height, 10, fp);
7405                string_newline(height);
7406               
7407                fgets(timeout, 10, fp);
7408                string_newline(timeout);
7409               
7410                fgets(flag, 10, fp);
7411                string_newline(flag);
7412               
7413                if(atoi(com1) == 0)
7414                {
7415                        free(comtext1);
7416                        comtext1 = NULL;
7417                }
7418                if(atoi(com2) == 0)
7419                {
7420                        free(comtext2);
7421                        comtext2 = NULL;
7422                }
7423                if(atoi(com3) == 0)
7424                {
7425                        free(comtext3);
7426                        comtext3 = NULL;
7427                }
7428                if(atoi(com4) == 0)
7429                {
7430                        free(comtext4);
7431                        comtext4 = NULL;
7432                }
7433                textbox(caption, body, comtext1, atoi(com1), comtext2, atoi(com2), comtext3, atoi(com3), comtext4, atoi(com4), atoi(width), atoi(height), atoi(timeout), atoi(flag));
7434        }
7435       
7436        fclose(fp);     
7437        system("rm /tmp/textbox_standby");
7438       
7439        free(caption);
7440        free(body);
7441        free(comtext1);
7442        free(com1);
7443        free(comtext2);
7444        free(com2);
7445        free(comtext3);
7446        free(com3);
7447        free(comtext4);
7448        free(com4);
7449        free(width);
7450        free(height);
7451        free(timeout);
7452        free(flag);
7453               
7454        return 0;
7455}
7456
7457void delunusedtransponder()
7458{
7459        struct transponder* node = transponder;
7460        struct transponder* prev = NULL;
7461
7462        while(node != NULL)
7463        {
7464                prev = node;
7465                node = node->next;
7466
7467                if(getsatbyorbitalpos(prev->orbitalpos) == NULL)
7468                        deltransponder(prev);
7469        }
7470}
7471
7472void delunusedchannel()
7473{
7474        struct channel* node = channel;
7475        struct channel* prev = NULL;
7476
7477        while(node != NULL)
7478        {
7479                prev = node;
7480                node = node->next;
7481
7482                if(prev->transponder == NULL)
7483                        delchannel(prev->serviceid, prev->transponderid, 0);
7484        }
7485}
7486
7487void delunusedbouquet()
7488{
7489        struct mainbouquet* mnode = mainbouquet;
7490        struct bouquet* first = NULL;
7491        struct bouquet* node = NULL;
7492        struct bouquet* prev = NULL;
7493
7494        while(mnode != NULL)
7495        {
7496                first = mnode->bouquet;
7497                node = mnode->bouquet;
7498                while(node != NULL)
7499                {
7500                        prev = node;   
7501                        node = node->next;
7502                        if(getchannel(prev->serviceid, prev->transponderid) == NULL)
7503                                delbouquet(prev->serviceid, prev->transponderid, &first);
7504                }
7505                mnode = mnode->next;
7506        }
7507}
7508
7509void delunusedepgchannel()
7510{
7511        struct epgscanlist* node = epgscanlist;
7512        struct epgscanlist* prev = NULL;
7513
7514        while(node != NULL)
7515        {
7516                prev = node;   
7517                node = node->next;
7518                if(getchannel(prev->serviceid, prev->transponderid) == NULL)
7519                        delepgscanlist(node->serviceid, node->transponderid);
7520        }
7521}
7522
7523void delunusedprovider()
7524{
7525        struct provider* node = provider;
7526        struct provider* prev = NULL;
7527
7528        while(node != NULL)
7529        {
7530                prev = node;   
7531                node = node->next;
7532                delprovidernotused(prev);
7533        }
7534}
7535
7536#endif
Note: See TracBrowser for help on using the repository browser.