database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Devel 21136 게시물 읽기
 News | Q&A | Columns | Tutorials | Devel | Files | Links
No. 21136
MySQL UDF -- Unix user interface
작성자
정재익(advance)
작성일
2004-02-24 14:38
조회수
12,402

/*
   Copyright ?1998 by Nem W Schlecht
   This file is public domain and comes with NO WARRANTY of any kind

MySQL SQL code for creating/deleting these functions:

CREATE FUNCTION mygetpwnam RETURNS STRING SONAME "userinf.so";
DROP FUNCTION mygetpwnam;

CREATE FUNCTION mygetpwuid RETURNS STRING SONAME "userinf.so";
DROP FUNCTION mygetpwuid;

CREATE FUNCTION mygetgrnam RETURNS STRING SONAME "userinf.so";
DROP FUNCTION mygetgrnam;

CREATE FUNCTION mygetgrgid RETURNS STRING SONAME "userinf.so";
DROP FUNCTION mygetgrgid;

*/

#ifdef STANDARD
#include <stdio.h>
#include <string.h>
#else
#include <global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>

/*
 * The following includes are for the user/group info functions
 */
#include <pwd.h>
#include <grp.h>

#ifdef HAVE_DLOPEN

/* These must be right or mysqld will not find the symbol! */

extern "C" {
my_bool mygetpwnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void mygetpwnam_deinit(UDF_INIT *initid);
char *mygetpwnam(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error);

my_bool mygetpwuid_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void mygetpwuid_deinit(UDF_INIT *initid);
char *mygetpwuid(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error);

char *_fill_pw_info(passwd *user, char *inf_sep, char *result);

}

my_bool mygetpwnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count == 0 || args->arg_count > 2 ||
      args->arg_type[0] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getpwnam");
    return 1;
  }

  if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getpwnam");
    return 1;
  } 

  initid->maybe_null = 1;
  return 0;
}

void mygetpwnam_deinit(UDF_INIT *initid)
{
}

char *mygetpwnam(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error)
{
    struct passwd *user;
    char *inf_sep;

    if (args->arg_count > 1) {
 inf_sep = (char *) malloc(strlen(args->args[1]));
 strcpy(inf_sep, args->args[1]);
    } else {
 inf_sep = (char *) malloc(sizeof(char));
 strcpy(inf_sep, ":");
    }

    user = getpwnam(args->args[0]);
    result = _fill_pw_info(user, inf_sep, result);
    if (!result) {
 *is_null = 1;
 return 0;
    }
    free(inf_sep);
    *length = (ulong) strlen(result);
    return result;
}


my_bool mygetpwuid_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count == 0 || args->arg_count > 2 ||
      args->arg_type[0] != INT_RESULT) {
    strcpy(message,"Wrong arguments to getpwuid");
    return 1;
  }

  if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getpwuid");
    return 1;
  } 

  initid->maybe_null = 1;
  return 0;
}

void mygetpwuid_deinit(UDF_INIT *initid)
{
}

char *mygetpwuid(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error)
{
    struct passwd *user;
    char *inf_sep;

    if (args->arg_count > 1) {
 inf_sep = (char *) malloc(strlen(args->args[1]));
 strcpy(inf_sep, args->args[1]);
    } else {
 inf_sep = (char *) malloc(sizeof(char));
 strcpy(inf_sep, ":");
    }

    user = getpwuid( *((long long*) args->args[0]) );
    result = _fill_pw_info(user, inf_sep, result);
    if (!result) {
 *is_null = 1;
 return 0;
    }
    free(inf_sep);
    *length = (ulong) strlen(result);
    return result;
}

char *_fill_pw_info(passwd *user, char *inf_sep, char *result) {
    char buf[10];

    if (user != 0) {
 strcpy(result, user->pw_name); strcat(result, inf_sep);
 strcat(result, user->pw_passwd); strcat(result, inf_sep);

 sprintf(buf, "%d", user->pw_uid);
 strcat(result, buf);
 strcat(result, inf_sep);

 sprintf(buf, "%d", user->pw_gid);
 strcat(result, buf);
 strcat(result, inf_sep);

/* strcat(result, user->pw_age); strcat(result, inf_sep);*/
/* strcat(result, user->pw_comment); strcat(result, inf_sep);*/
 strcat(result, user->pw_gecos); strcat(result, inf_sep);
 strcat(result, user->pw_dir); strcat(result, inf_sep);
 strcat(result, user->pw_shell);

 return result;
    } else {
 return 0;
    }
}

/*
 * GROUP functions
 */

