Print message

Table of Contents

1. Print message

My brain has a memory loss event so I can't use the #define directive tonight. But we can use <string.h> to extract a message from a QUERYSTRING like ?message=foo bar, but only in position 1 for now. Also you have to manually add the message in the request (address bar) if you want to see anything, like: https://screwtape.sdf.org/cgi-bin/print-message.cgi?message=spaces_wont_print&later=ignored

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

size_t MYMAXLEN =	64;
size_t MYMAXTOKENS =	64;

int
main(void)
{
  char *message, *totokenize;
  size_t message_len;

  char s[512], *p, *tokens[MYMAXTOKENS];
  char *last;
  int i = 0;    

  /* request was ok */
  printf("Status: 200 OK\r\n");
  printf("Content-Type: text/html\r\n");
  printf("\r\n");
  printf("<html>\n");

  /* See if there was a message parameter in the QUERY_STRING */
  message = getenv("QUERY_STRING");
  if (message == NULL) goto done;

  message_len = strnlen(message, MYMAXLEN);
  if (message_len == 0) goto done;

  (void)strlcpy(s, message, MYMAXLEN);

  for ((p = strtok_r(s, "&=", &last)); p;
      (p = strtok_r(NULL, "&=", &last)))
      if (i < MYMAXTOKENS - 1) tokens[i++]= p;
  tokens[i] = NULL;

  printf("<p>Message:%s</p>\n",
	 strcmp("message", tokens[0]) !=0 ? "Is not at position 0" : tokens[1]);

  done:
  printf("</html>\n");
  return EXIT_SUCCESS;
}
Status: 200 OK
Content-Type: text/html

<html>
</html>

Author: vfts

Created: 2022-11-14 Mon 03:48