home *** CD-ROM | disk | FTP | other *** search
/ Network CD 1 / Network CD.iso / fredfish / 811-820 / ff814 / noteedit / noteedit.c next >
Encoding:
C/C++ Source or Header  |  1993-02-14  |  22.5 KB  |  1,261 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <libraries/dos.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <fcntl.h>
  8.  
  9. #define isblank(c) ( ((c)!=32) && ((c)!=9) )
  10.  
  11. #define LEFT 20
  12. #define TOP 78
  13. #define RIGHT 347
  14. #define BOTTOM 108
  15. #define HEIGHT (BOTTOM-TOP)
  16.  
  17. /****************************/
  18. /* Var Definition        */
  19. /****************************/
  20.  
  21. char Editor[40] = "C:Ed";
  22. char More[40] = "Utilities:More";
  23. char Dir[40] = "SYS:";
  24.  
  25. char Date[9];
  26. char LoadDate[9];
  27. char Password[30];
  28.  
  29. struct tm *date;
  30. struct FileHandle *output = NULL;
  31.  
  32. #include "NoteEdit.h"
  33.  
  34. /****************************/
  35. /* Message            */
  36. /****************************/
  37.  
  38. void Message(char *text)
  39.     {
  40.     struct RastPort *rp = window->RPort;
  41.     SetAPen(rp, 0);
  42.     RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);
  43.  
  44.     SetAPen(rp, 1);
  45.     Move(rp, LEFT+8, TOP+HEIGHT/2+2);
  46.     Text(rp, text, strlen(text));
  47.     }
  48.  
  49. /****************************/
  50. /* ShowLoadDate         */
  51. /****************************/
  52.  
  53. void ShowLoadDate()
  54.     {
  55.     struct RastPort *rp = window->RPort;
  56.  
  57.     sprintf(LoadDate, "%02d-%02d-%02d", date->tm_mday, date->tm_mon+1, date->tm_year);
  58.  
  59.     SetAPen(rp, 1);
  60.     Move(rp, 154, 60);
  61.     Text(rp, LoadDate, 8);
  62.     }
  63.  
  64. /****************************/
  65. /* random            */
  66. /****************************/
  67.  
  68. LONG q0 = 31415821 / 10000;
  69. LONG q1 = 31415821 % 10000;
  70.  
  71. LONG random(LONG randombit)
  72.     {
  73.     LONG p0, p1, r;
  74.  
  75.     p0 = randombit / 10000;
  76.     p1 = randombit % 10000;
  77.  
  78.     r = ( ( (p1*q0+p0*q1) % 10000 ) * 10000 + p1*q1 ) % 100000000;
  79.  
  80.     return((r+1) % 255);
  81.     }
  82.  
  83. /****************************/
  84. /* LoadConfiguration        */
  85. /****************************/
  86.  
  87. void LoadConfiguration()
  88.     {
  89.     FILE *conf;
  90.     SHORT i;
  91.     char Buffer[256];
  92.     char Token[256];
  93.     char Arg[256];
  94.  
  95.     if ( conf = fopen("s:Note.config", "r") )
  96.         {
  97.         while ( !feof(conf) )
  98.             {
  99.             Arg[0] = Token[0] = 0;
  100.  
  101.             fgets(Buffer, 255, conf);
  102.             sscanf(Buffer, "%s : %s", Token, Arg);
  103.  
  104.             if ( !strcmp(Token, "EDITOR") )
  105.                 strcpy(Editor, Arg);
  106.  
  107.             if ( !strcmp(Token, "MORE") )
  108.                 strcpy(More, Arg);
  109.  
  110.             if ( !strcmp(Token, "DIR") )
  111.                 strcpy(Dir, Arg);
  112.             }
  113.  
  114.         fclose(conf);
  115.         }
  116.     }
  117.  
  118. /****************************/
  119. /* Decrypt            */
  120. /****************************/
  121.  
  122. SHORT Decrypt(char *name)
  123.     {
  124.     LONG size;
  125.  
  126.     /*******************/
  127.     /* get file length */
  128.     /*******************/
  129.         {
  130.         struct FileInfoBlock *fblock;
  131.  
  132.         Message("scanning...");
  133.  
  134.         if ( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) )
  135.             {
  136.             struct FileLock *slock;
  137.  
  138.             if ( slock = (struct FileLock *)Lock(name, ACCESS_READ) )
  139.                 {
  140.                 if ( Examine(slock, fblock) )
  141.                     {
  142.                     if ( fblock->fib_DirEntryType > 0 )
  143.                         {
  144.                         Message("It's a directory !");
  145.                         return(-1);
  146.                         }
  147.                     else
  148.                         size = fblock->fib_Size;
  149.                     }
  150.                 else
  151.                     {
  152.                     UnLock(fblock);
  153.                     FreeMem(fblock, sizeof(struct FileInfoBlock));
  154.                     Message("Can't examine file.");
  155.                     return(-1);
  156.                     }
  157.  
  158.                 UnLock(slock);
  159.                 }
  160.             else
  161.                 {
  162.                 FreeMem(fblock, sizeof(struct FileInfoBlock));
  163.                 Message("Can't lock file.");
  164.                 return(-1);
  165.                 }
  166.  
  167.             FreeMem(fblock, sizeof(struct FileInfoBlock));
  168.             }
  169.         else
  170.             {
  171.             Message("No memory !");
  172.             return(-1);
  173.             }
  174.         }
  175.  
  176.     /*************/
  177.     /* load data */
  178.     /*************/
  179.         {
  180.         struct FileHandle *in, *out;
  181.         UBYTE *newmem = NULL;
  182.  
  183.         Message("loading...");
  184.  
  185.         newmem = (UBYTE *)AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC);
  186.         if ( newmem )
  187.             {
  188.             in = (struct FileHandle *)Open(name, MODE_OLDFILE);
  189.             if ( in )
  190.                 {
  191.                 if ( Read(in, newmem, size) != size )
  192.                     {
  193.                     Close(in);
  194.                     FreeMem(newmem, size);
  195.                     Message("Read error.");
  196.                     return(-1);
  197.                     }
  198.                 Close(in);
  199.                 }
  200.             else
  201.                 {
  202.                 FreeMem(newmem, size);
  203.                 Message("Can't open file.");
  204.                 return(-1);
  205.                 }
  206.  
  207.             /*************/
  208.             /* open dest */
  209.             /*************/
  210.  
  211.             out = (struct FileHandle *)Open("T:Note", MODE_NEWFILE);
  212.  
  213.             if ( out )
  214.                 {
  215.                 /**************/
  216.                 /* decrypt it */
  217.                 /**************/
  218.  
  219.                 LONG seed;
  220.                 LONG count = size;
  221.                 LONG pwcount = 0, pwlen = strlen(Password);
  222.                 UBYTE *mem = newmem;
  223.  
  224.                 /************/
  225.                 /* get seed */
  226.                 /************/
  227.                     {
  228.                     char *pass = Password;
  229.  
  230.                     seed = pass[0] * pwlen;
  231.                     while ( *pass )
  232.                         seed += *pass++;
  233.                     }
  234.  
  235.                 Message("decrypting...");
  236.                 while ( count-- )
  237.                     {
  238.                     seed = random(seed);
  239.  
  240.                     if (pwcount == pwlen)
  241.                         pwcount = 0;
  242.  
  243.                     *mem = (*mem + Password[pwcount]);
  244.                     *mem = (*mem + seed);
  245.  
  246.                     pwcount++;
  247.                     mem++;
  248.                     }
  249.  
  250.                 Message("writing...");
  251.  
  252.                 if ( Write(out, newmem, size) != size )
  253.                     {
  254.                     Close(out);
  255.                     remove("T:Note");
  256.                     FreeMem(newmem, size);
  257.                     Message("Write error.");
  258.                     return(-1);
  259.                     }
  260.  
  261.                 Close(out);
  262.                 }
  263.             else
  264.                 {
  265.                 FreeMem(newmem, size);
  266.                 Message("Can't open temporary.");
  267.                 return(-1);
  268.                 }
  269.  
  270.             FreeMem(newmem, size);
  271.             }
  272.         else
  273.             {
  274.             Message("Out of memory.");
  275.             return(-1);
  276.             }
  277.         }
  278.  
  279.     Message("done.");
  280.     return(0);
  281.     }
  282.  
  283. /****************************/
  284. /* Crypt            */
  285. /****************************/
  286.  
  287. SHORT Crypt(char *name)
  288.     {
  289.     LONG size;
  290.  
  291.     /*******************/
  292.     /* get file length */
  293.     /*******************/
  294.         {
  295.         struct FileInfoBlock *fblock;
  296.  
  297.         Message("scanning...");
  298.  
  299.         if ( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) )
  300.             {
  301.             struct FileLock *slock;
  302.  
  303.             if ( slock = (struct FileLock *)Lock("T:Note", ACCESS_READ) )
  304.                 {
  305.                 if ( Examine(slock, fblock) )
  306.                     {
  307.                     if ( fblock->fib_DirEntryType > 0 )
  308.                         {
  309.                         Message("It's a directory !");
  310.                         return(-1);
  311.                         }
  312.                     else
  313.                         size = fblock->fib_Size;
  314.                     }
  315.                 else
  316.                     {
  317.                     UnLock(fblock);
  318.                     FreeMem(fblock, sizeof(struct FileInfoBlock));
  319.                     Message("Can't examine temporary.");
  320.                     return(-1);
  321.                     }
  322.  
  323.                 UnLock(slock);
  324.                 }
  325.             else
  326.                 {
  327.                 FreeMem(fblock, sizeof(struct FileInfoBlock));
  328.                 Message("Can't lock temporary.");
  329.                 return(-1);
  330.                 }
  331.  
  332.             FreeMem(fblock, sizeof(struct FileInfoBlock));
  333.             }
  334.         else
  335.             {
  336.             Message("No memory !");
  337.             return(-1);
  338.             }
  339.         }
  340.  
  341.     /*************/
  342.     /* load data */
  343.     /*************/
  344.         {
  345.         struct FileHandle *in, *out;
  346.         UBYTE *newmem = NULL;
  347.  
  348.         Message("loading...");
  349.  
  350.         newmem = (UBYTE *)AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC);
  351.         if ( newmem )
  352.             {
  353.             in = (struct FileHandle *)Open("T:Note", MODE_OLDFILE);
  354.             if ( in )
  355.                 {
  356.                 if ( Read(in, newmem, size) != size )
  357.                     {
  358.                     Close(in);
  359.                     FreeMem(newmem, size);
  360.                     Message("Read error.");
  361.                     return(-1);
  362.                     }
  363.                 Close(in);
  364.                 }
  365.             else
  366.                 {
  367.                 FreeMem(newmem, size);
  368.                 Message("Can't open temporary.");
  369.                 return(-1);
  370.                 }
  371.  
  372.             /*************/
  373.             /* open dest */
  374.             /*************/
  375.  
  376.             out = (struct FileHandle *)Open(name, MODE_NEWFILE);
  377.  
  378.             if ( out )
  379.                 {
  380.                 /************/
  381.                 /* crypt it */
  382.                 /************/
  383.  
  384.                 LONG seed;
  385.                 LONG count = size;
  386.                 LONG pwcount = 0, pwlen = strlen(Password);
  387.                 UBYTE *mem = newmem;
  388.  
  389.                 /************/
  390.                 /* get seed */
  391.                 /************/
  392.                     {
  393.                     char *pass = Password;
  394.  
  395.                     seed = pass[0] * pwlen;
  396.                     while ( *pass )
  397.                         seed += *pass++;
  398.                     }
  399.  
  400.                 Message("crypting...");
  401.  
  402.                 while (count --)
  403.                     {
  404.                     seed = random(seed);
  405.  
  406.                     if (pwcount == pwlen)
  407.                         pwcount = 0;
  408.  
  409.                     *mem = (*mem - Password[pwcount]);
  410.                     *mem = (*mem - seed);
  411.  
  412.                     pwcount++;
  413.                     mem++;
  414.                     }
  415.  
  416.                 Message("writing...");
  417.  
  418.                 if ( Write(out, newmem, size) != size )
  419.                     {
  420.                     Close(out);
  421.                     remove(name);
  422.                     FreeMem(newmem, size);
  423.                     Message("Write error.");
  424.                     return(-1);
  425.                     }
  426.                 Close(out);
  427.                 }
  428.             else
  429.                 {
  430.                 FreeMem(newmem, size);
  431.                 Message("Can't open file.");
  432.                 return(-1);
  433.                 }
  434.  
  435.             FreeMem(newmem, size);
  436.             }
  437.         else
  438.             {
  439.             Message("Out of memory.");
  440.             return(-1);
  441.             }
  442.         }
  443.  
  444.     Message("done.");
  445.     return(0);
  446.     }
  447.  
  448. /****************************/
  449. /* LoadText            */
  450. /****************************/
  451.  
  452. void LoadText()
  453.     {
  454.     char comline[256];
  455.     char filename[256];
  456.  
  457.     APTR oldfirst = window->FirstGadget;
  458.     window->FirstGadget = NULL;
  459.  
  460.     sprintf(filename, "%sData/%s", Dir, LoadDate);
  461.     if ( Decrypt(filename) != -1 )
  462.         {
  463.         Message("showing...");
  464.         sprintf(comline, "%s T:Note", More);
  465.         Execute(comline, NULL, output);
  466.  
  467.         remove("T:Note");
  468.  
  469.         Message("done.");
  470.         }
  471.  
  472.     window->FirstGadget = oldfirst;
  473.     }
  474.  
  475. /****************************/
  476. /* EditTodayText        */
  477. /****************************/
  478.  
  479. void EditTodayText()
  480.     {
  481.     char comline[256];
  482.     char filename[256];
  483.  
  484.     APTR oldfirst = window->FirstGadget;
  485.     window->FirstGadget = NULL;
  486.  
  487.     sprintf(filename, "%sData/%s", Dir, Date);
  488.     Decrypt(filename);
  489.  
  490.     Message("editing...");
  491.     sprintf(comline, "%s T:Note", Editor);
  492.     Execute(comline, NULL, output);
  493.  
  494.     Crypt(filename);
  495.     remove("T:Note");
  496.  
  497.     window->FirstGadget = oldfirst;
  498.     }
  499.  
  500. /****************************/
  501. /* EditConfig            */
  502. /****************************/
  503.  
  504. void EditConfig()
  505.     {
  506.     char comline[256];
  507.  
  508.     APTR oldfirst = window->FirstGadget;
  509.     window->FirstGadget = NULL;
  510.  
  511.     sprintf(comline, "%s s:Note.config", Editor);
  512.     Execute(comline, NULL, output);
  513.  
  514.     LoadConfiguration();
  515.     Message("Configuration loaded.");
  516.  
  517.     window->FirstGadget = oldfirst;
  518.     }
  519.  
  520. /****************************/
  521. /* GetPassword            */
  522. /****************************/
  523.  
  524. char GetChar(SHORT xpos)
  525.     {
  526.     struct RastPort *rp = window->RPort;
  527.     struct IntuiMessage *msg;
  528.     SHORT count = 0;
  529.  
  530.     while (TRUE)
  531.         {
  532.         Delay(1);
  533.         if (count == 0)
  534.             {
  535.             Move(rp, xpos, TOP+20);
  536.             Text(rp, "X", 1);
  537.             }
  538.         else if (count == 8)
  539.             {
  540.             Move(rp, xpos, TOP+20);
  541.             Text(rp, " ", 1);
  542.             }
  543.  
  544.         count = (count + 1) & 15;
  545.  
  546.         while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
  547.             {
  548.             ULONG class;
  549.             USHORT code;
  550.  
  551.             class = msg->Class;
  552.             code = msg->Code;
  553.  
  554.             ReplyMsg(msg);
  555.  
  556.             if ( class = VANILLAKEY )
  557.                 return((char)code);
  558.             } /* while (msg =... */
  559.         }
  560.     }
  561.  
  562. void GetPassword()
  563.     {
  564.     struct RastPort *rp = window->RPort;
  565.     BOOL bool = TRUE;
  566.     SHORT i = 0;
  567.     SHORT xpos = LEFT+16;
  568.  
  569.     APTR oldfirst = window->FirstGadget;
  570.     window->FirstGadget = NULL;
  571.  
  572.     SetAPen(rp, 0);
  573.     RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);
  574.  
  575.     SetAPen(rp, 1);
  576.     Move(rp, LEFT+8, TOP+10);
  577.     Text(rp, "Enter password :", 16);
  578.  
  579.     while (bool && (i < 20))
  580.         {
  581.         char c;
  582.  
  583.         c = GetChar(xpos);
  584.         if ( (c == 13) && (i > 4) )
  585.             bool = FALSE;
  586.         else
  587.             {
  588.             Password[i++] = c;
  589.  
  590.             Move(rp, xpos, TOP+20);
  591.             Text(rp, "*", 1);
  592.  
  593.             xpos += 8;
  594.             }
  595.         } /* while (bool) */
  596.     Password[i] = 0;
  597.  
  598.     /**********/
  599.     /* VERIFY */
  600.     /**********/
  601.         {
  602.         char Verify[30];
  603.  
  604.         SetAPen(rp, 0);
  605.         RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);
  606.  
  607.         SetAPen(rp, 1);
  608.         Move(rp, LEFT+8, TOP+10);
  609.         Text(rp, "Verify :", 8);
  610.  
  611.         bool = TRUE;
  612.         i = 0;
  613.         xpos = LEFT+16;
  614.         while (bool && (i < 20))
  615.             {
  616.             char c;
  617.  
  618.             c = GetChar(xpos);
  619.             if ( (c == 13) && (i > 4) )
  620.                 bool = FALSE;
  621.             else
  622.                 {
  623.                 Verify[i++] = c;
  624.  
  625.                 Move(rp, xpos, TOP+20);
  626.                 Text(rp, "*", 1);
  627.                 xpos += 8;
  628.                 }
  629.             } /* while (bool) */
  630.         Verify[i] = 0;
  631.  
  632.         if ( !strcmp(Password, Verify) )
  633.             Message("Password ok !");
  634.         else
  635.             Message("Wrong password !");
  636.         }
  637.  
  638.     window->FirstGadget = oldfirst;
  639.     }
  640.  
  641. /****************************/
  642. /* Other            */
  643. /****************************/
  644.  
  645. struct FileName
  646.     {
  647.     struct FileName *Next;
  648.     struct FileName *Prev;
  649.  
  650.     char Name[108];
  651.     };
  652.  
  653. void Other()
  654.     {
  655.     struct RastPort *rp;
  656.     struct FileName *FirstFile = NULL;
  657.     struct FileName *fm, *topf;
  658.     struct FileLock *dlock = NULL;
  659.     struct FileInfoBlock *fblock = NULL;
  660.     struct Window *win = NULL;
  661.     SHORT y, top = NULL;
  662.     char Name[256];
  663.     char comline[256];
  664.  
  665.     APTR oldfirst = window->FirstGadget;
  666.     window->FirstGadget = NULL;
  667.  
  668.     sprintf(Name, "%sOther", Dir);
  669.  
  670.     /******************/
  671.     /* scan directory */
  672.     /******************/
  673.  
  674.     Message("scanning directory...");
  675.  
  676.     if ( !( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) ) )
  677.         {
  678.         Message("No memory !");
  679.         goto fault;
  680.         }
  681.  
  682.     if ( !( dlock = (struct FileLock *)Lock(Name, ACCESS_READ) ) )
  683.         {
  684.         Message("Can't lock directory.");
  685.         goto fault;
  686.         }
  687.  
  688.     if ( !Examine(dlock, fblock) )
  689.         {
  690.         Message("Can't examine directory.");
  691.         goto fault;
  692.         }
  693.  
  694.     if ( fblock->fib_DirEntryType <= 0 )
  695.         {
  696.         Message("It's not a directory !");
  697.         goto fault;
  698.         }
  699.  
  700.     while ( ExNext(dlock, fblock) )
  701.         {
  702.         struct FileName *new;
  703.  
  704.         new = (struct FileName *)AllocMem(sizeof(struct FileName), MEMF_CLEAR | MEMF_PUBLIC);
  705.         if ( new )
  706.             {
  707.             if ( FirstFile )
  708.                 FirstFile->Prev = new;
  709.             new->Next = FirstFile;
  710.             new->Prev = NULL;
  711.             FirstFile = new;
  712.  
  713.             strcpy(new->Name, fblock->fib_FileName);
  714.             }
  715.         else
  716.             {
  717.             Message("Out of memory !!");
  718.             goto fault;
  719.             }
  720.         } /* while ( ExNext... */
  721.  
  722.     UnLock(dlock);
  723.     dlock = NULL;
  724.  
  725.     FreeMem(fblock, sizeof(struct FileInfoBlock));
  726.     fblock = NULL;
  727.  
  728.     /***************/
  729.     /* open window */
  730.     /***************/
  731.  
  732.     win = (struct Window *)OpenWindow(&lnw);
  733.     if ( !win )
  734.         {
  735.         Message("Window error.");
  736.         goto fault;
  737.         }
  738.  
  739.     rp = win->RPort;
  740.  
  741.     SetAPen(rp, 1);
  742.     Move(rp, 0,    0);
  743.     Draw(rp, 0,    119);
  744.     Draw(rp, 0+1,  119-1);
  745.     Draw(rp, 0+1,  0);
  746.     Draw(rp, 199-1, 0);
  747.     Draw(rp, 199-3, 0+2);
  748.     Draw(rp, 199-3, 119-2);
  749.     Move(rp, 199-2, 0+2);
  750.     Draw(rp, 199-2, 119-1);
  751.     Draw(rp, 0,    119-1);
  752.  
  753.     SetAPen(rp, 2);
  754.     Move(rp, 199-1, 0+1);
  755.     Draw(rp, 0+3,  0+1);
  756.     Draw(rp, 0+3,  119-2);
  757.     Move(rp, 0+2,  0+1);
  758.     Draw(rp, 0+2,  119-1);
  759.     Draw(rp, 0+1,  119);
  760.     Draw(rp, 199,   119);
  761.     Draw(rp, 199,   0);
  762.     Draw(rp, 199-1, 0+1);
  763.     Draw(rp, 199-1, 119);
  764.  
  765.     SetAPen(rp, 1);
  766.     for ( fm = FirstFile, y = 10 ; fm && (y < 115) ; fm = fm->Next, y += 8 )
  767.         {
  768.         Move(rp, 8, y);
  769.         Text(rp, fm->Name, (strlen(fm->Name) < 21) ? strlen(fm->Name) : 20);
  770.         }
  771.  
  772.     topf = FirstFile;
  773.  
  774.     /**********/
  775.     /* Select */
  776.     /**********/
  777.         {
  778.         BOOL bool = TRUE;
  779.  
  780.         while ( bool )
  781.             {
  782.             struct IntuiMessage *msg;
  783.  
  784.             Wait( 1 << win->UserPort->mp_SigBit );
  785.  
  786.             while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  787.                 {
  788.                 ULONG class;
  789.                 USHORT code;
  790.                 struct Gadget *iaddress;
  791.                 SHORT mousex, mousey;
  792.  
  793.                 class = msg->Class;
  794.                 code = msg->Code;
  795.                 iaddress = msg->IAddress;
  796.                 mousex = msg->MouseX;
  797.                 mousey = msg->MouseY;
  798.  
  799.                 ReplyMsg(msg);
  800.  
  801.                 switch (class)
  802.                     {
  803.                     case GADGETDOWN :
  804.                         {
  805.                         switch (iaddress->GadgetID)
  806.                             {
  807.                             case 0 :
  808.                                 {
  809.                                 while ( LGadgetUp.Flags & SELECTED )
  810.                                     {
  811.                                     if ( topf->Prev )
  812.                                         {
  813.                                         topf = topf->Prev;
  814.                                         top--;
  815.  
  816.                                         ScrollRaster(rp, 0, -8, 8, 4, 169, 115);
  817.                                         SetAPen(rp, 0);
  818.                                         RectFill(rp, 8, 4, 169, 11);
  819.                                         SetAPen(rp, 1);
  820.                                         Move(rp, 8, 10);
  821.                                         Text(rp, topf->Name, (strlen(topf->Name) < 21) ? strlen(topf->Name) : 20);
  822.                                         }
  823.                                     Delay(2);
  824.                                     }
  825.                                 break;
  826.                                 }
  827.                             case 1 :
  828.                                 {
  829.                                 while ( LGadgetDown.Flags & SELECTED )
  830.                                     {
  831.                                     fm = topf;
  832.                                     for ( y = 0 ; fm && (y < 14) ; y++, fm = fm->Next );
  833.  
  834.                                     if ( fm )
  835.                                         {
  836.                                         topf = topf->Next;
  837.                                         top++;
  838.  
  839.                                         ScrollRaster(rp, 0, 8, 8, 4, 169, 115);
  840.                                         SetAPen(rp, 0);
  841.                                         RectFill(rp, 8, 108, 169, 115);
  842.                                         SetAPen(rp, 1);
  843.                                         Move(rp, 8, 114);
  844.                                         Text(rp, fm->Name, (strlen(fm->Name) < 21) ? strlen(fm->Name) : 20);
  845.                                         }
  846.                                     Delay(2);
  847.                                     }
  848.                                 break;
  849.                                 }
  850.                             }
  851.                         break;
  852.                         } /* GADGETDOWN */
  853.                     case MOUSEBUTTONS :
  854.                         {
  855.                         switch ( code )
  856.                             {
  857.                             case SELECTUP :
  858.                                 {
  859.                                 if ( (mousex > 8) && (mousex < 170) && (mousey > 4) && (mousey <116) )
  860.                                     {
  861.                                     mousey = ( (mousey - 4) >> 3 ) + top;
  862.  
  863.                                     for ( fm = FirstFile ; fm && mousey ; mousey-- )
  864.                                         fm = fm->Next;
  865.  
  866.                                     if ( fm )
  867.                                         bool = FALSE;
  868.                                     }
  869.                                 break;
  870.                                 }
  871.                             case MENUUP :
  872.                                 {
  873.                                 fm = NULL;
  874.                                 bool = FALSE;
  875.                                 break;
  876.                                 }
  877.                             }
  878.                         } /* MOUSEBUTTONS */
  879.                     } /* switch (class) */
  880.                 } /* while ( msg =... */
  881.             } /* while (bool) */
  882.         } /* Select */
  883.  
  884.     CloseWindow(win);
  885.     win = NULL;
  886.  
  887.     /***********/
  888.     /* edit it */
  889.     /***********/
  890.  
  891.     if ( fm )
  892.         {
  893.         sprintf(Name, "%sOther/%s", Dir, fm->Name);
  894.         Decrypt(Name);
  895.  
  896.         Message("editing...");
  897.         sprintf(comline, "%s T:Note", Editor);
  898.         Execute(comline, NULL, output);
  899.  
  900.         Crypt(Name);
  901.         remove("T:Note");
  902.         } /* edit it */
  903.  
  904.     Message("done.");
  905.  
  906. fault:    if (fblock)
  907.         FreeMem(fblock, sizeof(struct FileInfoBlock));
  908.     if (dlock)
  909.         UnLock(dlock);
  910.     if (win)
  911.         CloseWindow(win);
  912.  
  913.     while ( FirstFile )
  914.         {
  915.         struct FileName *next;
  916.  
  917.         next = FirstFile->Next;
  918.         FreeMem(FirstFile, sizeof(struct FileName));
  919.         FirstFile = next;
  920.         }
  921.  
  922.     window->FirstGadget = oldfirst;
  923.     }
  924.  
  925. /****************************/
  926. /* New                */
  927. /****************************/
  928.  
  929. void New()
  930.     {
  931.     struct Window *win;
  932.     struct RastPort *rp;
  933.     BOOL ok;
  934.     char FileName[256];
  935.  
  936.     win = (struct Window *)OpenWindow(&nnw);
  937.     if ( !win )
  938.         {
  939.         Message("Window error.");
  940.         return();
  941.         }
  942.  
  943.     rp = win->RPort;
  944.  
  945.     SetAPen(rp, 1);
  946.     Move(rp, 0,    0);
  947.     Draw(rp, 0,    42);
  948.     Draw(rp, 0+1,  42-1);
  949.     Draw(rp, 0+1,  0);
  950.     Draw(rp, 187-1, 0);
  951.     Draw(rp, 187-3, 0+2);
  952.     Draw(rp, 187-3, 42-2);
  953.     Move(rp, 187-2, 0+2);
  954.     Draw(rp, 187-2, 42-1);
  955.     Draw(rp, 0,    42-1);
  956.  
  957.     SetAPen(rp, 2);
  958.     Move(rp, 187-1, 0+1);
  959.     Draw(rp, 0+3,  0+1);
  960.     Draw(rp, 0+3,  42-2);
  961.     Move(rp, 0+2,  0+1);
  962.     Draw(rp, 0+2,  42-1);
  963.     Draw(rp, 0+1,  42);
  964.     Draw(rp, 187,   42);
  965.     Draw(rp, 187,   0);
  966.     Draw(rp, 187-1, 0+1);
  967.     Draw(rp, 187-1, 42);
  968.  
  969.     Message("Enter filename.");
  970.  
  971.     /**********/
  972.     /* Select */
  973.     /**********/
  974.         {
  975.         BOOL bool = TRUE;
  976.  
  977.         while ( bool )
  978.             {
  979.             struct IntuiMessage *msg;
  980.  
  981.             Wait( 1 << win->UserPort->mp_SigBit );
  982.  
  983.             while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  984.                 {
  985.                 ULONG class;
  986.                 struct Gadget *iaddress;
  987.  
  988.                 class = msg->Class;
  989.                 iaddress = msg->IAddress;
  990.  
  991.                 ReplyMsg(msg);
  992.  
  993.                 if ( class == GADGETUP )
  994.                     {
  995.                     bool = FALSE;
  996.  
  997.                     switch (iaddress->GadgetID)
  998.                         {
  999.                         case 0 :
  1000.                         case 2 :
  1001.                             {
  1002.                             ok = TRUE;
  1003.                             break;
  1004.                             }
  1005.                         case 1 :
  1006.                             {
  1007.                             ok = FALSE;
  1008.                             break;
  1009.                             }
  1010.                         } /* switch */
  1011.                     } /* if (class) */
  1012.                 } /* while ( msg =... */
  1013.             } /* while (bool) */
  1014.         } /* Select */
  1015.  
  1016.     CloseWindow(win);
  1017.  
  1018.     if ( ok && strlen(NameBuffer) )
  1019.         {
  1020.         struct FileHandle *file;
  1021.  
  1022.         sprintf(FileName, "%sOther/%s", Dir, NameBuffer);
  1023.  
  1024.         if ( file = (struct FileHandle *)Open(FileName, MODE_OLDFILE) )
  1025.             {
  1026.             Close(file);
  1027.             Message("File already exists.");
  1028.             return();
  1029.             }
  1030.  
  1031.         if ( file = (struct FileHandle *)Open(FileName, MODE_NEWFILE) )
  1032.             {
  1033.             Close(file);
  1034.             Message("File created.");
  1035.             }
  1036.         }
  1037.     else
  1038.         Message("Job cancelled.");
  1039.     }
  1040.  
  1041. /****************************/
  1042. /* MAIN             */
  1043. /****************************/
  1044.  
  1045. int main()
  1046.     {
  1047.     if ( !output )
  1048.         output = (struct FileHandle *)Output();
  1049.  
  1050.     /************/
  1051.     /* get date */
  1052.     /************/
  1053.         {
  1054.         time_t t;
  1055.  
  1056.         t = time(NULL);
  1057.         date = localtime(&t);
  1058.         sprintf(Date, "%02d-%02d-%02d", date->tm_mday, date->tm_mon+1, date->tm_year);
  1059.         sprintf(Title, "NoteEdit V1.0   %s", Date);
  1060.         }
  1061.  
  1062.     /*************/
  1063.     /* load conf */
  1064.     /*************/
  1065.  
  1066.     LoadConfiguration();
  1067.  
  1068.     window = (struct Window *)OpenWindow(&nw);
  1069.     if (window)
  1070.         {
  1071.         struct RastPort *rp = window->RPort;
  1072.  
  1073.         SetWindowTitles(window, -1, "A Paradise Soft Production");
  1074.  
  1075.  
  1076.         SetAPen(rp, 1);
  1077.         Move(rp, LEFT,    TOP);
  1078.         Draw(rp, LEFT,    BOTTOM);
  1079.         Draw(rp, LEFT+1,  BOTTOM-1);
  1080.         Draw(rp, LEFT+1,  TOP);
  1081.         Draw(rp, RIGHT-1, TOP);
  1082.         Draw(rp, RIGHT-3, TOP+2);
  1083.         Draw(rp, RIGHT-3, BOTTOM-2);
  1084.         Move(rp, RIGHT-2, TOP+2);
  1085.         Draw(rp, RIGHT-2, BOTTOM-1);
  1086.         Draw(rp, LEFT,    BOTTOM-1);
  1087.  
  1088.         SetAPen(rp, 2);
  1089.         Move(rp, RIGHT-1, TOP+1);
  1090.         Draw(rp, LEFT+3,  TOP+1);
  1091.         Draw(rp, LEFT+3,  BOTTOM-2);
  1092.         Move(rp, LEFT+2,  TOP+1);
  1093.         Draw(rp, LEFT+2,  BOTTOM-1);
  1094.         Draw(rp, LEFT+1,  BOTTOM);
  1095.         Draw(rp, RIGHT,   BOTTOM);
  1096.         Draw(rp, RIGHT,   TOP);
  1097.         Draw(rp, RIGHT-1, TOP+1);
  1098.         Draw(rp, RIGHT-1, BOTTOM);
  1099.  
  1100.         ShowLoadDate();
  1101.  
  1102.         /****************/
  1103.         /* GET PASSWORD */
  1104.         /****************/
  1105.  
  1106.         GetPassword();
  1107.  
  1108.         /*************/
  1109.         /* MAIN LOOP */
  1110.         /*************/
  1111.  
  1112.         BOOL bool = TRUE;
  1113.  
  1114.         while (bool)
  1115.             {
  1116.             struct IntuiMessage *msg;
  1117.  
  1118.             Wait( 1 << window->UserPort->mp_SigBit );
  1119.  
  1120.             while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
  1121.                 {
  1122.                 ULONG class;
  1123.                 USHORT code;
  1124.                 struct Gadget *iaddress;
  1125.  
  1126.                 class = msg->Class;
  1127.                 code = msg->Code;
  1128.                 iaddress = msg->IAddress;
  1129.  
  1130.                 ReplyMsg(msg);
  1131.  
  1132.                 switch (class)
  1133.                     {
  1134.                     case GADGETUP :
  1135.                         {
  1136.                         switch (iaddress->GadgetID)
  1137.                             {
  1138.                             case 0 :
  1139.                                 {
  1140.                                 EditTodayText();
  1141.                                 break;
  1142.                                 }
  1143.                             case 1 :
  1144.                                 {
  1145.                                 GetPassword();
  1146.                                 break;
  1147.                                 }
  1148.                             case 2 :
  1149.                                 {
  1150.                                 EditConfig();
  1151.                                 break;
  1152.                                 }
  1153.                             case 3 :
  1154.                                 {
  1155.                                 LoadText();
  1156.                                 break;
  1157.                                 }
  1158.                             case 4 :
  1159.                                 {
  1160.                                 Other();
  1161.                                 break;
  1162.                                 }
  1163.                             case 5 :
  1164.                                 {
  1165.                                 New();
  1166.                                 break;
  1167.                                 }
  1168.                             case 10 :
  1169.                                 {
  1170.                                 date->tm_mday++;
  1171.                                 if ( date->tm_mday == 32 )
  1172.                                     date->tm_mday = 1;
  1173.  
  1174.                                 ShowLoadDate();
  1175.                                 break;
  1176.                                 }
  1177.                             case 11 :
  1178.                                 {
  1179.                                 date->tm_mon++;
  1180.                                 if ( date->tm_mon == 12 )
  1181.                                     date->tm_mon = 0;
  1182.  
  1183.                                 ShowLoadDate();
  1184.                                 break;
  1185.                                 }
  1186.                             case 12 :
  1187.                                 {
  1188.                                 date->tm_year++;
  1189.                                 if ( date->tm_year == 100 )
  1190.                                     date->tm_year = 00;
  1191.  
  1192.                                 ShowLoadDate();
  1193.                                 break;
  1194.                                 }
  1195.                             case 20 :
  1196.                                 {
  1197.                                 date->tm_mday--;
  1198.                                 if ( date->tm_mday == 0 )
  1199.                                     date->tm_mday = 31;
  1200.  
  1201.                                 ShowLoadDate();
  1202.                                 break;
  1203.                                 }
  1204.                             case 21 :
  1205.                                 {
  1206.                                 date->tm_mon--;
  1207.                                 if ( date->tm_mon == -1 )
  1208.                                     date->tm_mon = 11;
  1209.  
  1210.                                 ShowLoadDate();
  1211.                                 break;
  1212.                                 }
  1213.                             case 22 :
  1214.                                 {
  1215.                                 date->tm_year--;
  1216.                                 if ( date->tm_year == -1 )
  1217.                                     date->tm_year = 99;
  1218.  
  1219.                                 ShowLoadDate();
  1220.                                 break;
  1221.                                 }
  1222.                             }
  1223.                         break;
  1224.                         }
  1225.                     case CLOSEWINDOW :
  1226.                         {
  1227.                         bool = FALSE;
  1228.                         break;
  1229.                         }
  1230.                     } /* switch class */
  1231.                 } /* while msg */
  1232.             } /* while (bool) */
  1233.  
  1234.         /********/
  1235.         /* EXIT */
  1236.         /********/
  1237.  
  1238.         CloseWindow(window);
  1239.         } /* if (window) */
  1240.  
  1241.     return(0);
  1242.     }
  1243.  
  1244. /****************************/
  1245. /* WBMAIN            */
  1246. /****************************/
  1247.  
  1248. int wbmain(void *dummy)
  1249.     {
  1250.     output = (struct FileHandle *)Open("CON:0/200/640/56/Output", MODE_OLDFILE);
  1251.  
  1252.     if ( output )
  1253.         {
  1254.         main();
  1255.         Close(output);
  1256.         }
  1257.  
  1258.     return(0);
  1259.     }
  1260.  
  1261.