Просмотр исходного кода

lib/utils_base.c - Remformat die calls and add \n

die() does not have newlines automatically added. This also reformats the arguments for easier reading, standard formatting, and easier po generation.
Spenser Reinhardt 11 лет назад
Родитель
Сommit
d63d9c9c55
1 измененных файлов с 29 добавлено и 38 удалено
  1. 29 38
      lib/utils_base.c

+ 29 - 38
lib/utils_base.c

@@ -43,12 +43,11 @@ void np_init( char *plugin_name, int argc, char **argv ) {
 	if (this_nagios_plugin==NULL) {
 		this_nagios_plugin = calloc(1, sizeof(nagios_plugin));
 		if (this_nagios_plugin==NULL) {
-			die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-			    strerror(errno));
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		}
 		this_nagios_plugin->plugin_name = strdup(plugin_name);
 		if (this_nagios_plugin->plugin_name==NULL)
-			die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 		this_nagios_plugin->argc = argc;
 		this_nagios_plugin->argv = argv;
 	}
@@ -56,7 +55,7 @@ void np_init( char *plugin_name, int argc, char **argv ) {
 
 void np_set_args( int argc, char **argv ) {
 	if (this_nagios_plugin==NULL)
-		die(STATE_UNKNOWN, _("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->argv = argv;
@@ -161,8 +160,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
 	thresholds *temp_thresholds = NULL;
 
 	if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL)
-		die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-		    strerror(errno));
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 	temp_thresholds->warning = NULL;
 	temp_thresholds->critical = NULL;
@@ -190,9 +188,9 @@ set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_str
 	case 0:
 		return;
 	case NP_RANGE_UNPARSEABLE:
-		die(STATE_UNKNOWN, _("Range format incorrect"));
+		die(STATE_UNKNOWN, "%s\n", _("Range format incorrect"));
 	case NP_WARN_WITHIN_CRIT:
-		die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
+		die(STATE_UNKNOWN, "%s\n", _("Warning level is a subset of critical and will not be alerted"));
 		break;
 	}
 }
@@ -303,12 +301,12 @@ int np_check_if_root(void) { return (geteuid() == 0); }
 int np_warn_if_not_root(void) {
 	int status = np_check_if_root();
 	if(!status) {
-		printf(_("Warning: "));
-		printf(_("This plugin must be either run as root or setuid root.\n"));
-		printf(_("To run as root, you can use a tool like sudo.\n"));
-		printf(_("To set the setuid permissions, use the command:\n"));
+		printf("%s", _("Warning: "));
+		printf("%s\n", _("This plugin must be either run as root or setuid root."));
+		printf("%s\n", _("To run as root, you can use a tool like sudo."));
+		printf("%s\n", _("To set the setuid permissions, use the command:"));
 		/* XXX could we use something like progname? */
-		printf("\tchmod u+s yourpluginfile\n");
+		printf("\t%s\n", _("chmod u+s yourpluginfile"));
 	}
 	return status;
 }
@@ -413,7 +411,7 @@ char *_np_state_generate_key() {
 	
 	p = strdup(keyname);
 	if(p==NULL) {
-		die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 	}
 	return p;
 }
@@ -457,26 +455,24 @@ void np_enable_state(char *keyname, int expected_data_version) {
 	int ret;
 
 	if(this_nagios_plugin==NULL)
-		die(STATE_UNKNOWN, _("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));
 	if(this_state==NULL)
-		die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-		    strerror(errno));
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 	if(keyname==NULL) {
 		temp_keyname = _np_state_generate_key();
 	} else {
 		temp_keyname = strdup(keyname);
 		if(temp_keyname==NULL)
-			die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 	}
 	/* Die if invalid characters used for keyname */
 	p = temp_keyname;
 	while(*p!='\0') {
-		if(! (isalnum(*p) || *p == '_')) {
-			die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'"));
-		}
+		if(! (isalnum(*p) || *p == '_'))
+			die(STATE_UNKNOWN, "%s\n", _("Invalid character for keyname - only alphanumerics or '_'"));
 		p++;
 	}
 	this_state->name=temp_keyname;
@@ -506,7 +502,7 @@ state_data *np_state_read() {
 	int rc = FALSE;
 
 	if(this_nagios_plugin==NULL)
-		die(STATE_UNKNOWN, _("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 */
 	statefile = fopen( this_nagios_plugin->state->_filename, "r" );
@@ -514,8 +510,7 @@ state_data *np_state_read() {
 
 		this_state_data = (state_data *) calloc(1, sizeof(state_data));
 		if(this_state_data==NULL)
-			die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-			    strerror(errno));
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 		this_state_data->data=NULL;
 		this_nagios_plugin->state->state_data = this_state_data;
@@ -549,14 +544,12 @@ int _np_state_read_file(FILE *f) {
 	/* Note: This introduces a limit of 1024 bytes in the string data */
 	line = (char *) calloc(1, 1024);
 	if(line==NULL)
-		die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-		    strerror(errno));
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 	while(!failure && (fgets(line,1024,f))!=NULL){
 		pos=strlen(line);
-		if(line[pos-1]=='\n') {
+		if(line[pos-1]=='\n')
 			line[pos-1]='\0';
-		}
 
 		if(line[0] == '#') continue;
 
@@ -588,7 +581,7 @@ int _np_state_read_file(FILE *f) {
 			case STATE_DATA_TEXT:
 				this_nagios_plugin->state->state_data->data = strdup(line);
 				if(this_nagios_plugin->state->state_data->data==NULL)
-					die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
+					die(STATE_UNKNOWN, "%s %s\n", _("Cannot execute strdup:"), strerror(errno));
 				expected=STATE_DATA_END;
 				status=TRUE;
 				break;
@@ -627,8 +620,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
 		if (result < 0)
 			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		if(directories==NULL)
-			die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-			    strerror(errno));
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 		for(p=directories+1; *p; p++) {
 			if(*p=='/') {
@@ -636,7 +628,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
 				if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
 					/* Can't free this! Otherwise error message is wrong! */
 					/* np_free(directories); */ 
-					die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
+					die(STATE_UNKNOWN, "%s %s\n", _("Cannot create directory:"), directories);
 				}
 				*p='/';
 			}
@@ -648,12 +640,11 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	if (result < 0)
 		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 	if(temp_file==NULL)
-		die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
-		    strerror(errno));
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 
 	if((fd=mkstemp(temp_file))==-1) {
 		np_free(temp_file);
-		die(STATE_UNKNOWN, _("Cannot create temporary filename"));
+		die(STATE_UNKNOWN, "%s\n", _("Cannot create temporary filename"));
 	}
 
 	fp=(FILE *)fdopen(fd,"w");
@@ -661,7 +652,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
 		close(fd);
 		unlink(temp_file);
 		np_free(temp_file);
-		die(STATE_UNKNOWN, _("Unable to open temporary state file"));
+		die(STATE_UNKNOWN, "%s\n", _("Unable to open temporary state file"));
 	}
 	
 	fprintf(fp,"# NP State file\n");
@@ -681,13 +672,13 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	if(result!=0) {
 		unlink(temp_file);
 		np_free(temp_file);
-		die(STATE_UNKNOWN, _("Error writing temp file"));
+		die(STATE_UNKNOWN, "%s\n", _("Error writing temp file"));
 	}
 
 	if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
 		unlink(temp_file);
 		np_free(temp_file);
-		die(STATE_UNKNOWN, _("Cannot rename state temp file"));
+		die(STATE_UNKNOWN, "%s\n", _("Cannot rename state temp file"));
 	}
 
 	np_free(temp_file);