File: os.cc

package info (click to toggle)
starvoyager 0.4.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 1,508 kB
  • ctags: 922
  • sloc: cpp: 7,651; ansic: 825; sh: 129; makefile: 93
file content (97 lines) | stat: -rw-r--r-- 1,721 bytes parent folder | download | duplicates (6)
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
/*
	os.cc
	
	(c) Richard Thrippleton
	Licensing terms are in the 'LICENSE' file
	If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/

#include <SDL.h>
#include <SDL_net.h>
#include <signal.h>
#include <string.h>
#ifdef POSIX
#include <unistd.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#endif
#include "error.h"
#include "server.h"
#include "os.h"

void os::init()
{
	#ifdef POSIX
	signal(SIGPIPE,SIG_IGN);
	signal(SIGTERM,server::quitsignal);
	#endif
	SDL_Init(SDL_INIT_TIMER|SDL_INIT_NOPARACHUTE);
	SDLNet_Init();
}

void os::finish()
{
	SDLNet_Quit();
	SDL_Quit();
}

FILE* os::openpersonal(const char* fnam,const char* flag)
{
	char* path; //Path to generate
	FILE* out; //File handler to return

	#ifdef POSIX
	DIR* dir; //Opened dir
	passwd* me; //Who are we running as?

	me=getpwuid(getuid());
	if(!me)
		throw error("getpwuid failed: cannot find user's home directory");
	path=new char[strlen(fnam)+strlen(me->pw_dir)+15];
	sprintf(path,"%s/.starvoyager",me->pw_dir);
	dir=opendir(path);
	if(dir)	
		closedir(dir);
	else
		mkdir(path,0700);
	sprintf(path,"%s/.starvoyager/%s",me->pw_dir,fnam);
	#else
	path=new char[strlen(fnam)+1];
	sprintf(path,"%s",fnam);
	#endif

	out=fopen(path,flag);
	delete[] path;
	if(out)
		return out;
	else
		throw error("Cannot open user's file");
}

char* os::gettime()
{
	#ifdef POSIX
	time_t tst; //Time structure

	time(&tst);
	sprintf(tbuf,"%s",ctime(&tst));
	tbuf[strlen(tbuf)-1]='\0';
	#else
	tbuf[0]='\0';
	#endif		
	return tbuf;
}

long os::getseed()
{
	#ifdef POSIX
	return (long)time(NULL);
	#else
	return 12345;
	#endif
}

char os::tbuf[256];