📄 who.c
字号:
#ifndef lintstatic char *sccsid = "@(#)who.c 4.1 (ULTRIX) 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1985 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//*static char *sccsid = "@(#)who.c 4.6 (Berkeley) 8/19/83";*//* * who *//* * Modified by : Aki Hirai , Digital Equipment Corp. * 30 - May - 1985 * * /aki00/ ------- Fix the file name problem in error message. * /aki01/ ------- Fix the printing table problem when arguments * more than two. */#include <stdio.h>#include <utmp.h>#include <pwd.h>#include <ctype.h>#define NMAX sizeof(utmp.ut_name)#define LMAX sizeof(utmp.ut_line)#define HMAX sizeof(utmp.ut_host)struct utmp utmp;struct passwd *pw;struct passwd *getpwuid();char hostname[32];char *ttyname(), *rindex(), *ctime(), *strcpy();main(argc, argv) int argc; char **argv;{ register char *tp, *s; register FILE *fi; s = "/etc/utmp"; if(argc == 2) s = argv[1]; if (argc == 3) { tp = ttyname(0); if (tp) tp = rindex(tp, '/') + 1; else { /* no tty - use best guess from passwd file */ pw = getpwuid(getuid()); strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX); strcpy(utmp.ut_line, "tty??"); time(&utmp.ut_time); putline(); exit(0); } } if ((fi = fopen(s, "r")) == NULL) {/**aki00**/ printf("who: cannot open %s\n", s);/**aki01 end**/ exit(1); } while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) { if (argc == 3) { gethostname(hostname, sizeof (hostname)); if (strcmp(utmp.ut_line, tp)) continue; printf("%s!", hostname); putline(); exit(0); }/** aki01 **/ if (utmp.ut_name[0] == '\0' && argc == 1 || argc > 3)/** aki01 end **/ continue; putline(); }}putline(){ register char *cbuf; printf("%-*.*s %-*.*s", NMAX, NMAX, utmp.ut_name, LMAX, LMAX, utmp.ut_line); cbuf = ctime(&utmp.ut_time); printf("%.12s", cbuf+4); if (utmp.ut_host[0]) printf("\t(%.*s)", HMAX, utmp.ut_host); putchar('\n');}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -