kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit a477c9a2a3f518daeb259c9dedaea3dfcaa3e74f
parent 93fd33e00e2a593a43075645780dc882b84f4870
Author: Joris Vink <joris@coders.se>
Date:   Sun,  3 Aug 2014 16:59:19 +0200

Move contrib/examples/generic into the new build framework

Diffstat:
contrib/examples/generic/assets/index.html | 15+++++++++++++++
contrib/examples/generic/assets/intro.jpg | 0
contrib/examples/generic/assets/params.html | 33+++++++++++++++++++++++++++++++++
contrib/examples/generic/assets/private.html | 16++++++++++++++++
contrib/examples/generic/assets/private_test.html | 15+++++++++++++++
contrib/examples/generic/assets/style.css | 16++++++++++++++++
contrib/examples/generic/assets/upload.html | 22++++++++++++++++++++++
contrib/examples/generic/build.sh | 79-------------------------------------------------------------------------------
contrib/examples/generic/conf/generic.conf | 49+++++++++++++++++++++++++++++++++++++++++++++++++
contrib/examples/generic/media/index.html | 15---------------
contrib/examples/generic/media/intro.jpg | 0
contrib/examples/generic/media/params.html | 33---------------------------------
contrib/examples/generic/media/private.html | 16----------------
contrib/examples/generic/media/private_test.html | 15---------------
contrib/examples/generic/media/style.css | 16----------------
contrib/examples/generic/media/upload.html | 22----------------------
contrib/examples/generic/module.conf | 193-------------------------------------------------------------------------------
contrib/examples/generic/src/example.c | 26+++++++++++++-------------
contrib/examples/generic/tools/inject.c | 119-------------------------------------------------------------------------------
19 files changed, 179 insertions(+), 521 deletions(-)

