Corretta cattiva riallocazione su linee multiple, iniziato a inserire
[gapil.git] / sources / FortuneServer.c
index c00b3fedf5a80ca33d6de56dbb3652bd97b8132c..e767c1c849e8252650c5e91f0fce1c73227f05f4 100644 (file)
@@ -26,7 +26,7 @@
  *
  * Usage: fortuned -h give all info
  *
- * $Id: FortuneServer.c,v 1.1 2002/08/18 10:34:10 piccardi Exp $
+ * $Id: FortuneServer.c,v 1.2 2002/08/18 14:38:04 piccardi Exp $
  *
  ****************************************************************/
 /* 
@@ -50,9 +50,8 @@ 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";
+    int i, n = 0;
+    char *fortunefilename = "/usr/share/games/fortunes/kids";
     char *fifoname = "/tmp/fortune.fifo";
     char **fortune;
     char line[80];
@@ -62,8 +61,7 @@ int main(int argc, char *argv[])
      * Input section: decode parameters passed in the calling 
      * Use getopt function
      */
-    int i;
-    opterr = 0;         /* don't want writing to stderr */
+    opterr = 0;                                     /* don't want writing to stderr */
     while ( (i = getopt(argc, argv, "hn:f:")) != -1) {
        switch (i) {
        /* 
@@ -81,10 +79,10 @@ int main(int argc, char *argv[])
            n = strtol(optarg, NULL, 10);
            fortune = (char **) calloc(sizeof(*fortune), n);
            break;
-       case '?':   /* unrecognized options */
+       case '?':                                    /* unrecognized options */
            printf("Unrecognized options -%c\n",optopt);
            usage();
-       default:    /* should not reached */
+       default:                                       /* should not reached */
            usage();
        }
     }
@@ -95,35 +93,35 @@ int main(int argc, char *argv[])
      *               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]);
+    if (n==0) usage();          /* if no pool depth exit printing usage info */
+    i = FortuneParse(fortunefilename, fortune, n);          /* parse phrases */
+    for (n=0; n<i; n++) debug("%s\n\n", fortune[n]);
     /* 
      * Comunication section 
      */
-    if (mkfifo(fifoname, 0622)) {
+    if (mkfifo(fifoname, 0622)) {  /* create well known fifo if does't exist */
        if (errno!=EEXIST) {
            perror("Cannot create well known fifo");
            exit(-1);
        }
     }
+    /* Main body: loop over requests */
     while (1) {
-       fifo_server = open(fifoname, O_RDONLY);
+       fifo_server = open(fifoname, O_RDONLY);      /* open well known fifo */
        if (fifo_server < 0) {
            perror("Cannot open well known fifo");
            exit(-1);
        }
-       nread = read(fifo_server, line, 79);
+       nread = read(fifo_server, line, 79);                 /* read request */
        line[nread] = 0;
        debug("%s %d\n", line,nread);
-       n = random() % i;
+       n = random() % i;                             /* select random value */
        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);
+       fifo_client = open(line, O_WRONLY);              /* open client fifo */
+       nread = write(fifo_client,                           /* write phrase */
+                     fortune[n], strlen(fortune[n])+1);
+       close(fifo_client);                         /* close well known fifo */
+       close(fifo_server);                             /* close client fifo */
     }
 }
 /*