summaryrefslogtreecommitdiff
path: root/package/fonera-mp3/src/lib/mp3_misc.c
blob: 9fba30fbbdcf8e7c64fec6664f9c5dcd0e2d6f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* FOXMP3 
* Copyright (c) 2006 acmesystems.it - john@acmesystems.it
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
*
* Feedback, Bugs...  info@acmesystems.it
*
*/ 


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "mp3.h"

#define TMP_PLS_NAME "/var/tmp.pls"

char* mp3_shell_run(char *filename, char **args, char *buffer, int length){
  	int fd1[2], fd2[2], l;
	if((pipe(fd1) !=0)  || (pipe(fd2)!=0)){
		return NULL;
	}
	if (fork() == 0){
		close(fd1[1]);
		close(fd2[0]);
		if ((dup2(fd1[0], STDIN_FILENO) == -1) 
			|| (dup2(fd2[1], STDOUT_FILENO) == -1)){
			exit(0);
		}
		close(fd1[0]);
		close(fd2[1]);
		execvp(filename, args);
        	printf("ERROR running : %s\n", filename);
		exit(0);
	}
	memset(buffer,'\0',length);
	close(fd1[0]);
	close(fd2[1]);
	close(fd1[1]);
	wait(NULL);
	if((l = read(fd2[0], buffer, length -1)) == -1){
		printf("read failed");
		return NULL;
	}
	buffer[l] = '\0';
	close (fd2[2]);
	return buffer;
}

int mp3_pls_get_info(unsigned char *pls_url, unsigned char *url,
		unsigned char *path, unsigned int *port){
	int ret = MP3_ERROR;
	char *exec_args[6];
	int i;
	remove(TMP_PLS_NAME);
	for (i = 0; i < 5; i++){
		exec_args[i] = malloc(2048);
	}
	exec_args[0][0] = '\0';
	strcpy(exec_args[1], "wget");
	strcpy(exec_args[2], pls_url);
	strcpy(exec_args[3], "-O");
	strcpy(exec_args[4], TMP_PLS_NAME);
	exec_args[5] = NULL;
	printf("Getting pls file --> %s \n", exec_args[2]);  
	if(mp3_shell_run("wget", &exec_args[1], 
				exec_args[0], 2048)){
		struct stat s;
		stat(TMP_PLS_NAME, &s);
		if(s.st_size > 0){
			FILE *fp = fopen(TMP_PLS_NAME, "r");
			if(fp > 0){
				unsigned char *data = malloc(2048);
				*url = '\0';
				while((!*url) && (!feof(fp))){
					if(fgets(data, 2048, fp) != NULL){
						if(strstr(data, "File")){
							unsigned char *t = strstr(data, "=");
							if(t){
								t++;
								if(mp3_stream_parse_url(t, url, 
									path, port) != MP3_OK){
									*url = '\0';
								}
							}
						}
					}
				}
				fclose(fp);
				free(data);
				if(*url){
					ret = MP3_OK;
				}
			}
		}
	} else {
		printf("WGET error\n");
	}
	for (i = 0; i < 5; i++){
		free(exec_args[i]);
	}
	if(ret == MP3_OK){
		printf("Found file valid file in pls\n");
	} else {
		printf("Error whilst parsing pls\n");	
	}
	return ret;
}