4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
41 * This is a finger program. It prints out useful information about users
42 * by digging it up from various system files.
44 * There are three output formats, all of which give login name, teletype
45 * line number, and login time. The short output format is reminiscent
46 * of finger on ITS, and gives one line of information per user containing
47 * in addition to the minimum basic requirements (MBR), the user's full name,
48 * idle time and location.
49 * The quick style output is UNIX who-like, giving only name, teletype and
50 * login time. Finally, the long style output give the same information
51 * as the short (in more legible format), the home directory and shell
52 * of the user, and, if it exits, a copy of the file .plan in the users
53 * home directory. Finger may be called with or without a list of people
54 * to finger -- if no list is given, all the people currently logged in
57 * The program is validly called by one of the following:
59 * finger {short form list of users}
60 * finger -l {long form list of users}
61 * finger -b {briefer long form list of users}
62 * finger -q {quick list of users}
63 * finger -i {quick list of users with idle times}
64 * finger -m {matches arguments against only username}
65 * finger -f {suppress header in non-long form}
66 * finger -p {suppress printing of .plan file}
67 * finger -h {suppress printing of .project file}
68 * finger -i {forces "idle" output format}
69 * finger namelist {long format list of specified users}
70 * finger -s namelist {short format list of specified users}
71 * finger -w namelist {narrow short format list of specified users}
73 * where 'namelist' is a list of users login names.
74 * The other options can all be given after one '-', or each can have its
75 * own '-'. The -f option disables the printing of headers for short and
76 * quick outputs. The -b option briefens long format outputs. The -p
77 * option turns off plans for long format outputs.
80 #include <sys/types.h>
83 #include <sys/signal.h>
90 #include <sys/socket.h>
91 #include <netinet/in.h>
94 #include <sys/select.h>
100 #include <maillock.h>
103 #include <arpa/inet.h>
106 static char gecos_ignore_c
= '*'; /* ignore this in real name */
107 static char gecos_sep_c
= ','; /* separator in pw_gecos field */
108 static char gecos_samename
= '&'; /* repeat login name in real name */
110 #define TALKABLE 0220 /* tty is writable if this mode */
112 #define NMAX sizeof (((struct utmpx *)0)->ut_name)
113 #define LMAX sizeof (((struct utmpx *)0)->ut_line)
114 #define HMAX sizeof (((struct utmpx *)0)->ut_host)
116 struct person
{ /* one for each person fingered */
117 char *name
; /* name */
118 char tty
[LMAX
+1]; /* null terminated tty line */
119 char host
[HMAX
+1]; /* null terminated remote host name */
120 char *ttyloc
; /* location of tty line, if any */
121 time_t loginat
; /* time of (last) login */
122 time_t idletime
; /* how long idle (if logged in) */
123 char *realname
; /* pointer to full name */
124 struct passwd
*pwd
; /* structure of /etc/passwd stuff */
125 char loggedin
; /* person is logged in */
126 char writable
; /* tty is writable */
127 char original
; /* this is not a duplicate entry */
128 struct person
*link
; /* link to next person */
131 char LASTLOG
[] = "/var/adm/lastlog"; /* last login info */
132 char PLAN
[] = "/.plan"; /* what plan file is */
133 char PROJ
[] = "/.project"; /* what project file */
135 int unbrief
= 1; /* -b option default */
136 int header
= 1; /* -f option default */
137 int hack
= 1; /* -h option default */
138 int idle
= 0; /* -i option default */
139 int large
= 0; /* -l option default */
140 int match
= 1; /* -m option default */
141 int plan
= 1; /* -p option default */
142 int unquick
= 1; /* -q option default */
143 int small
= 0; /* -s option default */
144 int wide
= 1; /* -w option default */
147 * RFC 1288 says that system administrators should have the option of
148 * separately allowing ASCII characters less than 32 or greater than
149 * 126. The termpass variable keeps track of this.
151 char defaultfile
[] = "/etc/default/finger";
152 char passvar
[] = "PASS=";
153 int termpass
= 0; /* default is ASCII only */
161 #define TS_LOW (1 << TERM_LOW) /* print characters less than 32 */
162 #define TS_HIGH (1 << TERM_HIGH) /* print characters greater than 126 */
166 FILE *lf
; /* LASTLOG file pointer */
167 struct person
*person1
; /* list of people */
168 size_t nperson
; /* number of people */
169 time_t tloc
; /* current time */
171 char usagestr
[] = "Usage: "
172 "finger [-bfhilmpqsw] [name1 [name2 ...] ]\n";
174 int AlreadyPrinted(uid_t uid
);
175 void AnyMail(char *name
);
176 void catfile(char *s
, mode_t mode
, int trunc_at_nl
);
177 void decode(struct person
*pers
);
179 void donames(char **argv
);
180 void findidle(struct person
*pers
);
181 void findwhen(struct person
*pers
);
184 void initscreening(void);
185 void ltimeprint(char *before
, time_t *dt
, char *after
);
186 int matchcmp(char *gname
, char *login
, char *given
);
187 int namecmp(char *name1
, char *name2
);
188 int netfinger(char *name
);
189 void personprint(struct person
*pers
);
191 struct passwd
*pwdcopy(const struct passwd
*pfrom
);
192 void quickprint(struct person
*pers
);
193 void shortprint(struct person
*pers
);
194 void stimeprint(time_t *dt
);
195 void sort_by_username(void);
199 main(int argc
, char **argv
)
203 (void) setlocale(LC_ALL
, "");
204 /* parse command line for (optional) arguments */
205 while ((c
= getopt(argc
, argv
, "bfhilmpqsw")) != EOF
)
239 (void) fprintf(stderr
, usagestr
);
245 /* find out what filtering on .plan/.project files we should do */
249 * optind == argc means no names given
254 donames(&argv
[optind
]);
278 while ((u
= getutxent()) != NULL
) {
279 if (u
->ut_name
[0] == 0 ||
281 u
->ut_type
!= USER_PROCESS
)
284 p
= person1
= malloc(sizeof (*p
));
286 p
->link
= malloc(sizeof (*p
));
289 bcopy(u
->ut_name
, name
, NMAX
);
291 bcopy(u
->ut_line
, p
->tty
, LMAX
);
293 bcopy(u
->ut_host
, p
->host
, HMAX
);
295 p
->loginat
= u
->ut_tv
.tv_sec
;
298 if (unquick
&& (pw
= getpwnam(name
))) {
299 p
->pwd
= pwdcopy(pw
);
301 p
->name
= p
->pwd
->pw_name
;
303 p
->name
= strdup(name
);
314 (void) printf("No one logged on\n");
328 * get names from command line and check to see if they're
332 for (; *argv
!= NULL
; argv
++) {
333 if (netfinger(*argv
))
336 p
= person1
= malloc(sizeof (*p
));
338 p
->link
= malloc(sizeof (*p
));
352 * if we are doing it, read /etc/passwd for the useful info
357 for (p
= person1
; p
!= NULL
; p
= p
->link
) {
358 if ((pw
= getpwnam(p
->name
)) != NULL
)
359 p
->pwd
= pwdcopy(pw
);
362 while ((pw
= getpwent()) != NULL
) {
363 for (p
= person1
; p
!= NULL
; p
= p
->link
) {
366 if (strcmp(p
->name
, pw
->pw_name
) != 0 &&
367 !matchcmp(pw
->pw_gecos
, pw
->pw_name
,
371 if (p
->pwd
== NULL
) {
372 p
->pwd
= pwdcopy(pw
);
376 * Handle multiple login names.
377 * Insert new "duplicate" entry
380 new = malloc(sizeof (*new));
381 new->pwd
= pwdcopy(pw
);
398 /* Now get login information */
400 while ((u
= getutxent()) != NULL
) {
401 if (u
->ut_name
[0] == 0 || u
->ut_type
!= USER_PROCESS
)
403 for (p
= person1
; p
!= NULL
; p
= p
->link
) {
405 if (p
->loggedin
== 2)
407 if (strncmp((p
->pwd
!= NULL
) ?
408 p
->pwd
->pw_name
: p
->name
,
409 u
->ut_name
, NMAX
) != 0)
411 if (p
->loggedin
== 0) {
412 bcopy(u
->ut_line
, p
->tty
, LMAX
);
414 bcopy(u
->ut_host
, p
->host
, HMAX
);
416 p
->loginat
= u
->ut_tv
.tv_sec
;
418 } else { /* p->loggedin == 1 */
420 new = malloc(sizeof (*new));
422 bcopy(u
->ut_line
, new->tty
, LMAX
);
424 bcopy(u
->ut_host
, new->host
, HMAX
);
426 new->loginat
= u
->ut_tv
.tv_sec
;
442 for (p
= person1
; p
!= NULL
; p
= p
->link
)
455 * print out what we got
461 (void) printf("Login "
463 "Idle When Where\n");
465 (void) printf("Login TTY Idle "
470 (void) printf("Login TTY When");
472 (void) printf(" Idle");
473 (void) putchar('\n');
476 for (p
= person1
; p
!= NULL
; p
= p
->link
) {
486 if (p
->pwd
!= NULL
&& !AlreadyPrinted(p
->pwd
->pw_uid
)) {
487 AnyMail(p
->pwd
->pw_name
);
491 s
= malloc(strlen(p
->pwd
->pw_dir
) +
494 (void) strcpy(s
, p
->pwd
->pw_dir
);
495 (void) strcat(s
, PROJ
);
496 if (stat(s
, &sbuf
) != -1 &&
497 (S_ISREG(sbuf
.st_mode
) ||
498 S_ISFIFO(sbuf
.st_mode
)) &&
499 (sbuf
.st_mode
& S_IROTH
)) {
500 (void) printf("Project: ");
501 catfile(s
, sbuf
.st_mode
, 1);
502 (void) putchar('\n');
510 s
= malloc(strlen(p
->pwd
->pw_dir
) +
513 (void) strcpy(s
, p
->pwd
->pw_dir
);
514 (void) strcat(s
, PLAN
);
515 if (stat(s
, &sbuf
) == -1 ||
516 (!S_ISREG(sbuf
.st_mode
) &&
517 !S_ISFIFO(sbuf
.st_mode
)) ||
518 ((sbuf
.st_mode
& S_IROTH
) == 0))
519 (void) printf("No Plan.\n");
521 (void) printf("Plan:\n");
522 catfile(s
, sbuf
.st_mode
, 0);
529 (void) putchar('\n');
534 * Duplicate a pwd entry.
535 * Note: Only the useful things (what the program currently uses) are copied.
538 pwdcopy(const struct passwd
*pfrom
)
542 pto
= malloc(sizeof (*pto
));
543 pto
->pw_name
= strdup(pfrom
->pw_name
);
544 pto
->pw_uid
= pfrom
->pw_uid
;
545 pto
->pw_gecos
= strdup(pfrom
->pw_gecos
);
546 pto
->pw_dir
= strdup(pfrom
->pw_dir
);
547 pto
->pw_shell
= strdup(pfrom
->pw_shell
);
552 * print out information on quick format giving just name, tty, login time
553 * and idle time if idle is set.
556 quickprint(struct person
*pers
)
558 (void) printf("%-8.8s ", pers
->name
);
559 if (pers
->loggedin
) {
562 (void) printf("%c%-12s %-16.16s",
563 pers
->writable
? ' ' : '*',
564 pers
->tty
, ctime(&pers
->loginat
));
565 ltimeprint(" ", &pers
->idletime
, "");
567 (void) printf(" %-12s %-16.16s",
568 pers
->tty
, ctime(&pers
->loginat
));
570 (void) putchar('\n');
572 (void) printf(" Not Logged In\n");
577 * print out information in short format, giving login name, full name,
578 * tty, idle time, login time, and host.
581 shortprint(struct person
*pers
)
585 if (pers
->pwd
== NULL
) {
586 (void) printf("%-15s ???\n", pers
->name
);
589 (void) printf("%-8s", pers
->pwd
->pw_name
);
591 if (pers
->realname
!= NULL
) {
592 (void) printf(" %-20.20s", pers
->realname
);
594 (void) printf(" ??? ");
598 if (pers
->loggedin
&& !pers
->writable
) {
604 (void) printf("%-11.11s ", pers
->tty
);
606 (void) printf(" "); /* 12 spaces */
608 p
= ctime(&pers
->loginat
);
609 if (pers
->loggedin
) {
610 stimeprint(&pers
->idletime
);
611 (void) printf(" %3.3s %-5.5s ", p
, p
+ 11);
612 } else if (pers
->loginat
== 0) {
613 (void) printf(" < . . . . >");
614 } else if (tloc
- pers
->loginat
>= 180 * 24 * 60 * 60) {
615 (void) printf(" <%-6.6s, %-4.4s>", p
+ 4, p
+ 20);
617 (void) printf(" <%-12.12s>", p
+ 4);
620 (void) printf(" %-20.20s", pers
->host
);
622 if (pers
->ttyloc
!= NULL
)
623 (void) printf(" %-20.20s", pers
->ttyloc
);
625 (void) putchar('\n');
630 * print out a person in long format giving all possible information.
631 * directory and shell are inhibited if unbrief is clear.
634 personprint(struct person
*pers
)
636 if (pers
->pwd
== NULL
) {
637 (void) printf("Login name: %-10s\t\t\tIn real life: ???\n",
641 (void) printf("Login name: %-10s", pers
->pwd
->pw_name
);
642 if (pers
->loggedin
&& !pers
->writable
) {
643 (void) printf(" (messages off) ");
647 if (pers
->realname
!= NULL
) {
648 (void) printf("In real life: %s", pers
->realname
);
651 (void) printf("\nDirectory: %-25s", pers
->pwd
->pw_dir
);
652 if (*pers
->pwd
->pw_shell
)
653 (void) printf("\tShell: %-s", pers
->pwd
->pw_shell
);
655 if (pers
->loggedin
) {
656 char *ep
= ctime(&pers
->loginat
);
658 (void) printf("\nOn since %15.15s on %s from %s",
659 &ep
[4], pers
->tty
, pers
->host
);
660 ltimeprint("\n", &pers
->idletime
, " Idle Time");
662 (void) printf("\nOn since %15.15s on %-12s",
664 ltimeprint("\n", &pers
->idletime
, " Idle Time");
666 } else if (pers
->loginat
== 0) {
667 (void) printf("\nNever logged in.");
668 } else if (tloc
- pers
->loginat
> 180 * 24 * 60 * 60) {
669 char *ep
= ctime(&pers
->loginat
);
670 (void) printf("\nLast login %10.10s, %4.4s on %s",
671 ep
, ep
+20, pers
->tty
);
673 (void) printf(" from %s", pers
->host
);
676 char *ep
= ctime(&pers
->loginat
);
677 (void) printf("\nLast login %16.16s on %s", ep
, pers
->tty
);
679 (void) printf(" from %s", pers
->host
);
682 (void) putchar('\n');
687 * decode the information in the gecos field of /etc/passwd
690 decode(struct person
*pers
)
695 pers
->realname
= NULL
;
696 if (pers
->pwd
== NULL
)
698 gp
= pers
->pwd
->pw_gecos
;
701 if (gecos_ignore_c
!= '\0' &&
702 *gp
== gecos_ignore_c
) {
705 while (*gp
!= '\0' &&
706 *gp
!= gecos_sep_c
) { /* name */
707 if (*gp
== gecos_samename
) {
708 lp
= pers
->pwd
->pw_name
;
710 *bp
++ = toupper(*lp
++);
711 while (*bp
++ = *lp
++)
720 if (bp
> (buffer
+ 1))
721 pers
->realname
= strdup(buffer
);
729 * find the last log in of a user by checking the LASTLOG file.
730 * the entry is indexed by the uid, so this can only be done if
731 * the uid is known (which it isn't in quick mode)
736 if ((lf
= fopen(LASTLOG
, "r")) == NULL
)
737 (void) fprintf(stderr
, "finger: %s open error\n", LASTLOG
);
741 findwhen(struct person
*pers
)
746 if (fseeko(lf
, (off_t
)pers
->pwd
->pw_uid
* (off_t
)sizeof (ll
),
748 if (fread((char *)&ll
, sizeof (ll
), 1, lf
) == 1) {
751 l_max
= min(LMAX
, sizeof (ll
.ll_line
));
752 h_max
= min(HMAX
, sizeof (ll
.ll_host
));
754 bcopy(ll
.ll_line
, pers
->tty
, l_max
);
755 pers
->tty
[l_max
] = '\0';
756 bcopy(ll
.ll_host
, pers
->host
, h_max
);
757 pers
->host
[h_max
] = '\0';
758 pers
->loginat
= ll
.ll_time
;
761 (void) fprintf(stderr
,
762 "finger: %s read error\n", LASTLOG
);
768 (void) fprintf(stderr
, "finger: %s fseeko error\n",
786 * find the idle time of a user by doing a stat on /dev/tty??,
787 * where tty?? has been gotten from UTMPX_FILE, supposedly.
790 findidle(struct person
*pers
)
792 struct stat ttystatus
;
793 struct stat inputdevstatus
;
794 #define TTYLEN (sizeof ("/dev/") - 1)
795 static char buffer
[TTYLEN
+ LMAX
+ 1] = "/dev/";
797 time_t lastinputtime
;
799 (void) strcpy(buffer
+ TTYLEN
, pers
->tty
);
800 buffer
[TTYLEN
+LMAX
] = 0;
801 if (stat(buffer
, &ttystatus
) < 0) {
802 (void) fprintf(stderr
, "finger: Can't stat %s\n", buffer
);
805 lastinputtime
= ttystatus
.st_atime
;
806 if (strcmp(pers
->tty
, "console") == 0) {
808 * On the console, the user may be running a window system; if
809 * so, their activity will show up in the last-access times of
810 * "/dev/kbd" and "/dev/mouse", so take the minimum of the idle
811 * times on those two devices and "/dev/console" and treat that
814 if (stat("/dev/kbd", &inputdevstatus
) == 0) {
815 if (lastinputtime
< inputdevstatus
.st_atime
)
816 lastinputtime
= inputdevstatus
.st_atime
;
818 if (stat("/dev/mouse", &inputdevstatus
) == 0) {
819 if (lastinputtime
< inputdevstatus
.st_atime
)
820 lastinputtime
= inputdevstatus
.st_atime
;
824 if (t
< lastinputtime
)
825 pers
->idletime
= (time_t)0;
827 pers
->idletime
= t
- lastinputtime
;
828 pers
->writable
= (ttystatus
.st_mode
& TALKABLE
) == TALKABLE
;
832 * print idle time in short format; this program always prints 4 characters;
833 * if the idle time is zero, it prints 4 blanks.
836 stimeprint(time_t *dt
)
841 if (delta
->tm_yday
== 0)
842 if (delta
->tm_hour
== 0)
843 if (delta
->tm_min
== 0)
846 (void) printf(" %2d", delta
->tm_min
);
848 if (delta
->tm_hour
>= 10)
849 (void) printf("%3d:", delta
->tm_hour
);
851 (void) printf("%1d:%02d",
852 delta
->tm_hour
, delta
->tm_min
);
854 (void) printf("%3dd", delta
->tm_yday
);
858 * print idle time in long format with care being taken not to pluralize
859 * 1 minutes or 1 hours or 1 days.
860 * print "prefix" first.
863 ltimeprint(char *before
, time_t *dt
, char *after
)
868 if (delta
->tm_yday
== 0 && delta
->tm_hour
== 0 && delta
->tm_min
== 0 &&
871 (void) printf("%s", before
);
872 if (delta
->tm_yday
>= 10)
873 (void) printf("%d days", delta
->tm_yday
);
874 else if (delta
->tm_yday
> 0)
875 (void) printf("%d day%s %d hour%s",
876 delta
->tm_yday
, delta
->tm_yday
== 1 ? "" : "s",
877 delta
->tm_hour
, delta
->tm_hour
== 1 ? "" : "s");
879 if (delta
->tm_hour
>= 10)
880 (void) printf("%d hours", delta
->tm_hour
);
881 else if (delta
->tm_hour
> 0)
882 (void) printf("%d hour%s %d minute%s",
883 delta
->tm_hour
, delta
->tm_hour
== 1 ? "" : "s",
884 delta
->tm_min
, delta
->tm_min
== 1 ? "" : "s");
886 if (delta
->tm_min
>= 10)
887 (void) printf("%2d minutes", delta
->tm_min
);
888 else if (delta
->tm_min
== 0)
889 (void) printf("%2d seconds", delta
->tm_sec
);
891 (void) printf("%d minute%s %d second%s",
893 delta
->tm_min
== 1 ? "" : "s",
895 delta
->tm_sec
== 1 ? "" : "s");
896 (void) printf("%s", after
);
900 * The grammar of the pw_gecos field is sufficiently complex that the
901 * best way to parse it is by using an explicit finite-state machine,
902 * in which a table defines the rules of interpretation.
904 * Some special rules are necessary to handle the fact that names
905 * may contain certain punctuation characters. At this writing,
906 * the possible punctuation characters are '.', '-', and '_'.
908 * Other rules are needed to account for characters that require special
909 * processing when they appear in the pw_gecos field. At present, there
910 * are three such characters, with these default values and effects:
912 * gecos_ignore_c '*' This character is ignored.
913 * gecos_sep_c ',' Delimits displayed and nondisplayed contents.
914 * gecos_samename '&' Copies the login name into the output.
916 * As the program examines each successive character in the returned
917 * pw_gecos value, it fetches (from the table) the FSM rule applicable
918 * for that character in the current machine state, and thus determines
921 * The possible states are:
925 * S3 copy login name into output
926 * S4 end of GECOS field
928 * Here follows a depiction of the state transitions.
931 * gecos_ignore_c OR isspace OR any other character
936 * NULL OR | S0 | isalpha OR isdigit
937 * +---------------|start|------------------------+
938 * | gecos_sep_c +-----+ | isalpha OR isdigit
939 * | | | | +---------------------+
940 * | | | | | OR '.' '-' '_' |
942 * | | +-------+ V V |
943 * | | | +-----------+ |
945 * | | | | in a word | | isalpha OR |
946 * | | | +-----------+ | isdigit OR |
947 * | | | | | | | | '.' '-' '_' |
948 * | | +----- ---------------+ | | +-----+ |
950 * | | | | gecos_ignore_c | | |
951 * | | | | isspace | | |
952 * | | | | ispunct/other | | |
953 * | | | | any other char | | |
954 * | | | | +---------------+ | |
955 * | | | | | |NULL OR gecos_sep_c |
956 * | | | | | +------------------+ |
957 * | gecos_samename| | V V | |
958 * | +-------------+ | +---------------+ | |
959 * | | | | S2 | isspace OR '.' '-' '_' | |
960 * | | gecos_samename | | not in a word |<---------------------+ | |
961 * | | +---------------+ +---------------+ OR gecos_ignore_c | | |
962 * | | | | ^ | | OR ispunct OR other | | |
963 * | | | | | | | | | |
964 * | | | gecos_samename | | | +-----------------------+ | |
965 * | | | +---------------------+ | | | |
967 * | | | | gecos_ignore_c| | NULL OR gecos_sep_c | |
968 * | | | | gecos_samename| +-----------------------+ | |
969 * | | | | ispunct/other | | | |
970 * | V V V isspace | | | |
971 * | +-----------------+ any other char| | | |
972 * | | S3 |---------------+ isalpha OR isdigit OR | | |
973 * | |insert login name|------------------------------------------ ----- ---+
974 * | +-----------------+ '.' '-' '_' | |
975 * | | NULL OR gecos_sep_c | |
976 * | +------------------------------------------+ | |
980 * | NULL OR gecos_sep_c | S4 |
981 * +-------------------------------------------------------->|end of gecos|<--+
987 * The transitions from the above diagram are summarized in
988 * the following table of target states, which is implemented
989 * in code as the gecos_fsm array.
994 * | | +--gecos_samename
997 * | | | | | +--isspace
998 * | | | | | | +--punctuation possible in name
999 * | | | | | | | +--other punctuation
1000 * | | | | | | | | +--NULL character
1001 * | | | | | | | | | +--any other character
1002 * | | | | | | | | | |
1003 * V V V V V V V V V V
1004 * From: ---------------------------------------------------
1005 * S0 | S0 | S4 | S3 | S1 | S1 | S0 | S1 | S2 | S4 | S0 |
1006 * S1 | S2 | S4 | S3 | S1 | S1 | S2 | S1 | S2 | S4 | S2 |
1007 * S2 | S2 | S4 | S3 | S1 | S1 | S2 | S2 | S2 | S4 | S2 |
1008 * S3 | S2 | S4 | S2 | S1 | S1 | S2 | S1 | S2 | S4 | S2 |
1009 * S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 |
1014 * Data types and structures for scanning the pw_gecos field.
1016 typedef enum gecos_state
{
1019 S2
, /* not in a word */
1020 S3
, /* copy login */
1021 S4
/* end of gecos */
1025 #define GFSM_COLS 10
1027 gecos_state_t gecos_fsm
[GFSM_ROWS
][GFSM_COLS
] = {
1028 {S0
, S4
, S3
, S1
, S1
, S0
, S1
, S2
, S4
, S0
}, /* S0 */
1029 {S2
, S4
, S3
, S1
, S1
, S2
, S1
, S2
, S4
, S2
}, /* S1 */
1030 {S2
, S4
, S3
, S1
, S1
, S2
, S2
, S2
, S4
, S2
}, /* S2 */
1031 {S2
, S4
, S2
, S1
, S1
, S2
, S1
, S2
, S4
, S2
}, /* S3 */
1032 {S4
, S4
, S4
, S4
, S4
, S4
, S4
, S4
, S4
, S4
} /* S4 */
1036 * Scan the pw_gecos field according to defined state table;
1037 * return the next state according the the rules.
1040 gecos_scan_state(gecos_state_t instate
, char ch
)
1042 if (ch
== gecos_ignore_c
) {
1043 return (gecos_fsm
[instate
][0]);
1044 } else if (ch
== gecos_sep_c
) {
1045 return (gecos_fsm
[instate
][1]);
1046 } else if (ch
== gecos_samename
) {
1047 return (gecos_fsm
[instate
][2]);
1048 } else if (isalpha(ch
)) {
1049 return (gecos_fsm
[instate
][3]);
1050 } else if (isdigit(ch
)) {
1051 return (gecos_fsm
[instate
][4]);
1052 } else if (isspace(ch
)) {
1053 return (gecos_fsm
[instate
][5]);
1054 } else if (ch
== '.' || ch
== '-' || ch
== '_') {
1055 return (gecos_fsm
[instate
][6]);
1056 } else if (ispunct(ch
)) {
1057 return (gecos_fsm
[instate
][7]);
1058 } else if (ch
== '\0') {
1059 return (gecos_fsm
[instate
][8]);
1061 return (gecos_fsm
[instate
][9]);
1066 * Compare the given argument, which is taken to be a username, with
1067 * the login name and with strings in the the pw_gecos field.
1070 matchcmp(char *gname
, char *login
, char *given
)
1075 gecos_state_t kstate
= S0
;
1076 gecos_state_t kstate_next
= S0
;
1078 if (*gname
== '\0' && *given
== '\0')
1085 kstate_next
= gecos_scan_state(kstate
, *gp
);
1087 switch (kstate_next
) {
1093 if (bp
< buffer
+ sizeof (buffer
)) {
1098 if (kstate
== S1
|| kstate
== S3
) {
1107 } while (*bp
!= '\0' && bp
< buffer
+ sizeof (buffer
));
1117 kstate
= kstate_next
;
1119 } while ((bp
< buffer
+ sizeof (buffer
)) && kstate
!= S4
);
1121 gp
= strtok(buffer
, " ");
1123 while (gp
!= NULL
) {
1124 if (namecmp(gp
, given
) > 0) {
1127 gp
= strtok(NULL
, " ");
1133 * Perform the character-by-character comparison.
1134 * It is intended that "finger foo" should match "foo2", but an argument
1135 * consisting entirely of digits should not be matched too broadly.
1136 * Also, we do not want "finger foo123" to match "Mr. Foo" in the gecos.
1139 namecmp(char *name1
, char *name2
)
1142 boolean_t alphaseen
= B_FALSE
;
1143 boolean_t digitseen
= B_FALSE
;
1164 for (name2
--; isdigit(*name2
); name2
++)
1166 if (*name2
== '\0' && digitseen
) {
1170 for (name1
--; isdigit(*name1
); name1
++)
1172 if (*name1
== '\0' && alphaseen
) {
1181 netfinger(char *name
)
1185 struct sockaddr_in6 sin6
;
1186 struct in6_addr ipv6addr
;
1187 struct in_addr ipv4addr
;
1192 char abuf
[INET6_ADDRSTRLEN
];
1197 host
= strrchr(name
, '@');
1202 if ((hp
= getipnodebyname(host
, AF_INET6
, AI_ALL
| AI_ADDRCONFIG
|
1203 AI_V4MAPPED
, &error_num
)) == NULL
) {
1204 if (error_num
== TRY_AGAIN
) {
1205 (void) fprintf(stderr
,
1206 "unknown host: %s (try again later)\n", host
);
1208 (void) fprintf(stderr
, "unknown host: %s\n", host
);
1214 * If hp->h_name is a IPv4-mapped IPv6 literal, we'll convert it to
1215 * IPv4 literal address.
1217 if ((inet_pton(AF_INET6
, hp
->h_name
, &ipv6addr
) > 0) &&
1218 IN6_IS_ADDR_V4MAPPED(&ipv6addr
)) {
1219 IN6_V4MAPPED_TO_INADDR(&ipv6addr
, &ipv4addr
);
1220 (void) printf("[%s] ", inet_ntop(AF_INET
, &ipv4addr
, abuf
,
1223 (void) printf("[%s] ", hp
->h_name
);
1225 bzero(&sin6
, sizeof (sin6
));
1226 sin6
.sin6_family
= hp
->h_addrtype
;
1227 bcopy(hp
->h_addr_list
[0], (char *)&sin6
.sin6_addr
, hp
->h_length
);
1228 sin6
.sin6_port
= htons(IPPORT_FINGER
);
1229 s
= socket(sin6
.sin6_family
, SOCK_STREAM
, 0);
1231 (void) fflush(stdout
);
1236 while (connect(s
, (struct sockaddr
*)&sin6
, sizeof (sin6
)) < 0) {
1238 if (hp
&& hp
->h_addr_list
[1]) {
1241 bcopy(hp
->h_addr_list
[0],
1242 (caddr_t
)&sin6
.sin6_addr
, hp
->h_length
);
1244 s
= socket(sin6
.sin6_family
, SOCK_STREAM
, 0);
1246 (void) fflush(stdout
);
1254 (void) fflush(stdout
);
1263 (void) printf("\n");
1265 (void) write(s
, "/W ", 3);
1266 (void) write(s
, name
, strlen(name
));
1267 (void) write(s
, "\r\n", 2);
1271 while ((c
= getc(f
)) != EOF
) {
1272 /* map CRLF -> newline */
1273 if ((lastc
== '\r') && (c
!= '\n'))
1274 /* print out saved CR */
1275 (void) putchar('\r');
1283 (void) putchar('\n');
1289 * AnyMail - takes a username (string pointer thereto), and
1290 * prints on standard output whether there is any unread mail,
1291 * and if so, how old it is. (JCM@Shasta 15 March 80)
1296 struct stat buf
; /* space for file status buffer */
1297 char *mbxdir
= MAILDIR
; /* string with path preamble */
1298 char *mbxpath
; /* space for entire pathname */
1302 mbxpath
= malloc(strlen(name
) + strlen(MAILDIR
) + 1);
1303 if (mbxpath
== NULL
)
1306 (void) strcpy(mbxpath
, mbxdir
); /* copy preamble into path name */
1307 (void) strcat(mbxpath
, name
); /* concatenate user name to path */
1309 if (stat(mbxpath
, &buf
) == -1 || buf
.st_size
== 0) {
1310 /* Mailbox is empty or nonexistent */
1311 (void) printf("No unread mail\n");
1313 if (buf
.st_mtime
< buf
.st_atime
) {
1315 * No new mail since the last time the user read it.
1317 (void) printf("Mail last read ");
1318 (void) printf("%s", ctime(&buf
.st_atime
));
1319 } else if (buf
.st_mtime
> buf
.st_atime
) {
1321 * New mail has definitely arrived since the last time
1322 * mail was read. mtime is the time the most recent
1323 * message arrived; atime is either the time the oldest
1324 * unread message arrived, or the last time the mail
1327 (void) printf("New mail received ");
1328 timestr
= ctime(&buf
.st_mtime
); /* time last modified */
1329 timestr
[24] = '\0'; /* suppress newline (ugh) */
1330 (void) printf("%s", timestr
);
1331 (void) printf(";\n unread since ");
1332 (void) printf("%s", ctime(&buf
.st_atime
));
1335 * There is something in mailbox, but we can't really
1336 * be sure whether it is mail held there by the user
1337 * or a (single) new message that was placed in a newly
1338 * recreated mailbox, so punt and call it "unread mail."
1340 (void) printf("Unread mail since ");
1341 (void) printf("%s", ctime(&buf
.st_mtime
));
1348 * return true iff we've already printed project/plan for this uid;
1349 * if not, enter this uid into table (so this function has a side-effect.)
1351 #define PPMAX 4096 /* assume no more than 4096 logged-in users */
1352 uid_t PlanPrinted
[PPMAX
+1];
1353 int PPIndex
= 0; /* index of next unused table entry */
1356 AlreadyPrinted(uid_t uid
)
1360 while (i
++ < PPIndex
) {
1361 if (PlanPrinted
[i
] == uid
)
1365 PlanPrinted
[i
] = uid
;
1371 #define FIFOREADTIMEOUT (60) /* read timeout on select */
1373 #define PRINT_CHAR(c) \
1375 ((termpass & TS_HIGH) && ((int)c) > 126) \
1377 (isascii((int)c) && \
1378 (isprint((int)c) || isspace((int)c)) \
1381 ((termpass & TS_LOW) && ((int)c) < 32) \
1387 catfile(char *s
, mode_t mode
, int trunc_at_nl
)
1389 if (S_ISFIFO(mode
)) {
1392 fd
= open(s
, O_RDONLY
| O_NONBLOCK
);
1394 fd_set readfds
, exceptfds
;
1398 FD_ZERO(&exceptfds
);
1399 FD_SET(fd
, &readfds
);
1400 FD_SET(fd
, &exceptfds
);
1403 tv
.tv_sec
= FIFOREADTIMEOUT
;
1405 (void) fflush(stdout
);
1406 while (select(fd
+ 1, &readfds
, (fd_set
*) 0,
1407 &exceptfds
, &tv
) != -1) {
1408 unsigned char buf
[BUFSIZ
];
1411 nread
= read(fd
, buf
, sizeof (buf
));
1415 FD_SET(fd
, &readfds
);
1416 FD_SET(fd
, &exceptfds
);
1417 for (p
= buf
; p
< buf
+ nread
; p
++) {
1418 if (trunc_at_nl
&& *p
== '\n')
1421 (void) putchar((int)*p
);
1422 else if (isascii(*p
))
1423 (void) fputs(unctrl(*p
),
1438 while ((c
= getc(fp
)) != EOF
) {
1439 if (trunc_at_nl
&& c
== '\n')
1442 (void) putchar((int)c
);
1445 (void) fputs(unctrl(c
), stdout
);
1456 char *options
, *value
;
1458 if (defopen(defaultfile
) == 0) {
1465 flags
= defcntl(DC_GETFLAGS
, 0);
1466 TURNOFF(flags
, DC_CASE
);
1467 (void) defcntl(DC_SETFLAGS
, flags
);
1469 if (cp
= defread(passvar
)) {
1471 while (*options
!= '\0')
1472 switch (getsubopt(&options
, termopts
, &value
)) {
1477 termpass
|= TS_HIGH
;
1481 (void) defopen(NULL
); /* close default file */
1486 person_compare(const void *p1
, const void *p2
)
1488 const struct person
*pp1
= *(struct person
**)p1
;
1489 const struct person
*pp2
= *(struct person
**)p2
;
1495 r
= strcmp(pp1
->name
, pp2
->name
);
1501 * If usernames are the same, sort by idle time.
1503 r
= pp1
->idletime
- pp2
->idletime
;
1511 struct person
**sortable
, *loop
;
1514 sortable
= malloc(sizeof (sortable
[0]) * nperson
);
1516 if (sortable
== NULL
)
1519 for (i
= 0, loop
= person1
; i
< nperson
; i
++) {
1520 struct person
*next
= loop
->link
;
1528 qsort(sortable
, nperson
, sizeof (sortable
[0]), person_compare
);
1530 for (i
= 1; i
< nperson
; i
++)
1531 sortable
[i
-1]->link
= sortable
[i
];
1532 person1
= sortable
[0];