Initial import
[zanchey/dispense2.git] / sql-edition / ident / pg_ident.c
1 #include <ident.h>
2
3 #include <postgres.h>
4 #include <miscadmin.h>
5 #include <libpq/libpq-be.h>
6 #include <fmgr.h>
7
8 char *ident_result = NULL;
9
10 PG_FUNCTION_INFO_V1(get_ident);
11 Datum get_ident(PG_FUNCTION_ARGS) {
12         IDENT *id;
13
14         if (MyProcPort == NULL)
15                 elog(ERROR, "could not get ident for user: no port");
16
17         if (!ident_result) {
18                 id = ident_lookup(MyProcPort->sock, 5);
19                 if (id && id->identifier) ident_result = strdup(id->identifier);
20         }
21
22         if (ident_result) {
23                 int len;
24                 VarChar* result;
25
26                 len = strlen(ident_result);
27                 if (len > 8) len = 8;
28                 result = palloc(len + VARHDRSZ);
29                 VARATT_SIZEP(result) = len + VARHDRSZ;
30                 memcpy(VARDATA(result), ident_result, len);
31
32                 PG_RETURN_VARCHAR_P(result);
33         }
34
35         elog(ERROR, "could not get ident for user: ident failed");
36
37         /* not reached */
38         PG_RETURN_NULL();
39 }

UCC git Repository :: git.ucc.asn.au