Index: command.c =================================================================== RCS file: /home/pgsql/cvs/pgsql/src/bin/psql/command.c,v retrieving revision 1.43 diff -u -r1.43 command.c --- command.c 2000/12/30 14:47:06 1.43 +++ command.c 2001/01/04 18:16:00 @@ -309,7 +309,10 @@ case 'i': case 's': case 'S': - success = listTables(&cmd[1], name, show_verbose); + success = listTables(&cmd[1], name, show_verbose); + break; + case 'u': + success = describeUsers(name); break; default: status = CMD_UNKNOWN; Index: describe.c =================================================================== RCS file: /home/pgsql/cvs/pgsql/src/bin/psql/describe.c,v retrieving revision 1.26 diff -u -r1.26 describe.c --- describe.c 2000/10/25 20:36:52 1.26 +++ describe.c 2001/01/04 22:40:34 @@ -890,6 +890,106 @@ } +/* + * describeUsers() + * + * \du [user] + * + * Describes users, possibly based on a simplistic prefix search on the + * argument. + */ + +bool +describeUsers (const char *name) +{ + char buf[384 + REGEXP_CUTOFF]; + PGresult *res; + printTableOpt myopt = pset.popt.topt; + int i; + char *title = NULL; + const char *headers[4]; + char **cells = NULL; + unsigned int cols; + + /* + * All we want to know is the user names and permissions + * for the system. + */ + + /* set up title - static */ + asprintf(&title, "List of Users"); + + /* Set up header - static */ + cols = 0; + headers[cols++] = "User Name"; + headers[cols++] = "User ID"; + headers[cols++] = "Attributes"; + headers[cols] = NULL; + + strcpy(buf, + "SELECT u.usename AS \"User Name\"\n + , u.usesysid AS \"User ID\"\n + , u.usesuper AS \"Super User\"\n + , u.usecreatedb AS \"Create DB\"\n + FROM pg_user u\n"); + if (name) + { + strcat(buf, " WHERE u.usename ~ '^"); + strncat(buf, name, REGEXP_CUTOFF); + strcat(buf, "'\n"); + } + strcat(buf, "ORDER BY \"User Name\"\n"); + + res = PSQLexec(buf); + if (!res) + return false; + + cells = xmalloc((PQntuples(res) * cols + 1) * sizeof(*cells)); + cells[PQntuples(res) * cols] = NULL; + + for (i = 0; i < PQntuples(res); i++) + { + char createuser[2] = ""; + char createdb[2] = ""; + + /* Name */ + cells[i * cols + 0] = PQgetvalue(res, i, 0); + + /* ID */ + cells[i * cols + 1] = PQgetvalue(res, i, 1); + + /* Super */ + strcpy(createuser, PQgetvalue(res, i, 2)); + + /* Create DB */ + strcpy(createdb, PQgetvalue(res, i, 3)); + + cells[i * cols + 2] = xmalloc((strlen("create user, create DB") * sizeof(char)) + 1); + strcpy(cells[i * cols + 2], ""); + + if (strcmp(createuser, "t") == 0) + strcat(cells[i * cols + 2], "create user"); + + if (strcmp(createdb, "t") == 0) { + if (strcmp(createuser, "t") == 0) + strcat(cells[i * cols + 2], ", "); + strcat(cells[i * cols + 2], "create DB"); + } + } + + printTable(title, headers, + (const char **) cells, + NULL, + "lll", &myopt, pset.queryFout); + + /* clean up */ + free(title); + free(cells); + + PQclear(res); + return true; +} + /* * listTables() Index: describe.h =================================================================== RCS file: /home/pgsql/cvs/pgsql/src/bin/psql/describe.h,v retrieving revision 1.9 diff -u -r1.9 describe.h --- describe.h 2000/04/12 17:16:22 1.9 +++ describe.h 2001/01/04 18:14:42 @@ -22,6 +22,9 @@ /* \do */ bool describeOperators(const char *name); +/* \du */ +bool describeUsers(const char *name); + /* \z (or \dp) */ bool permissionsList(const char *name);