mysql-admtools/common.c

109 lines
1.9 KiB
C

/*
* @(#) $Header: /home/nalle/cvsroot/mysql-admutils/common.c,v 1.3 1998/07/06 12:33:07 ljosa Exp $
*
* functions used by mysql-dbadm.c and mysql-useradm.c
*
*/
#include <config.h>
#include <stdio.h>
#include <stdarg.h>
#include <mysql.h>
#include <assert.h>
#include <pwd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "mysql-admutils.h"
char *program_name;
static char *rcsheader = "@(#) " PACKAGE " " VERSION " ljosa@initio.no $Header: /home/nalle/cvsroot/mysql-admutils/common.c,v 1.3 1998/07/06 12:33:07 ljosa Exp $";
int
version()
{
printf("%s %s\n", program_name, rcsheader);
exit(0);
}
int
wrong_use(char *format, ...)
{
va_list ap;
if (format)
{
fprintf(stderr, "%s: ", program_name);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
putchar('\n');
}
fprintf(stderr, "Try `%s --help' for more information.\n", program_name);
return 1;
}
int
dberror(MYSQL *pmysql, char *format, ...)
{
const char *errmsg;
va_list ap;
char *p;
char token[1024] = "%";
fprintf(stderr, "%s: ", program_name);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
if (pmysql)
{
errmsg = mysql_error(pmysql);
if ((errmsg) && (strcmp(errmsg, "") != 0))
fprintf(stderr, "mysql: %s\n", errmsg);
mysql_close(pmysql);
}
return 1;
}
/* decides if the UNIX user is entitled to the MySQL database or MySQL user. */
int
owner(char *name)
{
struct passwd *p;
p = getpwuid(getuid());
if (!p)
{
dberror(NULL, "Failed to look up your UNIX username.");
exit(1);
}
if (strcmp(name, p->pw_name) == 0)
return 1; /* OK */
if ((strncmp(name, p->pw_name, strlen(p->pw_name)) == 0) &&
(*(name + strlen(p->pw_name)) == '_'))
return 1; /* OK */
return 0; /* not owner if we get as far as this */
}
int
reload(MYSQL *pmysql)
{
return mysql_reload(pmysql);
}