All pastes #483396 Raw Edit

Stuff

public c v1 · immutable
·published 2007-05-11 18:35 UTC
rendered paste body
#include <stdio.h>#include <signal.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <ctype.h>#include <time.h>char pname[1024];char tty[1024];char szeit[1024];char pid[1024];time_t boot_Time = 0;time_t start_zeit = 0;int bootzeit(){  FILE *pFile = NULL;  char rBuf[256];  char buf[20];    if(NULL==(pFile = fopen("/proc/stat","r"))) return 1;  while(NULL!=fgets(rBuf,255,pFile)){    if(strstr(rBuf,"btime") != NULL){      sscanf(rBuf,"%s%d",buf,(int*)&boot_Time);      break;    }  }  return 0;}int getAttr(char zeile[]){  FILE* inp;  char name [1024];  char zeile2[1024];  int i;  zeile=strtok(zeile,"\n");  sprintf(name,"/proc/%s/stat",zeile);  inp = fopen(name, "r");  if(inp==NULL) return -1;  fgets (zeile2,1024,inp);  strtok(zeile2," ");  strncpy(pname,strtok(NULL, " )")+1,1024);  for(i=0;i<4;i++) strtok(NULL, " ");  strncpy(tty,strtok(NULL, " "),1024);  for(i=0;i<14;i++) strtok(NULL, " ");  strncpy(szeit,strtok(NULL, " "),1024);  sscanf(szeit,"%d",(int*)&start_zeit);  fclose(inp);  return 0;}int main(int argc, char *argv[]){  unsigned int pid = 0;  char 	zeile [1024];  FILE* pipe;  char eingabe;  int anz=0;  int ret=0;  int feld [1024];  char vBuf[128+1];  char kill[1024];  time_t start;  struct tm zzeit;  if(argc!=2){    printf("Bitte geben Sie einen Programmnamen an \n");    exit(1);  }  pipe=popen ("ls /proc", "r");  if(bootzeit()!=0) return -1;   while(fgets(zeile,1024,pipe)!=NULL){      if(isdigit(zeile[0])){        ret=getAttr(zeile);        if(ret!=0)continue;        if(!strcmp(pname,argv[1])){          feld[anz]=atoi(zeile);          anz++;          start=boot_Time + start_zeit/100;           zzeit=*localtime(&start);          strftime(szeit,1024,"%H:%M",&zzeit);          if(strcmp(tty,"0")==0) tty[0]='?';          else            if(atoi(tty)<34816) sprintf(tty,"tty%d",atoi(tty)-1024);            else sprintf(tty,"pts/%d",atoi(tty)-34816);          printf("%3i. PID: %5s TTY: %7s Startzeit: %s Programm: %s \n", anz, zeile, tty, szeit, pname);          pid = atoi(zeile);        }      }  }  sprintf(zeile,"%d",pid);  if(anz==0){    printf("Der Prozess %s ist nicht aktiv \n", argv[1]);    exit(1);  }  if(anz>1){    printf("mehrere Einträge gefunden ! Bitte geben Sie die Zeilennummer ein !");    fgets(vBuf,128,stdin);    if(atoi(vBuf)>anz) exit(1);    sprintf(zeile,"%d",feld[atoi(vBuf)-1]);  }  printf("Wollen Sie den Prozess %s wirklich beenden ? (J/N)",argv[1]);  eingabe = getchar();  if (eingabe=='J'||eingabe=='j'){    sprintf(kill,"kill %s\n ",zeile);    system(kill);    printf("Der Prozess %s %s wurde gekillt\n", argv[1], zeile);  }  return 0;}