diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 114 |
1 files changed, 69 insertions, 45 deletions
diff --git a/src/utils.c b/src/utils.c index 0954bc4..5c83729 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.16 2001-10-25 17:27:39 rjkaes Exp $ +/* $Id: utils.c,v 1.17 2001-11-22 00:31:10 rjkaes Exp $ * * Misc. routines which are used by the various functions to handle strings * and memory allocation and pretty much anything else we can think of. Also, @@ -32,28 +32,35 @@ */ #ifndef NDEBUG -void *debugging_calloc(size_t nmemb, size_t size, const char *file, unsigned long line) +void * +debugging_calloc(size_t nmemb, size_t size, const char *file, + unsigned long line) { void *ptr = calloc(nmemb, size); - fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file, line); + fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file, + line); return ptr; } -void *debugging_malloc(size_t size, const char *file, unsigned long line) +void * +debugging_malloc(size_t size, const char *file, unsigned long line) { void *ptr = malloc(size); fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line); return ptr; } -void *debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line) +void * +debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line) { void *newptr = realloc(ptr, size); - fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size, file, line); + fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size, + file, line); return newptr; } -void debugging_free(void *ptr, const char *file, unsigned long line) +void +debugging_free(void *ptr, const char *file, unsigned long line) { fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line); free(ptr); @@ -66,17 +73,16 @@ void debugging_free(void *ptr, const char *file, unsigned long line) /* * Build the data for a complete HTTP & HTML message for the client. */ -int send_http_message(struct conn_s* connptr, int http_code, - const char *error_title, const char *message) +int +send_http_message(struct conn_s *connptr, int http_code, + const char *error_title, const char *message) { - static char *headers = \ - "HTTP/1.0 %d %s\r\n" \ - "Server: %s/%s\r\n" \ - "Date: %s\r\n" \ - "Content-Type: text/html\r\n" \ - "Content-Length: %d\r\n" \ - "Connection: close\r\n" \ - "\r\n"; + static char *headers = + "HTTP/1.0 %d %s\r\n" + "Server: %s/%s\r\n" + "Date: %s\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %d\r\n" "Connection: close\r\n" "\r\n"; char *header_buffer; char timebuf[30]; @@ -87,9 +93,11 @@ int send_http_message(struct conn_s* connptr, int http_code, return -1; global_time = time(NULL); - strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&global_time)); + strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", + gmtime(&global_time)); - snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, error_title, PACKAGE, VERSION, timebuf, strlen(message)); + snprintf(header_buffer, HEADER_SIZE - 1, headers, http_code, + error_title, PACKAGE, VERSION, timebuf, strlen(message)); safe_write(connptr->client_fd, header_buffer, strlen(header_buffer)); safe_write(connptr->client_fd, message, strlen(message)); @@ -104,15 +112,16 @@ int send_http_message(struct conn_s* connptr, int http_code, /* * Display an error to the client. */ -int httperr(struct conn_s *connptr, int err, const char *msg) +int +httperr(struct conn_s *connptr, int err, const char *msg) { - static char *message = \ - "<html><head><title>%s</title></head>\r\n" \ - "<body>\r\n" \ - "<font size=\"+2\">Cache Error!</font><br>\r\n" \ - "An error of type %d occurred: %s\r\n" \ - "<hr>\r\n" \ - "<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" \ + static char *message = + "<html><head><title>%s</title></head>\r\n" + "<body>\r\n" + "<font size=\"+2\">Cache Error!</font><br>\r\n" + "An error of type %d occurred: %s\r\n" + "<hr>\r\n" + "<font size=\"-1\"><em>Generated by %s (%s)</em></font>\r\n" "</body></html>\r\n\r\n"; char *message_buffer; @@ -121,7 +130,8 @@ int httperr(struct conn_s *connptr, int err, const char *msg) if (!message_buffer) return -1; - snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, PACKAGE, VERSION); + snprintf(message_buffer, MAXBUFFSIZE - 1, message, msg, err, msg, + PACKAGE, VERSION); if (send_http_message(connptr, err, msg, message_buffer) < 0) { safefree(message_buffer); @@ -132,7 +142,8 @@ int httperr(struct conn_s *connptr, int err, const char *msg) return 0; } -void makedaemon(void) +void +makedaemon(void) { if (fork() != 0) exit(0); @@ -154,7 +165,8 @@ void makedaemon(void) /* * Safely creates filename and returns the low-level file descriptor. */ -int create_file_safely(const char *filename) +int +create_file_safely(const char *filename) { struct stat lstatinfo; int fildes; @@ -170,7 +182,8 @@ int create_file_safely(const char *filename) * existing", exit. */ if (errno != ENOENT) { - log_message(LOG_ERR, "create_file_safely: Error checking PID file %s: %s.", + log_message(LOG_ERR, + "create_file_safely: Error checking PID file %s: %s.", filename, strerror(errno)); return -1; } @@ -180,19 +193,22 @@ int create_file_safely(const char *filename) * sure an attacker can't slip in a file between the lstat() * and open() */ - if ((fildes = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) { - log_message(LOG_ERR, "create_file_safely: Could not create PID file %s: %s.", + if ((fildes = + open(filename, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0) { + log_message(LOG_ERR, + "create_file_safely: Could not create PID file %s: %s.", filename, strerror(errno)); return -1; } } else { struct stat fstatinfo; - + /* * Open an existing file. */ if ((fildes = open(filename, O_RDWR)) < 0) { - log_message(LOG_ERR, "create_file_safely: Could not open PID file %s: %s.", + log_message(LOG_ERR, + "create_file_safely: Could not open PID file %s: %s.", filename, strerror(errno)); return -1; } @@ -205,7 +221,8 @@ int create_file_safely(const char *filename) || lstatinfo.st_mode != fstatinfo.st_mode || lstatinfo.st_ino != fstatinfo.st_ino || lstatinfo.st_dev != fstatinfo.st_dev) { - log_message(LOG_ERR, "create_file_safely: The PID file %s has been changed before it could be opened.", + log_message(LOG_ERR, + "create_file_safely: The PID file %s has been changed before it could be opened.", filename); close(fildes); return -1; @@ -219,7 +236,8 @@ int create_file_safely(const char *filename) * st_mode check would also find this) */ if (fstatinfo.st_nlink > 1 || !S_ISREG(lstatinfo.st_mode)) { - log_message(LOG_ERR, "create_file_safely: The PID file %s has too many links, or is not a regular file: %s.", + log_message(LOG_ERR, + "create_file_safely: The PID file %s has too many links, or is not a regular file: %s.", filename, strerror(errno)); close(fildes); return -1; @@ -237,12 +255,14 @@ int create_file_safely(const char *filename) ftruncate(fildes, 0); #else close(fildes); - if ((fildes = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) { - log_message(LOG_ERR, "create_file_safely: Could not open PID file %s: %s.", + if ((fildes = + open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0) { + log_message(LOG_ERR, + "create_file_safely: Could not open PID file %s: %s.", filename, strerror(errno)); return -1; } -#endif /* HAVE_FTRUNCATE */ +#endif /* HAVE_FTRUNCATE */ } return fildes; @@ -251,7 +271,8 @@ int create_file_safely(const char *filename) /* * Write the PID of the program to the specified file. */ -void pidfile_create(const char *filename) +void +pidfile_create(const char *filename) { int fildes; FILE *fd; @@ -266,14 +287,15 @@ void pidfile_create(const char *filename) * Open a stdio file over the low-level one. */ if ((fd = fdopen(fildes, "w")) == NULL) { - log_message(LOG_ERR, "pidfile_create: fdopen() error on PID file %s: %s.", + log_message(LOG_ERR, + "pidfile_create: fdopen() error on PID file %s: %s.", filename, strerror(errno)); close(fildes); unlink(filename); exit(1); } - fprintf(fd, "%ld\n", (long)getpid()); + fprintf(fd, "%ld\n", (long) getpid()); fclose(fd); } @@ -283,7 +305,8 @@ void pidfile_create(const char *filename) * buffer, and always NULL terminates the buffer. size is the size of the * destination buffer. */ -size_t strlcpy(char *dst, const char *src, size_t size) +size_t +strlcpy(char *dst, const char *src, size_t size) { size_t len = strlen(src); size_t ret = len; @@ -305,7 +328,8 @@ size_t strlcpy(char *dst, const char *src, size_t size) * buffer, which should be one more than the maximum resulting string * length. */ -size_t strlcat(char *dst, const char *src, size_t size) +size_t +strlcat(char *dst, const char *src, size_t size) { size_t len1 = strlen(dst); size_t len2 = strlen(src); |