瀏覽代碼

lib/utils_base.c - cleanup code

Cleaning up some of the unneeded checks for null and !=0 types. These are handled perfectly fine with if() statements and such.
Spenser Reinhardt 11 年之前
父節點
當前提交
488263cd3d
共有 1 個文件被更改,包括 45 次插入46 次删除
  1. 45 46
      lib/utils_base.c

+ 45 - 46
lib/utils_base.c

@@ -40,13 +40,13 @@ nagios_plugin *this_nagios_plugin=NULL;
 int _np_state_read_file(FILE *);
 int _np_state_read_file(FILE *);
 
 
 void np_init( char *plugin_name, int argc, char **argv ) {
 void np_init( char *plugin_name, int argc, char **argv ) {
-	if (this_nagios_plugin==NULL) {
+	if (!this_nagios_plugin) {
 		this_nagios_plugin = calloc(1, sizeof(nagios_plugin));
 		this_nagios_plugin = calloc(1, sizeof(nagios_plugin));
-		if (this_nagios_plugin==NULL) {
+		if (!this_nagios_plugin) {
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		}
 		}
 		this_nagios_plugin->plugin_name = strdup(plugin_name);
 		this_nagios_plugin->plugin_name = strdup(plugin_name);
-		if (this_nagios_plugin->plugin_name==NULL)
+		if (!this_nagios_plugin->plugin_name)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 		this_nagios_plugin->argc = argc;
 		this_nagios_plugin->argc = argc;
 		this_nagios_plugin->argv = argv;
 		this_nagios_plugin->argv = argv;
@@ -54,7 +54,7 @@ void np_init( char *plugin_name, int argc, char **argv ) {
 }
 }
 
 
 void np_set_args( int argc, char **argv ) {
 void np_set_args( int argc, char **argv ) {
-	if (this_nagios_plugin==NULL)
+	if (!this_nagios_plugin)
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 
 
 	this_nagios_plugin->argc = argc;
 	this_nagios_plugin->argc = argc;
@@ -63,8 +63,8 @@ void np_set_args( int argc, char **argv ) {
 
 
 
 
 void np_cleanup() {
 void np_cleanup() {
-	if (this_nagios_plugin!=NULL) {
-		if(this_nagios_plugin->state!=NULL) {
+	if (this_nagios_plugin) {
+		if(this_nagios_plugin->state) {
 			if(this_nagios_plugin->state->state_data) { 
 			if(this_nagios_plugin->state->state_data) { 
 				np_free(this_nagios_plugin->state->state_data->data);
 				np_free(this_nagios_plugin->state->state_data->data);
 				np_free(this_nagios_plugin->state->state_data);
 				np_free(this_nagios_plugin->state->state_data);
@@ -90,7 +90,7 @@ die (int result, const char *fmt, ...)
 	va_start (ap, fmt);
 	va_start (ap, fmt);
 	vprintf (fmt, ap);
 	vprintf (fmt, ap);
 	va_end (ap);
 	va_end (ap);
-	if(this_nagios_plugin!=NULL) {
+	if(this_nagios_plugin) {
 		np_cleanup();
 		np_cleanup();
 	}
 	}
 	exit (result);
 	exit (result);
@@ -128,7 +128,7 @@ range
 	}
 	}
 
 
 	end_str = index(str, ':');
 	end_str = index(str, ':');
-	if (end_str != NULL) {
+	if (end_str) {
 		if (str[0] == '~') {
 		if (str[0] == '~') {
 			temp_range->start_infinity = TRUE;
 			temp_range->start_infinity = TRUE;
 		} else {
 		} else {
@@ -140,12 +140,12 @@ range
 		end_str = str;
 		end_str = str;
 	}
 	}
 	end = strtod(end_str, NULL);
 	end = strtod(end_str, NULL);
-	if (strcmp(end_str, "") != 0) {
+	if (strcmp(end_str, "")) {
 		set_range_end(temp_range, end);
 		set_range_end(temp_range, end);
 	}
 	}
 
 
-	if (temp_range->start_infinity == TRUE ||
-		temp_range->end_infinity == TRUE ||
+	if (temp_range->start_infinity ||
+		temp_range->end_infinity ||
 		temp_range->start <= temp_range->end) {
 		temp_range->start <= temp_range->end) {
 		return temp_range;
 		return temp_range;
 	}
 	}
@@ -159,19 +159,19 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
 {
 {
 	thresholds *temp_thresholds = NULL;
 	thresholds *temp_thresholds = NULL;
 
 
-	if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL)
+	if (!(temp_thresholds = calloc(1, sizeof(thresholds))))
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
 	temp_thresholds->warning = NULL;
 	temp_thresholds->warning = NULL;
 	temp_thresholds->critical = NULL;
 	temp_thresholds->critical = NULL;
 
 
-	if (warn_string != NULL) {
-		if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
+	if (warn_string) {
+		if (!(temp_thresholds->warning = parse_range_string(warn_string))) {
 			return NP_RANGE_UNPARSEABLE;
 			return NP_RANGE_UNPARSEABLE;
 		}
 		}
 	}
 	}
-	if (critical_string != NULL) {
-		if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
+	if (critical_string) {
+		if (!(temp_thresholds->critical = parse_range_string(critical_string))) {
 			return NP_RANGE_UNPARSEABLE;
 			return NP_RANGE_UNPARSEABLE;
 		}
 		}
 	}
 	}
@@ -226,19 +226,19 @@ check_range(double value, range *my_range)
 		yes = FALSE;
 		yes = FALSE;
 	}
 	}
 
 
-	if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
+	if (!my_range->end_infinity && !my_range->start_infinity) {
 		if ((my_range->start <= value) && (value <= my_range->end)) {
 		if ((my_range->start <= value) && (value <= my_range->end)) {
 			return no;
 			return no;
 		} else {
 		} else {
 			return yes;
 			return yes;
 		}
 		}
-	} else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
+	} else if (!my_range->start_infinity && my_range->end_infinity) {
 		if (my_range->start <= value) {
 		if (my_range->start <= value) {
 			return no;
 			return no;
 		} else {
 		} else {
 			return yes;
 			return yes;
 		}
 		}
-	} else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
+	} else if (my_range->start_infinity && !my_range->end_infinity) {
 		if (value <= my_range->end) {
 		if (value <= my_range->end) {
 			return no;
 			return no;
 		} else {
 		} else {
@@ -253,13 +253,13 @@ check_range(double value, range *my_range)
 int
 int
 get_status(double value, thresholds *my_thresholds)
 get_status(double value, thresholds *my_thresholds)
 {
 {
-	if (my_thresholds->critical != NULL) {
-		if (check_range(value, my_thresholds->critical) == TRUE) {
+	if (my_thresholds->critical) {
+		if (check_range(value, my_thresholds->critical)) {
 			return STATE_CRITICAL;
 			return STATE_CRITICAL;
 		}
 		}
 	}
 	}
-	if (my_thresholds->warning != NULL) {
-		if (check_range(value, my_thresholds->warning) == TRUE) {
+	if (my_thresholds->warning) {
+		if (check_range(value, my_thresholds->warning)) {
 			return STATE_WARNING;
 			return STATE_WARNING;
 		}
 		}
 	}
 	}
@@ -325,7 +325,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
 		/* Strip any leading space */
 		/* Strip any leading space */
 		for (varlist; isspace(varlist[0]); varlist++);
 		for (varlist; isspace(varlist[0]); varlist++);
 
 
-		if (strncmp(name, varlist, strlen(name)) == 0) {
+		if (!strncmp(name, varlist, strlen(name))) {
 			varlist += strlen(name);
 			varlist += strlen(name);
 			/* strip trailing spaces */
 			/* strip trailing spaces */
 			for (varlist; isspace(varlist[0]); varlist++);
 			for (varlist; isspace(varlist[0]); varlist++);
@@ -344,7 +344,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
 					value[tmp-varlist] = '\0';
 					value[tmp-varlist] = '\0';
 				} else {
 				} else {
 					/* Value is delimited by a \0 */
 					/* Value is delimited by a \0 */
-					if (strlen(varlist) == 0) continue;
+					if (!strlen(varlist)) continue;
 					value = (char *)calloc(1, strlen(varlist) + 1);
 					value = (char *)calloc(1, strlen(varlist) + 1);
 					strncpy(value, varlist, strlen(varlist));
 					strncpy(value, varlist, strlen(varlist));
 					value[strlen(varlist)] = '\0';
 					value[strlen(varlist)] = '\0';
@@ -410,14 +410,14 @@ char *_np_state_generate_key() {
 	keyname[40]='\0';
 	keyname[40]='\0';
 	
 	
 	p = strdup(keyname);
 	p = strdup(keyname);
-	if(p==NULL) {
+	if(!p) {
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 	}
 	}
 	return p;
 	return p;
 }
 }
 
 
 void _cleanup_state_data() {
 void _cleanup_state_data() {
-	if (this_nagios_plugin->state->state_data!=NULL) {
+	if (this_nagios_plugin->state->state_data) {
 		np_free(this_nagios_plugin->state->state_data->data);
 		np_free(this_nagios_plugin->state->state_data->data);
 		np_free(this_nagios_plugin->state->state_data);
 		np_free(this_nagios_plugin->state->state_data);
 	}
 	}
@@ -434,7 +434,7 @@ char* _np_state_calculate_location_prefix(){
 	/* Do not allow passing NP_STATE_DIRECTORY in setuid plugins
 	/* Do not allow passing NP_STATE_DIRECTORY in setuid plugins
 	 * for security reasons */
 	 * for security reasons */
 
 
-	if (np_suid() == FALSE) {
+	if (!np_suid()) {
 		env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
 		env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
 		if(env_dir && env_dir[0] != '\0')
 		if(env_dir && env_dir[0] != '\0')
 			return env_dir;
 			return env_dir;
@@ -454,18 +454,18 @@ void np_enable_state(char *keyname, int expected_data_version) {
 	char *p=NULL;
 	char *p=NULL;
 	int ret;
 	int ret;
 
 
-	if(this_nagios_plugin==NULL)
+	if(!this_nagios_plugin)
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 
 
 	this_state = (state_key *) calloc(1, sizeof(state_key));
 	this_state = (state_key *) calloc(1, sizeof(state_key));
-	if(this_state==NULL)
+	if(!this_state)
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
-	if(keyname==NULL) {
+	if(!keyname) {
 		temp_keyname = _np_state_generate_key();
 		temp_keyname = _np_state_generate_key();
 	} else {
 	} else {
 		temp_keyname = strdup(keyname);
 		temp_keyname = strdup(keyname);
-		if(temp_keyname==NULL)
+		if(!temp_keyname)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 	}
 	}
 	/* Die if invalid characters used for keyname */
 	/* Die if invalid characters used for keyname */
@@ -501,15 +501,15 @@ state_data *np_state_read() {
 	FILE *statefile;
 	FILE *statefile;
 	int rc = FALSE;
 	int rc = FALSE;
 
 
-	if(this_nagios_plugin==NULL)
+	if(!this_nagios_plugin)
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 		die(STATE_UNKNOWN, "%s\n", _("This requires np_init to be called"));
 
 
 	/* Open file. If this fails, no previous state found */
 	/* Open file. If this fails, no previous state found */
 	statefile = fopen( this_nagios_plugin->state->_filename, "r" );
 	statefile = fopen( this_nagios_plugin->state->_filename, "r" );
-	if(statefile!=NULL) {
+	if(statefile) {
 
 
 		this_state_data = (state_data *) calloc(1, sizeof(state_data));
 		this_state_data = (state_data *) calloc(1, sizeof(state_data));
-		if(this_state_data==NULL)
+		if(!this_state_data)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
 		this_state_data->data=NULL;
 		this_state_data->data=NULL;
@@ -520,7 +520,7 @@ state_data *np_state_read() {
 		fclose(statefile);
 		fclose(statefile);
 	}
 	}
 
 
-	if(rc==FALSE) {
+	if(!rc) {
 		_cleanup_state_data();
 		_cleanup_state_data();
 	}
 	}
 
 
@@ -543,7 +543,7 @@ int _np_state_read_file(FILE *f) {
 
 
 	/* Note: This introduces a limit of 1024 bytes in the string data */
 	/* Note: This introduces a limit of 1024 bytes in the string data */
 	line = (char *) calloc(1, 1024);
 	line = (char *) calloc(1, 1024);
-	if(line==NULL)
+	if(!line)
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
 	while(!failure && (fgets(line,1024,f))!=NULL){
 	while(!failure && (fgets(line,1024,f))!=NULL){
@@ -609,23 +609,23 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	char *directories=NULL;
 	char *directories=NULL;
 	char *p=NULL;
 	char *p=NULL;
 
 
-	if(data_time==0)
+	if(!data_time)
 		time(&current_time);
 		time(&current_time);
 	else
 	else
 		current_time=data_time;
 		current_time=data_time;
 	
 	
 	/* If file doesn't currently exist, create directories */
 	/* If file doesn't currently exist, create directories */
-	if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
+	if(access(this_nagios_plugin->state->_filename,F_OK)) {
 		result = asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
 		result = asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
 		if (result < 0)
 		if (result < 0)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
-		if(directories==NULL)
+		if(!directories)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
 		for(p=directories+1; *p; p++) {
 		for(p=directories+1; *p; p++) {
 			if(*p=='/') {
 			if(*p=='/') {
 				*p='\0';
 				*p='\0';
-				if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
+				if(access(directories,F_OK) && mkdir(directories, S_IRWXU)) {
 					/* Can't free this! Otherwise error message is wrong! */
 					/* Can't free this! Otherwise error message is wrong! */
 					/* np_free(directories); */ 
 					/* np_free(directories); */ 
 					die(STATE_UNKNOWN, "%s %s\n", _("Cannot create directory:"), directories);
 					die(STATE_UNKNOWN, "%s %s\n", _("Cannot create directory:"), directories);
@@ -639,7 +639,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	result = asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
 	result = asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
 	if (result < 0)
 	if (result < 0)
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
-	if(temp_file==NULL)
+	if(!temp_file)
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 
 	if((fd=mkstemp(temp_file))==-1) {
 	if((fd=mkstemp(temp_file))==-1) {
@@ -648,7 +648,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	}
 	}
 
 
 	fp=(FILE *)fdopen(fd,"w");
 	fp=(FILE *)fdopen(fd,"w");
-	if(fp==NULL) {
+	if(!fp) {
 		close(fd);
 		close(fd);
 		unlink(temp_file);
 		unlink(temp_file);
 		np_free(temp_file);
 		np_free(temp_file);
@@ -669,13 +669,13 @@ void np_state_write_string(time_t data_time, char *data_string) {
 
 
 	fsync(fd);
 	fsync(fd);
 
 
-	if(result!=0) {
+	if(result) {
 		unlink(temp_file);
 		unlink(temp_file);
 		np_free(temp_file);
 		np_free(temp_file);
 		die(STATE_UNKNOWN, "%s\n", _("Error writing temp file"));
 		die(STATE_UNKNOWN, "%s\n", _("Error writing temp file"));
 	}
 	}
 
 
-	if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
+	if(rename(temp_file, this_nagios_plugin->state->_filename)) {
 		unlink(temp_file);
 		unlink(temp_file);
 		np_free(temp_file);
 		np_free(temp_file);
 		die(STATE_UNKNOWN, "%s\n", _("Cannot rename state temp file"));
 		die(STATE_UNKNOWN, "%s\n", _("Cannot rename state temp file"));
@@ -683,4 +683,3 @@ void np_state_write_string(time_t data_time, char *data_string) {
 
 
 	np_free(temp_file);
 	np_free(temp_file);
 }
 }
-