1 /*
2 * IceBreaker
3 * Copyright (c) 2002 Matthew Miller <mattdm@mattdm.org>
4 *
5 * <http://www.mattdm.org/icebreaker/>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc., 59
19 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22 
23 
24 
25 #include <SDL.h>
26 #include "icebreaker.h"
27 #include "globals.h"
28 
29 #ifdef NEEDCHANGETOARGV0PATH
30   #include <sys/stat.h>
31   #include <unistd.h>
32   #include <string.h>
33 #endif
34 
35 
36 #ifdef NEEDCHANGETOARGV0PATH
changetoargv0path(char * argv0)37 void changetoargv0path(char* argv0)
38 {
39 	int chari;
40 	struct stat st;
41 	char argv0path[256]; // fix -- should use OS-specific define
42 
43 	if (strlen(argv0)>256)
44 	{
45 		fprintf(stderr,"Hmmm -- the executable path seems to be longer than pathnames are allowed\n"
46 		               "to be. That should never ever happen -- please report this bug.\n");
47 		return;
48 	}
49 
50 	for (chari=strlen(argv0); chari>0 && argv0[chari]!='/'; chari--);
51 
52 	if (chari==0 && (!strcmp(".",homedir) || !strcmp(".",HISCOREPREFIX) || !strcmp(".",DATAPREFIX)))
53 	{
54 		// check if argv0 is in current directory, in case . is in the path
55 		if (stat(argv0,&st) || (!stat(argv0,&st) && !S_ISREG(st.st_mode)))
56 		{
57 			fprintf(stderr,"Error: you are running me on a platform where I need to look in the current\n"
58 			        "directory for some of my files, but I can't find the right place to look --\n"
59 			        "possibly because %s is in your PATH and you just typed the command\n"
60 			        "name without any path information. Everything is probably going to break.\n\n"
61 			        "If you would like to write a smarter function (perhaps one that searches\n"
62 			        "your path for the executable) for your operating system, please contact me.\n"
63 			        "See <http://www.mattdm.org/icebreaker/development.html> for more info.\n\n"
64 			       "Meanwhile, I'm going to try to run, but probably not successfully....\n",argv0);
65 			return;
66 		}
67 	}
68 	else
69 	{
70 		snprintf(argv0path, chari+1, argv0);
71 		chdir(argv0path);
72 	}
73 }
74 #endif /* NEEDCHANGETOARGV0PATH */
75