extern "C" {
my_bool mygetgrnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void mygetgrnam_deinit(UDF_INIT *initid);
char *mygetgrnam(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error);

my_bool mygetgrgid_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void mygetgrgid_deinit(UDF_INIT *initid);
char *mygetgrgid(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error);

char *_fill_gr_info(group *group, char *inf_sep, char *mem_sep,
        char *result);

}

my_bool mygetgrnam_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count == 0 || args->arg_count > 3 ||
      args->arg_type[0] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid;  Use the source");
    return 1;
  }

  if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid");
    return 1;
  } 

  if (args->arg_count == 3 && args->arg_type[2] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid");
    return 1;
  } 

  initid->maybe_null = 1;
  return 0;
}

void mygetgrnam_deinit(UDF_INIT *initid)
{
}

char *mygetgrnam(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error)
{
    struct group *group;
    char *inf_sep;
    char *mem_sep;

    if (args->args[1]) {
 inf_sep = (char *) malloc(strlen(args->args[1]));
 strcpy(inf_sep, args->args[1]);
    } else {
 inf_sep = (char *) malloc(sizeof(char));
 strcpy(inf_sep, ":");
    }

    if (args->arg_count > 2) {
 mem_sep = (char *) malloc(strlen(args->args[2]));
 strcpy(mem_sep, args->args[2]);
    } else {
 mem_sep = (char *) malloc(sizeof(char));
 strcpy(mem_sep, ",");
    }

    group = getgrnam(args->args[0]);
    result = _fill_gr_info(group, inf_sep, mem_sep, result);
    if (!result) {
 *is_null = 1;
 return 0;
    }

    *length = (ulong) strlen(result);
    free(inf_sep);
    free(mem_sep);
    return result;
}

my_bool mygetgrgid_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count == 0 || args->arg_count > 3 ||
      args->arg_type[0] != INT_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid;  Use the source");
    return 1;
  }

  if (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid");
    return 1;
  } 

  if (args->arg_count == 3 && args->arg_type[2] != STRING_RESULT) {
    strcpy(message,"Wrong arguments to getgrgid");
    return 1;
  } 

  initid->maybe_null = 1;
  return 0;
}

void mygetgrgid_deinit(UDF_INIT *initid)
{
}

char *mygetgrgid(UDF_INIT *initid, UDF_ARGS *args, char *result,
        unsigned long *length, char *is_null, char *error)
{
    struct group *group;
    char *inf_sep;
    char *mem_sep;

    if (args->arg_count > 1) {
 inf_sep = (char *) malloc(strlen(args->args[1]));
 strcpy(inf_sep, args->args[1]);
    } else {
 inf_sep = (char *) malloc(sizeof(char));
 strcpy(inf_sep, ":");
    }

    if (args->arg_count > 2) {
 mem_sep = (char *) malloc(strlen(args->args[2]));
 strcpy(mem_sep, args->args[2]);
    } else {
 mem_sep = (char *) malloc(sizeof(char));
 strcpy(mem_sep, ",");
    }

    group = getgrgid( *((long long*) args->args[0]) );
    result = _fill_gr_info(group, inf_sep, mem_sep, result);
    if (!result) {
 *is_null = 1;
 return 0;
    }

    *length = (ulong) strlen(result);
    free(inf_sep);
    free(mem_sep);
    return result;
}

char *_fill_gr_info(group *group, char *inf_sep, char *mem_sep,
        char *result) {
    char buf[10];
    char **elem;

    if (group != 0) {
 strcpy(result, group->gr_name); strcat(result, inf_sep);
 strcat(result, group->gr_passwd); strcat(result, inf_sep);

 sprintf(buf, "%d", group->gr_gid);
 strcat(result, buf);
 strcat(result, inf_sep);

 for (elem = group->gr_mem; elem && *elem; elem++) {
     strcat(result, *elem);
     strcat(result, mem_sep);
 }
 result[(strlen(result)-strlen(mem_sep))]='\0';
 return result;
    } else {
 return 0;
    }
}

#endif /* HAVE_DLOPEN */

[Top]
No.
제목
작성자
작성일
조회
23671MySQL UDF --- il_to_cp1250
정재익
2004-02-24
9196
23670MySQL UDF -- Longest common subsequence
정재익
2004-02-16
9925
23669MySQL UDF Token() - Tokenizing (Windows version) [1]
이노성
2004-03-08
10808
21136MySQL UDF -- Unix user interface
정재익
2004-02-24
12402
21133MySQL UDF -- square()
정재익
2004-02-24
11599
21132MySQL UDF -- soundex
정재익
2004-02-24
10711
21129MySQL UDF -- ip lookup
정재익
2004-02-24
11010
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.4로 자료를 관리합니다