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

📄 skill.c

📁 linux下获取一些环境信息的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
  if(argv[1][2]=='\0' && (argv[1][1]=='s' || argv[1][1]=='n')){    sigptr = argv[2];    argv+=3;    argc-=3;  }else{    sigptr = argv[1]+1;    argv+=2;    argc-=2;  }  signo = signal_name_to_number(sigptr);  if(signo<0){    fprintf(stderr, "ERROR: unknown signal name \"%s\".\n", sigptr);    kill_usage();  }no_more_args:  if(!argc) kill_usage();  /* nothing to kill? */  while(argc--){    long pid;    char *endp;    pid = strtol(argv[argc],&endp,10);    if(!*endp){      if(!kill((pid_t)pid,signo)) continue;      exitvalue = 1;      continue;    }    fprintf(stderr, "ERROR: garbage process ID \"%s\".\n", argv[argc]);    kill_usage();  }  exit(exitvalue);}/***** skill/snice help */static void skillsnice_usage(void) NORETURN;static void skillsnice_usage(void){  if(program==PROG_SKILL){    fprintf(stderr,      "Usage:   skill [signal to send] [options] process selection criteria\n"      "Example: skill -KILL -v pts/*\n"      "\n"      "The default signal is TERM. Use -l or -L to list available signals.\n"      "Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0.\n"      "Alternate signals may be specified in three ways: -SIGKILL -KILL -9\n"    );  }else{    fprintf(stderr,      "Usage:   snice [new priority] [options] process selection criteria\n"      "Example: snice netscape crack +7\n"      "\n"      "The default priority is +4. (snice +4 ...)\n"      "Priority numbers range from +20 (slowest) to -20 (fastest).\n"      "Negative priority numbers are restricted to administrative users.\n"    );  }  fprintf(stderr,    "\n"    "General options:\n"    "-f  fast mode            This is not currently useful.\n"    "-i  interactive use      You will be asked to approve each action.\n"    "-v  verbose output       Display information about selected processes.\n"    "-w  warnings enabled     This is not currently useful.\n"    "-n  no action            This only displays the process ID.\n"    "\n"    "Selection criteria can be: terminal, user, pid, command.\n"    "The options below may be used to ensure correct interpretation.\n"    "-t  The next argument is a terminal (tty or pty).\n"    "-u  The next argument is a username.\n"    "-p  The next argument is a process ID number.\n"    "-c  The next argument is a command name.\n"  );  exit(1);}#if 0static void _skillsnice_usage(int line){  fprintf(stderr,"Something at line %d.\n", line);  skillsnice_usage();}#define skillsnice_usage() _skillsnice_usage(__LINE__)#endif#define NEXTARG (argc?( argc--, ((argptr=*++argv)) ):NULL)/***** common skill/snice argument parsing code */#define NO_PRI_VAL ((int)0xdeafbeef)static void skillsnice_parse(int argc, const char *restrict const *restrict argv){  int signo = -1;  int prino = NO_PRI_VAL;  int force = 0;  int num_found = 0;  const char *restrict argptr;  if(argc<2) skillsnice_usage();  if(argc==2 && argv[1][0]=='-'){    if(!strcmp(argv[1],"-L")){      pretty_print_signals();      exit(0);    }    if(!strcmp(argv[1],"-l")){      unix_print_signals();      exit(0);    }    if(!strcmp(argv[1],"-V")|| !strcmp(argv[1],"--version")){      display_kill_version();      exit(0);    }    skillsnice_usage();  }  NEXTARG;  /* Time for serious parsing. What does "skill -int 123 456" mean? */  while(argc){    if(force && !num_found){  /* if forced, _must_ find something */      fprintf(stderr,"ERROR: -%c used with bad data.\n", force);      skillsnice_usage();    }    force = 0;    if(program==PROG_SKILL && signo<0 && *argptr=='-'){      signo = signal_name_to_number(argptr+1);      if(signo>=0){      /* found a signal */        if(!NEXTARG) break;        continue;      }    }    if(program==PROG_SNICE && prino==NO_PRI_VAL    && (*argptr=='+' || *argptr=='-') && argptr[1]){      long val;      char *endp;      val = strtol(argptr,&endp,10);      if(!*endp && val<=999 && val>=-999){        prino=val;        if(!NEXTARG) break;        continue;      }    }    /* If '-' found, collect any flags. (but lone "-" is a tty) */    if(*argptr=='-' && argptr[1]){      argptr++;      do{        switch(( force = *argptr++ )){        default:  skillsnice_usage();        case 't':        case 'u':        case 'p':        case 'c':          if(!*argptr){ /* nothing left here, *argptr is '\0' */            if(!NEXTARG){              fprintf(stderr,"ERROR: -%c with nothing after it.\n", force);              skillsnice_usage();            }          }          goto selection_collection;        case 'f': f_flag++; break;        case 'i': i_flag++; break;        case 'v': v_flag++; break;        case 'w': w_flag++; break;        case 'n': n_flag++; break;        case 0:          NEXTARG;          /*           * If no more arguments, all the "if(argc)..." tests will fail           * and the big loop will exit.           */        } /* END OF SWITCH */      }while(force);    } /* END OF IF */selection_collection:    num_found = 0; /* we should find at least one thing */    switch(force){ /* fall through each data type */    default: skillsnice_usage();    case 0: /* not forced */    case 't':      if(argc){        struct stat sbuf;        char path[32];        if(!argptr) skillsnice_usage(); /* Huh? Maybe "skill -t ''". */        snprintf(path,32,"/dev/%s",argptr);        if(stat(path, &sbuf)>=0 && S_ISCHR(sbuf.st_mode)){          num_found++;          ENLIST(tty,sbuf.st_rdev);          if(!NEXTARG) break;        }else if(!(argptr[1])){  /* if only 1 character */          switch(*argptr){          default:            if(stat(argptr,&sbuf)<0) break; /* the shell eats '?' */          case '-':          case '?':            num_found++;            ENLIST(tty,0);            if(!NEXTARG) break;          }        }      }      if(force) continue;    case 'u':      if(argc){        struct passwd *passwd_data;        passwd_data = getpwnam(argptr);        if(passwd_data){          num_found++;          ENLIST(uid,passwd_data->pw_uid);          if(!NEXTARG) break;        }      }      if(force) continue;    case 'p':      if(argc && *argptr>='0' && *argptr<='9'){        char *endp;        int num;        num = strtol(argptr, &endp, 0);        if(*endp == '\0'){          num_found++;          ENLIST(pid,num);          if(!NEXTARG) break;        }      }      if(force) continue;      if(num_found) continue; /* could still be an option */    case 'c':      if(argc){        num_found++;        ENLIST(cmd,argptr);        if(!NEXTARG) break;      }    } /* END OF SWITCH */  } /* END OF WHILE */  /* No more arguments to process. Must sanity check. */  if(!tty_count && !uid_count && !cmd_count && !pid_count){    fprintf(stderr,"ERROR: no process selection criteria.\n");    skillsnice_usage();  }  if((f_flag|i_flag|v_flag|w_flag|n_flag) & ~1){    fprintf(stderr,"ERROR: general flags may not be repeated.\n");    skillsnice_usage();  }  if(i_flag && (v_flag|f_flag|n_flag)){    fprintf(stderr,"ERROR: -i makes no sense with -v, -f, and -n.\n");    skillsnice_usage();  }  if(v_flag && (i_flag|f_flag)){    fprintf(stderr,"ERROR: -v makes no sense with -i and -f.\n");    skillsnice_usage();  }  /* OK, set up defaults */  if(prino==NO_PRI_VAL) prino=4;  if(signo<0) signo=SIGTERM;  if(n_flag){    program=PROG_SKILL;    signo=0; /* harmless */  }  if(program==PROG_SKILL) sig_or_pri = signo;  else sig_or_pri = prino;}/***** main body */int main(int argc, const char *argv[]){  const char *tmpstr;  my_pid = getpid();  saved_argc = argc;  if(!argc){    fprintf(stderr,"ERROR: could not determine own name.\n");    exit(1);  }  tmpstr=strrchr(*argv,'/');  if(tmpstr) tmpstr++;  if(!tmpstr) tmpstr=*argv;  program = PROG_GARBAGE;  if(*tmpstr=='s'){    setpriority(PRIO_PROCESS,my_pid,-20);    if(!strcmp(tmpstr,"snice")) program = PROG_SNICE;    if(!strcmp(tmpstr,"skill")) program = PROG_SKILL;  }else{    if(!strcmp(tmpstr,"kill")) program = PROG_KILL;  }  switch(program){  case PROG_SNICE:  case PROG_SKILL:    skillsnice_parse(argc, argv);/*    show_lists(); */    iterate(); /* this is it, go get them */    break;  case PROG_KILL:    kill_main(argc, argv);    break;  default:    fprintf(stderr,"ERROR: no \"%s\" support.\n",tmpstr);  }  return 0;}

⌨️ 快捷键说明

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