本文整理汇总了C++中confstr函数的典型用法代码示例。如果您正苦于以下问题:C++ confstr函数的具体用法?C++ confstr怎么用?C++ confstr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了confstr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: confstr
/*
* Class: sun_tools_attach_LinuxVirtualMachine
* Method: isLinuxThreads
* Signature: ()V
*/
JNIEXPORT jboolean JNICALL Java_sun_tools_attach_LinuxVirtualMachine_isLinuxThreads
(JNIEnv *env, jclass cls)
{
# ifndef _CS_GNU_LIBPTHREAD_VERSION
# define _CS_GNU_LIBPTHREAD_VERSION 3
# endif
size_t n;
char* s;
jboolean res;
n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
if (n <= 0) {
/* glibc before 2.3.2 only has LinuxThreads */
return JNI_TRUE;
}
s = (char *)malloc(n);
if (s == NULL) {
JNU_ThrowOutOfMemoryError(env, "malloc failed");
return JNI_TRUE;
}
confstr(_CS_GNU_LIBPTHREAD_VERSION, s, n);
/*
* If the LIBPTHREAD version include "NPTL" then we know we
* have the new threads library and not LinuxThreads
*/
res = (jboolean)(strstr(s, "NPTL") == NULL);
free(s);
return res;
}
示例2: main
/*
* Test pthread creation at different thread priorities.
*/
int main(int argc, char *argv[])
{
char *pathbuf;
size_t n;
rt_init("h", parse_args, argc, argv);
n = confstr(_CS_GNU_LIBC_VERSION, NULL, (size_t) 0);
pathbuf = malloc(n);
if (!pathbuf)
abort();
confstr(_CS_GNU_LIBC_VERSION, pathbuf, n);
printf("LIBC_VERSION: %s\n", pathbuf);
free(pathbuf);
n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, (size_t) 0);
pathbuf = malloc(n);
if (!pathbuf)
abort();
confstr(_CS_GNU_LIBPTHREAD_VERSION, pathbuf, n);
printf("LIBPTHREAD_VERSION: %s\n", pathbuf);
free(pathbuf);
if (sysconf(_SC_THREAD_PRIO_INHERIT) == -1)
printf("No Prio inheritance support\n");
printf("Prio inheritance support present\n");
return 0;
}
示例3: printvar
static void
printvar (const struct conf_variable *cp, const char *pathname)
{
size_t slen;
char *sval;
long val;
switch (cp->type)
{
case CONSTANT:
print_longvar (cp->name, cp->value);
break;
case CONFSTR:
errno = 0;
slen = confstr ((int) cp->value, NULL, 0);
if (slen == 0)
{
if (errno == 0)
print_strvar (cp->name, "undefined");
return;
}
if ((sval = malloc (slen)) == NULL)
error (EXIT_FAILURE, 0, "Can't allocate %zu bytes", slen);
errno = 0;
if (confstr ((int) cp->value, sval, slen) == 0)
print_strvar (cp->name, "undefined");
else
print_strvar (cp->name, sval);
free (sval);
break;
case SYSCONF:
errno = 0;
if ((val = sysconf ((int) cp->value)) == -1)
{
if (a_flag && errno != 0)
return; /* Just skip invalid variables */
print_strvar (cp->name, "undefined");
}
else
print_longvar (cp->name, val);
break;
case PATHCONF:
errno = 0;
if ((val = pathconf (pathname, (int) cp->value)) == -1)
{
if (a_flag && errno != 0)
return; /* Just skip invalid variables */
print_strvar (cp->name, "undefined");
}
else
print_longvar (cp->name, val);
break;
}
}
示例4: uname
int uname(struct utsname *name) {
if( confstr(_CS_SYSNAME, name->sysname, sizeof name->sysname) == 0 ||
confstr(_CS_HOSTNAME, name->nodename, sizeof name->nodename) == 0 ||
confstr(_CS_RELEASE, name->release, sizeof name->release) == 0 ||
confstr(_CS_VERSION, name->version, sizeof name->version) == 0 ||
confstr(_CS_MACHINE, name->machine, sizeof name->machine) == 0) {
return -1;
}
return 0;
}
示例5: citp_dump_config
static void citp_dump_config(void)
{
char buf[80];
confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
log("GNU_LIBC_VERSION = %s", buf);
confstr(_CS_GNU_LIBPTHREAD_VERSION, buf, sizeof(buf));
log("GNU_LIBPTHREAD_VERSION = %s", buf);
log("ci_glibc_uses_nptl = %d", ci_glibc_uses_nptl());
log("ci_is_multithreaded = %d", ci_is_multithreaded());
}
示例6: main
int main(int argc,char*argv[]) {
char*path;
size_t len;
len = confstr(_CS_PATH, (char *) NULL, 0);
if (!(path = malloc(1 + len)))
die("malloc...\n");
path[0] = ':';
confstr(_CS_PATH, path+1, len);
write(1,path,len);
return 0;
}
示例7: getAll
//' 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);
}
示例8: getConfig
//' 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;
}
示例9: isnptl
int
isnptl (void)
{
size_t n = confstr (_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
if (n > 0) {
char *buf = alloca (n);
confstr (_CS_GNU_LIBPTHREAD_VERSION, buf, n);
if (strstr (buf, "NPTL")) {
return 1;
}
}
return 0;
}
示例10: main
int main() {
int vals[] = {
_CS_PATH,
_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS,
_CS_GNU_LIBC_VERSION,
_CS_GNU_LIBPTHREAD_VERSION,
_CS_POSIX_V6_ILP32_OFF32_LIBS,
_CS_POSIX_V6_ILP32_OFFBIG_LIBS,
_CS_POSIX_V6_LP64_OFF64_CFLAGS,
_CS_POSIX_V6_LP64_OFF64_LDFLAGS,
_CS_POSIX_V6_LP64_OFF64_LIBS,
_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS,
_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS,
_CS_POSIX_V6_LPBIG_OFFBIG_LIBS,
_CS_POSIX_V6_ILP32_OFF32_CFLAGS,
_CS_POSIX_V6_ILP32_OFF32_LDFLAGS,
_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS,
_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS
};
char* names[] = {
"_CS_PATH",
"_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS",
"_CS_GNU_LIBC_VERSION",
"_CS_GNU_LIBPTHREAD_VERSION",
"_CS_POSIX_V6_ILP32_OFF32_LIBS",
"_CS_POSIX_V6_ILP32_OFFBIG_LIBS",
"_CS_POSIX_V6_LP64_OFF64_CFLAGS",
"_CS_POSIX_V6_LP64_OFF64_LDFLAGS",
"_CS_POSIX_V6_LP64_OFF64_LIBS",
"_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS",
"_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS",
"_CS_POSIX_V6_LPBIG_OFFBIG_LIBS",
"_CS_POSIX_V6_ILP32_OFF32_CFLAGS",
"_CS_POSIX_V6_ILP32_OFF32_LDFLAGS",
"_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS",
"_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS"
};
char buffer[256];
for (int i = 0; i < sizeof vals / sizeof vals[0]; i++) {
printf("ret: %d\n", confstr(vals[i], buffer, 256));
printf("%s: %s\n", names[i], buffer);
printf("errno: %d\n\n", errno);
errno = 0;
}
printf("(invalid) ret: %d\n", confstr(-123, buffer, 256));
printf("errno: %d\n", errno);
return 0;
}
示例11: findInPath
static const char* findInPath(const char* file)
{
static __thread char buffer[DARWIN_MAXPATHLEN];
if (*file == '/')
return file;
if (strchr(file, '/') != 0)
{
// No PATH resolution, only fix the case
strcpy(buffer, file);
translatePathCI(buffer);
return buffer;
}
const char* path = getenv("PATH");
if (!path)
{
// Get the default path.
size_t len = confstr(_CS_PATH, 0, 0);
char* buf = reinterpret_cast<char*>( alloca(len + 1) );
buf[0] = ':';
confstr(_CS_PATH, buf + 1, len);
path = buf;
}
const char* p = path;
do
{
const char* end = strchrnul(p, ':');
size_t len = end-p;
memcpy(buffer, p, len);
if (buffer[len-1] != '/')
buffer[len++] = '/';
strcpy(buffer+len, file);
translatePathCI(buffer);
if (::access(buffer, X_OK) == 0)
return buffer;
}
while (*p++);
return 0;
}
示例12: main
int main() {
printf("Using gnu_get_lib_version(): The glibc version is: %s\n", gnu_get_libc_version());
char *p;
size_t max_size = 1024;
p = (char *)malloc(max_size);
confstr(_CS_GNU_LIBC_VERSION, p, (size_t)1024 );
printf("Using confstr(): The glibc version is: %s\n", p);
printf("Max size of 'p' is %ld", (long)max_size);
newline();
printf("OK, now for some error handling....\n");
error_handling();
newline();
printf("Gangsta! Now for some system data type printing!");
printing_system_data_types();
free(p);
return 0;
}
示例13: defined
/*
* This function returns the absolute path to the executable it is running in.
*
* The implementation follows http://stackoverflow.com/a/933996/712014
*
*/
const char *get_exe_path(const char *argv0) {
static char destpath[PATH_MAX];
char tmp[PATH_MAX];
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
/* Linux and Debian/kFreeBSD provide /proc/self/exe */
#if defined(__linux__) || defined(__FreeBSD_kernel__)
const char *exepath = "/proc/self/exe";
#elif defined(__FreeBSD__)
const char *exepath = "/proc/curproc/file";
#endif
ssize_t linksize;
if ((linksize = readlink(exepath, destpath, sizeof(destpath) - 1)) != -1) {
/* readlink() does not NULL-terminate strings, so we have to. */
destpath[linksize] = '\0';
return destpath;
}
#endif
/* argv[0] is most likely a full path if it starts with a slash. */
if (argv0[0] == '/')
return argv0;
/* if argv[0] contains a /, prepend the working directory */
if (strchr(argv0, '/') != NULL &&
getcwd(tmp, sizeof(tmp)) != NULL) {
snprintf(destpath, sizeof(destpath), "%s/%s", tmp, argv0);
return destpath;
}
/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
char *path = getenv("PATH");
if (path == NULL) {
/* _CS_PATH is typically something like "/bin:/usr/bin" */
confstr(_CS_PATH, tmp, sizeof(tmp));
sasprintf(&path, ":%s", tmp);
} else {
path = strdup(path);
}
const char *component;
char *str = path;
while (1) {
if ((component = strtok(str, ":")) == NULL)
break;
str = NULL;
snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
/* Of course this is not 100% equivalent to actually exec()ing the
* binary, but meh. */
if (access(destpath, X_OK) == 0) {
free(path);
return destpath;
}
}
free(path);
/* Last resort: maybe it’s in /usr/bin? */
return "/usr/bin/i3-nagbar";
}
示例14: fopen_ex
/*
* fopen_ex - open a file for variable architecture
* return: FILE *
* filename(in): path to the file to open
* type(in): open type
*/
FILE *
fopen_ex (const char *filename, const char *type)
{
#if defined(SOLARIS)
size_t r = 0;
char buf[1024];
extern size_t confstr (int, char *, size_t);
r = confstr (_CS_LFS_CFLAGS, buf, 1024);
if (r > 0)
return fopen64 (filename, type);
else
return fopen (filename, type);
#elif defined(HPUX)
#if _LFS64_LARGEFILE == 1
return fopen64 (filename, type);
#else
return fopen (filename, type);
#endif
#elif defined(AIX) || (defined(I386) && defined(LINUX))
return fopen64 (filename, type);
#else /* NT, ALPHA_OSF, and the others */
return fopen (filename, type);
#endif
}
示例15: main
int main(void)
{
int confstr_len = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
char confstr_buf[confstr_len];
confstr(_CS_GNU_LIBC_VERSION, confstr_buf, confstr_len);
printf("At compile-time the version was %d.%d.\n",
__GLIBC__,
__GLIBC_MINOR__);
printf("At runtime the version is %s.\n",
gnu_get_libc_version());
printf("confstr reports %s.\n",
confstr_buf);
return 0;
}