libnetapi: add NetQueryDisplayInformation example.
[ira/wip.git] / source3 / lib / netapi / examples / user / user_dispinfo.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetQueryDisplayInformation query
4  *  Copyright (C) Guenther Deschner 2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <netapi.h>
27
28 #include "common.h"
29
30 int main(int argc, const char **argv)
31 {
32         NET_API_STATUS status;
33         struct libnetapi_ctx *ctx = NULL;
34         const char *hostname = NULL;
35         void *buffer = NULL;
36         uint32_t entries_read = 0;
37         uint32_t idx = 0;
38         int i;
39
40         struct NET_DISPLAY_USER *user;
41
42         poptContext pc;
43         int opt;
44
45         struct poptOption long_options[] = {
46                 POPT_AUTOHELP
47                 POPT_COMMON_LIBNETAPI_EXAMPLES
48                 POPT_TABLEEND
49         };
50
51         status = libnetapi_init(&ctx);
52         if (status != 0) {
53                 return status;
54         }
55
56         pc = poptGetContext("user_dispinfo", argc, argv, long_options, 0);
57
58         poptSetOtherOptionHelp(pc, "hostname");
59         while((opt = poptGetNextOpt(pc)) != -1) {
60         }
61
62         if (!poptPeekArg(pc)) {
63                 poptPrintHelp(pc, stderr, 0);
64                 goto out;
65         }
66         hostname = poptGetArg(pc);
67
68         /* NetQueryDisplayInformation */
69
70         do {
71                 status = NetQueryDisplayInformation(hostname,
72                                                     1,
73                                                     idx,
74                                                     1000,
75                                                     (uint32_t)-1,
76                                                     &entries_read,
77                                                     &buffer);
78                 if (status == 0 || status == ERROR_MORE_DATA) {
79                         user = (struct NET_DISPLAY_USER *)buffer;
80                         for (i=0; i<entries_read; i++) {
81                                 printf("user %d: %s\n", i, user->usri1_name);
82                                 user++;
83                         }
84                         NetApiBufferFree(buffer);
85                 }
86         } while (status == ERROR_MORE_DATA);
87
88         if (status != 0) {
89                 printf("NetQueryDisplayInformation failed with: %s\n",
90                         libnetapi_get_error_string(ctx, status));
91         }
92
93  out:
94         libnetapi_free(ctx);
95         poptFreeContext(pc);
96
97         return status;
98 }