| 1 | #cxx . . $ `<file>/ondir.c`/include <ctype.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | #include <limits.h> |
| 9 | #include <regex.h> |
| 10 | #include "conf.h"cxx . . $ `<file>/conf.h`/ |
| 11 | |
| 12 | /* Declarations */ |
| 13 | struct odpath_tcxx . . $ odpath_t# *rootcxx . . $ root.; |
| 14 | |
| 15 | char *substitutecxx . . $ substitute(11d6a3b64d462fd0).(const char *in, const char *var, const char *val); |
| 16 | int add_envarcxx . . $ add_envar(5ae29d726ae57f88).(const char *var, const char *val); |
| 17 | int check_regexcxx . . $ check_regex(fd52dac17d28f324).(const char *regex, const char *line, regmatch_tcxx . . $ regmatch_t# match[10]); |
| 18 | struct odpath_tcxx . . $ odpath_t# *find_pathcxx . . $ find_path(1248b958cd507890).(struct odpath_tcxx . . $ odpath_t# *last, const char *path, pathtype_tcxx . . $ pathtype_t# type, regmatch_tcxx . . $ regmatch_t# match[10]); |
| 19 | void usagecxx . . $ usage(85c52e162fed56f9).(const char *msg); |
| 20 | |
| 21 | #ifdef USE_ONENTERLEAVE |
| 22 | void display_file(const char *path); |
| 23 | int exec_ok(const char *path); |
| 24 | #endif |
| 25 | |
| 26 | /* Definitions */ |
| 27 | int maincxx . . $ main(d8e43b79a186046c).(int argc, const char **argv) { |
| 28 | #ifdef USE_ONENTERLEAVE |
| 29 | char onenter[PATH_MAX + 1]; |
| 30 | #endif |
| 31 | char working[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1], cwd[PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`! + 1]; |
| 32 | int len, i; |
| 33 | const char *src = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, *dst = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, *home = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 34 | |
| 35 | if (argc < 2) |
| 36 | usagecxx . . $ usage(85c52e162fed56f9).("Not enough arguments"); |
| 37 | |
| 38 | for (i = 1; i < argc; ++i) { |
| 39 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(argv[i], "-V")) { |
| 40 | printfcxx . . $ printf(581fddd72a8db8fc).("This is OnDir version %s\n", VERSIONcxx . . $ `<built-in>:412:9`!); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | if (argv[i][0] == '-') |
| 45 | usagecxx . . $ usage(85c52e162fed56f9).("Unknown argument"); |
| 46 | |
| 47 | if (!src) src = argv[i]; |
| 48 | else |
| 49 | if (!dst) dst = argv[i]; |
| 50 | else |
| 51 | usagecxx . . $ usage(85c52e162fed56f9).("You have already specified src and dst directories"); |
| 52 | } |
| 53 | |
| 54 | if (!dst) { |
| 55 | getcwdcxx . . $ getcwd(dff97f9eb74e1544).(cwd, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!); |
| 56 | dst = cwd; |
| 57 | } |
| 58 | |
| 59 | if (src[0] != '/' || dst[0] != '/') |
| 60 | fatalcxx . . $ fatal(326aeb464d16410c).("either the source or destination directory is not absolute"); |
| 61 | |
| 62 | rootcxx . . $ root. = load_confcxx . . $ load_conf(24e337d2fbce6db6).(GLOBAL_CONFcxx . . $ `<built-in>:413:9`!, rootcxx . . $ root.); |
| 63 | |
| 64 | if ((home = getenvcxx . . $ getenv(a8b8e32347182212).("HOME"))) { |
| 65 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(working, PATH_MAXcxx . . $ `<external>/6347054901304145714/limits.h:13:9`!, "%s/.ondirrc", home); |
| 66 | rootcxx . . $ root. = load_confcxx . . $ load_conf(24e337d2fbce6db6).(working, rootcxx . . $ root.); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | The basic algorithm is thus: |
| 71 | |
| 72 | 1. Traverse up source path until it matches the lead of dest path |
| 73 | 2. Traverse from the join to the end of dest path |
| 74 | */ |
| 75 | |
| 76 | strcpycxx . . $ strcpy(b7b3a83273496486).(working, src); |
| 77 | len = strlencxx . . $ strlen(751346f8406fd082).(working); |
| 78 | |
| 79 | /* Traverse up source path */ |
| 80 | while (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(working, dst, len)) { |
| 81 | regmatch_tcxx . . $ regmatch_t# match[10]; |
| 82 | struct odpath_tcxx . . $ odpath_t# *p; |
| 83 | |
| 84 | for (p = find_pathcxx . . $ find_path(1248b958cd507890).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, working, PT_LEAVEcxx . . $ PT_LEAVE., match); p; |
| 85 | p = find_pathcxx . . $ find_path(1248b958cd507890).(p, working, PT_LEAVEcxx . . $ PT_LEAVE., match)) { |
| 86 | char *sub; |
| 87 | |
| 88 | /* Construct temporary envars */ |
| 89 | add_envarcxx . . $ add_envar(5ae29d726ae57f88).("ONDIRWD", working); |
| 90 | for (i = 0; match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. != -1 && i < 10; ++i) { |
| 91 | int mlen = match[i].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. - match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.; |
| 92 | char var[4], val[mlen]; |
| 93 | |
| 94 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(var, "%i", i); |
| 95 | strncpycxx . . $ strncpy(255158a415b673b0).(val, working + match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., mlen); |
| 96 | val[mlen] = 0; |
| 97 | add_envarcxx . . $ add_envar(5ae29d726ae57f88).(var, val); |
| 98 | } |
| 99 | /* Expand envars */ |
| 100 | sub = expand_envarscxx . . $ expand_envars(a8b8e32347182212).(p->contentcxx . . $ odpath_t#content.); |
| 101 | /* Destroy temporary envars */ |
| 102 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).("ONDIRWD"); |
| 103 | for (i = 0; match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. != -1 && i < 10; ++i) { |
| 104 | char var[4]; |
| 105 | |
| 106 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(var, "%i", i); |
| 107 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).(var); |
| 108 | } |
| 109 | printfcxx . . $ printf(581fddd72a8db8fc).("%s\n", sub); |
| 110 | freecxx . . $ free(5d22bdacc48458e8).(sub); |
| 111 | if (p->finalcxx . . $ odpath_t#final.) |
| 112 | break; |
| 113 | } |
| 114 | #ifdef USE_ONENTERLEAVE |
| 115 | snprintf(onenter, PATH_MAX, "%s/.onleave", working); |
| 116 | if (exec_ok(onenter)) |
| 117 | display_file(onenter); |
| 118 | #endif |
| 119 | while (len && working[len] != '/') |
| 120 | --len; |
| 121 | working[len] = 0; |
| 122 | } |
| 123 | |
| 124 | if (dst[len]) ++len; |
| 125 | |
| 126 | /* Move up to destination */ |
| 127 | while (dst[len] != 0) { |
| 128 | regmatch_tcxx . . $ regmatch_t# match[10]; |
| 129 | struct odpath_tcxx . . $ odpath_t# *p; |
| 130 | |
| 131 | while (dst[len] != 0 && dst[len] != '/') |
| 132 | ++len; |
| 133 | strncpycxx . . $ strncpy(255158a415b673b0).(working, dst, len); |
| 134 | working[len] = 0; |
| 135 | for (p = find_pathcxx . . $ find_path(1248b958cd507890).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, working, PT_ENTERcxx . . $ PT_ENTER., match); p; |
| 136 | p = find_pathcxx . . $ find_path(1248b958cd507890).(p, working, PT_ENTERcxx . . $ PT_ENTER., match)) { |
| 137 | char *sub; |
| 138 | int i = 0; |
| 139 | |
| 140 | /* Construct temporary envars */ |
| 141 | add_envarcxx . . $ add_envar(5ae29d726ae57f88).("ONDIRWD", working); |
| 142 | for (i = 0; match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. != -1 && i < 10; ++i) { |
| 143 | int mlen = match[i].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. - match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so.; |
| 144 | char var[4], val[mlen]; |
| 145 | |
| 146 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(var, "%i", i); |
| 147 | strncpycxx . . $ strncpy(255158a415b673b0).(val, working + match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so., mlen); |
| 148 | val[mlen] = 0; |
| 149 | add_envarcxx . . $ add_envar(5ae29d726ae57f88).(var, val); |
| 150 | } |
| 151 | /* Expand envars */ |
| 152 | sub = expand_envarscxx . . $ expand_envars(a8b8e32347182212).(p->contentcxx . . $ odpath_t#content.); |
| 153 | /* Destroy temporary envars */ |
| 154 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).("ONDIRWD"); |
| 155 | for (i = 0; match[i].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. != -1 && i < 10; ++i) { |
| 156 | char var[4]; |
| 157 | |
| 158 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(var, "%i", i); |
| 159 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).(var); |
| 160 | } |
| 161 | printfcxx . . $ printf(581fddd72a8db8fc).("%s", sub); |
| 162 | freecxx . . $ free(5d22bdacc48458e8).(sub); |
| 163 | if (p->finalcxx . . $ odpath_t#final.) |
| 164 | break; |
| 165 | } |
| 166 | #ifdef USE_ONENTERLEAVE |
| 167 | snprintf(onenter, PATH_MAX, "%s/.onenter", working); |
| 168 | if (exec_ok(onenter)) |
| 169 | display_file(onenter); |
| 170 | #endif |
| 171 | if (dst[len] == '/') ++len; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | char *substitutecxx . . $ substitute(11d6a3b64d462fd0).(const char *in, const char *var, const char *val) { |
| 178 | int inlen = strlencxx . . $ strlen(751346f8406fd082).(in), outmax = inlen * 2, varlen = strlencxx . . $ strlen(751346f8406fd082).(var), |
| 179 | vallen = strlencxx . . $ strlen(751346f8406fd082).(val), outlen = 0; |
| 180 | char *out = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, outmax), |
| 181 | *var1 = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, varlen + 2), |
| 182 | *var2 = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, varlen + 4), |
| 183 | *offset1 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, *offset2 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 184 | |
| 185 | /* Construct $VAR */ |
| 186 | strcpycxx . . $ strcpy(b7b3a83273496486).(var1, "$"); strcatcxx . . $ strcat(b7b3a83273496486).(var1, var); |
| 187 | |
| 188 | /* Construct ${VAR} */ |
| 189 | strcpycxx . . $ strcpy(b7b3a83273496486).(var2, "${"); strcatcxx . . $ strcat(b7b3a83273496486).(var2, var); strcatcxx . . $ strcat(b7b3a83273496486).(var2, "}"); |
| 190 | |
| 191 | /* Do the replacement */ |
| 192 | while ((offset1 = strstrcxx . . $ strstr(a713fe7b9a57cf96).(in, var1)) || (offset2 = strstrcxx . . $ strstr(a713fe7b9a57cf96).(in, var2))) { |
| 193 | char *offset = offset1; |
| 194 | int vlen = varlen + 1; |
| 195 | |
| 196 | if (!offset || (offset2 && offset2 < offset1)) offset = offset2; |
| 197 | |
| 198 | if (offset[1] == '{') vlen += 2; |
| 199 | |
| 200 | if (outlen + vlen > outmax) { |
| 201 | outmax *= 2; |
| 202 | out = realloccxx . . $ realloc(417ab160f6baadc6).(out, outmax); |
| 203 | } |
| 204 | |
| 205 | out[outlen] = 0; |
| 206 | |
| 207 | /* Add difference between beginning of 'in' and 'offset' */ |
| 208 | strncatcxx . . $ strncat(255158a415b673b0).(out, in, offset - in); |
| 209 | outlen += offset - in; |
| 210 | in = offset; |
| 211 | |
| 212 | out[outlen] = 0; |
| 213 | |
| 214 | strcatcxx . . $ strcat(b7b3a83273496486).(out, val); |
| 215 | outlen += vallen; |
| 216 | in += vlen; |
| 217 | |
| 218 | offset1 = offset2 = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 219 | } |
| 220 | |
| 221 | strcatcxx . . $ strcat(b7b3a83273496486).(out, in); |
| 222 | return out; |
| 223 | } |
| 224 | |
| 225 | /* Check if a given regex matches a given line. |
| 226 | * |
| 227 | * Returns: |
| 228 | * 0 if they don't match. |
| 229 | * 1 if they match. |
| 230 | */ |
| 231 | int check_regexcxx . . $ check_regex(fd52dac17d28f324).(const char *regex, const char *line, regmatch_tcxx . . $ regmatch_t# match[10]) { |
| 232 | regex_tcxx . . $ regex_t# preg; |
| 233 | int retval = 0; |
| 234 | |
| 235 | if (!regex || !line) { |
| 236 | /* FAILURE */ |
| 237 | return (0); |
| 238 | } |
| 239 | |
| 240 | if ((retval = regcompcxx . . $ regcomp(aa05013d3db24b3e).(&preg, regex, REG_EXTENDEDcxx . . $ `<external>/18190116149591951179/regex.h:310:9`!))) { |
| 241 | char err_buf[512]; |
| 242 | |
| 243 | regerrorcxx . . $ regerror(b50a774f5c91fda6).(retval, &preg, err_buf, 512); |
| 244 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "ondir: regcomp failed, %s\n", err_buf); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | retval = regexeccxx . . $ regexec(7b724a18a85d26ac).(&preg, line, 10, match, 0); |
| 249 | regfreecxx . . $ regfree(b1814a8fd6af504b).(&preg); |
| 250 | |
| 251 | return retval == 0 && match[0].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. == 0 && match[0].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. == strlencxx . . $ strlen(751346f8406fd082).(line); |
| 252 | } |
| 253 | |
| 254 | struct odpath_tcxx . . $ odpath_t# *find_pathcxx . . $ find_path(1248b958cd507890).(struct odpath_tcxx . . $ odpath_t# *last, const char *path, pathtype_tcxx . . $ pathtype_t# type, regmatch_tcxx . . $ regmatch_t# match[10]) { |
| 255 | struct odpath_tcxx . . $ odpath_t# *i; |
| 256 | int j; |
| 257 | |
| 258 | if (last) |
| 259 | last = last->nextcxx . . $ odpath_t#next.; |
| 260 | else |
| 261 | last = rootcxx . . $ root.; |
| 262 | |
| 263 | for (i = last; i != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; i = i->nextcxx . . $ odpath_t#next.) |
| 264 | if (i->typecxx . . $ odpath_t#type. == type) { |
| 265 | for (j = 0; j < i->npathscxx . . $ odpath_t#npaths.; ++j) |
| 266 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(i->pathscxx . . $ odpath_t#paths.[j], path)) { |
| 267 | /* Emulate regex sub-pattern capturing */ |
| 268 | match[0].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. = 0; |
| 269 | match[0].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. = strlencxx . . $ strlen(751346f8406fd082).(path); |
| 270 | match[1].rm_socxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_so. = match[1].rm_eocxx . . $ $anonymous_type_2410a2af4c2f900a_1#rm_eo. = -1; |
| 271 | return i; |
| 272 | } else |
| 273 | if (check_regexcxx . . $ check_regex(fd52dac17d28f324).(i->pathscxx . . $ odpath_t#paths.[j], path, match)) |
| 274 | return i; |
| 275 | } |
| 276 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 277 | } |
| 278 | |
| 279 | int add_envarcxx . . $ add_envar(5ae29d726ae57f88).(const char *var, const char *val) { |
| 280 | char *set = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(strlencxx . . $ strlen(751346f8406fd082).(var) + strlencxx . . $ strlen(751346f8406fd082).(val) + 2); |
| 281 | |
| 282 | strcpycxx . . $ strcpy(b7b3a83273496486).(set, var); |
| 283 | strcatcxx . . $ strcat(b7b3a83273496486).(set, "="); |
| 284 | strcatcxx . . $ strcat(b7b3a83273496486).(set, val); |
| 285 | return putenvcxx . . $ putenv(1ef8e6ec083aabb9).(set); |
| 286 | } |
| 287 | |
| 288 | #ifdef USE_ONENTERLEAVE |
| 289 | void display_file(const char *path) { |
| 290 | FILE *fp; |
| 291 | |
| 292 | if ((fp = fopen(path, "r"))) { |
| 293 | char buffer[512]; |
| 294 | int len; |
| 295 | |
| 296 | while ((len = fread(buffer, 1, 512, fp)) > 0) |
| 297 | fwrite(buffer, len, 1, stdout); |
| 298 | fclose(fp); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | int exec_ok(const char *path) { |
| 303 | struct stat s; |
| 304 | |
| 305 | return |
| 306 | stat(path, &s) == 0 && /* exists.. */ |
| 307 | s.st_mode & (S_IRUSR | S_IRGRP | S_IROTH) && /* ...readable... */ |
| 308 | s.st_uid == getuid() && /* ...owned by me... */ |
| 309 | !(s.st_mode & (S_IWGRP | S_IWOTH)); /* ...not group/world writeable */ |
| 310 | } |
| 311 | #endif |
| 312 | |
| 313 | void usagecxx . . $ usage(85c52e162fed56f9).(const char *msg) { |
| 314 | printfcxx . . $ printf(581fddd72a8db8fc).( |
| 315 | "%s\n" |
| 316 | "usage: ondir [-V] <old-directory> [<new-directory>]\n" |
| 317 | "\n" |
| 318 | " -V Displays version information.\n" |
| 319 | "\n" |
| 320 | " <old-directory> is the last working directory.\n" |
| 321 | "\n" |
| 322 | " <new-directory> is the current working directory. If <new-directory> is\n" |
| 323 | " omitted, the current working directory is obtained via a call to getcwd().\n" |
| 324 | "\n", msg |
| 325 | ); |
| 326 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 327 | } |
| 328 |