File: connect.c

package info (click to toggle)
audtty 0.1.9a-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 712 kB
  • ctags: 139
  • sloc: ansic: 1,381; sh: 193; makefile: 53
file content (103 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download
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
/*
    This file is part of audtty, copyright 2008 Chris Taylor.

    audtty 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.

    audtty 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 audtty; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdlib.h>
#include <stdio.h>
#include <audacious/audctrl.h>
#include <string.h>
#include <dirent.h>
#include "main.h"
#include "dbus.h"
#include "settings.h"
#include "kbd_constants.h"
#include "curses_printf.h"

void check_env(void);
void fix_env(void);
static const char *dbussessionaddr = "DBUS_SESSION_BUS_ADDRESS";
static const char *xsession = "DISPLAY";

void check_env(void){
	if(getenv(dbussessionaddr)==NULL||getenv(xsession)==NULL)
		fix_env();
	else
		return;
}

void fix_env(void){
	DIR* dir;
	struct dirent* contents;
	char dbus[4096];
	char *dbussession = "/.dbus/session-bus/";
	char *check = NULL;
	FILE *filename;
	char buffer[4096];
	char *file;

	strcpy(dbus,getenv("HOME"));
	strcat(dbus,dbussession);
	dir = opendir(dbus);
	
	if(!dir){
		printf("Failed to open directory: %s \n",dbus);
		exit(1);
	}
	
	while((contents=readdir(dir))){
		if(strcmp(contents->d_name, ".") == 0)
			continue;
		if(strcmp(contents->d_name, "..") == 0)
			continue;
		else{
			file=contents->d_name;
			closedir(dir);
			strcat(dbus,file);
			filename=fopen(dbus,"r");
			if(filename==NULL){
				printf("Failed to open file: %s \n",contents->d_name);
			}
			else{
				while(fgets(buffer,4096,filename)){
					if(buffer[0]=='#')
						continue;
					else{
						if((check=strchr(buffer,'=')))
						{
							char *env=strtok(buffer,"=");
							char *val=strtok(NULL, "\n");
							if((strcmp(env,dbussessionaddr))==0)
							{
								setenv(env,val,0);
								//It really doesnt matter what $DISPLAY is set to, so long as it is set.
								setenv(xsession,":0",0); 
								break;
							}
							else
								continue;
						}
						else
							continue;
					}
				}
			fclose(filename);
			break;
			}
		}
	}
	return;
}