View Single Post
jaeezzy's Avatar
Posts: 664 | Thanked: 160 times | Joined on Jul 2008 @ Australia
#2
Ok, maybe someone can pin-point why I'm getting segmentation fault on this code, rather than the ugly output above :

Code:
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char* getUsbMode(void);
gboolean check(gchar*);

int main(){
	gchar *dir = "scratchbox/users/user/home/user/photoapplet/200910a0";
	GMainLoop *loop = NULL;
	loop = g_main_loop_new(NULL, FALSE);
	g_timeout_add_seconds(3, (GSourceFunc)check, dir);
	
	g_main_loop_run(loop);
	
	return 0;
}

gboolean check(gchar *dir){
	if (g_file_test(dir, G_FILE_TEST_EXISTS)){
		printf("Can access %s\n", dir);
	}else{
		printf("Can't access directory.\n");
		char *usb_mode = NULL;
		usb_mode = getUsbMode();
		if (strcmp(usb_mode, "MASS_STORAGE") == 0){
			printf("USB mass storage mode activated.\n");
		}
	}
	return TRUE;
}

char* getUsbMode(){
	char *file = "/tmp/.current_usb_mode";
	char *mode, val[15];
	mode = (char *) malloc(sizeof(char*));
	assert(mode != NULL);
	FILE *fPtr;
	if ((fPtr = fopen(file, "r")) != NULL){
		fgets(val, 15, fPtr);
		val[strlen(val) - 2] = '\0';
		strcpy(mode, val);
		printf("%s\n", mode);
		fclose(fPtr);
		return mode;
	}else
		return "READ_FAILED";
}
thanks..

Last edited by jaeezzy; 2010-06-13 at 08:58.