⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 output.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * Copyright 1999-2004 by Albert Cahalan; all rights reserved. * * This file may be used subject to the terms and conditions of the * GNU Library General Public License Version 2, or any later version * at your option, as published by the Free Software Foundation. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. *//* * This file is really gross, and I know it. I looked into several * alternate ways to deal with the mess, and they were all ugly. * * FreeBSD has a fancy hack using offsets into a struct -- that * saves code but it is _really_ gross. See the PO macro below. * * We could have a second column width for wide output format. * For example, Digital prints the real-time signals. *//* * Data table idea: * * table 1 maps aix to specifier * table 2 maps shortsort to specifier * table 3 maps macro to specifiers * table 4 maps specifier to title,datatype,offset,vendor,helptext * table 5 maps datatype to justification,width,widewidth,sorting,printing * * Here, "datatype" could be user,uid,u16,pages,deltaT,signals,tty,longtty... * It must be enough to determine printing and sorting. * * After the tables, increase width as needed to fit the header. * * Table 5 could go in a file with the output functions. */ #include <ctype.h>#include <fcntl.h>#include <grp.h>#include <limits.h>#include <pwd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/ioctl.h>#include <sys/resource.h>#include <sys/types.h>#include <sys/mman.h>#include <time.h>#include <unistd.h>#include <dlfcn.h>#include "../proc/readproc.h"#include "../proc/sysinfo.h"#include "../proc/wchan.h"#include "../proc/procps.h"#include "../proc/devname.h"#include "../proc/escape.h"#include "common.h"/* TODO: * Stop assuming system time is local time. */#define COLWID 240 /* satisfy snprintf, which is faster than sprintf */static unsigned max_rightward = 0x12345678; /* space for RIGHT stuff */static unsigned max_leftward = 0x12345678; /* space for LEFT stuff */static int wide_signals;  /* true if we have room */static unsigned long seconds_since_1970;static unsigned long time_of_boot;static unsigned long page_shift;/*************************************************************************//************ Lots of sort functions, starting with the NOP **************/static int sr_nop(const proc_t* a, const proc_t* b){  (void)a;(void)b; /* shut up gcc */  return 0;}#define CMP_STR(NAME) \static int sr_ ## NAME(const proc_t* P, const proc_t* Q) { \    return strcmp(P->NAME, Q->NAME); \}#define CMP_INT(NAME) \static int sr_ ## NAME (const proc_t* P, const proc_t* Q) { \    if (P->NAME < Q->NAME) return -1; \    if (P->NAME > Q->NAME) return  1; \    return 0; \}/* fast version, for values which either: * a. differ by no more than 0x7fffffff * b. only need to be grouped same w/ same */#define CMP_SMALL(NAME) \static int sr_ ## NAME (const proc_t* P, const proc_t* Q) { \    return (int)(P->NAME) - (int)(Q->NAME); \}CMP_INT(rtprio)CMP_SMALL(sched)CMP_INT(cutime)CMP_INT(cstime)CMP_SMALL(priority)                                             /* nice */CMP_SMALL(nlwp)CMP_SMALL(nice)                                                 /* priority */CMP_INT(rss)      /* resident set size from stat file */ /* vm_rss, resident */CMP_INT(alarm)CMP_INT(size)      /* total pages */                     /* vm_size, vsize */CMP_INT(resident)  /* resident pages */                     /* vm_rss, rss */CMP_INT(share)     /* shared pages */CMP_INT(trs)       /* executable pages */CMP_INT(lrs)       /* obsolete "library" pages above 0x60000000 */CMP_INT(drs)       /* other pages (assumed data?) */CMP_INT(dt)        /* dirty pages */CMP_INT(vm_size)    /* kB VM */                             /* size, vsize */CMP_INT(vm_lock)    /* kB locked */CMP_INT(vm_rss)     /* kB rss */                          /* rss, resident */CMP_INT(vm_data)    /* kB "data" == data-stack */CMP_INT(vm_stack)   /* kB stack */CMP_INT(vm_exe)     /* kB "exec" == exec-lib */CMP_INT(vm_lib)     /* kB "libraries" */CMP_INT(vsize)      /* pages VM */                        /* size, vm_size */CMP_INT(rss_rlim)CMP_SMALL(flags)CMP_INT(min_flt)CMP_INT(maj_flt)CMP_INT(cmin_flt)CMP_INT(cmaj_flt)CMP_INT(utime)CMP_INT(stime)    /* Old: sort by systime. New: show start time. Uh oh. */CMP_INT(start_code)CMP_INT(end_code)CMP_INT(start_stack)CMP_INT(kstk_esp)CMP_INT(kstk_eip)CMP_INT(start_time)CMP_INT(wchan)/* CMP_STR(*environ) *//* CMP_STR(*cmdline) */CMP_STR(ruser)CMP_STR(euser)CMP_STR(suser)CMP_STR(fuser)CMP_STR(rgroup)CMP_STR(egroup)CMP_STR(sgroup)CMP_STR(fgroup)CMP_STR(cmd)/* CMP_STR(ttyc) */    /* FIXME -- use strncmp with 8 max */CMP_INT(ruid)CMP_INT(rgid)CMP_INT(euid)CMP_INT(egid)CMP_INT(suid)CMP_INT(sgid)CMP_INT(fuid)CMP_INT(fgid)CMP_SMALL(tid)CMP_SMALL(tgid)CMP_SMALL(ppid)CMP_SMALL(pgrp)CMP_SMALL(session)CMP_INT(tty)CMP_SMALL(tpgid)CMP_SMALL(pcpu)CMP_SMALL(state)/* approximation to: kB of address space that could end up in swap */static int sr_swapable(const proc_t* P, const proc_t* Q) {  unsigned long p_swapable = P->vm_data + P->vm_stack;  unsigned long q_swapable = Q->vm_data + Q->vm_stack;  if (p_swapable < q_swapable) return -1;  if (p_swapable > q_swapable) return  1;  return 0;}/***************************************************************************//************ Lots of format functions, starting with the NOP **************/// so popular it can't be "static"int pr_nop(char *restrict const outbuf, const proc_t *restrict const pp){  (void)pp;  return snprintf(outbuf, COLWID, "%c", '-');}/********* Unix 98 ************//***Only comm and args are allowed to contain blank characters; all others arenot. Any implementation-dependent variables will be specified in the systemdocumentation along with the default header and indicating if the fieldmay contain blank characters.Some headers do not have a standardized specifier!%CPU	pcpu	The % of cpu time used recently, with unspecified "recently".ADDR		The address of the process.C		Processor utilisation for scheduling.CMD		The command name, or everything with -f.COMMAND	args	Command + args. May chop as desired. May use either version.COMMAND	comm	argv[0]ELAPSED	etime	Elapsed time since the process was started. [[dd-]hh:]mm:ssF		Flags (octal and additive)GROUP	group	Effective group ID, prefer text over decimal.NI	nice	Decimal system scheduling priority, see nice(1).PGID	pgid	The decimal value of the process group ID.PID	pid	Decimal PID.PPID	ppid	Decimal PID.PRI		Priority. Higher numbers mean lower priority.RGROUP	rgroup	Real group ID, prefer text over decimal.RUSER	ruser	Real user ID, prefer text over decimal.S		The state of the process.STIME		Starting time of the process.SZ		The size in blocks of the core image of the process.TIME	time	Cumulative CPU time. [dd-]hh:mm:ssTT	tty	Name of tty in format used by who(1).TTY		The controlling terminal for the process.UID		UID, or name when -fUSER	user	Effective user ID, prefer text over decimal.VSZ	vsz	Virtual memory size in decimal kB.WCHAN		Where waiting/sleeping or blank if running.The nice value is used to compute the priority.For some undefined ones, Digital does:F       flag    Process flags -- but in hex!PRI     pri     Process priorityS       state   Symbolic process statusTTY     tt,tty,tname,longtname  -- all do "ttyp1", "console", "??"UID     uid     Process user ID (effective UID)WCHAN   wchan   Address of event on which aFor some undefined ones, Sun does:ADDR	addr	memory address of the processC	c	Processor utilization  for  scheduling  (obsolete).CMDF	fS	s	state: OSRZTSTIME		start time, printed w/o blanks. If 24h old, months & daysSZ		size (in pages) of the swappable process's image in main memoryTTYUID	uidWCHAN	wchanFor some undefined ones, SCO does:ADDR	addr	Virtual address of the process' entry in the process table.SZ		swappable size in kB of the virtual data and stackSTIME	stime	hms or md time format***//* Source & destination are known. Return bytes or screen characters? */static int forest_helper(char *restrict const outbuf){  char *p = forest_prefix;  char *q = outbuf;  int rightward=max_rightward;  if(!*p) return 0;  /* Arrrgh! somebody defined unix as 1 */  if(forest_type == 'u') goto unixy;  while(*p){    switch(*p){    case ' ': strcpy(q, "    ");  break;    case 'L': strcpy(q, " \\_ "); break;    case '+': strcpy(q, " \\_ "); break;    case '|': strcpy(q, " |  ");  break;    case '\0': return q-outbuf;    /* redundant & not used */    }    if (rightward-4 < 0) {      *(q+rightward)='\0';      return max_rightward;    }    q += 4;    rightward -= 4;    p++;  }  return q-outbuf;   /* gcc likes this here */unixy:  while(*p){    switch(*p){    case ' ': strcpy(q, "  "); break;    case 'L': strcpy(q, "  "); break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -