mysql-admutils/common.c

128 lines
2.2 KiB
C
Raw Normal View History

/*
* @(#) $Header: /tmp/cvs/mysql-admutils/common.c,v 1.1.1.1 2001-11-25 00:41:16 lkarsten 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 <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include "mysql-admutils.h"
char *program_name;
static char *rcsheader = "@(#) " PACKAGE " " VERSION " ljosa@initio.no $Header: /tmp/cvs/mysql-admutils/common.c,v 1.1.1.1 2001-11-25 00:41:16 lkarsten 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;
}
/* always returns 1. */
int
dberror(MYSQL *pmysql, char *format, ...)
{
char *errmsg;
va_list ap;
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;
}
/* always returns 1. */
int
fatal_error(char *format, ...)
{
va_list ap;
fprintf(stderr, "%s: ", program_name);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
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);
}