diff --git a/contrib/examples/generic/assets/index.html b/contrib/examples/generic/assets/index.html @@ -0,0 +1,15 @@ +<!DOCTYPE> +<html> +<head> + <link rel="stylesheet" href="/css/style.css" type="text/css"> + <title>Your KORE module worked!</title> +</head> + +<body> + +<div class="content"> + <p>Your first Kore module worked.</p> +</div> + +</body> +</html> diff --git a/contrib/examples/generic/assets/intro.jpg b/contrib/examples/generic/assets/intro.jpg Binary files differ. diff --git a/contrib/examples/generic/assets/params.html b/contrib/examples/generic/assets/params.html @@ -0,0 +1,33 @@ +<!DOCTYPE> +<html> +<head> + <link rel="stylesheet" href="/css/style.css" type="text/css"> + <title>Kore params test</title> +</head> + +<body style="overflow: auto"> + +<div class="content" style="font-size: 12px; font-weight: normal"> + <p>You can pass one GET parameter (arg1), any other GET parameter will + be filtered out</p> + <p>Only two out of the three input fields will be visible to Kore.</p> + <p>The first field accepts the input "test"</p> + <p>The second field accepts anything like /test/[a-z]*</p> + <p>The third field will be removed by Kore, as it is not in the params + block configured for this page.</p> + <form method="POST"> + <input type="input" name="test1" value="$test1$"> + <input type="input" name="test2" value="$test2$"> + <input type="input" name="test3" value="$test3$"> + <input type="submit"> + </form> + + <p style="font-size: 12px; font-weight: normal">GET param arg1: $arg1$</p> + <p style="font-size: 12px; font-weight: normal">GET param arg2: $arg2$</p> + <p style="font-size: 12px; font-weight: normal">test1: $test1$</p> + <p style="font-size: 12px; font-weight: normal">test2: $test2$</p> + <p style="font-size: 12px; font-weight: normal">test3: $test3$</p> +</div> + +</body> +</html> diff --git a/contrib/examples/generic/assets/private.html b/contrib/examples/generic/assets/private.html @@ -0,0 +1,16 @@ +<!DOCTYPE> +<html> +<head> + <link rel="stylesheet" href="/css/style.css" type="text/css"> + <title>Kore Authentication tests</title> +</head> + +<body> + +<div class="content"> + <p style="font-size: small">The cookie session_id should now be set.</p> + <p style="font-size: small">You can continue to <a href="/private/test">view page handler in auth block</a></p> +</div> + +</body> +</html> diff --git a/contrib/examples/generic/assets/private_test.html b/contrib/examples/generic/assets/private_test.html @@ -0,0 +1,15 @@ +<!DOCTYPE> +<html> +<head> + <link rel="stylesheet" href="/css/style.css" type="text/css"> + <title>Kore Authentication tests</title> +</head> + +<body> + +<div class="content"> + <p style="font-size: small">If you see this, the authentication worked. This page should redirect back to /private once you remove your session_id cookie.</p> +</div> + +</body> +</html> diff --git a/contrib/examples/generic/assets/style.css b/contrib/examples/generic/assets/style.css @@ -0,0 +1,16 @@ +body { + width: 100%; + margin: 0px; + color: #000; + overflow: hidden; + background-color: #fff; +} + +.content { + width: 800px; + margin-left: auto; + margin-right: auto; + margin-top: 100px; + font-size: 60px; + text-align: center; +} diff --git a/contrib/examples/generic/assets/upload.html b/contrib/examples/generic/assets/upload.html @@ -0,0 +1,22 @@ +<!DOCTYPE> +<html> +<head> + <link rel="stylesheet" href="/css/style.css" type="text/css"> + <title>Kore upload test</title> +</head> + +<body style="overflow: auto"> + +<div class="content"> + <form method="POST" enctype="multipart/form-data"> + <input type="input" name="firstname"> + <input type="file" name="file"> + <input type="submit" value="upload"> + </form> + + <p style="font-size: 12px; font-weight: normal">$upload$</p> + <p style="font-size: 12px; font-weight: normal">$firstname$</p> +</div> + +</body> +</html> diff --git a/contrib/examples/generic/build.sh b/contrib/examples/generic/build.sh @@ -1,79 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2013 Joris Vink <joris@coders.se> -# -# Kore module build script, use this as a base for building -# your own modules for kore. - -# The name of the module you will be building -MODULE=example.module - -# The directory containing all your media files (HTML, CSS, ...). -# These files will be compiled into the module and symbols will -# be exported for you to use in your code. -MEDIA_DIR=media - -# The directory containing your module source. -SOURCE_DIR=src - -# Compiler settings. -CC=gcc -CFLAGS="-I. -I/usr/local/include -Wall -Wstrict-prototypes \ - -Wmissing-prototypes -Wmissing-declarations -Wshadow \ - -Wpointer-arith -Wcast-qual -Wsign-compare -g" - -OSNAME=$(uname -s | sed -e 's/[-_].*//g' | tr A-Z a-z) -if [ "${OSNAME}" = "darwin" ]; then - LDFLAGS="-dynamiclib -undefined suppress -flat_namespace" -else - LDFLAGS="-shared" -fi - -MODULE_BUILD_DATE=$(date +"%Y-%m-%d %H:%M:%S") - -### Begin building #### -echo "Building module ${MODULE}..." -rm -f ${MODULE} - -${CC} ${CFLAGS} tools/inject.c -o tools/inject - -if [ ! -d ${SOURCE_DIR}/${MEDIA_DIR} ]; then - mkdir ${SOURCE_DIR}/${MEDIA_DIR}; -fi -rm -f ${SOURCE_DIR}/${MEDIA_DIR}/* - -if [ ! -d .objs ]; then - mkdir .objs; -fi -rm -f .objs/* - -rm -f static.h - -for file in `find ${MEDIA_DIR} -type f \( ! -name \*.swp \)`; do - echo "Injecting $file"; - base=`basename $file`; - ./tools/inject $file $base > ${SOURCE_DIR}/${MEDIA_DIR}/${base}.c; - if [ $? -ne 0 ]; then - echo "Injection error, check above messages for clues."; - exit 1; - fi -done - -echo "#define MODULE_BUILD_DATE \"${MODULE_BUILD_DATE}\"" >> static.h - -for src in `find ${SOURCE_DIR} -type f -name \*.c`; do - base=`basename $src`; - ${CC} ${CFLAGS} -fPIC -c $src -o .objs/${base}.o - if [ $? -ne 0 ]; then - echo "Build error, check above messages for clues."; - exit 1; - fi -done - -${CC} ${LDFLAGS} `find .objs -name \*.o -type f` -o ${MODULE} -echo "Building completed!" - -rm -rf ${SOURCE_DIR}/${MEDIA_DIR} -rm -rf .objs -rm -f tools/inject -rm -f static.h diff --git a/contrib/examples/generic/conf/generic.conf b/contrib/examples/generic/conf/generic.conf @@ -0,0 +1,49 @@ +# Placeholder configuration + +bind 127.0.0.1 8888 +pidfile kore.pid + +load ./generic.so example_load + +validator v_example function v_example_func +validator v_regex regex ^/test/[a-z]*$ +validator v_number regex ^[0-9]*$ +validator v_session function v_session_validate + +ssl_no_compression + +authentication auth_example { + authentication_type cookie + authentication_value session_id + authentication_validator v_session + authentication_uri /private +} + +domain 127.0.0.1 { + certfile cert/server.crt + certkey cert/server.key + accesslog kore_access.log + + static /css/style.css serve_style_css + static / serve_index + static /intro.jpg serve_intro + static /b64test serve_b64test + static /spdy-reset serve_spdyreset + static /upload serve_file_upload + static /lock-test serve_lock_test + static /validator serve_validator + static /params-test serve_params_test + static /private serve_private + + static /private/test serve_private_test auth_example + + params post /params-test { + validate test1 v_example + validate test2 v_regex + } + + params get /params-test { + validate arg1 v_example + validate id v_number + } +} diff --git a/contrib/examples/generic/media/index.html b/contrib/examples/generic/media/index.html @@ -1,15 +0,0 @@ -<!DOCTYPE> -<html> -<head> - <link rel="stylesheet" href="/css/style.css" type="text/css"> - <title>Your KORE module worked!</title> -</head> - -<body> - -<div class="content"> - <p>Your first Kore module worked.</p> -</div> - -</body> -</html> diff --git a/contrib/examples/generic/media/intro.jpg b/contrib/examples/generic/media/intro.jpg Binary files differ. diff --git a/contrib/examples/generic/media/params.html b/contrib/examples/generic/media/params.html @@ -1,33 +0,0 @@ -<!DOCTYPE> -<html> -<head> - <link rel="stylesheet" href="/css/style.css" type="text/css"> - <title>Kore params test</title> -</head> - -<body style="overflow: auto"> - -<div class="content" style="font-size: 12px; font-weight: normal"> - <p>You can pass one GET parameter (arg1), any other GET parameter will - be filtered out</p> - <p>Only two out of the three input fields will be visible to Kore.</p> - <p>The first field accepts the input "test"</p> - <p>The second field accepts anything like /test/[a-z]*</p> - <p>The third field will be removed by Kore, as it is not in the params - block configured for this page.</p> - <form method="POST"> - <input type="input" name="test1" value="$test1$"> - <input type="input" name="test2" value="$test2$"> - <input type="input" name="test3" value="$test3$"> - <input type="submit"> - </form> - - <p style="font-size: 12px; font-weight: normal">GET param arg1: $arg1$</p> - <p style="font-size: 12px; font-weight: normal">GET param arg2: $arg2$</p> - <p style="font-size: 12px; font-weight: normal">test1: $test1$</p> - <p style="font-size: 12px; font-weight: normal">test2: $test2$</p> - <p style="font-size: 12px; font-weight: normal">test3: $test3$</p> -</div> - -</body> -</html> diff --git a/contrib/examples/generic/media/private.html b/contrib/examples/generic/media/private.html @@ -1,16 +0,0 @@ -<!DOCTYPE> -<html> -<head> - <link rel="stylesheet" href="/css/style.css" type="text/css"> - <title>Kore Authentication tests</title> -</head> - -<body> - -<div class="content"> - <p style="font-size: small">The cookie session_id should now be set.</p> - <p style="font-size: small">You can continue to <a href="/private/test">view page handler in auth block</a></p> -</div> - -</body> -</html> diff --git a/contrib/examples/generic/media/private_test.html b/contrib/examples/generic/media/private_test.html @@ -1,15 +0,0 @@ -<!DOCTYPE> -<html> -<head> - <link rel="stylesheet" href="/css/style.css" type="text/css"> - <title>Kore Authentication tests</title> -</head> - -<body> - -<div class="content"> - <p style="font-size: small">If you see this, the authentication worked. This page should redirect back to /private once you remove your session_id cookie.</p> -</div> - -</body> -</html> diff --git a/contrib/examples/generic/media/style.css b/contrib/examples/generic/media/style.css @@ -1,16 +0,0 @@ -body { - width: 100%; - margin: 0px; - color: #000; - overflow: hidden; - background-color: #fff; -} - -.content { - width: 800px; - margin-left: auto; - margin-right: auto; - margin-top: 100px; - font-size: 60px; - text-align: center; -} diff --git a/contrib/examples/generic/media/upload.html b/contrib/examples/generic/media/upload.html @@ -1,22 +0,0 @@ -<!DOCTYPE> -<html> -<head> - <link rel="stylesheet" href="/css/style.css" type="text/css"> - <title>Kore upload test</title> -</head> - -<body style="overflow: auto"> - -<div class="content"> - <form method="POST" enctype="multipart/form-data"> - <input type="input" name="firstname"> - <input type="file" name="file"> - <input type="submit" value="upload"> - </form> - - <p style="font-size: 12px; font-weight: normal">$upload$</p> - <p style="font-size: 12px; font-weight: normal">$firstname$</p> -</div> - -</body> -</html> diff --git a/contrib/examples/generic/module.conf b/contrib/examples/generic/module.conf @@ -1,193 +0,0 @@ -# Example Kore configuration - -# Server configuration. -bind 127.0.0.1 443 - -# The path worker processes will chroot into after starting. -chroot /home/joris/src/kore - -# Worker processes will run as the specified user. -runas joris - -# Set workers to the amount of CPU's available in your system, -# kore will automatically distribute all workers on them. -workers 4 - -# The number of active connections each worker can handle. -# You might have to tweak this number based on your hardware. -#worker_max_connections 250 - -# Store the main process its pid in this file. -#pidfile /var/run/kore.pid - -# You can define a callback Kore calls from its parent process or -# workers everytime the kore_cb_interval timer (in milliseconds) is reached. -# -# NOTE: Remember that the parent process runs as root and is not chroot(). -# NOTE: If you want the cb to run on a worker, be sure to set kore_cb_worker. -#kore_cb my_callback -#kore_cb_interval 1000 -#kore_cb_worker 3 - -# HTTP specific settings. -# http_header_max Maximum size of HTTP headers (in bytes). -# -# http_postbody_max Maximum size of an HTTP POST body (in bytes). -# -# http_keepalive_time Maximum seconds an HTTP connection can be -# kept alive by the browser. -# (Set to 0 to disable keepalive completely). -# -# http_hsts_enable Send Strict Transport Security header in -# all responses. Parameter is the age. -# (Set to 0 to disable sending this header). -#http_header_max 4096 -#http_postbody_max 10240000 -#http_keepalive_time 0 -#http_hsts_enable 31536000 - -# Load modules (you can load multiple at the same time). -# An additional parameter can be specified as the "onload" function -# which Kore will call when the module is loaded/reloaded. -load contrib/examples/generic/example.module example_load - -# Validators -# validator name type regex|function -# -validator v_example function v_example_func -validator v_regex regex ^/test/[a-z]*$ -validator v_number regex ^[0-9]*$ -validator v_session function v_session_validate - -# Specify the SSL ciphers that will be used. -#ssl_cipher ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK - -# If you wish to use EDH / ECDH specify a file containing -# a generated DH key (See OpenSSL dhparam). -#ssl_dhparam dh2048.pem - -# Set this if you want to disable SSL zlib compression. -ssl_no_compression - -# Specify the amount of seconds a SPDY connection is kept open. -# You can keep it open indefinately by setting this to 0. -#spdy_idle_time 120 - -# Authentication configuration -# -# Using authentication blocks you can define a standard way for -# Kore to validate your users. In the example below we create -# a authentication block called auth_example, which requires -# a cookie (session_id) to be set. -# -# If no cookie is present or the cookie is not valid according -# to the set validator, Kore will redirect the browser to the -# URI set in authentication_uri. -# -# Page handlers can be bound to authentication by specifying -# authentication block at the end of the page directive (see below). -authentication auth_example { - # The authentication type denotes the way the user should - # be authenticated. - # - # Allow values: - # - cookie (checks for the cookie presence + pass to validator) - # - header (checks for header presence + pass to validator) - # - requuest (passes the http_request to the validator) - # - # Use cases for request could for example be IP based ACLs or - # any other criteria that can be extracted from a http_request. - # - # The request type does not need an authentication_validator. - # - authentication_type cookie - - # The name of whatever we are looking for. - authentication_value session_id - - # The validator that will be called to verify the cookie. - # Note this is YOUR validator, Kore does not have built-in - # session support. You must add this manually using your - # preferred method (Storing it in postgres, redis, ...) - authentication_validator v_session - - # The URI Kore will redirect to if a authentication fails. - # If this is not set, Kore will return a simple 403. - authentication_uri /private -} - -# Domain configuration -# -# Each domain configuration starts with listing what domain -# the directives that follow are to be applied upon. -# -# Additionally you can specify the following in a domain configuration: -# -# accesslog -# - File where all requests are logged. -# require_client_cert -# - Asks the client to present a certificate -# matching the CA given to require_client_cert -# -# Handlers -# -# Handlers are either static (for fixed paths) or dynamic. -# Dynamic handlers take a POSIX regular expression as its path. -# -# Syntax: -# handler path module_callback [auth block] -# -# Note that the auth block is optional and if set will force Kore to -# authenticate the user according to the authentication block its settings -# before allowing access to the page. - -# Example domain that responds to localhost. -domain localhost { - certfile cert/server.crt - certkey cert/server.key - accesslog /var/log/kore_access.log - - # Page handlers with no authentication required. - static /css/style.css serve_style_css - static / serve_index - static /intro.jpg serve_intro - static /b64test serve_b64test - static /spdy-reset serve_spdyreset - static /upload serve_file_upload - static /lock-test serve_lock_test - static /validator serve_validator - static /params-test serve_params_test - static /private serve_private - - # Page handlers with authentication. - static /private/test serve_private_test auth_example - - # Configure /params-test POST to only accept the following parameters. - # They are automatically tested against the validator listed. - # If the validator would fail Kore will automatically remove the - # failing parameter, indicating something was wrong with it. - # Any parameters not present in the params block are also filtered out. - params post /params-test { - validate test1 v_example - validate test2 v_regex - } - - # Configure a GET parameter that /params-test can received. As before - # this is validated against the validator and removed if validation - # fails. All extra parameters in the GET query are filtered out. - params get /params-test { - validate arg1 v_example - validate id v_number - } -} - -#domain domain.com { -# certfile cert/other/server.crt -# certkey cert/other/server.key -# accesslog /var/log/other_kore_access.log -# require_client_cert cert/other/ca.crt - -# static /css/style.css serve_style_css -# static / serve_index -# dynamic ^/[a-z0-9_]*$ serve_profile -#} diff --git a/contrib/examples/generic/src/example.c b/contrib/examples/generic/src/example.c @@ -17,7 +17,7 @@ #include <kore/kore.h> #include <kore/http.h> -#include "static.h" +#include "assets.h" void example_load(int); @@ -79,15 +79,15 @@ serve_style_css(struct http_request *req) kore_debug("header was present with %ld", tstamp); } - if (tstamp != 0 && tstamp <= static_mtime_css_style) { + if (tstamp != 0 && tstamp <= asset_mtime_style_css) { http_response(req, 304, NULL, 0); } else { - date = kore_time_to_date(static_mtime_css_style); + date = kore_time_to_date(asset_mtime_style_css); if (date != NULL) http_response_header_add(req, "last-modified", date); http_response_header_add(req, "content-type", "text/css"); - http_response(req, 200, static_css_style, static_len_css_style); + http_response(req, 200, asset_style_css, asset_len_style_css); } return (KORE_RESULT_OK); @@ -97,7 +97,7 @@ int serve_index(struct http_request *req) { http_response_header_add(req, "content-type", "text/html"); - http_response(req, 200, static_html_index, static_len_html_index); + http_response(req, 200, asset_index_html, asset_len_index_html); return (KORE_RESULT_OK); } @@ -106,7 +106,7 @@ int serve_intro(struct http_request *req) { http_response_header_add(req, "content-type", "image/jpg"); - http_response(req, 200, static_jpg_intro, static_len_jpg_intro); + http_response(req, 200, asset_intro_jpg, asset_len_intro_jpg); return (KORE_RESULT_OK); } @@ -148,8 +148,8 @@ serve_file_upload(struct http_request *req) u_int32_t len; char *name, buf[BUFSIZ]; - b = kore_buf_create(static_len_html_upload); - kore_buf_append(b, static_html_upload, static_len_html_upload); + b = kore_buf_create(asset_len_upload_html); + kore_buf_append(b, asset_upload_html, asset_len_upload_html); if (req->method == HTTP_METHOD_POST) { http_populate_multipart_form(req, &r); @@ -253,8 +253,8 @@ serve_params_test(struct http_request *req) http_populate_arguments(req); - b = kore_buf_create(static_len_html_params); - kore_buf_append(b, static_html_params, static_len_html_params); + b = kore_buf_create(asset_len_params_html); + kore_buf_append(b, asset_params_html, asset_len_params_html); /* * The GET parameters will be filtered out on POST. @@ -313,7 +313,7 @@ serve_private(struct http_request *req) { http_response_header_add(req, "content-type", "text/html"); http_response_header_add(req, "set-cookie", "session_id=test123"); - http_response(req, 200, static_html_private, static_len_html_private); + http_response(req, 200, asset_private_html, asset_len_private_html); return (KORE_RESULT_OK); } @@ -323,8 +323,8 @@ serve_private_test(struct http_request *req) { http_response_header_add(req, "content-type", "text/html"); - http_response(req, 200, static_html_private_test, - static_len_html_private_test); + http_response(req, 200, asset_private_test_html, + asset_len_private_test_html); return (KORE_RESULT_OK); } diff --git a/contrib/examples/generic/tools/inject.c b/contrib/examples/generic/tools/inject.c @@ -1,119 +0,0 @@ -/* - * Copyright (c) 2013 Joris Vink <joris@coders.se> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <sys/param.h> -#include <sys/stat.h> - -#include <ctype.h> -#include <err.h> -#include <errno.h> -#include <inttypes.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#if defined(OpenBSD) -#define PRI_TIME_T "d" -#endif - -#if defined(linux) -#if defined(__x86_64__) -#define PRI_TIME_T PRIu64 -#else -#define PRI_TIME_T "ld" -#endif -#endif - -#if defined(__MACH__) -#define PRI_TIME_T "ld" -#endif - -int -main(int argc, char *argv[]) -{ - struct stat st; - ssize_t len; - FILE *hdr; - char *ext; - unsigned char c[1]; - int fd, newline, count; - - if (argc != 3) - exit(1); - - if ((fd = open(argv[1], O_RDONLY)) == -1) - err(1, "open() %d", errno); - if ((hdr = fopen("static.h", "a+")) == NULL) - err(1, "fopen() %d", errno); - if ((ext = strchr(argv[2], '.')) != NULL) - *(ext)++ = '\0'; - else - ext = ""; - - if (stat(argv[1], &st) == -1) { - printf("stat(%s) failed: %d\n", argv[1], errno); - exit(1); - } - - printf("/**** AUTO GENERATED BY MAKEFILE - DO NOT TOUCH ****/\n"); - printf("#include <sys/param.h>\n\n"); - printf("u_int8_t static_%s_%s[] = {", ext, argv[2]); - - len = 0; - count = 0; - newline = 1; - for (;;) { - if (newline) { - printf("\n\t"); - count = 0; - newline = 0; - } - - len = read(fd, c, 1); - if (len == 0) - break; - - if (len == -1) { - printf("read(): %d\n", errno); - exit(1); - } - - if (len != 1) - exit(1); - - printf("0x%02x, ", c[0]); - if (count++ == 10) - newline = 1; - } - - close(fd); - - printf("};\n\n"); - printf("u_int32_t static_len_%s_%s = %" PRIu32 ";\n", - ext, argv[2], (u_int32_t)st.st_size); - - printf("time_t static_mtime_%s_%s = %" PRI_TIME_T ";\n", - ext, argv[2], st.st_mtime); - - fprintf(hdr, "extern u_int8_t static_%s_%s[];\n", ext, argv[2]); - fprintf(hdr, "extern u_int32_t static_len_%s_%s;\n", ext, argv[2]); - fprintf(hdr, "extern time_t static_mtime_%s_%s;\n", ext, argv[2]); - fclose(hdr); - - return (0); -}