File: nwho.c

package info (click to toggle)
ngetty 1.1-3.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 392 kB
  • sloc: ansic: 1,588; makefile: 206; sh: 193; asm: 100
file content (131 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* nwho.c */
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include "lib.h"

#define S(a,b)		str_copy(a,b)
#define SN(a,b,c)	str_copynz(a,b,c)
#define SF(a,b,c)	fmt_str_(a,b,c)
#define N(a,b,c,d)	fmt_nmb_(a,b,c,d)

void usage(char *buf) {
  write(1,buf,str_copy(buf, "usage: nwho [-alpquw] [file]\n"));
  _exit(1);
}

int main(int argc, char **argv) {
  struct tm *tm;
  struct stat st;
  char *montab = "JanFebMarAprMayJunJulAugSepOctNovDec";
  char buf[1024];
  time_t id;
  char *ipasc, mesg, *p;
  unsigned char flagaddr = 0;
  char flagpid = 0;
  char flagcheck = 1;
  char flagup = 0;
  char flagmesg = 0;
#ifdef USE_LIBC_UTMP
  struct utmp_type *ut;
#else
  struct utmp_type ut[1];
  int fd;
#endif

  argc--; argv++;
  while (argc>0) {
    p = argv[0];
    if (*p != '-') break;
    for (++p; *p; ++p) 
      switch (*p) {
      case 'q': flagcheck = 0; break;
      case 'l': ++flagaddr; break;
      case 'p': flagpid = 1; break;
      case 'u': flagup = 1; break;
      case 'w': flagmesg = 1; break;
      case 'a': flagup=1; flagpid=1; ++flagaddr; flagmesg=1; break;
      default: usage(buf);
      }
    argc--; argv++;
  }

#ifdef USE_LIBC_UTMP
  if (argc>0)  f_utmpname(argv[0]);
  setutent();
#else
  if (argc>0)  p = argv[0];
  else p = Utmp_File;

  fd = open(p, O_RDONLY);
  if (fd == -1) {write(1,"error open input\n",17); _exit(1);}
#endif

  while (f_getutent()) {
    if (ut->ut_type != USER_PROCESS) continue;
    if (flagcheck)
      if (kill(ut->ut_pid, 0) && errno == ESRCH) continue;

    id = ut->ut_tv.tv_sec;
    id += get_tz(id);
    tm = nv_gmtime(&id);

    mesg = '?';
    id = -1;

    S(buf, "/dev/");
    buf[5 + SN(buf+5, ut->ut_line, sizeof(ut->ut_line))] = '\0';

    if (!stat(buf, &st)) {
      if ((st.st_mode & 0020) == 0020) mesg = '+';
      else mesg = '-';
      id = (time(0) - st.st_mtime)/60;
    }

    /*----------------------------------------------*/
    p = buf;
    p += SF(p, ut->ut_user,12); 

    if (flagmesg) {
      *p++ = mesg;                 *p++ = ' ';}

    p += SF(p, ut->ut_line, 7);    *p++ = ' ';
    if (flagpid) {
      p += N(p, ut->ut_pid,  5,0); *p++ = ' ';}

    p += SN(p, montab + (tm->tm_mon)*3,  3);   *p++ = ' ';
    p += N(p, tm->tm_mday, 2,0);   *p++ = ' ';
    p += N(p, tm->tm_hour, 2,0);   *p++ = ':';
    p += N(p, tm->tm_min,  2,1);   *p++ = ' ';

    if (flagup) {
      if (id >= 24*60) {
	id /= 60;
	p += N(p, id/24, 3,0);   p += S(p,"d+"); 
	p += N(p, id%24, 2,1);   p += S(p,"h "); 
      } else if (id > 0) {
	p += N(p, id/60, 2,0);   *p++ = ':';
	p += N(p, id%60, 2,1);   *p++ = ' ';
      } else if (id == 0) {
	p += SF(p,"  .",6);
      } else
	p += SF(p,"  ?",6);
    }

    *p++ = '(';
    ipasc = p;
    p += fmt_utmp_ip(p, (char *)ut->ut_addr_v6);

    if (flagaddr>1 ||
	(flagaddr && str_diffn(ipasc, ut->ut_host, sizeof(ut->ut_host)))) {
      *p++ = '=';
      p += SN(p,ut->ut_host,90);
    }
    *p++ = ')'; *p++ = '\n';

    write(1,buf, p-buf);
  }
  f_endutent();
  return 0;
}