forked from billy3321/lazyscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfx.c
112 lines (93 loc) · 3.16 KB
/
sfx.c
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
/*
* sfx.c: Creating self-extracting tar.gz for Lazybuntu
*
* Copyright 2007 Houng Jen Yee (PCMan) <[email protected]>
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
static GPid pid = -1;
static int std_in = -1;
static char *cmdv[]={ "tar", "xz", "-C", NULL, NULL };
static char magic[] = "XDATA_BEGIN_";
static const int magic_len = G_N_ELEMENTS( magic ) - 1;
void show_error( const char* msg )
{
GtkWidget* dlg;
dlg = gtk_message_dialog_new( NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, msg );
gtk_dialog_run( (GtkDialog*)dlg );
gtk_widget_destroy( dlg );
}
int main(int argc, char** argv)
{
GError *err = NULL;
GMappedFile *map;
char *buf = NULL, *pbuf = NULL, *data = NULL, *end;
gsize len = 0;
char *extract_path;
gtk_init( &argc, &argv );
/* load the executable file itself */
map = g_mapped_file_new( argv[0], FALSE, NULL );
if( !map )
return 1;
buf = g_mapped_file_get_contents(map);
len = g_mapped_file_get_length( map );
/* find the data */
magic[0] = '_';
for( pbuf = buf, end = buf + len - magic_len; G_LIKELY( pbuf < end ); ++pbuf )
{
if( G_UNLIKELY( 0 == memcmp( pbuf, magic, magic_len ) ) )
{
data = pbuf + magic_len + 1;
break;
}
}
if( G_UNLIKELY( ! data ) )
{
g_mapped_file_free( map );
show_error( "檔案損毀,請重新下載。" );
return 1; /* error! no data found */
}
len -= (data - buf); /* skip ourself */
extract_path = g_strconcat( "/tmp/Lazyscripts-", g_get_user_name(), NULL );
g_mkdir_with_parents( extract_path, 0755 ); /* FIXME: is 0755 OK? */
cmdv[3] = extract_path;
if( g_spawn_async_with_pipes( NULL, cmdv, NULL, G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &std_in, NULL, NULL, &err ) )
{
int status = 0;
write( std_in, data, len );
close( std_in );
waitpid( pid, &status, 0 );
g_spawn_close_pid( pid );
}
else
{
show_error( err->message );
g_error_free( err );
}
g_mapped_file_free( map );
g_chdir( extract_path );
g_free( extract_path );
g_chdir( "Lazyscripts" );
execl( "slzs gui", NULL );
show_error("錯誤,無法執行 Lazybuntu!");
return 0;
}