summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-24 22:54:12 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-03-24 22:54:12 +0800
commita1042f4ba94cf0324dee4c50b442de0e3b6df6e5 (patch)
tree5ac62ae15cdf66d259fa053ef27ff53235b5657c
parent08ead020dbe84be8507459fcd06dc4541ed3bae2 (diff)
downloadpttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar.gz
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar.bz2
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar.lz
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar.xz
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.tar.zst
pttbbs-a1042f4ba94cf0324dee4c50b442de0e3b6df6e5.zip
utmpsort
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@60 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-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)