summaryrefslogtreecommitdiffstats
path: root/source/install-mod.c
blob: 09ded12a23ce0c3ee6044cb9d03db3d608e2adae (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "install-zip.h"
#include "gecko.h"

//these are in main.h
void create_folders(char path[]);
void download_file(char file[], char savefile[], int status);

void install_mod(char file[], int row)
{
FILE *fp = fopen(file, "r");
if (!fp)
	{
	printf("Could not open %s\n", file);
	gprintf("Could not open %s", file);
	exit(1);
	}

fseek(fp, 0L, SEEK_END);//seek to the end of the file
int size = ftell(fp);//store lenght of file
rewind(fp);//seek back to start of file

char buff[size+1];//a little excessive perhaps, but such size is required if the file only contains 1 line
buff[size] = '\0';//add null char to end of buff

char *colon;//location of colon

int count = 0;
char *result;
while(1)
	{
	result = fgets(buff, size, fp);//copy a line from fp into buff
	if (!result)
		{
		printf("%s malformed0\n", file);
		gprintf("%s malformed0", file);
		exit(1);
		}

	if (count == row)
		{
		//replace a found newline with a null char. Also, stop if a null char is found
		for (int i = 0; i<size; ++i)
			{
			if (buff[i] == '\n' || buff[i] == '\0'){buff[i] = '\0'; break;}
			}

		colon = strstr(buff,":");//find colon in buff
		if (!colon)
			{
			printf("%s malformed1!\n", file);
			gprintf("%s malformed1!", file);
			exit(1);
			}
		*colon = '\0';//replace colon with null char

//		create_folders(colon+1);//create the destination folders

		//save as sd:/root.zip and extract if the file to download has ".zip" in it, otherwise save the file to sd:/boot.elf
		if (strstr(buff,".zip"))
			{
			download_file(buff, "root.zip", 1);
			unZip("root.zip");
			}
		else {download_file(buff, "boot.elf", 1);}
		break;
		}

	++count;
	}

fclose(fp);
}