Reputation: 682
I am writing a GTK+ frontend program that shreds files. The program shred
from GNU Coreutils is the backend. The programming language is C.
So, I have a progress bar that show how much of the shredding is finished. But, in order to give the user accurate data on how much is shredded, the output of shred must be analyzed.
So far I have tried g_spawn_command_line_async
, but I am not able to send the output of shred to a file.
Upvotes: 0
Views: 444
Reputation: 682
This is possible with GSubprocess (From GIO). Here is the simplified source code. For this to work you will need gimp installed. (It displays the help text from the option --help)
#include <gtk/gtk.h>
struct everything
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *new_process;
GtkWidget *gimp_help_label;
};
void on_new_process_clicked (GtkWidget *wid, gpointer data)
{
struct everything *wids = data;
GError *error = NULL; /* This contains and error that MAY be generated by the gio functions. */
GInputStream *stdout_output;
GSubprocess *process;
gchar hello[10000];
process = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error, "gimp", "--help", NULL);
stdout_output = g_subprocess_get_stdout_pipe(process);
g_input_stream_read (stdout_output, hello, 9995, NULL, &error);
gtk_widget_hide(wids->new_process); /* Hide the button */
wids->gimp_help_label = gtk_label_new(hello); /* Make a new label. */
gtk_box_pack_start(GTK_BOX(wids->box), wids->gimp_help_label, TRUE, TRUE, 20); /* Add it to the box. */
gtk_widget_show(wids->gimp_help_label); /* Show it */
}
void exit_prog (GtkWidget *wid, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
struct everything *widgets = (struct everything*)malloc(sizeof(struct everything));
widgets->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(widgets->window), 500, 500);
g_signal_connect(widgets->window, "destroy", G_CALLBACK(exit_prog), NULL);
widgets->box = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(widgets->window), widgets->box);
widgets->new_process = gtk_button_new_with_label("Show Gimp Help");
gtk_box_pack_start(GTK_BOX(widgets->box), widgets->new_process, TRUE, TRUE, 20);
g_signal_connect(widgets->new_process, "clicked", G_CALLBACK(on_new_process_clicked), widgets);
gtk_widget_show_all(widgets->window);
gtk_main();
return 0;
}
Thats how you do it.
Upvotes: 0
Reputation: 477
The easiest solution is using unix redirection. You can call system("shred -v file_to_shred > somefile.txt")
and the output will be redirect to the file. You can then read from the file using POSIX read or some other file reading method. Run the system()
call in a seperate thread, and the just incremently check the file in the main thread. You could use POSIX sleep()
to check the file at some interval, say, every half second. However, never use sleep on the GUI thread, as this will freeze the UI.
Upvotes: 1