libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

genMake.c (6010B)


      1 //
      2 #include <stdio.h>
      3 #include <stdint.h>
      4 #include <stdbool.h>
      5 #include <stdlib.h>
      6 #include <sys/stat.h>
      7 #include <string.h>
      8 #include <unistd.h>
      9 
     10 ssize_t fileSize(const char *filePath) {
     11   struct stat st;
     12 
     13   int r = stat(filePath, &st);;
     14   if (r) {
     15     return(-1);
     16   }
     17 
     18   // macOS returns a varying number a number above the constant below
     19   // when the file doesnt exists
     20   if ((uint64_t)(st.st_size) > 140734000000000) {
     21     return(-1);//LCOV_EXCL_LINE
     22   }
     23   return(st.st_size);
     24 }
     25 
     26 void *readFileToS(const char *filePath) {
     27   FILE *f = NULL;
     28   void *r = NULL;
     29   ssize_t len;
     30   size_t readStatus;
     31 
     32   len = fileSize(filePath);;
     33 
     34   // +1 to add \0 at the end
     35   r = malloc(len+1);
     36   if (!r) {
     37     return(NULL);
     38   }
     39 
     40   f = fopen(filePath, "r");
     41   readStatus = fread(r, 1, len , f);
     42   fclose(f);
     43 
     44   if  (readStatus != (size_t)len) {
     45     return(NULL);
     46   }
     47 
     48   *((char*)r+len) = 0;
     49 
     50   return(r);
     51 }
     52 
     53 int iReplaceS(char **s, const char *olds, const char *news, size_t max ) {
     54   char *r = NULL;
     55   char *tmp = NULL;
     56   char *workingS = NULL;
     57   // ins is next insert point
     58   char *ins = NULL;
     59   size_t count;
     60   size_t lfront;
     61   bool freeNewsStatus = false;;
     62   char *internalNews = NULL;
     63 
     64   // sanity checks and initialization
     65   // count the number of replacements needed
     66   // allocate result
     67   // replace olds with news
     68   // copy end of string
     69 
     70   size_t lolds;
     71   lolds = strlen(olds);
     72   if (!lolds) {
     73     // empty olds causes infinite loop
     74     return(-1);
     75   }
     76   internalNews = (char *) news;
     77 
     78   // count the number of replacements needed
     79   ins = workingS = *s;
     80   for (count = 0 ; (tmp = strstr(ins, olds)) ; ++count) {
     81      ins = tmp + lolds;
     82      if (max && (count == max)) {
     83        // the maximum number is replacements is reached, stop
     84        break;
     85   }
     86      }
     87 
     88 
     89   size_t lnews;
     90   lnews       = strlen(internalNews);
     91 
     92   // allocate result
     93   size_t rLen = strlen(*s) + (lnews - lolds) * count + 1;;
     94   tmp = r     = malloc(rLen);
     95   if (!r) {
     96     if (freeNewsStatus) {
     97       free(internalNews);
     98     }
     99     return(-1);
    100   }
    101 
    102   // replace olds with news
    103   // ins points to the start of olds in s
    104   // tmp is the front of s, tmp points the end of s front in r
    105   // copy news to tmp, tmp points the end of news in r
    106   // s is the remainder of s after end of olds
    107   while (count--) {
    108     ins       = strstr(workingS, olds);
    109     lfront    = ins - workingS;
    110     tmp       = strncpy(tmp, workingS, lfront) + lfront;
    111     tmp       = strcpy(tmp, internalNews) + lnews;
    112     workingS += lfront + lolds;
    113   }
    114 
    115   // copy end of string
    116   strcpy(tmp, workingS);
    117   if (freeNewsStatus) {
    118     free(internalNews);
    119   }
    120 
    121   free(*s);
    122   *s = r;
    123   return(0);
    124 }
    125 
    126 int writeFileS(const char *filePath, char *string) {
    127   FILE *f = NULL;
    128   size_t len;
    129   size_t writeStatus;
    130 
    131   len = strlen(string);
    132 
    133   f = fopen(filePath, "w");
    134   writeStatus = fwrite(string, 1, len , f);
    135   fclose(f);
    136 
    137   if (writeStatus != len) {
    138     return(-1);
    139   }
    140 
    141   return(0);
    142 }
    143 
    144 bool fileExists(const char *filePath) {
    145 
    146   int r = access(filePath, F_OK);
    147   //if r
    148   //  perror("fileExists error");
    149   // dont print error because this is a test
    150   return(r != -1);
    151 }
    152 
    153 int main(int ARGC, char** ARGV) {
    154 
    155 #if __arm__ || __aarch64__ || __riscv
    156 
    157   // remove -mrdrnd cflag in Makefile on ARM platforms
    158   // because there is no HW random number generator
    159   char *m = readFileToS("Makefile");
    160   char *b = readFileToS("build.sh");
    161   char *B = readFileToS("buildMemcheck.sh");
    162   char *a = readFileToS("buildAsan.sh");
    163 #if !TERMUX
    164 #if __riscv
    165   puts("RISCV cpu");
    166 #else
    167   puts("ARM cpu");
    168 #endif
    169   iReplaceS(&m, " -mrdrnd", "", 1);
    170   iReplaceS(&b, " -mrdrnd", "", 2);
    171   iReplaceS(&B, " -mrdrnd", "", 1);
    172   iReplaceS(&a, " -mrdrnd", "", 1);
    173 #else // #if !TERMUX
    174   puts("ARM cpu Termux");
    175   char *i = readFileToS("install.sh");
    176   iReplaceS(&m, "gnu99", "gnu11", 2);
    177   iReplaceS(&b, "gnu99", "gnu11", 2);
    178   iReplaceS(&B, "gnu99", "gnu11", 2);
    179   iReplaceS(&m, " -mrdrnd", " -D__TERMUX__=1 -D__arm__=1", 1);
    180   iReplaceS(&b, " -mrdrnd", " -D__TERMUX__=1 -D__arm__=1", 1);
    181   iReplaceS(&B, " -mrdrnd", " -D__TERMUX__=1 -D__arm__=1", 1);
    182   iReplaceS(&m, "/usr/local/include/", "/data/data/com.termux/files/usr/include/", 0);
    183   iReplaceS(&i, "/usr/local/include/", "/data/data/com.termux/files/usr/include/", 0);
    184   iReplaceS(&m, "/usr/local/lib/", "/data/data/com.termux/files/usr/lib/", 0);
    185   iReplaceS(&i, "/usr/local/lib/", "/data/data/com.termux/files/usr/lib/", 0);
    186   writeFileS("install.sh", i);
    187   free(i);
    188 #endif // #if !TERMUX
    189   writeFileS("Makefile", m);
    190   writeFileS("build.sh", b);
    191   writeFileS("buildMemcheck.sh", B);
    192   writeFileS("buildAsan.sh", a);
    193   free(m);
    194   free(b);
    195   free(B);
    196   free(a);
    197 
    198 #endif // #if __arm__ || __aarch64__ || __riscv
    199 
    200 #if __APPLE__
    201 
    202   puts("macOS");
    203   // compile with gcc (not clang)
    204   char *m = readFileToS("Makefile");
    205   iReplaceS(&m, "gnu99", "gnu11", 2);
    206   writeFileS("Makefile", m);
    207   free(m);
    208 
    209 #else // #if __APPLE__
    210   // check if the os is alpine linux because the libc in alpine is musl
    211   if (fileExists("/etc/alpine-release")) {
    212     puts("alpine linux detected, add define MUSL_LIBC=1 in Makefile and build.sh");
    213 
    214     char *M = readFileToS("Makefile");
    215     char *b = readFileToS("build.sh");
    216     char *B = readFileToS("buildMemcheck.sh");
    217     iReplaceS(&M, "-pipe", "-pipe -DMUSL_LIBC=1", 1);
    218     iReplaceS(&b, "-pipe", "-pipe -DMUSL_LIBC=1", 1);
    219     iReplaceS(&B, "-pipe", "-pipe -DMUSL_LIBC=1", 1);
    220     writeFileS("Makefile", M);
    221     writeFileS("build.sh", b);
    222     writeFileS("buildMemcheck.sh", B);
    223     free(M);
    224     free(b);
    225     free(B);
    226   }
    227 #endif // #if __APPLE__
    228 
    229 #if __HAIKU__
    230 
    231   // remove -pthread cflag in Makefile in Haiku
    232   // because pthread is linked by default
    233   char *m = readFileToS("Makefile");
    234   char *b = readFileToS("build.sh");
    235   char *B = readFileToS("buildMemcheck.sh");
    236   iReplaceS(&m, " -pthread", "", 1);
    237   iReplaceS(&b, " -pthread", "", 1);
    238   iReplaceS(&B, " -pthread", "", 1);
    239   writeFileS("Makefile", m);
    240   writeFileS("build.sh", b);
    241   writeFileS("buildMemcheck.sh", B);
    242   free(m);
    243   free(b);
    244   free(B);
    245 
    246 #endif // #if __HAIKU__
    247 }