⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolkit.c

📁 openPBS的开放源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
schd_val2byte(char *val){    char   *id = "schd_val2byte";     int     b = 0;    size_t num = 0;    char   *p;    if (val == NULL)	return (0);    if (val[0] == '?') {	(void)sprintf(log_buffer, "error from getreq(physmem): %s: [%d, %d]", 	     val, pbs_errno, errno);	log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);	return (0);    }    b = 0;    num = 0;    num = strtoul(val, &p, 0);    /* Now 'p' should point to first non-number character. */    /* If no units given, return the number. */    if ((p == val) || (*p == '\0'))	return (num);	/* XXX return num if p == val? */    /* Parse the given order of magnitude. */    switch (*p) {    case 'k':    case 'K':	num *= KILO;	break;    case 'm':    case 'M':	num *= MEGA;	break;    case 'g':    case 'G':	num *= GIGA;	break;    case 't':    case 'T':	num *= TERA;	break;    case 'b':    case 'B':	b++;	break;    default:	return (-1);    }    /* XXX if (b && *p != '\0') return error. */    /* Parse the word-size unit. */    p++;    if ((*p != '\0') && !b) {	switch (*p) {	case 'b':	case 'B':	    break;	case 'w':	case 'W':	    num *= NBPW;	/* Multiply by size of word. */	    break;	default:	    return (-1);	}    }    /* Return the total number of bytes represented by the value. */    return (num);}/* * Convert a boolean value (i.e. "True" or "False") to an int, where * the integer 1 represents a true value, and 0 represents non-true. */int schd_val2bool(char *val, int *bool){    /* char   *id = "val2bool"; */    char   lower[64];    /* Make a copy of the input string, and convert it to all lowercase. */    strncpy(lower, val, sizeof(lower) - 1);    schd_lowercase(lower);    /* Test for a variety of "yes" values. */    if ((strcmp(lower, "true") == 0)    ||        (strcmp(lower, "on") == 0)      ||        (strcmp(lower, "enable") == 0)  ||        (strcmp(lower, "enabled") == 0) ||        (strcmp(lower, "yes") == 0)     ||        (strcmp(lower, "yes") == 0)     ||        (strcmp(lower, "y") == 0)       ||        (strcmp(lower, "1") == 0))    {	*bool = 1;	return (0);    }    /* Test for a variety of "no" values. */    if ((strcmp(lower, "false") == 0) ||        (strcmp(lower, "off") == 0) ||        (strcmp(lower, "no") == 0) ||        (strcmp(lower, "disabled") == 0) ||        (strcmp(lower, "disable") == 0) ||        (strcmp(lower, "n") == 0) ||        (strcmp(lower, "0") == 0))    {	*bool = 0;	return (0);    }    return (1);		/* Did not parse successfully. */}char * schd_bool2val(int bool){    if (bool != 0)	return "true";    return "false";}/* *  Convert a value string from time to its equivalent value in seconds. */time_t schd_val2sec(char *val){    char   *id = "val2sec";    char   *p1, *p2, *p3, *end, *zero = "0";    time_t  v1, v2, v3;    time_t  sec;    char   *valcopy;    p3 = NULL;    valcopy = schd_strdup(val);    if (valcopy == NULL) {	(void)sprintf(log_buffer, "schd_strdup(val) failed.");	log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);	DBPRT(("%s: %s\n", id, log_buffer));	return ((time_t) -1);    }    /* Split string into at most 3 tokens. */    p1 = strtok(valcopy, ":");    p2 = strtok(NULL, ":");    if (p2 != NULL) {	p3 = strtok(NULL, ":");    }    /* Only seconds specified.  Shift right 2 places into p3, and zero fill */    if (p2 == NULL) {	p3 = p1;	p2 = zero;	p1 = zero;    }    /* Minutes and seconds specified.  Shift right into p3, and zero fill */    if (p3 == NULL) {	p3 = p2;	p2 = p1;	p1 = zero;    }    v1 = (time_t)strtol(p1, &end, 10);    if (*end != '\0')	goto error;    if (v1 < 0)	goto error;    v2 = (time_t)strtol(p2, &end, 10);    if (*end != '\0')	goto error;    if (v2 < 0 || v2 > 59)	goto error;    v3 = (time_t)strtol(p3, &end, 10);    if (*end != '\0')	goto error;    if (v3 < 0 || v3 > 59)	goto error;    free (valcopy);    sec = (v1 * 3600) + (v2 * 60) + v3;    return (sec);error:    (void)sprintf("Can't parse time '%s' into seconds.\n", val);    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);    DBPRT(("%s: %s\n", id, log_buffer));    free(valcopy);    return ((time_t) -1);}#define	DATEFMT	"%m/%d/%Y@%H:%M:%S"	/* MM/DD/YYYY@HH:MM:SS *//*  * Convert a string of the form MM/DD/YYYY@HH:MM:SS to a time_t.  Uses * strptime(3) function for parsing. */intschd_val2datetime(char *string, time_t *when){    /* char   *id = "get_datetime"; */    char   *remain;    struct tm tm;    time_t  then;    /* Call the "inverse" of strftime(3) to parse the string. */    remain = strptime(string, DATEFMT, &tm);    /* Check for successful parsing of the date string in DATEFMT. */    if (remain == NULL)	return -1;    /* Make sure the date@time was the only thing in the string. */    if (*remain != '\0' && !isspace(*remain))	return -1;    /*    * The struct tm now contains the right information.  Turn it into a    * valid time_t.    */    then = mktime(&tm);    if (then == (time_t) -1)	return -1;    /* Copy the time value and return success. */    *when = then;    return 0;}/* * Change a string into either a boolean (values 0 or 1), or a time_t if * the string gives a time in the format "MM/DD/YYYY@HH:MM:SS". */int schd_val2booltime(char *val, time_t *t){    int    ival;    time_t tval;    /* Try to parse into a boolean first. */    if (schd_val2bool(val, &ival) == 0) {	*t = (time_t) ival;	return 0;    }    /* Not a boolean -- try for a date@time string. */    if (schd_val2datetime(val, &tval) == 0) {	*t = tval;	return 0;    }    /* Not a valid boolean or date@time, so reject it. */    return 1;}/* * Change an integer value into string containing either "True" or "False", * or (if the value > 1) a string containing the equivalent 'date@time' for * that value. * The time_t is overloaded in the case of being a boolean. */char * schd_booltime2val(time_t bool){    static char val[64];    char   datetime[48];    if ((bool == (time_t) 0) || (bool == (time_t) 1))	return (schd_bool2val(bool));    (void)strftime(datetime, sizeof(datetime) - 1, DATEFMT, localtime(&bool));    if (time(NULL) >= bool)	(void)sprintf(val, "%s (since %s)", schd_bool2val(1), datetime);    else	(void)sprintf(val, "%s (until %s)", schd_bool2val(0), datetime);    return val;}/* * Routines to check if files have been changed, deleted, created, etc. since * the last invocation.  Used to automatically load new configuration and * statistics data. */struct filestatus {    struct filestatus *next;    char    *filename;    time_t   ctime;    int      exists;};typedef struct filestatus FileStatus;static FileStatus *filestats = NULL;intschd_register_file(char *filename){    char    *id = "schd_register_file";    FileStatus *stats, *tail, *new = NULL;    /*     * Look for the tail of the list.  While walking the list, check to see     * that the filename is not already registered.     */    tail = NULL;    for (stats = filestats; stats != NULL; stats = stats->next) {	if (strcmp(filename, stats->filename) == 0) {	    sprintf(log_buffer, "%s: file %s already registered.", id, 		filename);	    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);	    DBPRT(("%s: %s\n", id, log_buffer));	    return (-1);	}	tail = stats;    }    /* Create space for the new record. */    new = (FileStatus *) malloc (sizeof (FileStatus));    if (new == NULL) {	sprintf(log_buffer, 	    "%s: out of memory allocating FileStatus for file %s", 	    id, filename);	log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);	return (-1);    }    /* Clear the record out -- this clears the ctime and next pointer. */    memset (new, 0, sizeof(FileStatus));    /* Keep a copy of the filename around. */    new->filename = schd_strdup(filename);    if (new->filename == NULL) {	log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id,	    "schd_strdup(filename)");	free(new);	return (-1);    }    /*      * If this is not the first element, tack it on the end of the list.     * Otherwise, start the list with it.     */    if (tail)	tail->next = new;    else	filestats = new;    (void)sprintf(log_buffer, "%s: file %s registered.", id, filename);    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);    /*     * Load the new element with the initial values for the file.  Ignore     * the return value - only setting up the timestamp and file existence     * status are important.     */    (void)schd_file_has_changed(filename, 1);    return (0);}intschd_file_has_changed(char *filename, int reset_stamp){    char   *id = "schd_file_has_changed";    FileStatus *stats;    struct stat stat_buffer;    int     exists;    /* Assume that the file has not changed, and that it exists. */    exists      = 1;    if (filename == NULL) {	DBPRT(("%s: filename is null\n", id));	return (-1);    }    for (stats = filestats; stats != NULL; stats = stats->next) {	if (strcmp(filename, stats->filename) == 0)	    break;    }    if (stats == NULL) {	DBPRT(("%s: filename %s not registered\n", id, filename));	return (-1);    }    /* Get the file modification times from the filesystem. */    if (stat(filename, &stat_buffer) == -1) {	if (errno == ENOENT) {	    exists = 0;	} else {	    (void)sprintf(log_buffer,		"%s: stat(%s) failed: %d", id, filename, errno);	    log_record(PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, id, log_buffer);	    return (-1);	}    }    /*     * Has file has changed state?     */    if (exists != stats->exists) {	stats->exists = exists;	if (exists && reset_stamp)	    stats->ctime = stat_buffer.st_ctime;	return (1);    }    /*     * If the ctime is different from the previously recorded one, the     * file has changed.  stat(2) indicates that ctime will be changed     * on every write, truncate, etc.  Update the ctime value for the     * next call.     */    if (exists && (stat_buffer.st_ctime != stats->ctime)) {	if (reset_stamp)	    stats->ctime = stat_buffer.st_ctime;	return (1);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -