bfc1508d (kx 2023-03-24 03:55:33 +0300 1)
bfc1508d (kx 2023-03-24 03:55:33 +0300 2) #ifdef HAVE_CONFIG_H
bfc1508d (kx 2023-03-24 03:55:33 +0300 3) #include <config.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 4) #endif
bfc1508d (kx 2023-03-24 03:55:33 +0300 5)
bfc1508d (kx 2023-03-24 03:55:33 +0300 6) #include <stdlib.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 7) #include <stdio.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 8) #include <sys/sysinfo.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 9) #include <sys/types.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 10) #include <stdint.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 11) #include <dirent.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 12) #include <sys/stat.h> /* chmod(2) */
bfc1508d (kx 2023-03-24 03:55:33 +0300 13) #include <sys/file.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 14) #include <sys/mman.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 15) #include <fcntl.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 16) #include <limits.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 17) #include <string.h> /* strdup(3) */
bfc1508d (kx 2023-03-24 03:55:33 +0300 18) #include <libgen.h> /* basename(3) */
bfc1508d (kx 2023-03-24 03:55:33 +0300 19) #include <ctype.h> /* tolower(3) */
bfc1508d (kx 2023-03-24 03:55:33 +0300 20) #include <errno.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 21) #include <time.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 22) #include <sys/time.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 23) #include <pwd.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 24) #include <grp.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 25) #include <stdarg.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 26) #include <unistd.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 27)
bfc1508d (kx 2023-03-24 03:55:33 +0300 28) #include <defs.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 29) #include <fatal.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 30) #include <http.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 31) #include <html.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 32) #include <strbuf.h>
bfc1508d (kx 2023-03-24 03:55:33 +0300 33)
bfc1508d (kx 2023-03-24 03:55:33 +0300 34)
bfc1508d (kx 2023-03-24 03:55:33 +0300 35) #define HTML_ERRMSG_SIZE 4096
bfc1508d (kx 2023-03-24 03:55:33 +0300 36)
bfc1508d (kx 2023-03-24 03:55:33 +0300 37) void html_error( const char *fmt, ... )
bfc1508d (kx 2023-03-24 03:55:33 +0300 38) {
bfc1508d (kx 2023-03-24 03:55:33 +0300 39) va_list arg_ptr;
bfc1508d (kx 2023-03-24 03:55:33 +0300 40) char buf[HTML_ERRMSG_SIZE];
bfc1508d (kx 2023-03-24 03:55:33 +0300 41) char msg[HTML_ERRMSG_SIZE];
bfc1508d (kx 2023-03-24 03:55:33 +0300 42) char *format = "%s: %s\n";
bfc1508d (kx 2023-03-24 03:55:33 +0300 43)
bfc1508d (kx 2023-03-24 03:55:33 +0300 44) va_start( arg_ptr, fmt );
bfc1508d (kx 2023-03-24 03:55:33 +0300 45)
bfc1508d (kx 2023-03-24 03:55:33 +0300 46) vsnprintf( msg, HTML_ERRMSG_SIZE, (const void *)fmt, arg_ptr );
bfc1508d (kx 2023-03-24 03:55:33 +0300 47)
bfc1508d (kx 2023-03-24 03:55:33 +0300 48) va_end( arg_ptr ); /* Reset variable arguments. */
bfc1508d (kx 2023-03-24 03:55:33 +0300 49)
bfc1508d (kx 2023-03-24 03:55:33 +0300 50) snprintf( buf, HTML_ERRMSG_SIZE, format, "http", msg );
bfc1508d (kx 2023-03-24 03:55:33 +0300 51)
bfc1508d (kx 2023-03-24 03:55:33 +0300 52) (void)write( STDERR_FILENO, buf, strlen( buf ) );
bfc1508d (kx 2023-03-24 03:55:33 +0300 53)
bfc1508d (kx 2023-03-24 03:55:33 +0300 54) exit( 1 );
bfc1508d (kx 2023-03-24 03:55:33 +0300 55) }
bfc1508d (kx 2023-03-24 03:55:33 +0300 56)
bfc1508d (kx 2023-03-24 03:55:33 +0300 57) html_errfunc html_fatal = html_error;
bfc1508d (kx 2023-03-24 03:55:33 +0300 58)
bfc1508d (kx 2023-03-24 03:55:33 +0300 59)
bfc1508d (kx 2023-03-24 03:55:33 +0300 60) void html_raw( const char *data, size_t size )
bfc1508d (kx 2023-03-24 03:55:33 +0300 61) {
bfc1508d (kx 2023-03-24 03:55:33 +0300 62) if( write( STDOUT_FILENO, data, size ) != size )
bfc1508d (kx 2023-03-24 03:55:33 +0300 63) html_fatal( "write error on html output" );
bfc1508d (kx 2023-03-24 03:55:33 +0300 64) }
bfc1508d (kx 2023-03-24 03:55:33 +0300 65)
bfc1508d (kx 2023-03-24 03:55:33 +0300 66) void html( const char *txt )
bfc1508d (kx 2023-03-24 03:55:33 +0300 67) {
bfc1508d (kx 2023-03-24 03:55:33 +0300 68) html_raw( txt, strlen(txt) );
bfc1508d (kx 2023-03-24 03:55:33 +0300 69) }
bfc1508d (kx 2023-03-24 03:55:33 +0300 70)
bfc1508d (kx 2023-03-24 03:55:33 +0300 71) void htmlf( const char *format, ... )
bfc1508d (kx 2023-03-24 03:55:33 +0300 72) {
bfc1508d (kx 2023-03-24 03:55:33 +0300 73) va_list args;
bfc1508d (kx 2023-03-24 03:55:33 +0300 74) struct strbuf buf = STRBUF_INIT;
bfc1508d (kx 2023-03-24 03:55:33 +0300 75)
bfc1508d (kx 2023-03-24 03:55:33 +0300 76) va_start( args, format );
bfc1508d (kx 2023-03-24 03:55:33 +0300 77) strbuf_vaddf( &buf, format, args );
bfc1508d (kx 2023-03-24 03:55:33 +0300 78) va_end( args );
bfc1508d (kx 2023-03-24 03:55:33 +0300 79) html( buf.buf );
bfc1508d (kx 2023-03-24 03:55:33 +0300 80) strbuf_release( &buf );
bfc1508d (kx 2023-03-24 03:55:33 +0300 81) }