added fonera-mp3 support to k7.09
[openwrt/svn-archive/archive.git] / package / fonera-mp3 / src / lib / mp3_misc.c
1 /*
2 * FOXMP3
3 * Copyright (c) 2006 acmesystems.it - john@acmesystems.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
18 *
19 * Feedback, Bugs... info@acmesystems.it
20 *
21 */
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include "mp3.h"
33
34 #define TMP_PLS_NAME "/var/tmp.pls"
35
36 char* mp3_shell_run(char *filename, char **args, char *buffer, int length){
37 int fd1[2], fd2[2], l;
38 if((pipe(fd1) !=0) || (pipe(fd2)!=0)){
39 return NULL;
40 }
41 if (fork() == 0){
42 close(fd1[1]);
43 close(fd2[0]);
44 if ((dup2(fd1[0], STDIN_FILENO) == -1)
45 || (dup2(fd2[1], STDOUT_FILENO) == -1)){
46 exit(0);
47 }
48 close(fd1[0]);
49 close(fd2[1]);
50 execvp(filename, args);
51 printf("ERROR running : %s\n", filename);
52 exit(0);
53 }
54 memset(buffer,'\0',length);
55 close(fd1[0]);
56 close(fd2[1]);
57 close(fd1[1]);
58 wait(NULL);
59 if((l = read(fd2[0], buffer, length -1)) == -1){
60 printf("read failed");
61 return NULL;
62 }
63 buffer[l] = '\0';
64 close (fd2[2]);
65 return buffer;
66 }
67
68 int mp3_pls_get_info(unsigned char *pls_url, unsigned char *url,
69 unsigned char *path, unsigned int *port){
70 int ret = MP3_ERROR;
71 char *exec_args[6];
72 int i;
73 remove(TMP_PLS_NAME);
74 for (i = 0; i < 5; i++){
75 exec_args[i] = malloc(2048);
76 }
77 exec_args[0][0] = '\0';
78 strcpy(exec_args[1], "wget");
79 strcpy(exec_args[2], pls_url);
80 strcpy(exec_args[3], "-O");
81 strcpy(exec_args[4], TMP_PLS_NAME);
82 exec_args[5] = NULL;
83 printf("Getting pls file --> %s \n", exec_args[2]);
84 if(mp3_shell_run("wget", &exec_args[1],
85 exec_args[0], 2048)){
86 struct stat s;
87 stat(TMP_PLS_NAME, &s);
88 if(s.st_size > 0){
89 FILE *fp = fopen(TMP_PLS_NAME, "r");
90 if(fp > 0){
91 unsigned char *data = malloc(2048);
92 *url = '\0';
93 while((!*url) && (!feof(fp))){
94 if(fgets(data, 2048, fp) != NULL){
95 if(strstr(data, "File")){
96 unsigned char *t = strstr(data, "=");
97 if(t){
98 t++;
99 if(mp3_stream_parse_url(t, url,
100 path, port) != MP3_OK){
101 *url = '\0';
102 }
103 }
104 }
105 }
106 }
107 fclose(fp);
108 free(data);
109 if(*url){
110 ret = MP3_OK;
111 }
112 }
113 }
114 } else {
115 printf("WGET error\n");
116 }
117 for (i = 0; i < 5; i++){
118 free(exec_args[i]);
119 }
120 if(ret == MP3_OK){
121 printf("Found file valid file in pls\n");
122 } else {
123 printf("Error whilst parsing pls\n");
124 }
125 return ret;
126 }
127
128
129