static int get_pathconf_name_max(char *dir, long *name_max) { #if defined(HAVE_PATHCONF) *name_max = pathconf(dir, _PC_NAME_MAX); return 0; #else errno = ENOSYS; return -1; #endif /* HAVE_PATHCONF */ }
int main(int argc, char *argv[]) { char *progname; char *ttyname; int fd; int dofree; dofree = 0; progname = basename(argv[0]); if (argc != 2) errx(EX_USAGE, "usage: %s <ttyname>\n", progname); if (geteuid() != 0) errx(EX_NOPERM, "Sorry\n"); if (argv[1][0] == '/') { ttyname = argv[1]; } else { size_t len, maxpath, result; len = strlen(argv[1]) + sizeof(DEVPATHNAME) + 1; maxpath = pathconf(DEVPATHNAME, _PC_PATH_MAX); if (len > maxpath) { warnc(ENAMETOOLONG, ttyname); exit(EX_DATAERR); } ttyname = malloc(len); if (ttyname == NULL) { warnc(ENOMEM, "malloc"); exit(EX_OSERR); } dofree = 1; result = snprintf(ttyname, len, "%s/%s", DEVPATHNAME, argv[1]); if (result >= len) warnc(ENOMEM, "snprintf"); } fd = open(ttyname, O_RDWR); if (fd == -1) { warnc(errno, "open %s", ttyname); if (dofree) free(ttyname); exit(EX_OSERR); } if (0 != ioctl(fd, TIOCNXCL, 0)) warnc(errno, "ioctl TIOCNXCL %s", ttyname); if (dofree) free(ttyname); exit(0); }
static ssize_t uv__fs_pathmax_size(const char* path) { ssize_t pathmax; pathmax = pathconf(path, _PC_PATH_MAX); if (pathmax == -1) pathmax = UV__FS_PATH_MAX; return pathmax; }
size_t sml_pathconf(char *file, size_t name) { size_t res; int n = sml_pathconf_number(name); errno = 0; res = pathconf(file, n); if (res == -1 && errno == 0) res = -2; return res; }
TEST(UNISTD_TEST, pathconf_fpathconf) { TemporaryFile tf; long rc = 0L; // As a file system's block size is always power of 2, the configure values // for ALLOC and XFER should be power of 2 as well. rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN); ASSERT_TRUE(rc > 0 && powerof2(rc)); rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE); ASSERT_TRUE(rc > 0 && powerof2(rc)); rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN); ASSERT_TRUE(rc > 0 && powerof2(rc)); rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN); ASSERT_TRUE(rc > 0 && powerof2(rc)); rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE); ASSERT_TRUE(rc > 0 && powerof2(rc)); rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN); ASSERT_TRUE(rc > 0 && powerof2(rc)); }
int path_max( const char * filename ) { long result; if( !filename ) filename = "/"; errno = 0; result = pathconf( filename, _PC_PATH_MAX ); if( result < 0 ) { if( errno ) result = 256; else result = 1024; } else if( result < 256 ) result = 256; return result; }
static void test_chown(void) { //#define TEST_CHOWN #if defined(TEST_CHOWN) printf("LINK_MAX = %ld\n", pathconf("/", _PC_LINK_MAX)); print(sysconf(_POSIX_CHOWN_RESTRICTED)); #endif //TEST_CHOWN }
extern "C" int64_t SystemNative_GetMaximumPath() { int64_t result = pathconf("/", _PC_PATH_MAX); if (result == -1) { result = PATH_MAX; } return result; }
static int jlog_logio_cleanse(noit_log_stream_t ls) { jlog_asynch_ctx *actx; jlog_ctx *log; DIR *d; struct dirent *de, *entry; int cnt = 0; char path[PATH_MAX], current_log[9]; int size = 0; actx = (jlog_asynch_ctx *)ls->op_ctx; if(!actx) return -1; log = actx->log; if(!log) return -1; if(jlog_lspath_to_fspath(ls, path, sizeof(path), NULL) <= 0) return -1; d = opendir(path); snprintf(current_log, sizeof(current_log), "%08x", log->current_log); #ifdef _PC_NAME_MAX size = pathconf(path, _PC_NAME_MAX); if(size < 0) size = PATH_MAX + 128; #endif size = MIN(size, PATH_MAX + 128); de = alloca(size); if(!d) return -1; while(portable_readdir_r(d, de, &entry) == 0 && entry != NULL) { u_int32_t logid; /* the current log file isn't a deletion target. period. */ if(is_datafile(entry->d_name, &logid) && strcmp(current_log, entry->d_name)) { int rv; struct stat st; char fullfile[PATH_MAX]; char fullidx[PATH_MAX]; snprintf(fullfile, sizeof(fullfile), "%s/%s", path, entry->d_name); snprintf(fullidx, sizeof(fullidx), "%s/%s" INDEX_EXT, path, entry->d_name); /* coverity[fs_check_call] */ while((rv = stat(fullfile, &st)) != 0 && errno == EINTR); if(rv == 0) { int readers; readers = __jlog_pending_readers(log, logid); if(readers == 0) { /* coverity[toctou] */ unlink(fullfile); unlink(fullidx); } } } } closedir(d); return cnt; }
static bool is_case_sensitive_filesystem(const char *path) { #ifdef __APPLE__ return pathconf(path, _PC_CASE_SENSITIVE); #elif defined(_WIN32) unused_parameter(path); return false; #else unused_parameter(path); return true; #endif }
/* ------------------------------------------------------------------ * Opens and iterates through a given directory, operating on allowed * files found within it. . and .. are skipped, as are files beginning * with . unless it's been enabled in the flags. All other entries, * both files and directories, are thrown to process_entry to determine * whether they're a file/directory/link, and to read/explore them if * appropriate. If threads are being used, they wait for their children * threads to terminate after they themselves are done with their work. * * flags: The usual I/O option flags. * list_head: A list of already visited directories. Always will have * the working directory low was called from as the tail, * and will always have the current directory being explored * as the head (so the src = list_head->next->path assignment * is always safe). * fullpath: The realpath of the current directory being explored. * dir_depth: Passed to process_entry. * ------------------------------------------------------------------ */ int explore_dir(params *P, visited_dir *list_head, char *nextpath, int dir_depth) { DIR *d; struct dirent *entry; struct dirent **done; int len; if ( (d = opendir(nextpath)) == 0) { return print_file_error(P, errno, nextpath); } if (((P->max_dir_depth) - dir_depth) > read_stat(P, t_ddepth)) update_stat(P, t_ddepth, ((P->max_dir_depth) - dir_depth)); len = offsetof(struct dirent, d_name) + pathconf(nextpath, _PC_NAME_MAX) + 1; if ((entry = malloc(len)) == NULL || (done = malloc(sizeof(struct dirent*))) == NULL) { fprintf(stderr, "Malloc failed in explore_dir.\n"); return -1; } done = &entry; while ( (readdir_r(d, entry, done)) == 0 && (*done != NULL)) { /* don't process '.' or '..' in a directory! */ if ( (strcmp(entry->d_name, THIS_DIR) == 0) || (strcmp(entry->d_name, PARENT_DIR) == 0)) continue; /* do all files but ones beginning with a dot, * unless we've enabled it! */ if (entry->d_name[0] == '.') { if (enabled(P, READ_DOT_FILES)) { process_entry(P, list_head, entry->d_name, nextpath, dir_depth, 0); } else inc_stat(P, t_dotfiles, 1); } else process_entry(P, list_head, entry->d_name, nextpath, dir_depth, 0); } closedir(d); wait_for_children(list_head); free(nextpath); return 0; }
static int read_syshugepages(const char* path, const char* node) { static const char hugepages_dir[] = "hugepages"; DIR *dir; struct dirent *result; char path2[PATH_MAX]; struct entry_info e_info; long lim; dir = opendir(path); if (dir == NULL) { ERROR("%s: cannot open directory %s", g_plugin_name, path); return -1; } errno = 0; if ((lim = pathconf(path, _PC_NAME_MAX)) == -1) { /* Limit not defined if errno == 0, otherwise error */ if (errno != 0) { ERROR("%s: pathconf failed", g_plugin_name); closedir(dir); return -1; } else { lim = PATH_MAX; } } /* read "hugepages-XXXXXkB" entries */ while ((result = readdir(dir)) != NULL) { if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir)-1)) { /* not node dir */ errno = 0; continue; } /* /sys/devices/system/node/node?/hugepages/ */ ssnprintf(path2, (size_t) lim, "%s/%s", path, result->d_name); e_info.d_name = result->d_name; e_info.node = node; walk_directory(path2, read_hugepage_entry, &e_info, 0); errno = 0; } /* Check if NULL return from readdir() was an error */ if (errno != 0) { ERROR("%s: readdir failed", g_plugin_name); closedir(dir); return -1; } closedir(dir); return 0; }
static int get_path_max(const char *path) { #ifdef PATH_MAX return PATH_MAX; #else int path_max = pathconf(path, _PC_PATH_MAX); if (path_max <= 0) path_max = 4096; return path_max; #endif }
//' Retrieve one configuration setting //' //' This functions returns the configuration setting for a given input. //' in a data.frame object. The system-level functions \code{sysconf}, //' \code{pathconf} and \code{confstr} provide the underlying information. //' //' @title Return a System Configuration Setting //' @param var An character object specifying a value for which configuration //' is queried. //' @param path An optional character object specifying a path. Default is the //' current directory. //' @return A result value corresponding to the requested setting. The return //' type can be either integer for a numeric value, character for text or NULL //' in case to value could be retrieved. //' @author Dirk Eddelbuettel //' @seealso \code{\link{getAll}} //' @examples //' if (Sys.info()[["sysname"]] != "SunOS") { //' getConfig("_NPROCESSORS_CONF") # number of processor //' getConfig("LEVEL1_ICACHE_SIZE") # leve1 cache size //' getConfig("GNU_LIBC_VERSION") # libc version //' } // [[Rcpp::export]] SEXP getConfig(const std::string & var, const std::string & path = ".") { const char *vararg = var.c_str(); const struct conf *c; for (c = vars; c->name != NULL; ++c) { if (strcmp (c->name, vararg) == 0 || (strncmp (c->name, "_POSIX_", 7) == 0 && strcmp (c->name + 7, vararg) == 0)) { long int value; size_t clen; char *cvalue; switch (c->calltype) { case PATHCONF: value = pathconf (path.c_str(), c->call_name); if (value == -1) { Rcpp::stop("Error with path arg: %s", path.c_str()); } else { return Rcpp::wrap(value); } case SYSCONF: value = sysconf (c->call_name); if (value == -1l) { #if defined(_SC_UINT_MAX) && defined(_SC_ULONG_MAX) if (c->call_name == _SC_UINT_MAX || c->call_name == _SC_ULONG_MAX) { return Rcpp::wrap(value); } else { #endif Rcpp::stop("undefined"); #if defined(_SC_UINT_MAX) && defined(_SC_ULONG_MAX) } #endif } else { return Rcpp::wrap(value); } case CONFSTR: clen = confstr (c->call_name, (char *) NULL, 0); cvalue = R_alloc(clen, sizeof(char)); if (cvalue == NULL) { Rcpp::stop("memory exhausted"); } if (confstr(c->call_name, cvalue, clen) != clen) { Rcpp::stop("Error with confstr"); } return Rcpp::wrap(std::string(cvalue)); } } } // fallback return R_NilValue; }
int pathconf_path_max( size_t* length ) { #if defined( PATH_MAX ) return PATH_MAX; #else /* defined( PATH_MAX ) */ const int pathconf_result = pathconf("." , _PC_PATH_MAX ); if( length ){ *length = (size_t)(( pathconf_result ) < 0 ? 4098 : pathconf_result ); } return pathconf_result; #endif /* defined( PATH_MAX ) */ }
//' Retrieve all configuration settings //' //' This functions returns all configuration settings which can be queried //' in a data.frame object. The system-level functions \code{sysconf}, //' \code{pathconf} and \code{confstr} provide all the underlying information. //' //' @title Return all System Configuration Settings //' @param path An optional character object specifying a path. Default is the //' current directory. //' @return A data.frame with three colums for key, value and (source) type. //' Not all keys return a value; in those cases an empty string is returned. //' Type is one of \code{path}, \code{sys} and \code{conf} and signals how the //' value was obtained. //' @author Dirk Eddelbuettel //' @seealso \code{\link{getConfig}} //' @examples //' if (Sys.info()[["sysname"]] != "SunOS") { //' head(getAll(), 30) //' subset(getAll(), type=="path") //' } // [[Rcpp::export]] Rcpp::DataFrame getAll(const std::string & path = ".") { const struct conf *c; size_t clen; long int value; char *cvalue; std::vector<std::string> vname, vvalue, vtype; char buf[256]; for (c = vars; c->name != NULL; ++c) { //printf("%-35s", c->name); vname.push_back(std::string(c->name).c_str()); snprintf(buf, 1, "%s", ""); // fallback switch (c->calltype) { case PATHCONF: value = pathconf (path.c_str(), c->call_name); if (value != -1) { snprintf(buf, 255, "%ld", value); } vtype.push_back("path"); break; case SYSCONF: value = sysconf (c->call_name); if (value == -1l) { #if defined(_SC_UINT_MAX) && defined(_SC_ULONG_MAX) if (c->call_name == _SC_UINT_MAX || c->call_name == _SC_ULONG_MAX) snprintf(buf, 255, "%lu", value); #endif } else { snprintf(buf, 255, "%ld", value); } vtype.push_back("sys"); break; case CONFSTR: clen = confstr (c->call_name, (char *) NULL, 0); cvalue = R_alloc(clen, sizeof(char)); if (cvalue == NULL) { Rcpp::stop("Memory allocation error"); } if (confstr (c->call_name, cvalue, clen) != clen) { Rcpp::stop("Confstr error"); } snprintf(buf, 255, "%.*s", (int) clen, cvalue); vtype.push_back("conf"); break; } vvalue.push_back(buf); } return Rcpp::DataFrame::create(Rcpp::Named("key") = vname, Rcpp::Named("value") = vvalue, Rcpp::Named("type") = vtype); }
static int load_plugins(void) { const char *fsdir = getenv("FSIMAGE_FSDIR"); struct dirent *dp = NULL; struct dirent *dpp; DIR *dir = NULL; char *tmp = NULL; size_t name_max; int err; int ret = -1; if (fsdir == NULL) fsdir = FSIMAGE_FSDIR; if ((name_max = pathconf(fsdir, _PC_NAME_MAX)) == -1) goto fail; if ((tmp = malloc(name_max + 1)) == NULL) goto fail; if ((dp = malloc(sizeof (struct dirent) + name_max + 1)) == NULL) goto fail; if ((dir = opendir(fsdir)) == NULL) goto fail; bzero(dp, sizeof (struct dirent) + name_max + 1); while (readdir_r(dir, dp, &dpp) == 0 && dpp != NULL) { if (strcmp(dpp->d_name, ".") == 0) continue; if (strcmp(dpp->d_name, "..") == 0) continue; (void) snprintf(tmp, name_max, "%s/%s/fsimage.so", fsdir, dpp->d_name); if (init_plugin(tmp) != 0) goto fail; } ret = 0; fail: err = errno; if (dir != NULL) (void) closedir(dir); free(tmp); free(dp); errno = err; return (ret); }
int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path, uv_fs_cb cb) { ssize_t size; int status; char* buf; status = -1; uv_fs_req_init(loop, req, UV_FS_READLINK, path, cb); if (cb) { if ((req->eio = eio_readlink(path, EIO_PRI_DEFAULT, uv__fs_after, req))) { uv_ref(loop); return 0; } else { uv_err_new(loop, ENOMEM); return -1; } } else { /* pathconf(_PC_PATH_MAX) may return -1 to signify that path * lengths have no upper limit or aren't suitable for malloc'ing. */ if ((size = pathconf(path, _PC_PATH_MAX)) == -1) { #if defined(PATH_MAX) size = PATH_MAX; #else size = 4096; #endif } if ((buf = malloc(size + 1)) == NULL) { uv_err_new(loop, ENOMEM); return -1; } if ((size = readlink(path, buf, size)) == -1) { req->errorno = errno; req->result = -1; free(buf); } else { /* Cannot conceivably fail since it shrinks the buffer. */ buf = realloc(buf, size + 1); buf[size] = '\0'; req->result = 0; req->ptr = buf; } return 0; } assert(0 && "unreachable"); }
/* * Set an ACL, translates acl to ace_t when appropriate. */ static int cacl_set(acl_inp *acl_inp, acl_t *aclp, int type) { int error = 0; int acl_flavor_target; struct stat64 statbuf; int stat_error; int isdir; if (type == ACL_PATH) { stat_error = stat64(acl_inp->file, &statbuf); if (stat_error) return (-1); acl_flavor_target = pathconf(acl_inp->file, _PC_ACL_ENABLED); } else { stat_error = fstat64(acl_inp->fd, &statbuf); if (stat_error) return (-1); acl_flavor_target = fpathconf(acl_inp->fd, _PC_ACL_ENABLED); } /* * If target returns an error or 0 from pathconf call then * fall back to UFS/POSIX Draft interface. * In the case of 0 we will then fail in either acl(2) or * acl_translate(). We could erroneously get 0 back from * a file system that is using fs_pathconf() and not answering * the _PC_ACL_ENABLED question itself. */ if (acl_flavor_target == 0 || acl_flavor_target == -1) acl_flavor_target = _ACL_ACLENT_ENABLED; isdir = S_ISDIR(statbuf.st_mode); if ((error = acl_translate(aclp, acl_flavor_target, isdir, statbuf.st_uid, statbuf.st_gid)) != 0) { return (error); } if (type == ACL_PATH) { error = acl(acl_inp->file, (aclp->acl_type == ACE_T) ? ACE_SETACL : SETACL, aclp->acl_cnt, aclp->acl_aclp); } else { error = facl(acl_inp->fd, (aclp->acl_type == ACE_T) ? ACE_SETACL : SETACL, aclp->acl_cnt, aclp->acl_aclp); } return (error); }
static void check_extension (char *file, size_t filelen, char e) { char *base = last_component (file); size_t baselen = base_len (base); size_t baselen_max = HAVE_LONG_FILE_NAMES ? 255 : NAME_MAX_MINIMUM; if (HAVE_DOS_FILE_NAMES || NAME_MAX_MINIMUM < baselen) { /* The new base name is long enough to require a pathconf check. */ long name_max; /* Temporarily modify the buffer into its parent directory name, invoke pathconf on the directory, and then restore the buffer. */ char tmp[sizeof "."]; memcpy (tmp, base, sizeof "."); strcpy (base, "."); errno = 0; name_max = pathconf (file, _PC_NAME_MAX); if (0 <= name_max || errno == 0) { long size = baselen_max = name_max; if (name_max != size) baselen_max = SIZE_MAX; } memcpy (base, tmp, sizeof "."); } if (HAVE_DOS_FILE_NAMES && baselen_max <= 12) { /* Live within DOS's 8.3 limit. */ char *dot = strchr (base, '.'); if (!dot) baselen_max = 8; else { char const *second_dot = strchr (dot + 1, '.'); baselen_max = (second_dot ? second_dot - base : dot + 1 - base + 3); } } if (baselen_max < baselen) { baselen = file + filelen - base; if (baselen_max <= baselen) baselen = baselen_max - 1; base[baselen] = e; base[baselen + 1] = '\0'; } }
u_char *unchroot_filename(u_char *filename, const u_char *chrootdir) { // Chroot filename. Relative paths default to the home directory anyway, so these may be automatically // chrooted to the users home directory. Absolite paths are concatenated with the home directory, as they are assumed to // already by chrooted (and if they're not, it is an attempt to break out the chroot). u_char *rewrite_filename, *translated_path; int file_len; int chrootdir_len = strlen((char*) chrootdir); int maxpathlen; debug("%s: Request to unchroot path: %s", __FUNCTION__, filename); // Allocate space for new filename file_len = strlen((char*) filename); file_len += chrootdir_len; if ((char) *filename != '/') file_len++; /* additional slash */ rewrite_filename = xmalloc(file_len + 1); /* for the null byte */ // Chroot directory strlcpy((char*) rewrite_filename, (char*) chrootdir, chrootdir_len + 1); // Trailing slash for relative paths if ((char) *filename != '/') strncat((char*) rewrite_filename, "/", 1); /* additional slash */ // Actual path strncat((char*) rewrite_filename, (char*) filename, (file_len - chrootdir_len)); // Validate with realpath maxpathlen = pathconf("/", _PC_PATH_MAX); maxpathlen++; translated_path = xmalloc(maxpathlen + 1); realpath((char*) rewrite_filename, (char*) translated_path); free(rewrite_filename); // Verify translated path is within chroot path if ((void*) strstr((char*) translated_path, (char*) chrootdir) != (void*) translated_path) { // Path has fallen outside translated path, rewrite to home directory debug("%s: Path escapes chroot root path. Defaulting to chroot root path", __FUNCTION__); free(translated_path); translated_path = (u_char*) chrootdir; } // Replace original with new xfree(filename); filename = translated_path; debug("%s: Unchrooted path: %s", __FUNCTION__, filename); return filename; }
static long get_max_pathname(const char *path) { long max_path; errno = 0; max_path = pathconf(path, _PC_PATH_MAX); if (max_path == -1) { if (errno == 0) max_path = 4096; /* guess */ else max_path = 4096; /* bury the error -- return guess here, too */ } return max_path + 1; }
static int print_pathconf(const struct conf_variable *cp, const char *pathname) { long val; errno = 0; if ((val = pathconf(pathname, (int)cp->value)) == -1) { if (all && errno == EINVAL) return 0; if (errno != 0) err(EXIT_FAILURE, "pathconf(%s, %ld)", pathname, cp->value); return -1; } print_long(cp->name, val); return 0; }
int main (){ char *cwd; if ((cwd = ps_test_getcwd( NULL, pathconf(".",_PC_PATH_MAX))) != NULL){ puts(cwd); }else{ perror("Klaida iškviečiant getcwd() funkcija"); } puts("Failu sarasas: "); ps_test_list_dir(cwd); free(cwd); return 0; }
CaseSensitivity getCaseSensitivityForPath(const char *path) { #ifdef __APPLE__ return pathconf(path, _PC_CASE_SENSITIVE) ? CaseSensitivity::CaseSensitive : CaseSensitivity::CaseInSensitive; #elif defined(_WIN32) unused_parameter(path); return CaseSensitivity::CaseInSensitive; #else unused_parameter(path); return CaseSensitivity::CaseSensitive; #endif }
/* * Initialize the find files "global" variables */ FF_PKT *init_find_files() { FF_PKT *ff; ff = (FF_PKT *)malloc(sizeof(FF_PKT)); memset(ff, 0, sizeof(FF_PKT)); /* Get system path and filename maximum lengths */ path_max = pathconf(".", _PC_PATH_MAX); if (path_max < 1024) { path_max = 1024; } name_max = pathconf(".", _PC_NAME_MAX); if (name_max < 1024) { name_max = 1024; } path_max++; /* add for EOS */ name_max++; /* add for EOS */ return ff; }
// Documented in header. char *utilCwd(void) { // The maximum buffer size is determined per the recommendation // in the Posix docs for `getcwd`. long maxSize = pathconf(".", _PC_PATH_MAX); char buf[maxSize + 1]; if (getcwd(buf, maxSize) == NULL) { die("Trouble with `getcwd`: %s", strerror(errno)); } return utilStrdup(buf); }
int PathConfTest (void) { int error = 0, retval = FALSE; error = pathconf("thisfiledoesnotexist", _PC_LINK_MAX); if (error == -1) { error = pathconf("testfile1.tst", _PC_LINK_MAX); if (error != -1) retval = TRUE; else retval = FALSE; } else retval = FALSE; /* assert (retval == TRUE);*/ return(retval); }
void eval_macro(char *macro, char *prompt) { if (!strcmp(macro, "pwd")) { // @pwd@ macro char *cwd; char *ptr; long size = pathconf(".", _PC_PATH_MAX); if ((cwd = (char *) malloc((size_t) size)) != NULL) { ptr = getcwd(cwd, (size_t) size); } else { ptr = "/somewhere?"; } if (!strcmp(ptr, getenv("HOME"))) { ptr = "~"; } strcat(prompt, ptr); } else if (!strncmp("date", macro, 4)) { // @date@ macro /* * %a -- short day of week, %A -- long day of week * %b -- short month, %B -- long month * %m -- month number, %e -- day of month * %H -- hour, %M -- minutes, %S -- seconds * %Z -- time zone * %Y -- four digit year, %y -- two digit year * probably more... */ char format[strlen(macro) - 4]; strcpy(format, "date +'"); strcat(format, ¯o[4]); strcat(format, "'"); FILE *fp = popen(format, "r"); if (fp != NULL) { char data[256]; while (fgets(data, sizeof data, fp)) {} pclose(fp); strncat(prompt, data, strlen(data) - 1); } } else if (!strcmp(macro, "user")) { // @user@ macro char *data = getlogin(); strcat(prompt, data); } else if (!strcmp(macro, "userid")) { // @userid@ macro char *data = calloc(1, sizeof (char *)); sprintf(data, "%d", getuid()); strcat(prompt, data); free(data); } else if (!strcmp(macro, "groupid")) { // @groupid@ macro char *data = calloc(1, sizeof (char *)); sprintf(data, "%d", getgid()); strcat(prompt, data); free(data); } }
static int jlog_logio_cleanse(noit_log_stream_t ls) { asynch_log_ctx *actx; jlog_ctx *log; DIR *d; struct dirent *de, *entry; int cnt = 0, readers; u_int32_t earliest = 0; char path[PATH_MAX]; int size = 0; actx = (asynch_log_ctx *)ls->op_ctx; if(!actx) return -1; log = actx->userdata; if(!log) return -1; if(jlog_lspath_to_fspath(ls, path, sizeof(path), NULL) <= 0) return -1; d = opendir(path); /* populate earliest, if this fails, we assume */ readers = jlog_pending_readers(log, log->current_log, &earliest); if(readers < 0) return -1; if(readers == 0) return 0; #ifdef _PC_NAME_MAX size = pathconf(path, _PC_NAME_MAX); if(size < 0) size = PATH_MAX + 128; #endif size = MIN(size, PATH_MAX + 128); de = alloca(size); if(!d) return -1; while(portable_readdir_r(d, de, &entry) == 0 && entry != NULL) { u_int32_t logid; /* the current log file isn't a deletion target. period. */ if(is_datafile(entry->d_name, &logid) && /* make sure it is a datafile */ logid < earliest && /* and that is older enough */ logid != log->current_log) { /* and that isn't current */ char fullfile[PATH_MAX]; char fullidx[PATH_MAX]; snprintf(fullfile, sizeof(fullfile), "%s/%s", path, entry->d_name); snprintf(fullidx, sizeof(fullidx), "%s/%s" INDEX_EXT, path, entry->d_name); (void)unlink(fullfile); (void)unlink(fullidx); /* this might fail ENOENT; don't care */ cnt++; } } closedir(d); return cnt; }