免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3617 | 回复: 1
打印 上一主题 下一主题

[C] c4droid shell插件编译问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-01-02 18:15 |只看该作者 |倒序浏览
本人新手,在github上发现c4droid的插件。在c4droid上编译出现错误信息:


貌似数组不支持什么····
请教如何改!谢谢··

附上源码:
  1. #include <sys/file.h>
  2. #include <sys/stat.h>
  3. #include <sys/param.h>
  4. #include <stdio.h>
  5. #include <readline/readline.h>
  6. #include <readline/history.h>
  7. #include <signal.h>
  8. #include <stdlib.h>
  9. #include <linux/binfmts.h>
  10. #include <stdbool.h>
  11. #define MAXCONFNAMELEN 25
  12. #define MAXCONFVALUELEN 1024
  13. #define MAXCONFSIZE 40

  14. struct myconfig {
  15.         char *name[MAXCONFSIZE];
  16.         char *value[MAXCONFSIZE];
  17. };

  18. struct SHEBANG {
  19.         char path2script[MAXPATHLEN];
  20.         char shebang[BINPRM_BUF_SIZE];
  21.         char opt[BINPRM_BUF_SIZE];
  22. };

  23. /* Forward declarations. */
  24. char *stripwhite();
  25. char *dupstr(char *s);
  26. static char *rstrip(char *s);
  27. static char *lskip(const char *s);
  28. static char* find_char(const char* s, char c);
  29. int update_config(struct myconfig *config, int *configsize);
  30. char *promt_line();
  31. int getShebang(char *execname,struct SHEBANG *pTRshebang);
  32. char *replace(char *instring,char *old,char *new);
  33. static char cur4dir[MAXPATHLEN];

  34. /* When non-zero, this global means the user is done using this program. */
  35. int done;


  36. char *promt_line()
  37. {
  38.         static char buf[MAXPATHLEN];
  39.         char *tom=(char*) xmalloc(15);
  40.         char *p, *prompt;
  41.         prompt = (getuid()? " $ " : " # ");
  42.         char *path=replace(getcwd(cur4dir, MAXPATHLEN),getenv("HOME"),"~");
  43.         int n=0;
  44.         tom[0]='\0';
  45.         while(1)
  46.         {
  47.                 if(!(p=strchr(path+1,'/')))
  48.                 {
  49.                         break;
  50.                 }
  51.                 if(n==0)
  52.                 {
  53.                         if((p-path)>14){break;}
  54.                         strncpy(tom,path,(p-path));
  55.                         tom[(p-path)]='\0';
  56.                 }
  57.                 path=p;
  58.                 n++;
  59.         }       
  60.         if(n<=1){
  61.                 sprintf(buf, "\001\e[1;33m\002%s%s%s\001\e[00m\002", tom,path, prompt);
  62.         }else{
  63.                 size_t dot_len=(n-1)*4-1;
  64.                 char dot[dot_len+1];
  65.                 memset(dot,'.',dot_len);
  66.                 dot[dot_len] = '\0';
  67.                 int i;
  68.                 for(i=0;i<dot_len;i++)
  69.                 {
  70.                         dot[i]=((i&3) ? '.' : '/');
  71.                 }               
  72.                 sprintf(buf, "\001\e[1;33m\002%s%s%s%s\001\e[00m\002", tom,dot,path, prompt);
  73.         }
  74.         buf[sizeof(buf) - 1] = '\0';
  75.         return buf;       
  76. }

  77. void sigint_init()
  78. {
  79.         struct sigaction act;
  80.         memset(&act,0,sizeof(struct sigaction));
  81.         act.sa_handler=SIG_IGN;
  82.         sigaction (SIGINT,&act,0);
  83.         return;
  84. }

  85. main(int argc, char **argv)
  86. {
  87.        
  88.         if (access("/data/data/com.n0n3m4.droidc/files/gcc", 0)) {
  89.                 printf("Please install gcc in internal memory\n");
  90.                 exit(1);
  91.         }

  92.         int error=0;
  93.         struct myconfig config;
  94.         int configsize;
  95.         configsize = 0;
  96.        
  97.        
  98.         if (access("/data/data/com.n0n3m4.droidc/home", 0)) {
  99.                 if(getuid()==0){
  100.                     printf("Please uncheck 'Run programs as root' in C4droid preferences for first start\n");
  101.                     exit(0);
  102.             }
  103.                 mkdir("/data/data/com.n0n3m4.droidc/home", 0777);
  104.         }
  105.        
  106.         if (access("/data/data/com.n0n3m4.droidc/tmp",0)) {
  107.                 if(getuid()==0){
  108.                     printf("Please uncheck 'Run programs as root' in C4droid preferences for first start\n");
  109.                     exit(0);
  110.             }
  111.                 mkdir("/data/data/com.n0n3m4.droidc/tmp",0777);
  112.                 chmod("/data/data/com.n0n3m4.droidc/tmp", S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX);
  113.         }
  114.         struct stat sb;
  115.         if (stat("/data/data/com.n0n3m4.droidc/files/.C4dENV", &sb)== -1  || sb.st_size==0) {
  116.                 if(getuid()==0){
  117.                     printf("Please uncheck 'Run programs as root' in C4droid preferences for first start\n");
  118.                     exit(0);
  119.             }
  120.             
  121.                 config.name[0]= "PATH";
  122.                 config.value[0] = "/data/data/com.n0n3m4.droidc/usr/bin:/busybox-virtual:/data/data/com.n0n3m4.droidc/files/:/data/data/com.n0n3m4.droidc/files/gcc/bin:/data/data/com.n0n3m4.droidc/files/gcc/qt/bin:/data/data/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/bin:/data/data/com.n0n3m4.droidc/usr/bin/scripts:/sbin:/system/bin:/system/xbin:/data/local/bin";
  123.                 config.name[1] = "SHELL";
  124.                 config.value[1] = "/data/data/com.n0n3m4.droidc/files/busybox sh";
  125.                 config.name[2] = "CONFIG_SHELL";
  126.                 config.value[2] = "sh";
  127.                 config.name[3] = "CFLAGS";
  128.                 config.value[3] = "-Os -s  -DANDROID -I/sdcard/C4droid_EXT/include -I/data/data/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/include/ncurses -I/data/data/com.n0n3m4.droidc/usr/include -L/data/data/com.n0n3m4.droidc/usr/lib -L/sdcard/C4droid_EXT/lib";
  129.                 config.name[4] = "CXXFLAGS";
  130.                 config.value[4] = "-Os -s -DANDROID -I/sdcard/C4droid_EXT/include -I/data/data/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/include/ncurses -I/data/data/com.n0n3m4.droidc/usr/include -L/data/data/com.n0n3m4.droidc/usr/lib -L/sdcard/C4droid_EXT/lib";
  131.                 config.name[5] = "LDFLAGS";
  132.                 config.value[5] = "-lm -ldl -llog -lz -lncurses -L/sdcard/C4droid_EXT/lib -L/data/data/com.n0n3m4.droidc/usr/lib -Wl,-allow-shlib-undefined";
  133.                 config.name[6]= "PKG_CONFIG_PATH";
  134.                 config.value[6] = "/data/data/com.n0n3m4.droidc/files/gcc/arm-linux-androideabi/lib/pkgconfig:/sdcard/C4droid_EXT/lib/pkgconfig;/data/data/com.n0n3m4.droidc/usr/lib/pkgconfig";
  135.                 config.name[7] = "CC";
  136.                 config.value[7] = "/data/data/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-gcc";
  137.                 config.name[8] = "CXX";
  138.                 config.value[8] = "/data/data/com.n0n3m4.droidc/files/gcc/bin/arm-linux-androideabi-g++";
  139.                 config.name[9] = "LD_LIBRARY_PATH";
  140.                 config.value[9] = "/data/data/com.n0n3m4.droidc/usr/lib:/vendor/lib:/system/lib";
  141.                 config.name[10] = "PREFIX";
  142.                 config.value[10] = "/data/data/com.n0n3m4.droidc/usr";               
  143.                 config.name[11] = "TEMP";
  144.                 config.value[11] = "/data/data/com.n0n3m4.droidc/tmp";
  145.                 config.name[12] = "TMP";
  146.                 config.value[12] = "/data/data/com.n0n3m4.droidc/tmp";
  147.                 config.name[13] = "TMPDIR";
  148.                 config.value[13] = "/data/data/com.n0n3m4.droidc/tmp";
  149.                 config.name[14] = "CMAKE_ROOT";
  150.                 config.value[14] = "/mnt/sdcard/C4droid_EXT";
  151.                 config.name[15] = "LC_ALL";
  152.                 config.value[15] = "en_EN.UTF-8";
  153.                
  154.                 config.name[16]="PERL";
  155.                 config.value[16]="/data/data/com.n0n3m4.droidc/usr/bin/perl";
  156.                
  157.                 config.name[17] = "GIT_SSL_CAINFO";
  158.                 config.value[17] = "/data/data/com.n0n3m4.droidc/usr/lib/ssl/certs/ca-bundle-cert.pem";
  159.                 config.name[18] = "GIT_CURL_VERBOSE";
  160.                 config.value[18] = "0";
  161.                 config.name[19] = "CURL_CA_BUNDLE";
  162.                 config.value[19] = "/data/data/com.n0n3m4.droidc/usr/lib/ssl/certs/ca-bundle-cert.pem";
  163.                 config.name[20] = "SSL_CERT_FILE";
  164.                 config.value[20] = "/data/data/com.n0n3m4.droidc/usr/lib/ssl/certs/ca-bundle-cert.pem";
  165.                 config.name[21] = "SSL_CERT_DIR";
  166.                 config.value[21] = "/data/data/com.n0n3m4.droidc/usr/lib/ssl/certs";
  167.                 /*
  168.                 in doio.c PerlProc_execl(exsh,nash,cosh,cmd, (char *)NULL);
  169.                 for exec script with shebang #!/bin/sh
  170.                 */
  171.                 config.name[22] = "PERL_EXSH";
  172.             config.value[22] = "/data/data/com.n0n3m4.droidc/files/busybox";
  173.             config.name[23] = "PERL_NASH";
  174.             config.value[23] = "sh";
  175.             config.name[24] = "PERL_COSH";
  176.             config.value[24] = "-c";
  177.                 configsize = 25;               
  178.         }
  179.        
  180.         error=update_config(&config, &configsize);
  181.         if(error!=0) {
  182.                 done =1;
  183.         }
  184.        
  185.         setenv("HOME", "/data/data/com.n0n3m4.droidc/home", 1);
  186.        
  187.         if (chdir("/data/data/com.n0n3m4.droidc/home") == -1) {
  188.                 perror("/data/data/com.n0n3m4.droidc/home");
  189.         }
  190.         char *line, *s;       

  191.         sigint_init();

  192.         if(read_history(NULL)!=0){
  193.                 write_history(NULL);
  194.         }       
  195.         register HIST_ENTRY **the_list;
  196.         register int i;
  197.         register int j;
  198.         the_list = history_list ();
  199.     if (the_list){
  200.         for (i= 1; the_list[i]; i++){
  201.                 for (j= i+1; the_list[j]; j++){
  202.                         if(strcmp(the_list[i]->line,the_list[j]->line)==0){
  203.                                
  204.                                 HIST_ENTRY * entry=remove_history(i);
  205.                                 free(entry->line);
  206.                                 free(entry);
  207.                                     i--;
  208.                                     break;
  209.                             }
  210.                     }
  211.         
  212.             }
  213.     }
  214.     write_history(NULL);       
  215.        
  216.         error=1;

  217.         for (; done == 0;) {

  218.                 line = readline(promt_line());
  219.                 if (!line){
  220.                         printf("\n");
  221.                         continue;
  222.                 }               
  223.                 s = stripwhite(line);
  224.                 if (*s) {
  225.                         if(error==0){
  226.                                 add_history(s);
  227.                                 append_history(1,NULL);
  228.                         }else{error=0;}
  229.                         execute_line(s);
  230.                 }
  231.                
  232.                 free(line);
  233.         }
  234.         exit(0);
  235. }

  236. /* Execute a command line. */
  237. int execute_line(line)
  238. char *line;
  239. {
  240.         register int i;       
  241.         char *word;
  242.         char *wordplus;
  243.         char *command;
  244.         struct SHEBANG *execFile;
  245.         execFile=(struct SHEBANG*)xmalloc(sizeof(struct SHEBANG));
  246.         line=replace(line,"~",getenv("HOME"));
  247.         if(!line){
  248.                 printf("ERROR:allocate memmory for replace()\n");
  249.                 return 0;
  250.         }
  251.         char *origline=dupstr(line);
  252.         static char syscom[1024];
  253.        
  254.         i = 0;
  255.         while (line[i] && whitespace(line[i]))
  256.                 i++;
  257.         word = line + i;

  258.         while (line[i] && !whitespace(line[i]))
  259.                 i++;

  260.         if (line[i]){
  261.                 line[i++] = '\0';
  262.         }
  263.        
  264.         while (whitespace(line[i])){
  265.                 i++;
  266.         }
  267.         wordplus = line + i;
  268.        
  269.        
  270.         if(!wordplus || !*wordplus){wordplus="";}
  271.         if (strstr(wordplus,"&& ")){
  272.                 printf("WARNING!!:found '&&'\nThe correct execution of multiple commands can not be guaranteed.\n");
  273.         }
  274.         if (*word) {
  275.                 command=(char *)basename(word);               
  276.                 /*configure*/
  277.                 if(strcmp(command,"configure")==0){
  278.                         if((strcmp(wordplus,"--help")!=0 \
  279.                                 && strcmp(wordplus,"-h")!=0 ) \
  280.                                 && (strcmp(wordplus,"-help")!=0 \
  281.                                 && strcmp(wordplus,"?")!=0)){
  282.                             sprintf(syscom,\
  283.                                         "/data/data/com.n0n3m4.droidc/usr/bin/CONFIGFIX %s --host=arm-linux-androideabi --build=i686-linux --disable-nls --prefix=%s CFLAGS='%s' CXXFLAGS='%s' LDFLAGS='%s' %s CONFIG_SHELL=sh",\
  284.                                         word,getenv("PREFIX"),getenv("CFLAGS"),\
  285.                                         getenv("CXXFLAGS"),getenv("LDFLAGS"),wordplus);
  286.                                 printf("\001\e[1;33m\002 %s\001\e[00m\002\n",syscom);
  287.                         }else{
  288.                                 sprintf(syscom,"%s %s --help",getenv("SHELL"),word);
  289.                         }
  290.                         system(syscom);
  291.                 /*make*/
  292.                 }else if(strcmp(command,"make")==0){
  293.                         sprintf(syscom,"%s %s CC='%s' CXX='%s' SHELL='%s'",\
  294.                                 word,wordplus,getenv("CC"),getenv("CXX"),getenv("SHELL"));
  295.                         printf("\001\e[1;33m\002 %s\001\e[00m\002\n",syscom);
  296.                         system(syscom);
  297.                 /*cd*/
  298.                 }else if(strcmp(command,"cd")==0){                       
  299.                     if (wordplus==""){
  300.                         if(chdir(getenv("HOME"))==-1){
  301.                                         perror(getenv("HOME"));
  302.                         }                                               
  303.                         }else if (chdir(wordplus) == -1) {
  304.                                 perror(wordplus);
  305.                         }      
  306.                 /*ls*/
  307.                 }else if(strcmp(command,"ls")==0){
  308.                         sprintf(syscom, "busybox ls %s", wordplus);
  309.                         system(syscom);
  310.                 /*shebang*/
  311.                 }else if(strcmp(command,"shebang")==0){
  312.                         if(wordplus!=""){
  313.                             if(getShebang(wordplus,execFile)){
  314.                                     printf("\001\e[1;34m\002File:\001\e[00m\002 %s\n\001\e[1;34m\002Shebang:\001\e[00m\002 %s\n",execFile->path2script,execFile->shebang);
  315.                                         if(*execFile->opt){
  316.                                             printf("\001\e[1;34m\002Options:\001\e[00m\002 %s\n",execFile->opt);
  317.                                         }
  318.                             }else if(execFile->path2script){
  319.                                     printf("\001\e[1;34m\002File:\001\e[00m\002 %s\n",execFile->path2script);
  320.                             }
  321.                         }else{
  322.                                 printf("usage: shebang <file> \n");
  323.                         }
  324.                 /*su*/
  325.                 }else if(strcmp(command,"su")==0){
  326.                         free(line);
  327.                         free(origline);
  328.                         free(execFile);
  329.                         system("su -c c4dsh");
  330.                         return 0;
  331.                 /*quit*/
  332.                 }else if(strcmp(command,"quit")==0 || strcmp(command,"exit")==0){
  333.                         done=1;
  334.                 /*not substituted commands*/
  335.                 /*if script /bin/sh*/
  336.                 }else if(getShebang(word,execFile) && strstr(execFile->shebang,"#!/bin/sh")){
  337.                         sprintf(syscom,"%s %s %s",getenv("SHELL"),execFile->path2script,wordplus);
  338.                         system(syscom);
  339.                 /*if file exist*/
  340.                 }else if(execFile->path2script[0]){
  341.                         sprintf(syscom,"%s %s",execFile->path2script,wordplus);
  342.                         system(syscom);
  343.                 /*try exec*/
  344.                 }else{
  345.                         sprintf(syscom,"%s",origline);
  346.                         system(syscom);
  347.                 }
  348.                
  349.         }
  350.         free(execFile);
  351.         free(line);
  352.         free(origline);
  353.         return 0;
  354. }

  355. char *dupstr(s)
  356. char *s;
  357. {
  358.         char *r;

  359.         r =(char*) xmalloc(strlen(s) + 1);
  360.         strcpy(r, s);
  361.         return (r);
  362. }

  363. static char* find_char(const char* s, char c)
  364. {
  365.        
  366.         while (*s && *s != c) {
  367.                 s++;
  368.         }
  369.         return (char*)s;
  370. }
  371. static char *rstrip(char *s)
  372. {
  373.         char *p = s + strlen(s);
  374.         while (p > s && isspace((unsigned char)(*--p)))
  375.                 *p = '\0';
  376.         return s;
  377. }

  378. static char *lskip(const char *s)
  379. {
  380.         while (*s && isspace((unsigned char)(*s)))
  381.                 s++;
  382.         return (char *)s;
  383. }



  384. int update_config(struct myconfig *config, int *configsize)
  385. {

  386.         FILE *file;
  387.         int mode=0;
  388.         int i;
  389.         int count=*configsize;
  390.         int error=0;
  391.         if(*configsize) {
  392.                 file = fopen("/data/data/com.n0n3m4.droidc/files/.C4dENV", "w+");
  393.         } else {
  394.                 file = fopen("/data/data/com.n0n3m4.droidc/files/.C4dENV", "r");
  395.         }
  396.         if (!file) {
  397.                 printf("ERROR:open file '/data/data/com.n0n3m4.droidc/files/.C4dENV'\n");
  398.                 return -1;
  399.         }
  400.         if(*configsize==0) {
  401.                 char* start;
  402.             char* end;
  403.             char *line;
  404.             int maxlenline=MAXCONFVALUELEN+MAXCONFNAMELEN+5;
  405.             line = (char*)malloc(maxlenline);
  406.             if(!line)
  407.                 {
  408.                         printf("ERROR:allocate memmory for line\n");
  409.                         fclose(file);
  410.                         return -2;
  411.                 }
  412.                 while (fgets(line, maxlenline, file) != NULL)
  413.                 {
  414.                        
  415.                     start = line;
  416.                     if (count == 0 && (unsigned char)start[0] == 0xEF &&
  417.                         (unsigned char)start[1] == 0xBB &&
  418.                         (unsigned char)start[2] == 0xBF) {
  419.                             start += 3;
  420.                     }
  421.                     start = lskip(rstrip(start));
  422.                     if (*start) {
  423.                             end = find_char(start, '=');
  424.                             if (*end == '=') {
  425.                                     *end = '\0';
  426.                                     config->name[count] = dupstr(rstrip(start));
  427.                                     start = lskip(end + 1);
  428.                                     if(*start='[') {
  429.                                             end = find_char(start, ']');
  430.                                             if (*end == ']') {
  431.                                                     *end = '\0';
  432.                                                         start = lskip(start + 1);
  433.                                                     config->value[count]=dupstr(rstrip(start));
  434.                                                     //printf("%s\n",rstrip(start));
  435.                                                     
  436.                                             } else {
  437.                                                         printf("ERROR:']' not found in line: %i (config broken)\n",count);
  438.                                                         error=1;
  439.                                                     break;
  440.                                             }
  441.                                     } else {
  442.                                                 printf("ERROR:'[' not found in line: %i (config broken)\n",count);
  443.                                                 error=1;
  444.                                             break;
  445.                                     }

  446.                             } else {
  447.                                         printf("ERROR:'=' not found in line: %i (config broken)\n",count);
  448.                                         error=1;
  449.                                     break;
  450.                             }

  451.                     } else {
  452.                                 printf("ERROR:config file is empty or broken\n");
  453.                                 error=1;
  454.                             break;
  455.                     }                    
  456.                     count++;

  457.             }
  458.                 if(error){
  459.                         fclose(file);
  460.                         free(line);
  461.                         return -3;
  462.                 }
  463.                 *configsize=count;               
  464.                 if(*configsize==0) {
  465.                         fclose(file);
  466.                         free(line);
  467.                         printf("ERROR:parse config emty or broken\n");
  468.                         return -3;
  469.                 }
  470.                 free(line);
  471.                 mode=1;
  472.         }
  473.        
  474.         for (i = 0; i < count; i++) {
  475.                 if(!mode) {
  476.                         if(!(fprintf(file, "%s = [%s]\n", config->name[i], config->value[i]))) {
  477.                                 printf("ERROR:write %i line in config\n",i);
  478.                                 error=-4;
  479.                         }
  480.                 }
  481.                
  482.                 if(setenv(config->name[i],config->value[i],1)!=0){
  483.                         printf("ERROR:set environment from line %i \n",i);
  484.                         error=-5;
  485.                 }
  486.     }
  487.     fclose(file);
  488.         return error;
  489. }

  490. char *stripwhite(string)
  491. char *string;
  492. {
  493.         register char *s, *t;

  494.         for (s = string; whitespace(*s); s++)
  495.                 ;

  496.         if (*s == 0)
  497.                 return (s);

  498.         t = s + strlen(s) - 1;
  499.         while (t > s && whitespace(*t))
  500.                 t--;
  501.         *++t = '\0';

  502.         return s;
  503. }



  504. char *replace(char *instring,char *old,char *new)
  505. {
  506.         if(!instring || !old || !new){
  507.                 return (char*)NULL;
  508.         }
  509.         size_t instring_size=strlen(instring);
  510.         size_t new_size=strlen(new);
  511.         size_t old_size=strlen(old);
  512.         size_t outstring_size=instring_size + 1;
  513.         char *outstring;
  514.         char *test;
  515.         test=(char*)malloc(old_size+1);
  516.         outstring =(char*) malloc(outstring_size);
  517.         if(!outstring || !test){
  518.                 return (char*)NULL;
  519.         }
  520.         if(instring_size<old_size || old_size==0)
  521.         {               
  522.            strcpy(outstring, instring);
  523.            free(test);
  524.            return outstring;
  525.         }       
  526.         outstring[0]='\0';
  527.         int i;
  528.         for(i=0; i <= instring_size; i++)
  529.         {               
  530.                 strncpy(test,(instring+i),old_size);
  531.                 test[old_size]='\0';
  532.                 if(strcmp(test,old)==0){
  533.                         if(new_size!=old_size)
  534.                         {
  535.                                 outstring_size=outstring_size+new_size-old_size;
  536.                                 outstring=realloc(outstring,outstring_size);
  537.                                 if(!outstring){
  538.                                         free(test);
  539.                                         return (char*)NULL;
  540.                                 }
  541.                         }
  542.                         strcat(outstring,new);
  543.                         i=i+old_size-1;
  544.                 }else{
  545.                         test[1]='\0';
  546.                         strcat(outstring,test);
  547.                 }
  548.         }
  549.         free(test);
  550.         return outstring;
  551. }


  552. //#include <linux/binfmts.h>
  553. //#include <sys/param.h>
  554. int getShebang(char *execname,struct SHEBANG *pTRshebang){

  555.         if(!execname || !pTRshebang){return 0;}
  556.         /*clean*/
  557.         memset(pTRshebang,0,sizeof(*pTRshebang));
  558.     FILE *file;
  559.         int shebang_len =0;
  560.         int opt_len =0;
  561.         bool optexist =false;
  562.         char ch= '\0';       
  563.         struct stat sb;
  564.         sprintf(pTRshebang->path2script,"%s",execname);
  565.         /*if not full path?*/
  566.         if(stat(pTRshebang->path2script,&sb)==-1 || S_ISDIR(sb.st_mode)){
  567.                 /*not in curdir?*/

  568.                 //static char cur4dir[MAXPATHLEN];
  569.                 //if(getcwd(cur4dir, MAXPATHLEN)!=NULL){
  570.                 if(cur4dir){
  571.                         sprintf(pTRshebang->path2script,"%s/%s",cur4dir,basename(execname));
  572.                     if(stat(pTRshebang->path2script,&sb)==-1 || S_ISDIR(sb.st_mode)){
  573.                             /* search in PATH */
  574.                             char *start,*end,*sline;
  575.                                 sline=(char*)malloc(strlen(getenv("PATH")) + 1);
  576.                                 if(!sline){return 0;}
  577.                             strcpy(sline,getenv("PATH"));
  578.                             start=sline;
  579.                             while(*start){
  580.                                         end=find_char(start,':');
  581.                                     if (*end == ':'){*end = '\0';}
  582.                                     sprintf(pTRshebang->path2script,"%s/%s",start,basename(execname));
  583.                                     if (stat(pTRshebang->path2script, &sb)!= -1 && !S_ISDIR(sb.st_mode)){break;}
  584.                                         pTRshebang->path2script[0]='\0';
  585.                                     start = lskip(end + 1);
  586.                             }
  587.                             free(sline);                               
  588.                     }
  589.             }
  590.         }else{
  591.                 if(execname[0]!='/'){
  592.                 sprintf(pTRshebang->path2script,"%s/%s",cur4dir,basename(execname));
  593.                 }
  594.         }
  595.         if(!pTRshebang->path2script[0] || !(file = fopen(pTRshebang->path2script, "r"))){
  596.             return 0;
  597.         }
  598.         /*without fgets*/
  599.         while(shebang_len < 2 && (ch=fgetc(file)) != EOF) {
  600.         pTRshebang->shebang[shebang_len]=ch;
  601.                 shebang_len++;
  602.     }
  603.         /*if script*/
  604.         if(pTRshebang->shebang && strstr(pTRshebang->shebang,"#!")){
  605.             while((ch=fgetc(file)) != EOF && (ch!='\n' && (shebang_len+opt_len)< BINPRM_BUF_SIZE)) {
  606.                     if(isspace(ch)){
  607.                         if(shebang_len==2){
  608.                                     continue;
  609.                                 }else{
  610.                                     optexist=true;
  611.                                     if(opt_len==0){continue;}
  612.                                 }
  613.                         }
  614.                         if(optexist){
  615.                             pTRshebang->opt[opt_len]=ch;
  616.                                 opt_len++;
  617.                         }else{
  618.                 pTRshebang->shebang[shebang_len]=ch;
  619.                 shebang_len++;
  620.                         }                    
  621.                 }
  622.                                
  623.     }else{
  624.                 fclose(file);
  625.             return 0;
  626.         }
  627.         fclose(file);       
  628.         if(ch =='\n'){return 1;}
  629.     return 0;
  630. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-01-02 18:17 |只看该作者
项目:github.com/fann95/C4droid-Shell
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP