
#include <stdio.h>
#include <unistd.h>
#include <config.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

#include <glib.h>

#include "misc.h"

void delete_dir(const char *dir)
{
	DIR *tempdir;
	struct dirent *entry;
	struct stat st;

	tempdir=opendir(dir);

        if (tempdir == NULL)
        {
                return;
        }

	while( (entry=readdir(tempdir))!=NULL) {
		gchar *fullname;

		fullname=g_strconcat(dir,"/",entry->d_name,NULL);
		
		stat(fullname,&st);
		
		if (S_ISDIR(st.st_mode) && strncmp(entry->d_name, ".",1) &&
			strncmp(entry->d_name, "..",2)) {
			delete_dir(fullname);
		} else {
			unlink(fullname);
		}
		g_free(fullname);
	}
	closedir(tempdir);
	rmdir(dir);
}

char *parse_program(const char *data)
{
	gchar **strs;
	gchar *str;

	if (data==NULL)
		return NULL;

	strs=g_strsplit(data," ",1);
	if (strs && strs[0]) {
		str=g_strdup(strs[0]);
		g_strfreev(strs);
		return str;
	}
	g_strfreev(strs);

	return NULL;
}
