/* * ----------------------------------------------------------------------- * Patch for dbMetrix Database Tool v0.1 * Copyright (c) 1998 Tsukahara Ken * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * ----------------------------------------------------------------------- * Function: Save and restore previous Database connection information * in the file $HOME/.dbMetrix/dbalias. * Alias count <= 64 * Alias length <= 31 characters * Hostname length <= 31 characters * Username length <= 31 characters * Password length <= 31 characters * DBname length <= 31 characters * Port No. length <= 31 characters * * Warning : Password will be saved in plain text format!! * ----------------------------------------------------------------------- */ #include #include "config.h" #include "global.h" #include #include #include dbAlias infAlias[64]; int cntAlias; char fnameAlias[256]; char crntAlias[256]; GtkWidget *Gentry[5]; char *Ghidden; void regist_db_alias (int i, char *h, char *u, char *p, char *d, char *n) { char *def = "*\0"; char *a = crntAlias; strncpy(infAlias[i].alias, (a ? a : def), sizeof(infAlias[0].alias)); strncpy(infAlias[i].host, (h ? h : def), sizeof(infAlias[0].host)); strncpy(infAlias[i].user, (u ? u : def), sizeof(infAlias[0].user)); strncpy(infAlias[i].pass, (p ? p : def), sizeof(infAlias[0].pass)); strncpy(infAlias[i].dbname, (d ? d : def), sizeof(infAlias[0].dbname)); strncpy(infAlias[i].nport, (n ? n : def), sizeof(infAlias[0].nport)); return; } void save_db_alias (void) { char *homedir; FILE *fp; int i; char *a, *h, *u, *p, *d, *n; char *def = "*\0"; time_t t = time(0); struct tm *tinf; char *format = "%s\t%s\t%s\t%s\t%s\t%s\n"; if (fnameAlias[0] == 0x00) { homedir = getenv("HOME"); if (homedir == NULL) return; strcpy(fnameAlias, homedir); strcat(fnameAlias, "/.dbMetrix/dbalias"); } fp = fopen(fnameAlias, "w+"); if (fp == NULL) return; tinf = localtime(&t); fprintf(fp, "# dbMetrix Database definition\n"); fprintf(fp, "# Created on %04d-%02d-%02d %02d:%02d:%02d\n", tinf->tm_year + 1900, tinf->tm_mon + 1, tinf->tm_mday, tinf->tm_hour, tinf->tm_min, tinf->tm_sec); fprintf(fp, "# [Alias] [Hostname] [Username] [Password] [Dababase] [Port#]\n"); fprintf(fp, "# separator='\\t', default='*'\n"); fprintf(fp, "# \n"); for (i = 0; i < cntAlias; i++) { a = infAlias[i].alias; h = infAlias[i].host; u = infAlias[i].user; p = infAlias[i].pass; d = infAlias[i].dbname; n = infAlias[i].nport; fprintf(fp, format, (*a ? a : def), (*h ? h : def), (*u ? u : def), (*p ? p : def), (*d ? d : def), (*n ? n : def)); } fclose(fp); return; } void load_db_alias (void) { char *homedir; FILE *fp; char buf[256]; int i = 0; int n; char *format = "%s\t%s\t%s\t%s\t%s\t%s"; if (fnameAlias[0] == 0x00) { homedir = getenv("HOME"); if (homedir == NULL) return; strcpy(fnameAlias, homedir); strcat(fnameAlias, "/.dbMetrix/dbalias"); } fp = fopen(fnameAlias, "r"); if (fp == NULL) return; while (fgets(buf, sizeof(buf), fp) != NULL) { if (buf[0] == '#' || buf[0] == '\n') continue; n = sscanf(buf, format, infAlias[i].alias, infAlias[i].host, infAlias[i].user, infAlias[i].pass, infAlias[i].dbname, infAlias[i].nport); if (n != 6) continue; if (infAlias[i].alias[0] == '*') infAlias[i].alias[0] = '\0'; if (infAlias[i].host[0] == '*') infAlias[i].host[0] = '\0'; if (infAlias[i].user[0] == '*') infAlias[i].user[0] = '\0'; if (infAlias[i].pass[0] == '*') infAlias[i].pass[0] = '\0'; if (infAlias[i].dbname[0] == '*') infAlias[i].dbname[0] = '\0'; if (infAlias[i].nport[0] == '*') infAlias[i].nport[0] = '\0'; if (++i >= 64) break; } fclose(fp); cntAlias = i; return; } void on_combo1_activate (GtkWidget *widget, gpointer data) { int i, j; int found = 0; char tmp[256] = ""; GtkWidget *combo; combo = get_widget(widget, "combo1"); strncpy(crntAlias, gtk_entry_get_text(GTK_ENTRY (GTK_COMBO (combo)->entry)), sizeof(crntAlias)); for (i = 0; i < cntAlias; i++) { if (strcmp(infAlias[i].alias, crntAlias) == 0) { found = 1; break; } } if (found == 0) return; /* store password and make it invisible */ strcpy(Ghidden, infAlias[i].pass); for (j = 0; Ghidden[j] != 0x00; j++) tmp[j] = '*'; /* How can I get entry widgets' ID without using global variable? */ gtk_entry_set_text(GTK_ENTRY(Gentry[0]), infAlias[i].host); gtk_entry_set_text(GTK_ENTRY(Gentry[1]), infAlias[i].user); gtk_entry_set_text(GTK_ENTRY(Gentry[2]), tmp); gtk_entry_set_text(GTK_ENTRY(Gentry[3]), infAlias[i].dbname); gtk_entry_set_text(GTK_ENTRY(Gentry[4]), infAlias[i].nport); return; }