char *server_socket_fname () { char *buf = getenv(ENV_MMJVMD_DIR_NAME_ABS); char *dir = (buf != NULL && strlen(buf) > 1 ? copy_str(buf) : concat_paths(get_home_dir(), MMJVMD_DIR_NAME)); return concat_paths(dir, SERVER_SOCKET_FNAME); }
static char*dataset_filename(uint8_t*hash) { char*basename = hash_to_string(hash); char*path = concat_paths(config_dataset_cache_directory, basename); free(basename); return path; }
/* * deref the link (in link_buf) read from path_buf into res_buf * * if there's any problem then just return the contents of the link buffer */ static void volmgt_deref_link(char *res_buf, char *path_buf, char *link_buf) { static char *vol_dirname(char *); char buf[MAXPATHLEN+1]; char *path_dirname; if (IS_ABS_PATH(link_buf)) { /* degenerate case -- link is okay the way it is */ (void) strncpy(res_buf, link_buf, MAXPATHLEN); } else { /* link pathname is relative */ /* get a writable copy of the orig path */ (void) strncpy(buf, path_buf, MAXPATHLEN); /* get the dir from the orig path */ if ((path_dirname = vol_dirname(buf)) == NULL) { /* oh oh -- just use the link contents */ (void) strncpy(res_buf, link_buf, MAXPATHLEN); } else { /* concat the orig dir with the link path (if room) */ (void) concat_paths(res_buf, path_dirname, link_buf, NULL); } } }
Alignment merge_alignments(const Alignment& a1, const Alignment& a2, bool debug) { //cerr << "overlap is " << overlap << endl; // if either doesn't have a path, then treat it like a massive softclip if (debug) cerr << "merging alignments " << endl << pb2json(a1) << endl << pb2json(a2) << endl; // concatenate them Alignment a3; a3.set_sequence(a1.sequence() + a2.sequence()); *a3.mutable_path() = concat_paths(a1.path(), a2.path()); if (debug) cerr << "merged alignments, result is " << endl << pb2json(a3) << endl; return a3; }
/* * Convert path to an absolute path. * * If path starts with "/", return it unchanged. * If path starts with "./", prefix it with the current directory. * Otherwise, prefix it with the muforth build directory. */ static char* abs_path(char *dest, size_t destsize, char *path) { /* If path is absolute, just return it as is. */ if (path[0] == '/') return path; /* * If path starts with "./", strip the "./" and then prepend the * current working dir. If getcwd fails, or if the resulting path is * too long, return NULL. */ if (path[0] == '.' && path[1] == '/') { char pwd[PATH_MAX]; if (getcwd(pwd, PATH_MAX) == NULL) return NULL; return concat_paths(dest, destsize, pwd, path + 2); } /* Otherwise, rewrite path as MU_DIR/path */ return concat_paths(dest, destsize, MU_DIR, path); }
/* * This is an ON Consolidation Private interface. * * Print out the aliases available to the program user. Changes * depending in whether volume management is running. */ void _media_printaliases(void) { struct alias *s; DIR *dirp; struct dirent64 *dp; char pathbuf[MAXPATHLEN+1]; char *p; static const char *vold_root = NULL; if (vold_root == NULL) { vold_root = volmgt_root(); } if (!volmgt_running()) { /* no volume management */ for (s = device_aliases; *s->alias != NULLC; s++) { (void) printf("\t%s -> %s\n", s->alias, s->name); } return; } for (s = volmgt_aliases; *s->alias != NULLC; s++) { (void) printf("\t%s -> %s\n", s->alias, s->name); } (void) concat_paths(pathbuf, (char *)vold_root, ALIAS_DIR, NULL); if ((dirp = opendir(pathbuf)) == NULL) { return; } while (dp = readdir64(dirp)) { if (strcmp(dp->d_name, ".") == 0) { continue; } if (strcmp(dp->d_name, "..") == 0) { continue; } if ((p = media_findname(dp->d_name)) != NULL) { (void) printf("\t%s -> %s\n", dp->d_name, p); } } (void) closedir(dirp); }
char*find_file(const char*filename, char error) { include_dir_t*i = current_include_dirs; FILE*fi = 0; if(is_absolute(filename)) { FILE*fi = fopen(filename, "rb"); if(fi) { fclose(fi); return strdup(filename); } } else { if(!i && error) { as3_warning("Include directory stack is empty, while looking for file %s", filename); } while(i) { char*p = concat_paths(i->path, filename); fi = fopen(p, "rb"); if(fi) { fclose(fi); return p; } else { free(p); } i = i->next; } } if(!error) { return 0; } as3_error("Couldn't find file %s", filename); i = current_include_dirs; while(i) { fprintf(stderr, "include dir: %s\n", i->path); i = i->next; } return 0; }
int move_aout_libs(void) { int n, backedup = 0; char prefix[MAXPATHLEN], src[MAXPATHLEN]; struct stat st; n = handle_aout_libs(target_expand("/usr/lib"), LIB_COUNT, NULL); if (n <= 0) return n; /* * See if /emul/aout already exists, taking symlinks into * account. If so, no need to create it, just use it. */ if (target_realpath("/emul/aout", prefix) != NULL && stat(prefix, &st) == 0) goto domove; /* * See if /emul exists. If not, create it. */ if (target_realpath("/emul", prefix) == NULL || stat(prefix, &st) < 0) { strlcpy(prefix, target_expand("/emul"), sizeof(prefix)); if (lstat(prefix, &st) == 0) { run_program(0, "mv -f %s %s", prefix, target_expand("/emul.old")); backedup = 1; } scripting_fprintf(NULL, "mkdir %s\n", prefix); mkdir(prefix, 0755); } /* * Can use strcpy, target_expand has made sure it fits into * MAXPATHLEN. XXX all this copying is because concat_paths * returns a pointer to a static buffer. * * If an old aout link exists (apparently pointing to nowhere), * move it out of the way. */ strlcpy(src, concat_paths(prefix, "aout"), sizeof(src)); if (lstat(src, &st) == 0) { run_program(0, "mv -f %s %s", src, concat_paths(prefix, "aout.old")); backedup = 1; } /* * We have created /emul if needed. Since no previous /emul/aout * existed, we'll use a symbolic link in /emul to /usr/aout, to * avoid overflowing the root partition. */ strlcpy(prefix, target_expand("/usr/aout"), sizeof(prefix)); if (run_program(0, "mkdir -p %s", prefix)) abort_libupdate(); if (run_program(0, "ln -s %s %s", "/usr/aout", src)) abort_libupdate(); domove: /* * Rename etc and usr/lib if they already existed, so that we * do not overwrite old files. * * Then, move /etc/ld.so.conf to /emul/aout/etc/ld.so.conf, * and all a.out dynamic libraries from /usr/lib to * /emul/aout/usr/lib. This is where the a.out code in ldconfig * and ld.so respectively will find them. */ strlcpy(src, concat_paths(prefix, "usr/lib"), sizeof(src)); run_program(0, "mv -f %s %s", src, concat_paths(prefix, "usr/lib.old")); strlcpy(src, concat_paths(prefix, "etc/ld.so.conf"), sizeof(src)); run_program(0, "mv -f %s %s", src, concat_paths(prefix, "etc/ld.so.conf.old")); if (run_program(0, "mkdir -p %s ", concat_paths(prefix, "usr/lib"))) abort_libupdate(); if (run_program(0, "mkdir -p %s ", concat_paths(prefix, "etc"))) abort_libupdate(); strlcpy(src, target_expand("/etc/ld.so.conf"), sizeof(src)); if (run_program(0, "mv -f %s %s", src, concat_paths(prefix, "etc/ld.so.conf"))) abort_libupdate(); strlcpy(src, target_expand("/usr/lib"), sizeof(src)); n = handle_aout_libs(src, LIB_MOVE, concat_paths(prefix, "usr/lib")); if (run_program(0, "mkdir -p %s ", concat_paths(prefix, "usr/X11R6/lib"))) abort_libupdate(); strlcpy(src, target_expand("/usr/X11R6/lib"), sizeof(src)); handle_aout_x_libs(src, concat_paths(prefix, "usr/X11R6/lib")); if (backedup) { msg_display(MSG_emulbackup); process_menu(MENU_ok, NULL); } return n; }
merge_error_t merge_include_statement(stream_t* stream, const char* dir, size_t dir_len, const std::vector<merge_definition>& definitions, const std::vector<std::string>& already_included, merge_result& out) { stream_guard guard(stream); auto token = next_token(stream); if (token.type != tok_identifier || strncmp(token.str, "include", token.len) != 0) { return merge_skip; } token = next_token(stream); if (token.type != tok_string) { return merge_skip; } // TODO: Use hashes or hashmap maybe. auto name = std::string(token.str, token.len); if (std::find(already_included.begin(), already_included.end(), name) != already_included.end()) { fprintf(stderr, "Potential circular dependency detected: %s.\nUse flat hiearchies.\n", name.c_str()); return merge_error; } auto filename = concat_paths(dir, dir_len, name); if (filename.empty()) { fprintf(stderr, "Include path is empty.\n"); return merge_error; } auto file = read_file(filename.c_str(), /*nullterminate=*/false); if (!file.success) { // If we can't find the file, assume that it wasn't meant for merging. return merge_skip; } erase_undef_statements(file.data, definitions); for (auto& def : definitions) { // replace all occurences of def.name with def.value auto pos = file.data.begin(); for (;;) { pos = std::search(pos, file.data.end(), def.name, def.name + def.name_len); if (pos == file.data.end()) { break; } auto last = pos + def.name_len; // only replace if we matched to a whole identifier and not a substr if ((pos != file.data.begin() && isalnum(*(pos - 1))) || (last + 1 != file.data.end() && isalnum(*(last + 1)))) { pos = last; continue; } pos = file.data.erase(pos, last); pos = file.data.insert(pos, def.value, def.value + def.value_len); pos += def.value_len; } } if (std::find(out.merged_files.begin(), out.merged_files.end(), name) == out.merged_files.end()) { out.merged_files.emplace_back(std::move(name)); } auto indentation = get_indent(out.data.begin(), out.data.end()); indent_range(file.data, file.data.begin(), file.data.end(), indentation); out.data.insert(out.data.end(), file.data.begin(), file.data.end()); guard.dismiss(); return merge_ok; }
/* * Return a raw name, given a starting point. * * Assume: input string ptr is not null */ static char * media_findname_work(char *start) { extern char *vol_basename(char *); static void volmgt_deref_link(char *, char *, char *); char pathbuf[MAXPATHLEN+1]; char *rv; char *s; char linkbuf[MAXNAMELEN+1]; char *nameptr; struct stat64 sb; int n; int i; static const char *vold_root = NULL; static char vold_alias_dir[MAXPATHLEN+1]; char *res = NULL; DIR *dirp = NULL; struct dirent64 *dp; #ifdef DEBUG denter("media_findname_work(%s): entering\n", start); #endif if (vold_root == NULL) { vold_root = volmgt_root(); (void) concat_paths(vold_alias_dir, (char *)vold_root, (char *)ALIAS_DIR, NULL); } /* * if this is an absolute path name then * if it's a symlink deref it * if it's a raw device then we're done * else if it's a directory then look for a dev under it */ if (IS_ABS_PATH(start)) { /* try to get data on name passed in */ if (lstat64(start, &sb) < 0) { #ifdef DEBUG dprintf( "media_findname_work: lstat of \"%s\" (errno %d)\n", start, errno); #endif goto dun; /* error exit */ } /* * if is this a link to something else (e.g. ".../floppy0") * and it's in the volmgt namespace, then deref it */ if (S_ISLNK(sb.st_mode) && (strncmp(start, vold_alias_dir, strlen(vold_alias_dir)) == 0)) { /* it's a symlink */ if ((n = readlink(start, linkbuf, MAXNAMELEN)) <= 0) { /* we can't read the link */ #ifdef DEBUG dprintf( "media_findname_work: readlink(\"%s\") failed (errno %d)\n", start, errno); #endif goto dun; /* error exit */ } linkbuf[n] = NULLC; /* dereference the link */ volmgt_deref_link(pathbuf, start, linkbuf); /* stat where "start" pointed at */ if (stat64(pathbuf, &sb) < 0) { #ifdef DEBUG dprintf( "media_findname_work: stat failed on \"%s\" (errno %d)\n", pathbuf, errno); #endif goto dun; /* error exit */ } nameptr = pathbuf; } else { nameptr = start; } /* do we already have a char-spcl device ?? */ if (S_ISCHR(sb.st_mode)) { /* * absoluate pathname of a char-spcl device passed in */ res = strdup(nameptr); goto dun; /* success */ } /* not a char-spcl device -- is it a dir ?? */ if (S_ISDIR(sb.st_mode)) { /* open the dir and find first char-spcl device */ if ((s = getrawpart0(nameptr)) != NULL) { /* * absoluate pathname to a directory passed * in, under which there is at least one * char-spcl device */ free(s); res = strdup(nameptr); goto dun; /* success */ } } /* * try to get the char-spcl name if this is a blk-spcl * * XXX: shouldn't we ensure this is a blk spcl device? */ rv = volmgt_getfullrawname(nameptr); if ((rv == NULL) || (*rv == NULLC)) { goto dun; /* error exit */ } /* stat the fullrawname device (to see if it's char-spcl) */ if (stat64(rv, &sb) < 0) { #ifdef DEBUG dprintf( "media_findname_work: stat of \"%s\" (errno %d)\n", rv, errno); #endif goto dun; /* error exit */ } /* have we found the char-spcl device ?? */ if (S_ISCHR(sb.st_mode)) { /* * absolute pathname to block device supplied and * converted to an absoluate pathname to a char device */ res = rv; /* already malloc'ed */ goto dun; /* success */ } /* * fullrawname not a char-spcl device -- is it a dir ?? * * XXX: didn't we already check for a directory name * being supplied above? */ if (S_ISDIR(sb.st_mode)) { /* open dir and find first char-spcl device */ if ((s = getrawpart0(rv)) != NULL) { /* * the absolute pathname of directory * containing at least one char-spcl device * was passed in */ free(s); res = strdup(rv); goto dun; /* success */ } } /* having a full pathname didn't help us */ goto dun; /* failure -- pathname not found */ } /* * Ok, now we check to see if it's an alias. * Note here that in the case of an alias, we prefer * to return what the alias (symbolic link) points * at, rather than the symbolic link. Makes for * nicer printouts and such. */ (void) concat_paths(pathbuf, vold_alias_dir, start, NULL); #ifdef DEBUG dprintf("media_findname_work: looking for \"%s\"\n", pathbuf); #endif if (stat64(pathbuf, &sb) == 0) { #ifdef DEBUG dprintf("media_findname_work: is \"%s\" a chr-spcl dev?\n", pathbuf); #endif /* is this a char-spcl device ?? */ if (S_ISCHR(sb.st_mode)) { /* it's probably a link, so ... */ if ((n = readlink(pathbuf, linkbuf, MAXNAMELEN)) <= 0) { /* * error (since we are in the symlink * directory) not a link, but just punt * anyway */ res = strdup(pathbuf); } else { /* it was a link */ linkbuf[n] = NULLC; res = strdup(linkbuf); } goto dun; /* success */ } #ifdef DEBUG dprintf("media_findname_work: not chr-spcl -- is it a dir?\n"); #endif /* not a char-spcl device -- is it a dir ?? */ if (S_ISDIR(sb.st_mode)) { /* it's probably a link, so ... */ if ((n = readlink(pathbuf, linkbuf, MAXNAMELEN)) <= 0) { /* * error, but just punt anyway */ nameptr = pathbuf; s = getrawpart0(pathbuf); } else { /* it was a link */ linkbuf[n] = NULLC; /* open dir, finding first char-spcl dev */ nameptr = linkbuf; s = getrawpart0(linkbuf); } if (s != NULL) { free(s); res = strdup(nameptr); goto dun; } } } /* * check all aliases in the alias dir, to see if any match */ if ((dirp = opendir(vold_alias_dir)) == NULL) { goto try_hack; } while (dp = readdir64(dirp)) { /* skip uninteresting entries */ if (strcmp(dp->d_name, ".") == 0) { continue; } if (strcmp(dp->d_name, "..") == 0) { continue; } #ifdef DEBUG dprintf("media_findname_work: scanning alias \"%s\" ...\n", dp->d_name); #endif /* * open the link and see if it points at our entry */ (void) concat_paths(pathbuf, vold_alias_dir, dp->d_name, NULL); if ((n = readlink(pathbuf, linkbuf, MAXNAMELEN)) <= 0) { #ifdef DEBUG dprintf( "media_findname_work: readlink(\"%s\") failed (errno %d)\n", pathbuf, errno); #endif continue; } linkbuf[n] = NULLC; #ifdef DEBUG dprintf("media_findname_work: scanning link \"%s\" ...\n", linkbuf); #endif if (strcmp(vol_basename(linkbuf), start) == 0) { /* we *think* we've found a match */ if (stat64(linkbuf, &sb) == 0) { if (S_ISCHR(sb.st_mode)) { res = strdup(linkbuf); goto dun; } if (S_ISDIR(sb.st_mode)) { res = getrawpart0(linkbuf); if (res != NULL) { free(res); res = strdup(linkbuf); } goto dun; } } } } try_hack: /* * Ok, well maybe that's not it. Let's try the * hackname alias. */ /* * This creates the "hack" name. The model * is that xx# has the alias xx. So, cdrom# * and floppy# (the most frequent case) can * be referred to as cdrom and floppy. * We poke at what we consider to be a reasonable number of * devices (currently 5) before giving up. */ for (i = 0; i < HACKNAME_MAX; i++) { char num_buf[NUMBUF_SZ]; (void) sprintf(num_buf, "%d", i); (void) concat_paths(pathbuf, vold_alias_dir, start, num_buf); if (stat64(pathbuf, &sb) == 0) { /* is it a char-spcl device ?? */ if (S_ISCHR(sb.st_mode)) { /* it's probably a link, so... */ if ((n = readlink(pathbuf, linkbuf, MAXNAMELEN)) <= 0) { /* it wasn't a link */ res = strdup(pathbuf); } else { /* it was a link */ linkbuf[n] = NULLC; res = strdup(linkbuf); } goto dun; } /* not a char-spcl device -- is it a dir ?? */ if (S_ISDIR(sb.st_mode)) { /* it's probably a link, so ... */ if ((n = readlink(pathbuf, linkbuf, MAXNAMELEN)) <= 0) { /* get fist char-spcl dev in dir */ nameptr = pathbuf; s = getrawpart0(pathbuf); } else { /* it was a link */ linkbuf[n] = NULLC; /* get fist char-spcl dev in dir */ nameptr = linkbuf; s = getrawpart0(linkbuf); } if (s != NULL) { free(s); res = strdup(nameptr); goto dun; } } } } #ifdef DEBUG dprintf("media_findname_work: %s didn't match any test!\n", start); #endif dun: if (dirp != NULL) { (void) closedir(dirp); } #ifdef DEBUG dexit("media_findname_work: returning \"%s\"\n", res ? res : "<null ptr>"); #endif return (res); }