Tue Aug 18 00:42:33 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / userlist.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
6 #include "citadel.h"
7 #include <unistd.h>
8
9 void attach_to_server(int argc, char **argv);
10
11 /*
12  * num_parms()  -  discover number of parameters...
13  */
14 int num_parms(char *source)
15 {
16         int a;
17         int count = 1;
18
19         for (a=0; a<strlen(source); ++a) 
20                 if (source[a]=='|') ++count;
21         return(count);
22         }
23
24
25 /*
26  * extract()  -  extract a parameter from a series of "|" separated...
27  */
28 void extract(char *dest, char *source, int parmnum)
29 {
30         char buf[256];
31         int count = 0;
32         int n;
33
34         n = num_parms(source);
35
36         if (parmnum >= n) {
37                 strcpy(dest,"");
38                 return;
39                 }
40         strcpy(buf,source);
41         if ( (parmnum == 0) && (n == 1) ) {
42                 strcpy(dest,buf);
43                 return;
44                 }
45
46         while (count++ < parmnum) do {
47                 strcpy(buf,&buf[1]);
48                 } while( (strlen(buf)>0) && (buf[0]!='|') );
49         if (buf[0]=='|') strcpy(buf,&buf[1]);
50         for (count = 0; count<strlen(buf); ++count)
51                 if (buf[count] == '|') buf[count] = 0;
52         strcpy(dest,buf);
53         }
54
55 /*
56  * extract_int()  -  extract an int parm w/o supplying a buffer
57  */
58 int extract_int(char *source, int parmnum)
59 {
60         char buf[256];
61         
62         extract(buf,source,parmnum);
63         return(atoi(buf));
64         }
65
66
67 /*
68  * extract_long()  -  extract an long parm w/o supplying a buffer
69  */
70 long extract_long(char *source, int parmnum)
71 {
72         char buf[256];
73         
74         extract(buf,source,parmnum);
75         return(atol(buf));
76         }
77
78
79 void logoff(int code)
80 {
81         exit(code);
82         }
83
84 void userlist(void) { 
85         char buf[256];
86         char fl[256];
87         struct tm *tmbuf;
88         long lc;
89
90         serv_puts("LIST");
91         serv_gets(buf);
92         if (buf[0]!='1') {
93                 printf("%s\n",&buf[4]);
94                 return;
95                 }
96         printf("       User Name           Num  L  LastCall  Calls Posts\n");
97         printf("------------------------- ----- - ---------- ----- -----\n");
98         while (serv_gets(buf), strcmp(buf,"000")) {
99                 extract(fl,buf,0);
100                 printf("%-25s ",fl);
101                 printf("%5ld %d ",extract_long(buf,2),
102                         extract_int(buf,1));
103                 lc = extract_long(buf,3);
104                 tmbuf = (struct tm *)localtime(&lc);
105                 printf("%02d/%02d/%04d ",
106                         (tmbuf->tm_mon+1),
107                         tmbuf->tm_mday,
108                         (tmbuf->tm_year + 1900));
109                 printf("%5ld %5ld\n",
110                         extract_long(buf,4),extract_long(buf,5));
111                 }
112         printf("\n");
113         }
114
115
116 void main(int argc, char **argv)
117 {
118         char buf[256];
119
120         attach_to_server(argc,argv);
121         serv_gets(buf);
122         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
123                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
124                 logoff(atoi(buf));
125                 }
126
127         userlist();
128
129         serv_puts("QUIT");
130         serv_gets(buf);
131         exit(0);
132         }
133
134
135 #ifdef NO_STRERROR
136 /*
137  * replacement strerror() for systems that don't have it
138  */
139 char *strerror(int e)
140 {
141         static char buf[32];
142
143         sprintf(buf,"errno = %d",e);
144         return(buf);
145         }
146 #endif
147
148
149
150
151