@@ -162,6 +162,22 @@ int start_protective_mode(lua_CFunction func, const char* file, char** parameter
162162 return EXIT_SUCCESS ;
163163}
164164
165+ // returns a malloc'd string with each split item being seperated by \0
166+ char * strsplit (const char * str1 , const char lookout , size_t len , size_t max ) {
167+ char * cpy = malloc (len );
168+ memcpy (cpy , str1 , len );
169+
170+ for (size_t i = 0 ; i < len - 1 ; i ++ ) {
171+ if (str1 [i ] == lookout ) {
172+ cpy [i ] = '\0' ;
173+ max -- ;
174+ }
175+ if (max == 0 )
176+ break ;
177+ }
178+ return cpy ;
179+ }
180+
165181// handles arguments, cwd, loads necessary data, executes lua
166182int main (int argc , char * argv [])
167183{
@@ -264,27 +280,18 @@ int main(int argc, char* argv[])
264280 return EXIT_FAILURE ;
265281 }
266282
267- // initiate global variables set up, needs solidifed
283+ // initiate global variables set up
268284 if (globals != 0 ) {
269- // this is a hack, need to switch to strtok that doesn't modify original string
270285 for (size_t i = 0 ; i < globals ; i ++ ) {
271- size_t len = strlen (globals_argv [i ] + 2 );
272- char * cpy = calloc (len + 1 , 1 );
273- memcpy (cpy , globals_argv [i ] + 2 , len + 1 );
274-
275- char * temp ;
276- char * left ;
277- char * right ;
278-
279- left = strtok_r (globals_argv [i ] + 2 , "=" , & temp );
280- right = strtok_r (NULL , "\0" , & temp );
286+ char * globals_D_offset = globals_argv [i ] + 2 ;
281287
282- // TODO: check if argument has no =, therefore invalid and error
288+ char * arg1 = strsplit (globals_D_offset , '=' , strlen (globals_D_offset ) + 1 , 2 );
289+ char * arg2 = arg1 + (strlen (arg1 ) + 1 );
283290
284- lua_pushlstring (L , right , strlen (right ));
285- lua_setglobal (L , left );
291+ lua_pushlstring (L , arg2 , strlen (arg2 ));
292+ lua_setglobal (L , arg1 );
286293
287- free (cpy );
294+ free (arg1 );
288295 }
289296 free (globals_argv );
290297 }
0 commit comments