Nå vil "mysql-dbadm show" også liste ut gruppenes databaser

This commit is contained in:
Thomas Langas 2002-03-06 16:47:12 +00:00
parent c4b3c3bd04
commit fe7ad3f488
1 changed files with 75 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.3 2002-03-06 02:28:27 tlan Exp $
* @(#) $Header: /tmp/cvs/mysql-admutils/mysql-dbadm.c,v 1.4 2002-03-06 16:47:12 tlan Exp $
*
* mysql-dbadm.c
*
@ -13,6 +13,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <unistd.h>
#include "mysql-admutils.h"
@ -110,17 +111,48 @@ drop(MYSQL *pmysql, char *db)
return 0;
}
/* return a list of the user's groupnames */
/* numgroups is the total number of groups found */
char **get_group_names(int *numgroups)
{
char **grouplist;
gid_t gids[33];
int nr_groups, i;
struct group *g;
nr_groups = 0;
nr_groups = getgroups(32, &gids[0]); /* Allow a max of 32 groups */
if (nr_groups == -1) {
dberror(NULL, "Error while trying to fetch group info");
return NULL;
}
*numgroups = nr_groups;
grouplist = malloc((nr_groups+1) * sizeof(char *));
for (i = 0; i < nr_groups; i++) {
g = getgrgid(gids[i]);
grouplist[i] = strdup(g->gr_name);
}
grouplist[i] = NULL;
return grouplist;
}
/* return a list of the user's databases */
char **
list(MYSQL *pmysql)
{
char *wild;
char **usr_groups, **cp; /* holds all group names this dude is a member of [tlan]*/
MYSQL_RES *res;
int rows;
int rows, numgroups, numgroupdbs;
MYSQL_ROW row;
char **dblist;
int i;
int i, counter;
struct passwd *p;
p = getpwuid(getuid());
@ -129,31 +161,66 @@ list(MYSQL *pmysql)
dberror(NULL, "Failed to lookup your UNIX username.");
exit(1);
}
dblist = malloc(2 * sizeof(char*)); /* one for username (if used), rest is done below */
numgroupdbs = 0;
counter = 0;
usr_groups = get_group_names(&numgroups);
cp = usr_groups;
while (*cp) {
if (*cp == NULL)
break;
wild = malloc(strlen(*cp)+3);
sprintf(wild, "%s_%%", *cp);
res = mysql_list_dbs(pmysql, wild);
rows = mysql_num_rows(res);
if (rows > 0) {
numgroupdbs += rows;
dblist = realloc(dblist, (numgroupdbs+2) * sizeof(char *));
for (i = 0; i < rows; i++)
if ((row = mysql_fetch_row(res))) {
dblist[counter++] = strdup(row[0]);
}
}
free(wild);
cp++;
}
wild = malloc(strlen(p->pw_name) + 3);
sprintf(wild, "%s_%%", p->pw_name);
res = mysql_list_dbs(pmysql, wild);
rows = mysql_num_rows(res);
dblist = malloc((rows + 2) * sizeof(char *)); /* one for the username
(might not be used)*/
dblist = realloc(dblist, (numgroupdbs+rows+2) * sizeof(char *));
if (!dblist)
{
dberror(NULL, "Out of memory.\n");
free(wild);
return NULL;
}
for (i = 0; i < rows; i++)
if ((row = mysql_fetch_row(res)))
{
dblist[i] = strdup(row[0]);
dblist[counter++] = strdup(row[0]);
}
res = mysql_list_dbs(pmysql, p->pw_name);
rows = mysql_num_rows(res);
if (rows == 1)
dblist[i++] = strdup(p->pw_name);
dblist[counter++] = strdup(p->pw_name);
dblist[i] = NULL;
dblist[counter] = NULL;
return dblist;
}