fortune.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* =============================================================================
  2. * PROGRAM: ularn
  3. * FILENAME: fortune.c
  4. *
  5. * DESCRIPTION:
  6. * This module contains functions for displaying fortunes inside fortune
  7. * cookies.
  8. *
  9. * =============================================================================
  10. * EXPORTED VARIABLES
  11. *
  12. * None
  13. *
  14. * =============================================================================
  15. * EXPORTED FUNCTIONS
  16. *
  17. * fortune - Returns a random fortune string.
  18. * free_fortunes - Frees the fortunes data.
  19. *
  20. * =============================================================================
  21. */
  22. #include <stdio.h>
  23. #include "fortune.h"
  24. #include "header.h"
  25. /* =============================================================================
  26. * Local variables
  27. */
  28. #define MAX_FORTUNE_LEN 80
  29. /*
  30. * Data structure for a linked list of fortune strings read from the
  31. * fortune file
  32. */
  33. struct FortuneType {
  34. char Line[MAX_FORTUNE_LEN];
  35. struct FortuneType *Next;
  36. };
  37. static struct FortuneType *fortunes = NULL;
  38. static int fortune_read = 0; /* true if we have loaded the fortune info */
  39. static int nlines = 0; /* # lines in fortune database */
  40. /* =============================================================================
  41. * Exported functions
  42. */
  43. /* =============================================================================
  44. * FUNCTION: fortune
  45. */
  46. char *fortune(char *file) {
  47. FILE *fp;
  48. char *Line;
  49. char Buffer[80];
  50. struct FortuneType *NewFortune;
  51. int Len;
  52. int Pos;
  53. if (fortune_read == 0) {
  54. /* open the file */
  55. fp = fopen(file, "r");
  56. if (fp == NULL)
  57. /* can't find file */
  58. return 0;
  59. /* Read the fortune lines from the file */
  60. while (!feof(fp)) {
  61. Line = fgets(Buffer, MAX_FORTUNE_LEN, fp);
  62. if (Line != NULL) {
  63. /* trim white space and CR/LF fromt he end of the line */
  64. Len = strlen(Buffer);
  65. Len--;
  66. while ((Len > 0) && isspace((int)Buffer[Len])) {
  67. Buffer[Len] = 0;
  68. Len--;
  69. }
  70. /* Allocate a new fortune */
  71. NewFortune = (struct FortuneType *)malloc(sizeof(struct FortuneType));
  72. if (NewFortune == NULL)
  73. return NULL;
  74. strcpy(NewFortune->Line, Buffer);
  75. NewFortune->Next = fortunes;
  76. fortunes = NewFortune;
  77. nlines++;
  78. }
  79. }
  80. fortune_read = 1;
  81. fclose(fp);
  82. }
  83. if (fortune_read) {
  84. if (nlines > 0) {
  85. Pos = rund(nlines);
  86. NewFortune = fortunes;
  87. while (Pos > 0) {
  88. NewFortune = NewFortune->Next;
  89. Pos--;
  90. }
  91. return NewFortune->Line;
  92. } else
  93. return NULL;
  94. } else
  95. return NULL;
  96. }
  97. /* =============================================================================
  98. * FUNCTION: free_fortunes
  99. */
  100. void free_fortunes(void) {
  101. struct FortuneType *Fortune;
  102. while (fortunes != NULL) {
  103. Fortune = fortunes->Next;
  104. free(fortunes);
  105. fortunes = Fortune;
  106. }
  107. }