summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--util/shmctl.c89
1 files changed, 88 insertions, 1 deletions
diff --git a/util/shmctl.c b/util/shmctl.c
index 77b9c99b..9d6846e9 100644
--- a/util/shmctl.c
+++ b/util/shmctl.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
@@ -42,11 +43,97 @@ int fixutmp(int argc, char **argv)
return 0;
}
+/* ulistsort */
+static int cmputmpuserid(const void *i, const void *j){
+ return strcasecmp((*((userinfo_t**)i))->userid, (*((userinfo_t**)j))->userid);
+}
+
+static int cmputmpmode(const void *i, const void *j){
+ return (*((userinfo_t**)i))->mode-(*((userinfo_t**)j))->mode;
+}
+
+static int cmputmpidle(const void *i, const void *j){
+ return (*((userinfo_t**)i))->lastact-(*((userinfo_t**)j))->lastact;
+}
+
+static int cmputmpfrom(const void *i, const void *j){
+ return strcasecmp((*((userinfo_t**)i))->from, (*((userinfo_t**)j))->from);
+}
+
+static int cmputmpfive(const void *i, const void *j){
+ int type;
+ if((type=(*((userinfo_t**)j))->five_win - (*((userinfo_t**)i))->five_win))
+ return type;
+ if((type=(*((userinfo_t**)i))->five_lose - (*((userinfo_t**)j))->five_lose))
+ return type;
+ return (*((userinfo_t**)i))->five_tie-(*((userinfo_t**)j))->five_tie;
+}
+
+static int cmputmpsex(const void *i, const void *j){
+ static int ladyfirst[]={1,0,1,0,1,0,3,3};
+ return ladyfirst[(*((userinfo_t**)i))->sex]-ladyfirst[(*((userinfo_t**)j))->sex];
+}
+static int cmputmppid(const void *i, const void *j){
+ return (*((userinfo_t**)i))->pid-(*((userinfo_t**)j))->pid;
+}
+static int cmputmpuid(const void *i, const void *j){
+ return (*((userinfo_t**)i))->uid-(*((userinfo_t**)j))->uid;
+}
+
+int ulistsort(int argc, char **argv)
+{
+ time_t now=time(NULL);
+ int count, i, ns;
+ userinfo_t *uentp;
+
+ if(now-utmpshm->uptime<60 && (now==utmpshm->uptime || utmpshm->busystate)){
+ puts("lazy sort");
+ //return; /* lazy sort */
+ }
+ utmpshm->busystate=1;
+ utmpshm->uptime = now;
+ ns=(utmpshm->currsorted?0:1);
+
+ for(uentp = &utmpshm->uinfo[0], count=0, i=0;
+ i< USHM_SIZE; i++,uentp = &utmpshm->uinfo[i])
+ if(uentp->pid){
+ utmpshm->sorted[ns][0][count++]= uentp;
+ }
+ utmpshm->number = count;
+ qsort(utmpshm->sorted[ns][0],count,sizeof(userinfo_t*),cmputmpuserid);
+ memcpy(utmpshm->sorted[ns][1],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][2],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][3],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][4],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][5],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][6],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ memcpy(utmpshm->sorted[ns][7],utmpshm->sorted[ns][0],
+ sizeof(userinfo_t *)*count);
+ qsort(utmpshm->sorted[ns][1], count, sizeof(userinfo_t *), cmputmpmode );
+ qsort(utmpshm->sorted[ns][2], count, sizeof(userinfo_t *), cmputmpidle );
+ qsort(utmpshm->sorted[ns][3], count, sizeof(userinfo_t *), cmputmpfrom );
+ qsort(utmpshm->sorted[ns][4], count, sizeof(userinfo_t *), cmputmpfive );
+ qsort(utmpshm->sorted[ns][5], count, sizeof(userinfo_t *), cmputmpsex );
+ qsort(utmpshm->sorted[ns][6], count, sizeof(userinfo_t *), cmputmpuid );
+ qsort(utmpshm->sorted[ns][7], count, sizeof(userinfo_t *), cmputmppid );
+ utmpshm->currsorted=ns;
+ utmpshm->busystate=0;
+ return 0;
+}
+
+/* end of ulistsort */
struct {
int (*func)(int, char **);
char *cmd, *descript;
} cmd[] =
- { {fixutmp, "fixutmp", "clear dead userlist entry"},
+ { {fixutmp, "fixutmp", "clear dead userlist entry"},
+ {ulistsort, "ulistsort", "sort ulist"},
{NULL, NULL, NULL} };
int main(int argc, char **argv)