Created
February 24, 2015 10:23
-
-
Save AlessandroSpallina/2d581e402efa67b3c5f2 to your computer and use it in GitHub Desktop.
verifica la frammentazione del disco (linux)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
* Copyright © 2015 Alessandro Spallina | |
* | |
* This program 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 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>. | |
* | |
****************************************************************************/ | |
#include <stdio.h> | |
#include <gtk/gtk.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include <string.h> | |
static GtkWidget *window; | |
//--------------------------------- | |
void fatal_error(char *message) | |
{ | |
fprintf(stderr, "\n\nFATAL ERROR: %s\n\n", message); | |
exit(EXIT_FAILURE); | |
} | |
//--------------------------------- | |
void result_message (GtkWindow *parent, gchar *message) | |
{ | |
GtkWidget *dialog, *label, *content_area; | |
GtkDialogFlags flags; | |
flags = (GTK_DIALOG_MODAL); | |
dialog = gtk_dialog_new_with_buttons ("Checked", parent, flags, ("OK"), GTK_RESPONSE_NONE, NULL); | |
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); | |
label = gtk_label_new (message); | |
g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); | |
gtk_container_add (GTK_CONTAINER (content_area), label); | |
gtk_widget_show_all (dialog); | |
} | |
//--------------------------------- | |
void quick_message (GtkWindow *parent, gchar *message) | |
{ | |
GtkWidget *dialog, *label, *content_area; | |
GtkDialogFlags flags; | |
flags = (GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL); | |
dialog = gtk_dialog_new_with_buttons ("Warning!", parent, flags, ("OK"), GTK_RESPONSE_NONE, NULL); | |
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); | |
label = gtk_label_new (message); | |
g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_widget_destroy), parent); | |
gtk_container_add (GTK_CONTAINER (content_area), label); | |
gtk_widget_show_all (dialog); | |
} | |
//--------------------------------- | |
void f_check(GtkWidget *widget, gpointer data) | |
{ | |
FILE *fp=NULL; | |
char percentuale[5]=" "; | |
char media[20]=" "; | |
char messaggiofinale[65]=" "; | |
gtk_spinner_start(data); | |
if((system("./.fragck.pl > .temp_fck"))==-1) | |
fatal_error("ERRORE DURANTE L'ESECUZIONE DELLO SCRIPT!"); | |
if((fp=fopen(".temp_fck", "r"))==NULL) | |
fatal_error("NON RIESCO AD APRIRE L'OUTPUT DELLO SCRIPT!"); | |
fscanf(fp, "%s %s", percentuale, media); | |
printf("\nPercentuale file non contigui: %s\nMedia di frammenti non contigui: %s\n", percentuale, media); | |
fclose(fp); | |
strcat(messaggiofinale, "Percentuale file non contigui: "); | |
strcat(messaggiofinale, percentuale); | |
strcat(messaggiofinale, "\nMedia frammenti: "); | |
strcat(messaggiofinale, media); | |
result_message(GTK_WINDOW(window), messaggiofinale); | |
remove(".temp_fck"); | |
gtk_spinner_stop(data); | |
} | |
//--------------------------------- | |
void init_fragck(void) | |
{ | |
int pid; | |
FILE *fp=NULL; | |
if((fp=fopen(".fragck.pl", "r"))==NULL) | |
{ | |
pid=fork(); | |
switch(pid) | |
{ | |
case 0: | |
system("exit"); | |
system("wget https://www.dropbox.com/s/2mhuntauvovsrgs/.fragck.pl?dl=1"); | |
rename(".fragck.pl?dl=1", ".fragck.pl"); | |
if((chmod("./.fragck.pl", S_IXUSR | S_IXGRP | S_IXOTH))!=0) | |
fatal_error("NON TROVO LO SCRIPT!"); | |
exit(EXIT_SUCCESS); | |
break; | |
default: | |
wait(NULL); | |
printf("\nInfo: script scaricato correttamente\n"); | |
break; | |
case -1: | |
fatal_error("ERRORE FORK"); | |
exit(EXIT_FAILURE); | |
break; | |
} | |
} | |
else fclose(fp); | |
} | |
//--------------------------------- | |
int main(int argc, char *argv[]) | |
{ | |
GtkWidget *check_button; | |
GtkWidget *info_label, *status_label; | |
GtkWidget *spinner; | |
GtkWidget *vbox, *hbox; | |
GtkWidget *dialog; | |
gtk_init(&argc, &argv); | |
//init widgets | |
window=gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
info_label=gtk_label_new("Check your disk fragmentation!"); | |
check_button=gtk_button_new_with_label("Go!"); | |
status_label=gtk_label_new(" Status: "); | |
vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); | |
hbox=gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10); | |
spinner=gtk_spinner_new(); | |
//window info | |
gtk_window_set_title(GTK_WINDOW(window), "FragCheck"); | |
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS); | |
gtk_container_set_border_width(GTK_CONTAINER(window), 15); | |
gtk_window_set_resizable(GTK_WINDOW(window), FALSE); | |
//gest signals | |
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); | |
g_signal_connect(check_button, "clicked", G_CALLBACK(f_check), spinner); | |
//packing | |
gtk_container_add(GTK_CONTAINER(vbox), info_label); | |
gtk_container_add(GTK_CONTAINER(hbox), status_label); | |
gtk_container_add(GTK_CONTAINER(hbox), spinner); | |
gtk_container_add(GTK_CONTAINER(vbox), hbox); | |
gtk_container_add(GTK_CONTAINER(vbox), check_button); | |
gtk_container_add(GTK_CONTAINER(window), vbox); | |
if(getuid()) | |
quick_message(GTK_WINDOW(window), " Run as Administrator "); | |
else init_fragck(); | |
gtk_widget_show_all(window); | |
gtk_main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment