removed heap dependency in datetime() and simplified the function
This commit is contained in:
		
							parent
							
								
									25eb9ff35e
								
							
						
					
					
						commit
						1d257999ed
					
				
							
								
								
									
										23
									
								
								slstatus.c
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								slstatus.c
									
									
									
									
									
								
							| @ -163,27 +163,14 @@ cpu_perc(void) | |||||||
| static char * | static char * | ||||||
| datetime(const char *timeformat) | datetime(const char *timeformat) | ||||||
| { | { | ||||||
| 	time_t tm; | 	time_t t; | ||||||
| 	size_t bufsize = 64; | 	char timestr[80]; | ||||||
| 	char *buf = malloc(bufsize); |  | ||||||
| 	if (buf == NULL) { |  | ||||||
| 		fprintf(stderr, "Failed to get date/time.\n"); |  | ||||||
| 		return smprintf(UNKNOWN_STR); |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	time(&tm); | 	t = time(NULL); | ||||||
| 	setlocale(LC_TIME, ""); | 	if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0) | ||||||
| 	if (!strftime(buf, bufsize, timeformat, localtime(&tm))) { |  | ||||||
| 		setlocale(LC_TIME, "C"); |  | ||||||
| 		free(buf); |  | ||||||
| 		fprintf(stderr, "Strftime failed.\n"); |  | ||||||
| 		return smprintf(UNKNOWN_STR); | 		return smprintf(UNKNOWN_STR); | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	setlocale(LC_TIME, "C"); | 	return smprintf("%s", timestr); | ||||||
| 	char *ret = smprintf("%s", buf); |  | ||||||
| 	free(buf); |  | ||||||
| 	return ret; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static char * | static char * | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user