home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / pfixp10.lzh / PREFIX.C < prev    next >
C/C++ Source or Header  |  1993-08-22  |  6KB  |  245 lines

  1. /*
  2.  
  3. PREFIX -- Stores Squish lastread pointers before SQFIX
  4.  
  5. Version 1.0  (10/12/92)
  6.  
  7. Written by Bob Quinlan of Austin, Texas, USA
  8. Sysop of Red October at 512-834-2593 (1:382/111)
  9.  
  10. Copyright 1992 by Bob Quinlan
  11.  
  12. Compatible with Maximus 2.00 and 2.01
  13.  
  14.  
  15. This program is intended to be used before SQFIX and various other
  16. utilities that reset the lastread pointers.  It generates a pair of
  17. files (areaname.PFH and areaname.PFL) which contain the data necessary
  18. to reconstruct the lastread information.
  19.  
  20. PREFIX must be followed by the name of a message area.
  21.  
  22. Once PREFIX has been run you can run SQFIX or perform whatever other
  23. maintenance is needed.  Then run the companion program POSTFIX to
  24. reconstruct the lastread pointers.
  25.  
  26.  
  27. PREFIX returns ERRORLEVEL 0 after a successful run.  ERRORLEVEL 1 is
  28. returned to indicate an error.
  29.  
  30. NOTICE:  You may use, copy, and distribute this program freely as long
  31. as you insure that both the executable and the documentation (.DOC)
  32. files are included in the distribution package.  The source code does
  33. not need to be included.  You may modify this program and document, so
  34. long as reasonable credit is given to the original author if a
  35. substantial portion of the original remains intact.  The author is not
  36. responsible for any losses which may occur either directly or indirectly
  37. as a result of using this program.
  38.  
  39. This program uses the Squish MsgAPI and the Maximus structures written
  40. by Scott J. Dudley.  "Squish" is a trademark of Scott J. Dudley.
  41.  
  42. HISTORY:
  43. Version 1.0  (10/12/92) -- Original release.  Written in Borland C.
  44.  
  45. Large memory model
  46.  
  47. OS/2 version (8/22/93) -- Ported using IBM's C SET/2 by Richard Butler
  48.                         - no code changes made to PREFIX.C
  49. */
  50.  
  51. /* Needed for use with MSGAPI modified for C SET/2 */
  52.  
  53. #define __386__
  54. #define __MSC__
  55. #define _MSC_VER 600
  56. #define OS_2
  57. #define msgapierr _msgapierr
  58.  
  59. /* end */
  60.  
  61. #include <ctype.h>
  62. #include <errno.h>
  63. #include <fcntl.h>
  64. #include <io.h>
  65. /* #include <dos.h> */
  66. #include <share.h>
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <string.h>
  70. #include <sys\stat.h>
  71. #include <sys\types.h>
  72. #include "prog.h"     /* From MsgAPI by Scott J. Dudley modified for C SET/2 */
  73. #include "alc.h"      /* From MsgAPI by Scott J. Dudley modified for C SET/2 */
  74. #include "msgapi.h"   /* From MsgAPI by Scott J. Dudley modified for C SET/2 */
  75. #include "mstruct.h"  /* Maximus structures by Scott J. Dudley: modified
  76.                          to avoid duplicate definitions of datestamps  */
  77.  
  78. #define MAXLINE     (128)
  79. #define MAXNAME     (36)
  80.  
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84. struct _area arearef;
  85. XMSG   xmsg;
  86. MSG    *area;
  87. MSGH   *msg;
  88. UMSGID uid;
  89. struct _minf mi;
  90. UMSGID umsgid;
  91. dword  msgn;
  92.  
  93. char   msgpath[MAXLINE];
  94. char   stamppath[MAXLINE];
  95. FILE   *stamp_fp;
  96. char   lastumsgidpath[MAXLINE];
  97. FILE   *lastumsgid_fp;
  98. char   lastmsgnpath[MAXLINE];
  99. FILE   *lastmsgn_fp;
  100.  
  101. char   line[MAXLINE];
  102. char   *ch;
  103. int    i;
  104.  
  105.  
  106. /************/
  107. /*  PREFIX  */
  108. /************/
  109.  
  110. printf("PREFIX/2 1.0 -- Copyright 1992 by Bob Quinlan (10/12/92)\n");
  111.  
  112. if (argc == 2)
  113.     {
  114.     strncpy(msgpath, argv[1], MAXLINE);
  115.     }
  116. else
  117.     {
  118.     fprintf(stderr, "No message area specified!\n");
  119.     exit(1);
  120.     }
  121.  
  122. /*  Open the message API  */
  123. mi.req_version = 0;
  124. mi.def_zone = 1;
  125. if (MsgOpenApi(&mi) != 0)
  126.     {
  127.     fprintf(stderr, "Unable to initialize MsgAPI!\n");
  128.     exit(1);
  129.     }
  130.  
  131. /*  Open the message area  */
  132. if ((area=MsgOpenArea(msgpath, MSGAREA_NORMAL, MSGTYPE_SQUISH)) == NULL)
  133.     {
  134.     fprintf(stderr, "Unable to open area %s (%d)!\n", msgpath, msgapierr);
  135.     exit(1);
  136.     }
  137.  
  138. /*  Lock the message area for better efficiency  */
  139. MsgLock(area);
  140.  
  141. /*  Open the datestamp file  */
  142. strcpy(stamppath, msgpath);
  143. strcat(stamppath, ".pfh");
  144. if ((stamp_fp = fopen(stamppath, "wb")) == NULL)
  145.     {
  146.     fprintf(stderr, "Unable to open %s\n", stamppath);
  147.     exit(1);
  148.     }
  149.  
  150. /*  Write datestamps to the datestamp file  */
  151. printf("Extracting datestamps");
  152. for (msgn = 1; msgn <= MsgGetHighMsg(area); msgn++)
  153.     {
  154.     /*  Read the message  */
  155.     if ((msg=MsgOpenMsg(area, MOPEN_READ, msgn)) == NULL)
  156.         {
  157.         fprintf(stderr, "Unable to open message %ld for reading (%d)!\n",
  158.               msgn, msgapierr);
  159.         exit(1);
  160.         }
  161.     if (MsgReadMsg(msg, &xmsg, 0L, 0L, NULL, 0L, NULL) == -1)
  162.         {
  163.         fprintf(stderr, "Unable to read message %ld (%d)!\n", msgn, msgapierr);
  164.         exit(1);
  165.         }
  166.     if (MsgCloseMsg(msg) != 0)
  167.         {
  168.         fprintf(stderr, "Unable to close message %ld (%d)!\n", msgn, msgapierr);
  169.         exit(1);
  170.         }
  171.     if (fwrite(&xmsg.date_arrived, sizeof(struct _stamp), 1, stamp_fp) < 1)
  172.         {
  173.         fprintf(stderr, "Unable to write datestamp %ld!\n", msgn);
  174.         exit(1);
  175.         }
  176.     printf(".");
  177.     }
  178. printf("\n");
  179.  
  180. /*  Close the datestamp file  */
  181. fclose(stamp_fp);
  182.  
  183. /*  Open the lastread UMSGID file  */
  184. strcpy(lastumsgidpath, msgpath);
  185. strcat(lastumsgidpath, ".SQL");
  186. if ((lastumsgid_fp = fopen(lastumsgidpath, "rb")) == NULL)
  187.     {
  188.     fprintf(stderr, "Unable to open %s\n", lastumsgidpath);
  189.     exit(1);
  190.     }
  191.  
  192. /*  Open the lastread msg number file  */
  193. strcpy(lastmsgnpath, msgpath);
  194. strcat(lastmsgnpath, ".pfl");
  195. if ((lastmsgn_fp = fopen(lastmsgnpath, "wb")) == NULL)
  196.     {
  197.     fprintf(stderr, "Unable to open %s\n", lastmsgnpath);
  198.     exit(1);
  199.     }
  200.  
  201. printf("Converting message numbers");
  202. /*  Convert and write lastread message numbers  */
  203. while (fread(&umsgid, sizeof(UMSGID), 1, lastumsgid_fp) > 0)
  204.     {
  205.     if ((msgn = MsgUidToMsgn(area, umsgid, UID_NEXT)) == 0)
  206.         {
  207.         msgn = 1;
  208.         }
  209.     if (fwrite(&msgn, sizeof(dword), 1, lastmsgn_fp) < 1)
  210.         {
  211.         fprintf(stderr, "Unable to write lastread pointer.\n");
  212.         exit(1);
  213.         }
  214.     printf(".");
  215.     }
  216. printf("\n");
  217.  
  218. /*  Close the lastread msg number file  */
  219. fclose(lastmsgn_fp);
  220.  
  221. /*  Close the lastread UMSGID file  */
  222. fclose(lastumsgid_fp);
  223.  
  224. /*  Unlock the message area  */
  225. MsgUnlock(area);
  226.  
  227. /*  Close the message area  */
  228. if (MsgCloseArea(area) != 0)
  229.     {
  230.     fprintf(stderr, "Unable to close area %s (%d)!\n", msgpath, msgapierr);
  231.     exit(1);
  232.     }
  233.  
  234. /*  Close the message API  */
  235. if (MsgCloseApi() != 0)
  236.     {
  237.     fprintf(stderr, "Unable to deinitialize MsgAPI!\n");
  238.     exit(1);
  239.     }
  240.  
  241. return 0;
  242. }
  243.  
  244.  
  245.