--- /dev/null
+/* FortuneClient.c
+ *
+ * Copyright (C) 2002 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program fortune
+ * Fortune client
+ *
+ * Author: Simone Piccardi
+ * Aug. 2002
+ *
+ * Usage: fortune -h give all info
+ *
+ * $Id: FortuneClient.c,v 1.1 2002/08/18 10:34:09 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <sys/types.h> /* predefined types */
+#include <unistd.h> /* include unix standard library */
+#include <arpa/inet.h> /* IP addresses conversion utiliites */
+#include <stdio.h> /* include standard I/O library */
+#include <errno.h>
+#include <fcntl.h>
+
+#include "macros.h"
+
+/* Subroutines declaration */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/* Variables definition */
+ int n = 0;
+ char *fortunefilename = "/tmp/fortune.fifo";
+ char line[80];
+ int fifo_server, fifo_client;
+ char fifoname[80];
+ int nread;
+ char buffer[PIPE_BUF];
+ /*
+ * Input section: decode parameters passed in the calling
+ * Use getopt function
+ */
+ int i;
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "hf:")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h':
+ printf("Wrong -h option use\n");
+ usage();
+ return(0);
+ break;
+ case 'f':
+ fortunefilename = optarg;
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ snprintf(fifoname, 80, "/tmp/fortune.%d", getpid());
+ if (mkfifo(fifoname, 0622)) {
+ if (errno!=EEXIST) {
+ perror("Cannot create well known fifo");
+ exit(-1);
+ }
+ }
+ fifo_server = open(fortunefilename, O_WRONLY);
+ if (fifo_server < 0) {
+ perror("Cannot open well known fifo");
+ exit(-1);
+ }
+ debug("%s\n", fifoname);
+ nread = write(fifo_server, fifoname, strlen(fifoname)+1);
+ close(fifo_server);
+ fifo_client = open(fifoname, O_RDONLY);
+ if (fifo_client < 0) {
+ perror("Cannot open well known fifo");
+ exit(-1);
+ }
+ nread = read(fifo_client, buffer, sizeof(buffer));
+ printf("%s", buffer);
+ close(fifo_client);
+ close(fifo_server);
+ unlink(fifoname);
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Elementary fortune server\n");
+ printf("Usage:\n");
+ printf(" fortune [-h] [-f] \n");
+ printf(" -h print this help\n");
+ printf(" -f filename set file for fortunes\n");
+ exit(1);
+}
--- /dev/null
+/* FortuneParse.c
+ *
+ * Copyright (C) 2002 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Routine FortuneParse
+ * parse fortunes from a fortune file
+ *
+ * Author: Simone Piccardi
+ * Aug. 2002
+ *
+ * Usage: int FortuneParse(char *file, char ** fortune, int n);
+ * Read n fortunes from fortune file file, and put it into the
+ * string array fortune
+ *
+ * $Id: FortuneParse.c,v 1.1 2002/08/18 10:34:10 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <sys/types.h> /* predefined types */
+#include <sys/stat.h> /* */
+#include <unistd.h> /* include unix standard library */
+#include <stdio.h> /* include standard I/O library */
+#include <stdlib.h> /* standard library */
+#include <string.h> /* ANSI C standard string */
+#include <errno.h> /* error definitions */
+#include <fcntl.h> /* */
+
+/* Subroutines declaration */
+extern void usage(void);
+
+int FortuneParse(char *file, char **fortune, int n)
+{
+/* Variables definition */
+ FILE *fortunefile;
+ char line[80];
+ int i;
+ /*
+ * fortune file scanning, read string in memory
+ */
+ fortunefile = fopen(file,"r");
+ if (fortunefile == NULL) { /* on open error exit */
+ perror("Opening fortune file");
+ exit(-1);
+ }
+ i = 0;
+ do {
+ if (!fgets(line, 80, fortunefile)) {
+ if (feof(fortunefile)) break;
+ perror("Read error");
+ exit(-1);
+ }
+ if (line[0]=='\n') {
+ if (fortune[i]!=NULL) i++;
+ continue;
+ }
+ if (fortune[i]==NULL) {
+ fortune[i] = (char *) malloc(strlen(line)+1);
+ strncpy(fortune[i], line, strlen(line)+1);
+ } else {
+ fortune[i] = (char *) realloc(fortune[i], strlen(line)+1);
+ strncat(fortune[i], line, strlen(line)+1);
+ }
+ } while (i<n);
+ return i;
+}
--- /dev/null
+/* FortuneServer.c
+ *
+ * Copyright (C) 2002 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Program fortuned
+ * Fortune server
+ *
+ * Author: Simone Piccardi
+ * Aug. 2002
+ *
+ * Usage: fortuned -h give all info
+ *
+ * $Id: FortuneServer.c,v 1.1 2002/08/18 10:34:10 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <sys/types.h> /* predefined types */
+#include <sys/stat.h> /* */
+#include <unistd.h> /* include unix standard library */
+#include <stdio.h> /* include standard I/O library */
+#include <stdlib.h> /* standard library */
+#include <string.h> /* ANSI C standard string */
+#include <errno.h> /* errorstring */
+#include <fcntl.h> /* */
+
+#include "macros.h"
+
+/* Subroutines declaration */
+void usage(void);
+int FortuneParse(char *file, char **fortune, int n);
+
+int main(int argc, char *argv[])
+{
+/* Variables definition */
+ int n = 0;
+ FILE *fortunefile;
+ char *fortunefilename = "/usr/share/misc/fortune/fortune.it";
+ char *fifoname = "/tmp/fortune.fifo";
+ char **fortune;
+ char line[80];
+ int fifo_server, fifo_client;
+ int nread;
+ /*
+ * Input section: decode parameters passed in the calling
+ * Use getopt function
+ */
+ int i;
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h':
+ printf("Wrong -h option use\n");
+ usage();
+ return(0);
+ break;
+ case 'f':
+ fortunefilename = optarg;
+ break;
+ case 'n':
+ n = strtol(optarg, NULL, 10);
+ fortune = (char **) calloc(sizeof(*fortune), n);
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ if (n==0) { /* if no pool depth exit */
+ usage(); /* print usage info */
+ }
+ i = FortuneParse(fortunefilename, fortune, n);
+ for (n=0; n<i; n++) debug("%s", fortune[n]);
+ /*
+ * Comunication section
+ */
+ if (mkfifo(fifoname, 0622)) {
+ if (errno!=EEXIST) {
+ perror("Cannot create well known fifo");
+ exit(-1);
+ }
+ }
+ while (1) {
+ fifo_server = open(fifoname, O_RDONLY);
+ if (fifo_server < 0) {
+ perror("Cannot open well known fifo");
+ exit(-1);
+ }
+ nread = read(fifo_server, line, 79);
+ line[nread] = 0;
+ debug("%s %d\n", line,nread);
+ n = random() % i;
+ debug("fortune[%d]=%s\n", n, fortune[n]);
+ fifo_client = open(line, O_WRONLY);
+ nread = write(fifo_client, fortune[n], strlen(fortune[n])+1);
+ close(fifo_client);
+ close(fifo_server);
+ }
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Elementary fortune server\n");
+ printf("Usage:\n");
+ printf(" fortuned [-h] [-f] -n XXX \n");
+ printf(" -h print this help\n");
+ printf(" -f filename set file for fortunes\n");
+ printf(" -n XXX set pool depth\n");
+ exit(1);
+}
FINAL = forktest errcode echo echod daytimed iterdaytimed daytime testfopen \
testren
+all: fortune fortuned
+
+fortune: FortuneClient.c
+ $(CC) $^ -o $@
+
+fortuned: FortuneServer.c FortuneParse.c
+ $(CC) $^ -o $@
+
+
barcode: BarCode.c
$(CC) $(CFLAGS) $^ -o $@