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

📄 display.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 1998-2003 by Albert Cahalan; all rights resered. * 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. */                                                                     #include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#if (__GNU_LIBRARY__ >= 6)# include <locale.h>#endif/* username lookups */#include <sys/types.h>#include <pwd.h>#include <grp.h>/* major/minor number */#include <sys/sysmacros.h>#include <signal.h>   /* catch signals */#include "common.h"#include "../proc/wchan.h"#include "../proc/version.h"#include "../proc/readproc.h"#include "../proc/sysinfo.h"#include "../proc/sig.h"#ifndef SIGCHLD#define SIGCHLD SIGCLD#endif/* just reports a crash */static void signal_handler(int signo){  if(signo==SIGPIPE) _exit(0);  /* "ps | head" will cause this */  /* fprintf() is not reentrant, but we _exit() anyway */  fprintf(stderr,    "\n\n"    "Signal %d (%s) caught by ps (%s).\n"    "Please send bug reports to <feedback@lists.sf.net> or <albert@users.sf.net>\n",    signo,    signal_number_to_name(signo),    procps_version  );  _exit(signo+128);}/////////////////////////////////////////////////////////////////////////////////////#undef DEBUG#ifdef DEBUGvoid init_stack_trace(char *prog_name);#include <ctype.h>void hex_dump(void *vp){  char *charlist;  int i = 0;  int line = 45;  char *cp = (char *)vp;  while(line--){      printf("%8lx  ", (unsigned long)cp);      charlist = cp;      cp += 16;      for(i=0; i<16; i++){        if((charlist[i]>31) && (charlist[i]<127)){          printf("%c", charlist[i]);        }else{          printf(".");        }      }      printf(" ");      for(i=0; i<16; i++) printf(" %2x",(unsigned int)((unsigned char)(charlist[i])));      printf("\n");      i=0;  }}static void show_tgid(char *s, int n, sel_union *data){  printf("%s  ", s);  while(--n){    printf("%d,", data[n].tgid);  }  printf("%d\n", data[0].tgid);}static void show_uid(char *s, int n, sel_union *data){  struct passwd *pw_data;  printf("%s  ", s);  while(--n){    pw_data = getpwuid(data[n].uid);    if(pw_data) printf("%s,", pw_data->pw_name);    else        printf("%d,", data[n].uid);  }  pw_data = getpwuid(data[n].uid);  if(pw_data) printf("%s\n", pw_data->pw_name);  else        printf("%d\n", data[n].uid);}static void show_gid(char *s, int n, sel_union *data){  struct group *gr_data;  printf("%s  ", s);  while(--n){    gr_data = getgrgid(data[n].gid);    if(gr_data) printf("%s,", gr_data->gr_name);    else        printf("%d,", data[n].gid);  }  gr_data = getgrgid(data[n].gid);  if(gr_data) printf("%s\n", gr_data->gr_name);  else        printf("%d\n", data[n].gid);}static void show_tty(char *s, int n, sel_union *data){  printf("%s  ", s);  while(--n){    printf("%d:%d,", (int)major(data[n].tty), (int)minor(data[n].tty));  }  printf("%d:%d\n", (int)major(data[n].tty), (int)minor(data[n].tty));}static void show_cmd(char *s, int n, sel_union *data){  printf("%s  ", s);  while(--n){    printf("%.8s,", data[n].cmd);  }  printf("%.8s\n", data[0].cmd);}static void arg_show(void){  selection_node *walk = selection_list;  while(walk){    switch(walk->typecode){    case SEL_RUID: show_uid("RUID", walk->n, walk->u); break;    case SEL_EUID: show_uid("EUID", walk->n, walk->u); break;    case SEL_SUID: show_uid("SUID", walk->n, walk->u); break;    case SEL_FUID: show_uid("FUID", walk->n, walk->u); break;    case SEL_RGID: show_gid("RGID", walk->n, walk->u); break;    case SEL_EGID: show_gid("EGID", walk->n, walk->u); break;    case SEL_SGID: show_gid("SGID", walk->n, walk->u); break;    case SEL_FGID: show_gid("FGID", walk->n, walk->u); break;    case SEL_PGRP: show_pid("PGRP", walk->n, walk->u); break;    case SEL_PID : show_pid("PID ", walk->n, walk->u); break;    case SEL_PPID: show_pid("PPID", walk->n, walk->u); break;    case SEL_TTY : show_tty("TTY ", walk->n, walk->u); break;    case SEL_SESS: show_pid("SESS", walk->n, walk->u); break;    case SEL_COMM: show_cmd("COMM", walk->n, walk->u); break;    default: printf("Garbage typecode value!\n");    }    walk = walk->next;  }}#endif///////////////////////////////////////////////////////////////////////////***** check the header *//* Unix98: must not print empty header */static void check_headers(void){  format_node *walk = format_list;  int head_normal = 0;  if(header_type==HEAD_MULTI){    header_gap = screen_rows-1;  /* true BSD */    return;  }  if(header_type==HEAD_NONE){    lines_to_next_header = -1;  /* old Linux */    return;  }  while(walk){    if(!*(walk->name)){      walk = walk->next;      continue;    }    if(walk->pr){      head_normal++;      walk = walk->next;      continue;    }    walk = walk->next;  }  if(!head_normal) lines_to_next_header = -1; /* how UNIX does --noheader */}/***** check sort needs *//* see what files need to be read, etc. */static unsigned check_sort_needs(sort_node *walk){  unsigned needs = 0;  while(walk){    needs |= walk->need;    walk = walk->next;  }  return needs;}/***** check needs *//* see what files need to be read, etc. */static unsigned collect_format_needs(format_node *walk){  unsigned needs = 0;  while(walk){    needs |= walk->need;    walk = walk->next;  }  return needs;}static format_node *proc_format_list;static format_node *task_format_list;static unsigned needs_for_threads;static unsigned needs_for_sort;static unsigned proc_format_needs;static unsigned task_format_needs;#define needs_for_format (proc_format_needs|task_format_needs)#define PROC_ONLY_FLAGS (PROC_FILLENV|PROC_FILLARG|PROC_FILLCOM|PROC_FILLMEM)/***** munge lists and determine openproc() flags */static void lists_and_needs(void){  check_headers();  // only care about the difference when showing both  if(thread_flags & TF_show_both){    format_node pfn, tfn; // junk, to handle special case at begin of list    format_node *walk = format_list;    format_node *p_end = &pfn;    format_node *t_end = &tfn;    while(walk){      format_node *new = malloc(sizeof(format_node));      memcpy(new,walk,sizeof(format_node));      p_end->next = walk;      t_end->next = new;      p_end       = walk;      t_end       = new;      switch(walk->flags & CF_PRINT_MASK){      case CF_PRINT_THREAD_ONLY:        p_end->pr   = pr_nop;        p_end->need = 0;        break;      case CF_PRINT_PROCESS_ONLY:        t_end->pr   = pr_nop;        t_end->need = 0;        break;      default:        fprintf(stderr, "please report this bug\n");        // FALL THROUGH      case CF_PRINT_AS_NEEDED:      case CF_PRINT_EVERY_TIME:        break;      }      walk = walk->next;    }    t_end->next = NULL;    p_end->next = NULL;    proc_format_list = pfn.next;    task_format_list = tfn.next;  }else{    proc_format_list = format_list;    task_format_list = format_list;  }  proc_format_needs = collect_format_needs(proc_format_list);  task_format_needs = collect_format_needs(task_format_list);  needs_for_sort = check_sort_needs(sort_list);  // move process-only flags to the process  proc_format_needs |= (task_format_needs &~ PROC_ONLY_FLAGS);  task_format_needs &= ~PROC_ONLY_FLAGS;  if(bsd_c_option){    proc_format_needs &= ~PROC_FILLARG;    needs_for_sort    &= ~PROC_FILLARG;  }  if(!unix_f_option){    proc_format_needs &= ~PROC_FILLCOM;    needs_for_sort    &= ~PROC_FILLCOM;  }  // convert ARG to COM as a standard  if(proc_format_needs & PROC_FILLARG){    proc_format_needs |= PROC_FILLCOM;    proc_format_needs &= ~PROC_FILLARG;  }  if(bsd_e_option){    if(proc_format_needs&PROC_FILLCOM) proc_format_needs |= PROC_FILLENV;  }    /* FIXME  broken filthy hack -- got to unify some stuff here */

⌨️ 快捷键说明

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