1 /*
2 Copyright (C) 2003 Cedric Cellier, Dominique Lavault
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include <errno.h>
23 //#include <assert.h>
24 #ifndef _WINDOWS
25 	#include <unistd.h>
26 	#include <sys/stat.h>
27 	#include <sys/types.h>
28 	#include <fcntl.h>
29 #else
30 	#include <windows.h>
31 	#include <direct.h>
32 #endif
33 
34 #include "system.h"
35 #include "memspool.h"
36 #include "log.h"
37 
38 #ifdef _WINDOWS
39 #define GETUSERNAME_MAXLEN 255
40 char g_login_name[GETUSERNAME_MAXLEN+1];
41 #endif
42 
sys_goto_dir(const char * dir)43 int sys_goto_dir(const char *dir)
44 {
45 	int ret = chdir(dir);
46 	if (!ret) return 1;
47 	else {
48 		perror("chdir");
49 		return 0;
50 	}
51 }
52 
sys_make_dir(const char * dir)53 int sys_make_dir(const char *dir)
54 {
55 #ifdef _WINDOWS
56 	/* TODO */
57 	return 0;
58 #else
59 	int ret;
60 	struct stat stat_buf;
61 	if (0==stat(dir, &stat_buf) && S_ISDIR(stat_buf.st_mode)) return 1;	/* the directory already exists */
62 	ret = mkdir(dir, 0700);
63 	if (!ret) return 0;
64 	else {
65 		perror("mkdir");
66 		return 0;
67 	}
68 #endif
69 }
70 
sys_get_user_name(char ** dest)71 int sys_get_user_name(char **dest)
72 {
73 #ifdef _WINDOWS
74 	DWORD dwLen=GETUSERNAME_MAXLEN;
75 	*dest=g_login_name;
76 	return (GetUserName(g_login_name, &dwLen)==NULL?0:1);
77 #else
78 	char *login_name = getenv("LOGNAME");
79 	if (!login_name) return 0;
80 	*dest = login_name;
81 	return 1;
82 #endif
83 }
84 
sys_get_user_dir(char ** dest)85 int sys_get_user_dir(char **dest)
86 {
87 #ifdef _WINDOWS
88 	*dest =".";
89 	return 1;
90 #else
91 	char *user_dir = getenv("HOME");
92 	if (!user_dir) return 0;
93 	*dest = user_dir;
94 	return 1;
95 #endif
96 }
97 
98 /* NOT USED ANYMORE
99 
100 SYS_MUTEX sys_mutex_new()
101 {
102 	pthread_mutex_t *mutex = gltv_memspool_alloc(sizeof(*mutex));
103 	pthread_mutex_init(mutex, NULL);
104 	return mutex;
105 }
106 
107 void sys_mutex_del(SYS_MUTEX mutex)
108 {
109 	pthread_mutex_destroy(mutex);
110 	gltv_memspool_unregister(mutex);
111 }
112 
113 int sys_mutex_blockget(SYS_MUTEX mutex)
114 {
115 	int ret = pthread_mutex_lock(mutex);
116 	if (ret) gltv_log_warning(GLTV_LOG_MUSTSEE, "sys_mutex_blockget error : %s", strerror(ret));
117 	return ret==0;
118 }
119 
120 int sys_mutex_nonblockget(SYS_MUTEX mutex)
121 {
122 	int ret = pthread_mutex_trylock(mutex);
123 	if (ret && ret!=EBUSY) gltv_log_warning(GLTV_LOG_MUSTSEE, "sys_mutex_nonblockget error : %s", strerror(ret));
124 	return ret==0;
125 }
126 
127 int sys_mutex_release(SYS_MUTEX mutex)
128 {
129 	int ret = pthread_mutex_unlock(mutex);
130 	if (ret) gltv_log_warning(GLTV_LOG_MUSTSEE, "sys_mutex_release error : %s", strerror(ret));
131 	return ret==0;
132 }
133 */
134