Print this page
3124 Remove any existing references to utmp, use utmpx instead


 216 
 217                                 default:
 218                                         (void) fprintf(stderr, gettext(
 219                                             "%s: bad flag %s\n"),
 220                                             prog, argv[1]);
 221                                         exit(1);
 222                                 }
 223                         }
 224                 } else {
 225                         if (!isalnum(argv[1][0]) || argc > 2) {
 226                                 (void) fprintf(stderr, gettext(
 227                                     "usage: %s [ -hlsuw ] [ user ]\n"), prog);
 228                                 exit(1);
 229                         } else
 230                                 sel_user = argv[1];
 231                 }
 232                 argc--; argv++;
 233         }
 234 
 235         /*
 236          * read the UTMP_FILE (contains information about each logged in user)
 237          */
 238         if (stat(UTMPX_FILE, &sbuf) == ERR) {
 239                 (void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
 240                     prog, UTMPX_FILE, strerror(errno));
 241                 exit(1);
 242         }
 243         entries = sbuf.st_size / sizeof (struct futmpx);
 244         size = sizeof (struct utmpx) * entries;
 245         if ((ut = malloc(size)) == NULL) {
 246                 (void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
 247                     prog, UTMPX_FILE, strerror(errno));
 248                 exit(1);
 249         }
 250 
 251         (void) utmpxname(UTMPX_FILE);
 252 
 253         utmpbegin = ut;
 254         utmpend = (struct utmpx *)((char *)utmpbegin + size);
 255 
 256         setutxent();
 257         while ((ut < utmpend) && ((utp = getutxent()) != NULL))
 258                 (void) memcpy(ut++, utp, sizeof (*ut));
 259         endutxent();
 260 
 261         (void) time(&now);  /* get current time */
 262 
 263         if (header) {   /* print a header */
 264                 prtat(&now);
 265                 for (ut = utmpbegin; ut < utmpend; ut++) {
 266                         if (ut->ut_type == USER_PROCESS) {
 267                                 if (!nonuser(*ut))
 268                                         nusers++;
 269                         } else if (ut->ut_type == BOOT_TIME) {
 270                                 uptime = now - ut->ut_xtime;
 271                                 uptime += 30;
 272                                 days = uptime / (60*60*24);
 273                                 uptime %= (60*60*24);
 274                                 hrs = uptime / (60*60);
 275                                 uptime %= (60*60);
 276                                 mins = uptime / 60;
 277 
 278                                 PRINTF((gettext("up")));
 279                                 if (days > 0)
 280                                         PRINTF((gettext(
 281                                             " %d day(s),"), days));
 282                                 if (hrs > 0 && mins > 0) {
 283                                         PRINTF((" %2d:%02d,", hrs, mins));
 284                                 } else {
 285                                         if (hrs > 0)
 286                                                 PRINTF((gettext(
 287                                                     " %d hr(s),"), hrs));




 216 
 217                                 default:
 218                                         (void) fprintf(stderr, gettext(
 219                                             "%s: bad flag %s\n"),
 220                                             prog, argv[1]);
 221                                         exit(1);
 222                                 }
 223                         }
 224                 } else {
 225                         if (!isalnum(argv[1][0]) || argc > 2) {
 226                                 (void) fprintf(stderr, gettext(
 227                                     "usage: %s [ -hlsuw ] [ user ]\n"), prog);
 228                                 exit(1);
 229                         } else
 230                                 sel_user = argv[1];
 231                 }
 232                 argc--; argv++;
 233         }
 234 
 235         /*
 236          * read the UTMPX_FILE (contains information about each logged in user)
 237          */
 238         if (stat(UTMPX_FILE, &sbuf) == ERR) {
 239                 (void) fprintf(stderr, gettext("%s: stat error of %s: %s\n"),
 240                     prog, UTMPX_FILE, strerror(errno));
 241                 exit(1);
 242         }
 243         entries = sbuf.st_size / sizeof (struct futmpx);
 244         size = sizeof (struct utmpx) * entries;
 245         if ((ut = malloc(size)) == NULL) {
 246                 (void) fprintf(stderr, gettext("%s: malloc error of %s: %s\n"),
 247                     prog, UTMPX_FILE, strerror(errno));
 248                 exit(1);
 249         }
 250 
 251         (void) utmpxname(UTMPX_FILE);
 252 
 253         utmpbegin = ut;
 254         utmpend = (struct utmpx *)((char *)utmpbegin + size);
 255 
 256         setutxent();
 257         while ((ut < utmpend) && ((utp = getutxent()) != NULL))
 258                 (void) memcpy(ut++, utp, sizeof (*ut));
 259         endutxent();
 260 
 261         (void) time(&now);  /* get current time */
 262 
 263         if (header) {   /* print a header */
 264                 prtat(&now);
 265                 for (ut = utmpbegin; ut < utmpend; ut++) {
 266                         if (ut->ut_type == USER_PROCESS) {
 267                                 if (!nonuserx(*ut))
 268                                         nusers++;
 269                         } else if (ut->ut_type == BOOT_TIME) {
 270                                 uptime = now - ut->ut_xtime;
 271                                 uptime += 30;
 272                                 days = uptime / (60*60*24);
 273                                 uptime %= (60*60*24);
 274                                 hrs = uptime / (60*60);
 275                                 uptime %= (60*60);
 276                                 mins = uptime / 60;
 277 
 278                                 PRINTF((gettext("up")));
 279                                 if (days > 0)
 280                                         PRINTF((gettext(
 281                                             " %d day(s),"), days));
 282                                 if (hrs > 0 && mins > 0) {
 283                                         PRINTF((" %2d:%02d,", hrs, mins));
 284                                 } else {
 285                                         if (hrs > 0)
 286                                                 PRINTF((gettext(
 287                                                     " %d hr(s),"), hrs));