/* Write the DB login file return: -1 error (cannot read file) 0 OK */ int write_file(LOGIN * login) { int i; const char *file; FILE *fd; file = login_filename(); G_debug(3, "DB login file = <%s>", file); fd = fopen(file, "w"); if (fd == NULL) return -1; /* fchmod is not available on Windows */ /* fchmod ( fileno(fd), S_IRUSR | S_IWUSR ); */ chmod(file, S_IRUSR | S_IWUSR); for (i = 0; i < login->n; i++) { fprintf(fd, "%s|%s", login->data[i].driver, login->data[i].database); if (login->data[i].user) { fprintf(fd, "|%s", login->data[i].user); if (login->data[i].password) fprintf(fd, "|%s", login->data[i].password); } fprintf(fd, "\n"); } fclose(fd); return 0; }
/* Read the DB login file if it exists return: -1 error (cannot read file) number of items (0 also if file does not exist) */ static int read_file(LOGIN * login) { int ret; const char *file; FILE *fd; char buf[2001], dr[500], db[500], usr[500], pwd[500], host[500], port[500]; login->n = 0; file = login_filename(); G_debug(3, "read_file(): DB login file = <%s>", file); if (access(file, F_OK) != 0) { G_debug(3, "login file does not exist"); return 0; } fd = fopen(file, "r"); if (fd == NULL) { G_warning(_("Unable to read file '%s'"), file); return -1; } while (G_getl2(buf, 2000, fd)) { G_chop(buf); usr[0] = pwd[0] = host[0] = port[0] = '\0'; ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd, host, port); G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s] host=[%s], port=[%s]", ret, dr, db, usr, pwd, host, port); if (ret < 2) { G_warning(_("Login file (%s) corrupted (line: %s)"), file, buf); continue; } add_login(login, dr, db, usr, pwd, host, port, -1); } fclose(fd); return (login->n); }
/* Read the DB login file if it exists return: -1 error (cannot read file) number of items (0 also if file does not exist) */ int read_file(LOGIN * login) { int ret; const char *file; struct stat info; FILE *fd; char buf[2001], dr[500], db[500], usr[500], pwd[500]; login->n = 0; file = login_filename(); G_debug(3, "DB login file = <%s>", file); if (stat(file, &info) != 0) { G_debug(3, "login file does not exist"); return 0; } fd = fopen(file, "r"); if (fd == NULL) return -1; while (G_getl2(buf, 2000, fd)) { G_chop(buf); usr[0] = pwd[0] = '\0'; ret = sscanf(buf, "%[^|]|%[^|]|%[^|]|%[^\n]", dr, db, usr, pwd); G_debug(3, "ret = %d : drv=[%s] db=[%s] usr=[%s] pwd=[%s]", ret, dr, db, usr, pwd); if (ret < 2) { G_warning(_("Login file corrupted")); continue; } add_login(login, dr, db, usr, pwd); } fclose(fd); return (login->n); }