home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / XDEV_117.ZIP / XMOVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  5.1 KB  |  242 lines

  1. /* WARNING:  This has never been tested _at all_!!!!!  */
  2.  
  3.  
  4. #include "msgg.h"
  5.  
  6. int get_mess(word,word,char *);
  7. int put_mess(word,char *);
  8. char *get_text(word,char *,char *);
  9. void put_text(word,char *,char *);
  10.  
  11.  
  12. char buffer[333];
  13. struct _xmsg msg;
  14.  
  15. void main (argc,argv)
  16.  
  17.     int argc;
  18.     char *argv[];
  19.  
  20. {
  21.  
  22.     struct ffblk f;
  23.     char *hold=NULL;
  24.  
  25.     if (argc<6) {
  26.         fputs("\nError calling XMove.\n",stdout);
  27.         fputs("\nUsage: Xport <directory\\> <frombase#> <msg#> <tobase#>\n",stdout);
  28.         exit(0);
  29.     }
  30.  
  31.     sprintf(buffer,"%sXDATA.%03x",argv[1],(word)atol(argv[2]));
  32.     if(!findfirst(buffer,&f,0)) {
  33.         fputs("\nCan't find message data file.\n",stdout);
  34.         exit(1);
  35.     }
  36.     if(((word)(f.ff_fsize/(long)sizeof(struct _xmsg))<(word)atol(argv[3])) {
  37.         fputs("\nRequested message doesn't exist!\n",stdout);
  38.         exit(2);
  39.     }
  40.     get_mess((word)atol(argv[3]),(word)atol(argv[2]),argv[1]);
  41.     msg.m_attr = msg.m_attr & (~MSGDELETED);
  42.     get_text((word)atol(argv[2]),argv[1],hold);
  43.     if(hold==NULL) {
  44.         fputs("\nCouldn't load message text...\n",stdout);
  45.         exit(5);
  46.     }
  47.     put_text((word)atol(argv[4]),argv[1],hold);
  48.     put_mess((word)atol(argv[4]),argv[1]);
  49. }
  50.  
  51.  
  52.  
  53.  
  54. int get_mess (word messno,word areano,char *directory)
  55.  
  56. {
  57.  
  58.  int handle;
  59.  register word x=0;
  60.  
  61.  sprintf(buffer,"%sXDATA.%03x",directory,areano);
  62.  
  63.  while ((handle=_open(buffer,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  64.      if (errno==EACCES) {
  65.         x++;
  66.         if(x>2) {
  67.             fputs("\nGot tired of waiting...\n",stdout);
  68.             exit (3);
  69.         }
  70.         fputs("\nAwaiting access...\n",stdout);
  71.         sleep(1);
  72.      }
  73.      else  {
  74.         fputs("\nError opening message data file...\n",stdout);
  75.         exit(4);
  76.      }
  77.  }
  78.  if ((lseek(handle,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET)==(-1)) || (_read(handle,&msg,sizeof(struct _xmsg))<1)) {
  79.     if (eof(handle)==(-1)) perror ("\nSEEK ERROR");
  80.     _close(handle);
  81.     return -1;
  82.  }
  83.  _close(handle);
  84.  if(msg.m_attr & MSGPACKED) {
  85.     fputs("\nI can't handle compressed messages\n",stdout);
  86.     exit(3);
  87.  }
  88.  return 0;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. int put_mess (word areano,char *directory)
  95.  
  96. {
  97.  
  98.  int handle;
  99.  register word x=0;
  100.  long temp;
  101.  
  102.  sprintf(buffer,"%sXDATA.%03x",directory,areano);
  103.  
  104.  while ((handle=_open(buffer,O_RDWR | O_BINARY | O_DENYNONE))==-1) {
  105.      if (errno==EACCES) {
  106.         x++;
  107.         if(x>2) {
  108.             fputs("\nGot tired of waiting...\n",stdout);
  109.             return -1;
  110.         }
  111.         fputs("\nAwaiting access....\n",stdout);
  112.         sleep(1);
  113.      }
  114.      else {
  115.         if((handle=creat(buffer,S_IWRITE))==-1) {
  116.             fputs("\nCan't open message data file...\n",stdout);
  117.             return -1;
  118.         }
  119.         break;
  120.      }
  121.  }
  122.  temp=filelength(handle);
  123.  lock(handle,temp,(long)sizeof(struct _xmsg));
  124.  if ((lseek(handle,0L,SEEK_END)==(-1)) || (_write(handle,&msg,sizeof(struct _xmsg))<1)) {
  125.     if (eof(handle)==(-1))  {
  126.         fputs(" SEEK ERROR ",stdout);
  127.         unlock(handle,temp,(long)sizeof(struct _xmsg));
  128.         _close(handle);
  129.         return -1;
  130.     }
  131.  }
  132.  unlock(handle,temp,(long)sizeof(struct _xmsg));
  133.   _close(handle);
  134.  return 0;
  135. }
  136.  
  137.  
  138.  
  139. char far * get_text (word areano,char *directory,char *hold)
  140.  
  141. {
  142.  
  143.  char once;
  144.  char *tempo;
  145.  word temp=0;
  146.  int  handle;
  147.  
  148.  sprintf(buffer,"%sXTEXT.%03x",directory,areano);
  149.  
  150.  while ((handle=_open(buffer,O_RDONLY | O_BINARY | O_DENYNONE))==-1) {
  151.      if (errno==EACCES) {
  152.         temp++;
  153.         if (temp>2) {
  154.             fputs("\nGot tired of waiting...\n",stdout);
  155.             return NULL;
  156.         }
  157.         fputs("\nAwaiting access...\n",stdout);
  158.         sleep(1);
  159.      }
  160.      else {
  161.         fputs("\nCan't open message text file...\n",stdout);
  162.         return NULL;
  163.      }
  164.  }
  165.  once=0;
  166. TryThatAgain:
  167.  if (lseek(handle,msg.start,SEEK_SET)) {
  168.      if (eof(handle)) {
  169.         if (!once) {
  170.             once++;
  171.             goto TryThatAgain;
  172.         }
  173.         else perror (" TEXT SEEK ERROR");
  174.         return NULL;
  175.      }
  176.  }
  177.  temp=msg.length;
  178. ReTry:
  179.   hold=(char far*)farmalloc(msg.length+1);
  180.   if (hold==NULL) {
  181.     if (msg.length>1024) {
  182.         msg.length-=256;
  183.         goto ReTry;
  184.     }
  185.     else {
  186.         fputs("\nReducing text size...\n",stdout);
  187.         msg.length=temp;
  188.         return NULL;
  189.     }
  190.   }
  191.   memset(hold,0,msg.length);
  192.   _read(handle,hold,msg.length);
  193.   _close(handle);
  194.   hold[msg.length-1]=0;
  195.   while ((tempo=strstr(hold," \x8d"))) memmove(&tempo[1],&tempo[2],strlen(&tempo[2])+1);
  196.   while ((tempo=strchr(hold,'\x8d'))) *tempo=' ';
  197.   while ((tempo=strchr(hold,'\n'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
  198.   if (hold[strlen(hold)-1]!='\r') strcat(hold,"\r");
  199.   msg.length=temp;
  200.   return hold;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. void put_text (word areano,char *directory,char *hold)
  207.  
  208. {
  209.  
  210.  word temp=0;
  211.  int  handle;
  212.  
  213.  sprintf(buffer,"%sXTEXT.%03x",directory,areano);
  214.  
  215.  while ((handle=_open(buffer,O_RDWR| O_BINARY | O_DENYNONE))==-1) {
  216.      if (errno==EACCES) {
  217.         temp++;
  218.         if (temp>2) {
  219.             fputs("\nGot tired of waiting...\n",stdout);
  220.             return;
  221.         }
  222.         fputs("\nAwaiting access...\n",stdout);
  223.         sleep(1);
  224.      }
  225.      else {
  226.         if((handle=creat(buffer,S_IWRITE))==-1) {
  227.             fputs("\nCan't open message text file...\n",stdout);
  228.             return;
  229.         }
  230.         else break;
  231.      }
  232.  }
  233.   msg.length=(word)strlen(hold);
  234.   msg.start=tell(handle);
  235.   lock(handle,msg.start,msg.length);
  236.   _write(handle,hold,msg.length);
  237.   unlock(handle,msg.start,msg.length);
  238.   _close(handle);
  239.   return;
  240. }
  241.  
  242.