📄 callbacks.c
字号:
// -------------------------------- // ---> Fill Entry: Apache Status // -------------------------------- gtk_label_set_text(GTK_LABEL(label_apache), apache_str); gtk_widget_set_sensitive((GtkWidget*)label_apache, FALSE); if ((pipein_fp = popen("ps -e | grep -i httpd", "r")) == NULL) { perror("popen"); exit(1); } while(fgets(readbuf, 1024, pipein_fp)) { if (strcmp(readbuf, "httpd")) { sprintf(apache_str, "Accepting Connections"); gtk_widget_set_sensitive((GtkWidget*)label_apache, TRUE); } } gtk_label_set_text(GTK_LABEL(label_apache), apache_str); pclose(pipein_fp); // -------------------------------- // ---> Fill Entry: Sendmail Status // -------------------------------- gtk_label_set_text(GTK_LABEL(label_sendmail), sendmail_str); gtk_widget_set_sensitive((GtkWidget*)label_sendmail, FALSE); if ((pipein_fp = popen("ps -e | grep -i sendmail", "r")) == NULL) { perror("popen"); exit(1); } while(fgets(readbuf, 1024, pipein_fp)) { if (strcmp(readbuf, "accepting connections")) { sprintf(sendmail_str, "Accepting Connections"); gtk_widget_set_sensitive((GtkWidget*)label_sendmail, TRUE); } } gtk_label_set_text(GTK_LABEL(label_sendmail), sendmail_str); pclose(pipein_fp); // -------------------------------- // ---> Fill Entry: Samba Status // -------------------------------- gtk_label_set_text(GTK_LABEL(label_samba), samba_str); gtk_widget_set_sensitive((GtkWidget*)label_samba, FALSE); if ((pipein_fp = popen("ps -e | grep -i smbd", "r")) == NULL) { perror("popen"); exit(1); } while (fgets(readbuf, 1024, pipein_fp)) { if (strcmp(readbuf, "accepting connections")) { sprintf(samba_str, "Accepting Connections"); gtk_widget_set_sensitive((GtkWidget*)label_samba, TRUE); } } gtk_label_set_text(GTK_LABEL(label_samba), samba_str); pclose(pipein_fp); // -------------------------------- // ---> Fill CList: Users // -------------------------------- if ((pipein_fp = popen("who", "r")) == NULL) { perror("popen"); exit(1); } gtk_clist_clear(GTK_CLIST(users_clist)); while (fgets(readbuf, 1024, pipein_fp)) { strcpy(users_str, readbuf); users_str_list[0] = strtok(users_str, " "); users_str_list[1] = strtok(NULL, " "); tmp1 = strtok(NULL, " "); tmp2 = strtok(NULL, " "); tmp3 = strtok(NULL, " "); sprintf(readbuf, "%s %s %s", tmp1, tmp2, tmp3); users_str_list[2] = strcpy(tmp1, readbuf); gtk_clist_append(GTK_CLIST(users_clist), users_str_list); } pclose(pipein_fp); // -------------------------------- // ---> Fill Entry: IP Address // -------------------------------- sprintf(readbuf, " "); // Use shell to get local machine IP Address if ((pipein_fp = popen("netstat -i eth0 -e | grep -i Bcast | cut -d':' -f2 | cut -d' ' -f1 | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); pclose(pipein_fp); strcpy(IPaddress, readbuf); gtk_entry_set_text(GTK_ENTRY(entry_ipaddr), (gchar *)IPaddress); // `ifconfig` didnt work (RH 7.2), so switching to `netstat` for 0.9.8 and on // GNU C TCP/IP API's still not "doing it" for me (01/2002)... :-//* if (strlen(IPaddress) <= 0) { hostentAnswer = gethostent(); printf("DEBUG: hostentAnswer = %s \n", hostentAnswer->h_name); printf("DEBUG: in_addr = %ld \n", (long int)hostentAnswer->h_addr); printf("DEBUG: inet_addr = %d \n", hostentAnswer->h_addr_list[1]);// printf("DEBUG: in_addr = %ld \n", netAddr->s_addr);// printf("DEBUG: inet_nsap_ntoa = %s \n", *inet_nsap_ntoa); gethostname(hostname, sizeof(hostname)); hostentAnswer = gethostbyname(hostname); if ((h_errno != TRY_AGAIN) && (h_errno != NO_RECOVERY) && (h_errno != NO_ADDRESS)) { strcpy(IPaddress, inet_ntoa(*(struct in_addr*)hostentAnswer->h_addr)); } else { strcpy(IPaddress, "Cant Determine IPADDR!"); } }*/ // -------------------------------- // ---> Fill Entries: PROCS- sleeping, running, zombie (parse /proc) // -------------------------------- dir = opendir("/proc/"); while ((dinfo = readdir(dir)) != 0) { // Step through /proc directory name one letter at-a-time dir_chr = dinfo->d_name; while (*dir_chr) { if ((*dir_chr >= '0') && (*dir_chr <= '9')) { dir_chr++; //printf("DEBUG: dir_chr = %s \n", dir_chr); } else { break; } } if (*dir_chr) { continue; } // Read each entry in the /proc directory strcpy(proc_file_name, "/proc/"); strcat(proc_file_name, dinfo->d_name); strcat(proc_file_name, "/stat"); pipein_fp = fopen(proc_file_name, "r"); fscanf(pipein_fp, "%*d %s %c", proc_buffer, &proc_state); fclose(pipein_fp); // Keep running total of procs states if (proc_state == 'S') { num_proc_sleep++; } else if (proc_state == 'R') { num_proc_run++; } else if (proc_state == 'Z') { num_proc_zombie++; } else if (proc_state == 'T') { num_proc_stopped++; } else { num_proc_sleep++; } num_proc_total++; } // Set Process Summary ProgressBars gtk_progress_configure((GtkProgress *)progressbar_sleep, (gfloat)num_proc_sleep, 0.0, (gfloat)num_proc_total); gtk_progress_configure((GtkProgress *)progressbar_run, (gfloat)num_proc_run, 0.0, (gfloat)num_proc_total); gtk_progress_configure((GtkProgress *)progressbar_zombie, (gfloat)num_proc_zombie, 0.0, (gfloat)num_proc_total); gtk_progress_configure((GtkProgress *)progressbar_stopped, (gfloat)num_proc_stopped, 0.0, (gfloat)num_proc_total); // -------------------------------- // ---> Fill Entries: Hardware & Kernel // -------------------------------- // Get hardware (cpu) string // Macintosh & Intel machines have different /proc/cpuinfo files sprintf(readbuf, " "); if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i pmac | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); pclose(pipein_fp); // Motorola (PowerPC) Motherboard if (strlen(readbuf) > 1) { if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i machine | sed -e 's/ //' | sed -e 's/Macintosh/Mac/i' | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); hardware_str_ptr = strtok(readbuf, ":"); hardware_machine = strtok(NULL, ":"); pclose(pipein_fp); strcpy(hardware_str, hardware_machine); strcat(hardware_str, " "); if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i cpu | sed -e 's/ //' | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); hardware_str_ptr = strtok(readbuf, ":"); hardware_cpu = strtok(NULL, ":"); pclose(pipein_fp); strcat(hardware_str, hardware_cpu); strcat(hardware_str, " "); if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i clock | sed -e 's/ //' | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); hardware_str_ptr = strtok(readbuf, ":"); hardware_clock = strtok(NULL, ":"); pclose(pipein_fp); strcat(hardware_str, hardware_clock); } // Intel Motherboard (i hope!) else { sprintf(readbuf, " "); // Clear string before next use if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i 'model name' | cut -d':' -f2 | sed -e 's/ //' | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); pclose(pipein_fp); if (strlen(readbuf) > 1) { hardware_machine = readbuf; } else { hardware_machine = "Unknown"; } strcpy(hardware_str, hardware_machine); strcat(hardware_str, " "); sprintf(readbuf, " "); // Clear string before next use if ((pipein_fp = popen("cat /proc/cpuinfo | grep -i 'cpu mhz' | sed -e 's/ //g' | tr -d '\n'", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); pclose(pipein_fp); if (strlen(readbuf) > 1) { hardware_str_ptr = strtok(readbuf, ":"); hardware_cpu = strtok(NULL, ":"); } else { hardware_cpu = "Unknown"; } strcat(hardware_str, hardware_cpu); } // Get version string if ((pipein_fp = popen("cat /proc/version", "r")) == NULL) { perror("popen"); exit(1); } fgets(readbuf, 80, pipein_fp); kernel_str_ptr = strtok(readbuf, " "); kernel_str_ptr = strtok(NULL, " "); kernel_str_ptr = strtok(NULL, " "); pclose(pipein_fp); // Compose final kernel string// strcpy(kernel_str, "Version "); strcat(kernel_str, kernel_str_ptr);// gtk_entry_set_text(GTK_ENTRY(entry_hardware), hardware_str);// gtk_entry_set_text(GTK_ENTRY(entry_kernel), kernel_str);// brent - 04/28/01 - perhaps we should ditch the texboxes and use LABELS (?)// - moving to 0.9.5 gtk_label_set_justify(GTK_LABEL(label_hardware), GTK_JUSTIFY_RIGHT); gtk_label_set_text(GTK_LABEL(label_hardware), hardware_str); gtk_label_set_text(GTK_LABEL(label_kernel), kernel_str);}voidon_users_clist_click_column (GtkCList *clist, gint column, gpointer user_data){ // Get pointers to window widgets, declare local vars GtkWidget *users_clist = lookup_widget(app1, "users_clist"); static int current_col = -1; // Set the column number to apply sort to gtk_clist_set_sort_column(GTK_CLIST(users_clist), column); // Choose sort type to pass to GTK (NULL = default, which is good for CHAR strings) gtk_clist_set_compare_func(GTK_CLIST(users_clist), NULL); // Based upon current sort (ASC or DESC), choose sort order for this iteration. if (current_col == column) { gtk_clist_set_sort_type(GTK_CLIST(users_clist), GTK_SORT_DESCENDING); current_col = -1; } else { gtk_clist_set_sort_type(GTK_CLIST(users_clist), GTK_SORT_ASCENDING); current_col = column; } // Sort the column in users_clist widget gtk_clist_sort(GTK_CLIST(users_clist));}// ------------------------------------------------------------------------- //// --------------------> Printing Function <----------------------- //// ------------------------------------------------------------------------- //voidon_button_print_okay_clicked (GtkButton *button, gpointer user_data){ // Get pointers to window widgets and declare local variables. GtkWidget *radiobutton_lpr = lookup_widget(dialog_print, "radiobutton_lpr"); GtkWidget *radiobutton_other = lookup_widget(dialog_print, "radiobutton_other"); GtkWidget *entry_command = lookup_widget(dialog_print, "entry_command"); gchar *entry_text = ""; char print_command[80] = ""; // Determine which print command to use: 'lpr' or "users choice" if (GTK_TOGGLE_BUTTON(radiobutton_lpr)->active) { strcpy(print_command, "lpr"); } else if (GTK_TOGGLE_BUTTON(radiobutton_other)->active) { entry_text = gtk_entry_get_text((GtkEntry *) entry_command); sprintf(print_command, "%s", entry_text); if (strlen(print_command) == 0) { // Create Standard GNOME Error Dialog on-the-fly (i love these!) gnome_dialog_run_and_close(GNOME_DIALOG(gnome_error_dialog ("You must enter an alternate Print Command since you chose that option."))); return; } } // Pipe print command and voila!, the doc is printed. strcat(print_command, " /tmp/gwcc_out.txt"); if (system(print_command) == -1) {// sprintf(entry_text, "The System Call '%s' failed!", print_command); gnome_dialog_run_and_close(GNOME_DIALOG(gnome_error_dialog(entry_text))); } // Destroy Print Dialog gtk_widget_destroy(dialog_print);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -