Re: Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-11 Thread Dimitris Papastamos
On Tue, Nov 11, 2014 at 02:21:35PM +0400, Vadim Zhukov wrote:
> Why TAILQ? SLIST should be enough.

SLIST makes sense yes.

> Also, moving sys/types.h is wrong.

OK, will fix.

> Why do you want to get this in? Any more ac(8) patches coming?

Not really, I was just randomly browsing through the source tree
and was looking for something to improve.



Re: Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-11 Thread Vadim Zhukov
Why TAILQ? SLIST should be enough.

Also, moving sys/types.h is wrong.

Why do you want to get this in? Any more ac(8) patches coming?

--
Vadim Zhukov
05 нояб. 2014 г. 14:34 пользователь "Dimitris Papastamos" 
написал:

> Hi,
>
> I've attempted to convert the hand-rolled linked lists over to
> TAILQ.  I've tested this briefly and it seems to work okay.
>
> Does it look good?  Let me know if I need to rework anything.
>
> ===
> RCS file: /cvs/src/usr.sbin/ac/ac.c,v
> retrieving revision 1.22
> diff -u -p -r1.22 ac.c
> --- ac.c31 Aug 2012 19:57:12 -  1.22
> +++ ac.c5 Nov 2014 11:30:46 -
> @@ -25,9 +25,11 @@
>   * SUCH DAMAGE.
>   */
>
> -#include 
>  #include 
> +#include 
>  #include 
> +#include 
> +
>  #include 
>  #include 
>  #include 
> @@ -40,28 +42,28 @@
>  /*
>   * this is for our list of currently logged in sessions
>   */
> -struct utmp_list {
> -   struct utmp_list *next;
> +struct utmp_entry {
> struct utmp usr;
> +   TAILQ_ENTRY(utmp_entry) next;
>  };
>
>  /*
>   * this is for our list of users that are accumulating time.
>   */
> -struct user_list {
> -   struct user_list *next;
> +struct user_entry {
> charname[UT_NAMESIZE+1];
> time_t  secs;
> +   TAILQ_ENTRY(user_entry) next;
>  };
>
>  /*
>   * this is for chosing whether to ignore a login
>   */
> -struct tty_list {
> -   struct tty_list *next;
> +struct tty_entry {
> charname[UT_LINESIZE+3];
> size_t  len;
> int ret;
> +   TAILQ_ENTRY(tty_entry) next;
>  };
>
>  /*
> @@ -70,8 +72,9 @@ struct tty_list {
>  static time_t  Total = 0;
>  static time_t  FirstTime = 0;
>  static int Flags = 0;
> -static struct user_list *Users = NULL;
> -static struct tty_list *Ttys = NULL;
> +static TAILQ_HEAD(Utmphead, utmp_entry) Utmphead;
> +static TAILQ_HEAD(Userhead, user_entry) Userhead;
> +static TAILQ_HEAD(Ttyhead, tty_entry) Ttyhead;
>
>  #defineAC_W1   /* not _PATH_WTMP
> */
>  #defineAC_D2   /* daily totals
> (ignore -p) */
> @@ -88,14 +91,13 @@ int ac(FILE *);
>  void   add_tty(char *);
>  intdo_tty(char *);
>  FILE   *file(char *);
> -struct utmp_list   *log_in(struct utmp_list *, struct utmp *);
> -struct utmp_list   *log_out(struct utmp_list *, struct utmp *);
> -inton_console(struct utmp_list *);
> +void   log_in(struct utmp *);
> +void   log_out(struct utmp *);
> +inton_console(void);
>  void   show(char *, time_t);
> -void   show_today(struct user_list *, struct utmp_list *,
> -   time_t);
> -void   show_users(struct user_list *);
> -struct user_list   *update_user(struct user_list *, char *, time_t);
> +void   show_today(time_t);
> +void   show_users(void);
> +void   update_user(char *, time_t);
>  void   usage(void);
>
>  /*
> @@ -119,12 +121,12 @@ file(char *name)
>  void
>  add_tty(char *name)
>  {
> -   struct tty_list *tp;
> +   struct tty_entry *tp;
> char *rcp;
>
> Flags |= AC_T;
>
> -   if ((tp = malloc(sizeof(struct tty_list))) == NULL)
> +   if ((tp = malloc(sizeof(sizeof(*tp == NULL)
> err(1, "malloc");
> tp->len = 0;/* full match */
> tp->ret = 1;/* do if match */
> @@ -137,8 +139,7 @@ add_tty(char *name)
> *rcp = '\0';
> tp->len = strlen(tp->name); /* match len bytes only */
> }
> -   tp->next = Ttys;
> -   Ttys = tp;
> +   TAILQ_INSERT_HEAD(&Ttyhead, tp, next);
>  }
>
>  /*
> @@ -147,10 +148,10 @@ add_tty(char *name)
>  int
>  do_tty(char *name)
>  {
> -   struct tty_list *tp;
> +   struct tty_entry *tp;
> int def_ret = 0;
>
> -   for (tp = Ttys; tp != NULL; tp = tp->next) {
> +   TAILQ_FOREACH(tp, &Ttyhead, next) {
> if (tp->ret == 0)   /* specific don't */
> def_ret = 1;/* default do */
> if (tp->len != 0) {
> @@ -167,31 +168,30 @@ do_tty(char *name)
>  /*
>   * update user's login time
>   */
> -struct user_list *
> -update_user(struct user_list *head, char *name, time_t secs)
> +void
> +update_user(char *name, time_t secs)
>  {
> -   struct user_list *up;
> +   struct user_entry *up;
>
> -   for (up = head; up != NULL; up = up->next) {
> +   TAILQ_FOREACH(up, &Userhead, next) {
> if (strncmp(up->name, name, sizeof (up->name) - 1) == 0) {
> up->secs += secs;
> Total += secs;
> -   return head;
> +   

Re: Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-11 Thread Dimitris Papastamos
Hi everyone,

Any interest in this?  It is a cleanup change and not intended to have
any functional differences.

Cheers,
Dimitris



Convert hand-rolled lists to TAILQ_* in ac(8)

2014-11-05 Thread Dimitris Papastamos
Hi,

I've attempted to convert the hand-rolled linked lists over to
TAILQ.  I've tested this briefly and it seems to work okay.

Does it look good?  Let me know if I need to rework anything.

===
RCS file: /cvs/src/usr.sbin/ac/ac.c,v
retrieving revision 1.22
diff -u -p -r1.22 ac.c
--- ac.c31 Aug 2012 19:57:12 -  1.22
+++ ac.c5 Nov 2014 11:30:46 -
@@ -25,9 +25,11 @@
  * SUCH DAMAGE.
  */
 
-#include 
 #include 
+#include 
 #include 
+#include 
+
 #include 
 #include 
 #include 
@@ -40,28 +42,28 @@
 /*
  * this is for our list of currently logged in sessions
  */
-struct utmp_list {
-   struct utmp_list *next;
+struct utmp_entry {
struct utmp usr;
+   TAILQ_ENTRY(utmp_entry) next;
 };
 
 /*
  * this is for our list of users that are accumulating time.
  */
-struct user_list {
-   struct user_list *next;
+struct user_entry {
charname[UT_NAMESIZE+1];
time_t  secs;
+   TAILQ_ENTRY(user_entry) next;
 };
 
 /*
  * this is for chosing whether to ignore a login
  */
-struct tty_list {
-   struct tty_list *next;
+struct tty_entry {
charname[UT_LINESIZE+3];
size_t  len;
int ret;
+   TAILQ_ENTRY(tty_entry) next;
 };
 
 /*
@@ -70,8 +72,9 @@ struct tty_list {
 static time_t  Total = 0;
 static time_t  FirstTime = 0;
 static int Flags = 0;
-static struct user_list *Users = NULL;
-static struct tty_list *Ttys = NULL;
+static TAILQ_HEAD(Utmphead, utmp_entry) Utmphead;
+static TAILQ_HEAD(Userhead, user_entry) Userhead;
+static TAILQ_HEAD(Ttyhead, tty_entry) Ttyhead;
 
 #defineAC_W1   /* not _PATH_WTMP */
 #defineAC_D2   /* daily totals (ignore 
-p) */
@@ -88,14 +91,13 @@ int ac(FILE *);
 void   add_tty(char *);
 intdo_tty(char *);
 FILE   *file(char *);
-struct utmp_list   *log_in(struct utmp_list *, struct utmp *);
-struct utmp_list   *log_out(struct utmp_list *, struct utmp *);
-inton_console(struct utmp_list *);
+void   log_in(struct utmp *);
+void   log_out(struct utmp *);
+inton_console(void);
 void   show(char *, time_t);
-void   show_today(struct user_list *, struct utmp_list *,
-   time_t);
-void   show_users(struct user_list *);
-struct user_list   *update_user(struct user_list *, char *, time_t);
+void   show_today(time_t);
+void   show_users(void);
+void   update_user(char *, time_t);
 void   usage(void);
 
 /*
@@ -119,12 +121,12 @@ file(char *name)
 void
 add_tty(char *name)
 {
-   struct tty_list *tp;
+   struct tty_entry *tp;
char *rcp;
 
Flags |= AC_T;
 
-   if ((tp = malloc(sizeof(struct tty_list))) == NULL)
+   if ((tp = malloc(sizeof(sizeof(*tp == NULL)
err(1, "malloc");
tp->len = 0;/* full match */
tp->ret = 1;/* do if match */
@@ -137,8 +139,7 @@ add_tty(char *name)
*rcp = '\0';
tp->len = strlen(tp->name); /* match len bytes only */
}
-   tp->next = Ttys;
-   Ttys = tp;
+   TAILQ_INSERT_HEAD(&Ttyhead, tp, next);
 }
 
 /*
@@ -147,10 +148,10 @@ add_tty(char *name)
 int
 do_tty(char *name)
 {
-   struct tty_list *tp;
+   struct tty_entry *tp;
int def_ret = 0;
 
-   for (tp = Ttys; tp != NULL; tp = tp->next) {
+   TAILQ_FOREACH(tp, &Ttyhead, next) {
if (tp->ret == 0)   /* specific don't */
def_ret = 1;/* default do */
if (tp->len != 0) {
@@ -167,31 +168,30 @@ do_tty(char *name)
 /*
  * update user's login time
  */
-struct user_list *
-update_user(struct user_list *head, char *name, time_t secs)
+void
+update_user(char *name, time_t secs)
 {
-   struct user_list *up;
+   struct user_entry *up;
 
-   for (up = head; up != NULL; up = up->next) {
+   TAILQ_FOREACH(up, &Userhead, next) {
if (strncmp(up->name, name, sizeof (up->name) - 1) == 0) {
up->secs += secs;
Total += secs;
-   return head;
+   return;
}
}
/*
 * not found so add new user unless specified users only
 */
if (Flags & AC_U)
-   return head;
+   return;
 
-   if ((up = malloc(sizeof(struct user_list))) == NULL)
+   if ((up = malloc(sizeof(*up))) == NULL)
err(1, "malloc");
-   up->next = head;
strlcpy(up->name, name, sizeof (up->name));
up->secs = secs;
Total += secs;
-   return u