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

📄 output.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 5 页
字号:
 * * So this isn't broken, but could be renamed to u98_std_stime, * as long as it still shows as STIME when using the -f option. */static int pr_stime(char *restrict const outbuf, const proc_t *restrict const pp){  struct tm *proc_time;  struct tm *our_time;  time_t t;  const char *fmt;  int tm_year;  int tm_yday;  our_time = localtime(&seconds_since_1970);   /* not reentrant */  tm_year = our_time->tm_year;  tm_yday = our_time->tm_yday;  t = time_of_boot + pp->start_time / Hertz;  proc_time = localtime(&t); /* not reentrant, this corrupts our_time */  fmt = "%H:%M";                                   /* 03:02 23:59 */  if(tm_yday != proc_time->tm_yday) fmt = "%b%d";  /* Jun06 Aug27 */  if(tm_year != proc_time->tm_year) fmt = "%Y";    /* 1991 2001 */  return strftime(outbuf, 42, fmt, proc_time);}static int pr_start(char *restrict const outbuf, const proc_t *restrict const pp){  time_t t;  char *str;  t = time_of_boot + pp->start_time / Hertz;  str = ctime(&t);  if(str[8]==' ')  str[8]='0';  if(str[11]==' ') str[11]='0';  if((unsigned long)t+60*60*24 > seconds_since_1970)    return snprintf(outbuf, COLWID, "%8.8s", str+11);  return snprintf(outbuf, COLWID, "  %6.6s", str+4);}#ifdef SIGNAL_STRINGstatic int help_pr_sig(char *restrict const outbuf, const char *restrict const sig){  long len = 0;  len = strlen(sig);  if(wide_signals){    if(len>8) return snprintf(outbuf, COLWID, "%s", sig);    return snprintf(outbuf, COLWID, "00000000%s", sig);  }  if(len-strspn(sig,"0") > 8)    return snprintf(outbuf, COLWID, "<%s", sig+len-8);  return snprintf(outbuf, COLWID,  "%s", sig+len-8);}#elsestatic int help_pr_sig(unsigned long long sig){  if(wide_signals) return snprintf(outbuf, COLWID, "%016Lx", sig);  if(sig>>32)      return snprintf(outbuf, COLWID, "<%08Lx", sig&0xffffffffLL);  return                  snprintf(outbuf, COLWID,  "%08Lx", sig&0xffffffffLL);}#endifstatic int pr_sig(char *restrict const outbuf, const proc_t *restrict const pp){  return help_pr_sig(outbuf, pp->signal);}static int pr_sigmask(char *restrict const outbuf, const proc_t *restrict const pp){  return help_pr_sig(outbuf, pp->blocked);}static int pr_sigignore(char *restrict const outbuf, const proc_t *restrict const pp){  return help_pr_sig(outbuf, pp->sigignore);}static int pr_sigcatch(char *restrict const outbuf, const proc_t *restrict const pp){  return help_pr_sig(outbuf, pp->sigcatch);}/////////////////////////////////////////////////////////////////////////////////* * internal terms:  ruid  euid  suid  fuid * kernel vars:      uid  euid  suid fsuid * command args:    ruid   uid svuid   n/a */static int pr_egid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->egid);}static int pr_rgid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->rgid);}static int pr_sgid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->sgid);}static int pr_fgid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->fgid);}static int pr_euid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->euid);}static int pr_ruid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->ruid);}static int pr_suid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->suid);}static int pr_fuid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->fuid);}// The Open Group Base Specifications Issue 6 (IEEE Std 1003.1, 2004 Edition)// requires that user and group names print as decimal numbers if there is// not enough room in the column, so tough luck if you don't like it.//// The UNIX and POSIX way to change column width is to rename it://      ps -o pid,user=CumbersomeUserNames -o comm// The easy way is to directly specify the desired width://      ps -o pid,user:19,comm//static int do_pr_name(char *restrict const outbuf, const char *restrict const name, unsigned u){  if(!user_is_number){    int rightward = OUTBUF_SIZE;	/* max cells */    int len;				/* real cells */        escape_str(outbuf, name, OUTBUF_SIZE, &rightward);    len = OUTBUF_SIZE-rightward;        if(len <= (int)max_rightward)      return len;  /* returns number of cells */  }  return snprintf(outbuf, COLWID, "%u", u);}static int pr_ruser(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->ruser, pp->ruid);}static int pr_euser(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->euser, pp->euid);}static int pr_fuser(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->fuser, pp->fuid);}static int pr_suser(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->suser, pp->suid);}static int pr_egroup(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->egroup, pp->egid);}static int pr_rgroup(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->rgroup, pp->rgid);}static int pr_fgroup(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->fgroup, pp->fgid);}static int pr_sgroup(char *restrict const outbuf, const proc_t *restrict const pp){  return do_pr_name(outbuf, pp->sgroup, pp->sgid);}//////////////////////////////////////////////////////////////////////////////////// TID tid LWP lwp SPID spidstatic int pr_thread(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%u", pp->tid);}// thcount THCNTstatic int pr_nlwp(char *restrict const outbuf, const proc_t *restrict const pp){    return snprintf(outbuf, COLWID, "%d", pp->nlwp);}static int pr_sess(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%u", pp->session);}static int pr_tpgid(char *restrict const outbuf, const proc_t *restrict const pp){  return snprintf(outbuf, COLWID, "%d", pp->tpgid);}/* SGI uses "cpu" to print the processor ID with header "P" */static int pr_sgi_p(char *restrict const outbuf, const proc_t *restrict const pp){          /* FIXME */  if(pp->state == 'R') return snprintf(outbuf, COLWID, "%d", pp->processor);  return snprintf(outbuf, COLWID, "*");}/****************** FLASK & seLinux security stuff **********************/// move the bulk of this to libproc sometimestatic int pr_context(char *restrict const outbuf, const proc_t *restrict const pp){  char filename[48];  size_t len;  ssize_t num_read;  int fd;// wchan file is suitable for testing//snprintf(filename, sizeof filename, "/proc/%d/wchan", pp->tgid);snprintf(filename, sizeof filename, "/proc/%d/attr/current", pp->tgid);  fd = open(filename, O_RDONLY, 0);  if(likely(fd==-1)) goto fail;  num_read = read(fd, outbuf, 666);  close(fd);  if(unlikely(num_read<=0)) goto fail;  outbuf[num_read] = '\0';  len = 0;  while(outbuf[len]>' ' && outbuf[len]<='~') len++;  outbuf[len] = '\0';  if(len) return len;fail:  outbuf[0] = '-';  outbuf[1] = '\0';  return 1;}#if 0// This needs more study, considering:// 1. the static linking option (maybe disable this in that case)// 2. the -z and -Z option issue// 3. width of outputstatic int pr_context(char *restrict const outbuf, const proc_t *restrict const pp){  static int (*ps_getpidcon)(pid_t pid, char **context) = 0;  static int tried_load = 0;  size_t len;  char *context;  if(!ps_getpidcon && !tried_load){    void *handle = dlopen("libselinux.so.1", RTLD_NOW);    if(handle){      dlerror();      ps_getpidcon = dlsym(handle, "getpidcon");      if(dlerror())        ps_getpidcon = 0;    }    tried_load++;  }  if(ps_getpidcon && !ps_getpidcon(pp->tgid, &context)){    size_t max_len = OUTBUF_SIZE-1;    len = strlen(context);    if(len > max_len) len = max_len;    memcpy(outbuf, context, len);    outbuf[len] = '\0';    free(context);  }else{    outbuf[0] = '-';    outbuf[1] = '\0';    len = 1;  }  return len;}#endif////////////////////////////// Test code /////////////////////////////////// like "args"static int pr_t_unlimited(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"[123456789-12345] <defunct>","ps","123456789-123456"};  (void)pp;  snprintf(outbuf, max_rightward+1, "%s", vals[lines_to_next_header%3u]);  return strlen(outbuf);}static int pr_t_unlimited2(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"unlimited", "[123456789-12345] <defunct>","ps","123456789-123456"};  (void)pp;  snprintf(outbuf, max_rightward+1, "%s", vals[lines_to_next_header%4u]);  return strlen(outbuf);}// like "etime"static int pr_t_right(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"999-23:59:59","99-23:59:59","9-23:59:59","59:59"};  (void)pp;  return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%4u]);}static int pr_t_right2(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"999-23:59:59","99-23:59:59","9-23:59:59"};  (void)pp;  return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%3u]);}// like "tty"static int pr_t_left(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"tty7","pts/9999","iseries/vtty42","ttySMX0","3270/tty4"};  (void)pp;  return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%5u]);}static int pr_t_left2(char *restrict const outbuf, const proc_t *restrict const pp){  static const char *const vals[] = {"tty7","pts/9999","ttySMX0","3270/tty4"};  (void)pp;  return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%4u]);}/***************************************************************************//*************************** other stuff ***********************************//* * Old header specifications. * * short   Up  "  PID TTY STAT  TIME COMMAND" * long  l Pp  " FLAGS   UID   PID  PPID PRI  NI   SIZE   RSS WCHAN       STA TTY TIME COMMAND * user  u up  "USER       PID %CPU %MEM  SIZE   RSS TTY STAT START   TIME COMMAND * jobs  j gPp " PPID   PID  PGID   SID TTY TPGID  STAT   UID   TIME COMMAND * sig   s p   "  UID   PID SIGNAL   BLOCKED  IGNORED  CATCHED  STAT TTY   TIME COMMAND * vm    v r   "  PID TTY STAT  TIME  PAGEIN TSIZ DSIZ  RSS   LIM %MEM COMMAND * m     m r   "  PID TTY MAJFLT MINFLT   TRS   DRS  SIZE  SWAP   RSS  SHRD   LIB  DT COMMAND * regs  X p   "NR   PID    STACK      ESP      EIP TMOUT ALARM STAT TTY   TIME COMMAND *//* * Unix98 requires that the heading for tty is TT, though XPG4, Digital,

⌨️ 快捷键说明

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