genMake.c (3999B)
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 TERMUX 156 157 puts("ARM cpu Termux"); 158 // define __arm__ 159 char *m = readFileToS("src/compileSheepy.sh"); 160 iReplaceS(&m, "gcc", "gcc -D__TERMUX__=1 -D__arm__=1", 1); 161 writeFileS("src/compileSheepy.sh", m); 162 free(m); 163 164 #endif // __APPLE__ 165 166 #if __APPLE__ 167 168 // not needed 169 /* // Compile with gcc in MacOS */ 170 /* char *m = readFileToS("src/compileSheepy.sh"); */ 171 /* iReplaceS(&m, "gcc", "gcc-6", 1); */ 172 /* writeFileS("src/compileSheepy.sh", m); */ 173 /* free(m); */ 174 175 #else // #if __APPLE__ 176 // check if the os is alpine linux because the libc in alpine is musl 177 if (fileExists("/etc/alpine-release")) { 178 puts("alpine linux detected, add define MUSL_LIBC=1 in compileSheepy.sh"); 179 180 char *M = readFileToS("src/compileSheepy.sh"); 181 iReplaceS(&M, "gcc", "gcc -DMUSL_LIBC=1", 1); 182 writeFileS("src/compileSheepy.sh", M); 183 free(M); 184 } 185 #endif // __APPLE__ 186 187 }