Last active
June 28, 2021 11:17
-
-
Save daerich/09488761a2f714caff116efda3449ddc to your computer and use it in GitHub Desktop.
Parse proc directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* BEGIN Cue */ | |
#include <std{io,lib}.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <dirent.h> | |
#include <unistd.h> | |
static int in_numbers(char *); | |
static int check_by_slsh(char*,char*); | |
static void check_proc(char*,char*,char*); | |
static char * addread(char*,char,int); | |
int main(int argc, char ** argv) /* run /dir/ prog */ | |
{ | |
if(argc != 3) | |
return 1; | |
DIR* stream=opendir(argv[1]); | |
char buf[4096]; | |
struct dirent * data ={0}; | |
while((data=readdir(stream))!= NULL){ | |
if(in_numbers(data->d_name)){ | |
printf("%s\n",data->d_name); | |
check_proc(argv[2],data->d_name,argv[1]); | |
} | |
} | |
closedir(stream); | |
return 0; | |
} | |
static void check_proc(char * proc,char * dat,char * defdir){ | |
char * exe = "/exe"; | |
char * conc_s = malloc((strlen(dat)+strlen(defdir)+strlen(exe)) * sizeof(char)); | |
conc_s=memcpy(conc_s,defdir,strlen(defdir)); | |
conc_s=strcat(conc_s,dat); | |
conc_s=strcat(conc_s,exe); | |
char * buf = malloc(4096 * sizeof(char)); /* Current kernel path limit */ | |
buf=memset(buf,0,4096); /* Clean up dirty pages(don't bug OS with calloc) */ | |
printf("CONC_S:%s\n",conc_s); | |
readlink(conc_s,buf,4096); | |
printf("LINK:%s\n",buf); | |
if(check_by_slsh(buf,proc)){ | |
printf("Found %s\n",buf); | |
} | |
else | |
printf("Fail\n"); | |
free(conc_s); | |
free(buf); | |
} | |
static int in_numbers(char * dat) | |
{ | |
char num[10] = {'1','2','3','4','5','6','7','8','9'}; | |
for(int x = 0;x < 10 ;x++){ | |
if(dat[0] == num[x]) | |
return 1; | |
} | |
return 0; | |
} | |
static int check_by_slsh(char * sub,char * sam) | |
{ printf("DATA:%s,SAM:%s\n",sub,sam); | |
char * buf = NULL; | |
int len = 0; | |
int lastslsh = 0; | |
for(int x = 0;x < strlen(sub);x++){ | |
if(sub[x] == '/'){ | |
lastslsh=x; | |
} | |
} | |
for(int x = lastslsh + 1;sub[x] != '\0';x++){ | |
buf=addread(buf,sub[x],len); | |
len++; | |
} | |
buf=addread(buf,'\0',len); | |
printf("BUF;%s\n",buf); | |
if(strcmp(buf,sam) == 0){ | |
free(buf); | |
return 1; | |
} | |
else{ | |
free(buf); | |
return 0; | |
} | |
} | |
static char* addread(char * buf,char r, int length){ | |
char * res=malloc((length+1) * sizeof(char)); | |
if(res == NULL){ | |
fprintf(stderr,"Malloc Error\n"); /* Quit on malloc*/ | |
exit(1); | |
} | |
for(int x=0;x < length ;x++){ | |
res[x]=buf[x]; | |
} | |
res[length]=r; /* Those are offsets FFS */ | |
if(length > 0) | |
free(buf); | |
return res; | |
} | |
/* END Cue */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment