?login_element?

Subversion Repositories NedoOS

Rev

Rev 1980 | Rev 2072 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <intrz80.h>
  4. #include <stdlib.h>
  5. #include <oscalls.h>
  6. #include <terminal.c>
  7. #include <tcp.h>
  8. #include <osfs.h>
  9. #include <intrz80.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. unsigned char uVer[] = "0.46";
  13. unsigned char curPath[128];
  14. unsigned char curLetter;
  15. unsigned char oldBinExt;
  16. unsigned int errn, headlng;
  17. unsigned long contLen;
  18. unsigned char saveFlag, saveBak;
  19. unsigned char crlf[2] = {13, 10};
  20. unsigned long downloaded;
  21. unsigned char status, key, curFormat;
  22. struct sockaddr_in targetadr;
  23. struct readstructure readStruct;
  24. FILE *fp2;
  25.  
  26. struct window
  27. {
  28.         unsigned char x;
  29.         unsigned char y;
  30.         unsigned char w;
  31.         unsigned char h;
  32.         unsigned char text;
  33.         unsigned char back;
  34.         unsigned char tittle[80];
  35.  
  36. } cw;
  37.  
  38. struct configuration
  39. {
  40.         unsigned char kernelName[32];
  41.         unsigned char machineName[32];
  42.         unsigned char kernelLink[256];
  43.         unsigned char is_atm;
  44. } config;
  45.  
  46. unsigned int bufSize = 2048; // Some memory corruption at this point, some QnD
  47. unsigned char netbuf[4000];
  48.  
  49. void clearStatus(void)
  50. {
  51.         AT(1, 24);
  52.         printf("                                                                                \r");
  53. }
  54.  
  55. void printTable(void)
  56. {
  57.         unsigned int cycle;
  58.  
  59.         for (cycle = 32; cycle < 256; cycle++)
  60.         {
  61.                 printf("%03u:", cycle);
  62.                 putchar(cycle);
  63.                 printf(" ");
  64.                 if (cycle % 10 == 0)
  65.                 {
  66.                         printf("\r\n");
  67.                 }
  68.         }
  69. }
  70.  
  71. void delay(unsigned long counter)
  72. {
  73.         unsigned long start, finish;
  74.         counter = counter / 20;
  75.         if (counter < 1)
  76.         {
  77.                 counter = 1;
  78.         }
  79.         start = time();
  80.         finish = start + counter;
  81.  
  82.         while (start < finish)
  83.         {
  84.                 start = time();
  85.         }
  86. }
  87.  
  88. void printNews(void) // max 20 lines in total and 59 col.
  89. {
  90.         FILE *fpNews;
  91.         unsigned char str[1];
  92.         unsigned char curLine, nbyte;
  93.  
  94.         fpNews = OS_OPENHANDLE("updater.new", 0x80);
  95.         if (((int)fpNews) & 0xff)
  96.         {
  97.  
  98. #include <printnews.c>
  99.  
  100.                 return;
  101.         }
  102.         curLine = 0;
  103.         while (curLine < 20)
  104.         {
  105.                 AT(20, 3 + curLine);
  106.                 while (1)
  107.                 {
  108.                         OS_READHANDLE(str, fpNews, sizeof(str));
  109.  
  110.                         if (errno != 0)
  111.                         {
  112.                                 OS_CLOSEHANDLE(fpNews);
  113.                                 return;
  114.                         }
  115.  
  116.                         nbyte = str[0];
  117.  
  118.                         if (nbyte != 13)
  119.                         {
  120.                                 putchar(nbyte);
  121.                         }
  122.                         else
  123.                         {
  124.                                 break;
  125.                         }
  126.                 }
  127.                 OS_READHANDLE(str, fpNews, sizeof(str));
  128.                 curLine++;
  129.         }
  130.         OS_CLOSEHANDLE(fpNews);
  131. }
  132.  
  133. void drawWindow(struct window w)
  134. {
  135.         unsigned char wcount, tempx, tittleStart;
  136.  
  137.         AT(w.x, w.y - 1);
  138.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2) + 1;
  139.         BOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  140.         AT(w.x, w.y);
  141.         ATRIB(w.text);
  142.         putchar(201);
  143.         for (wcount = 0; wcount < w.w; wcount++)
  144.         {
  145.                 putchar(205);
  146.         }
  147.         putchar(187);
  148.  
  149.         AT(w.x, w.y + w.h);
  150.         putchar(200);
  151.         for (wcount = 0; wcount < w.w; wcount++)
  152.         {
  153.                 putchar(205);
  154.         }
  155.         putchar(188);
  156.  
  157.         tempx = w.x + w.w + 1;
  158.         for (wcount = 1; wcount < w.h; wcount++)
  159.         {
  160.                 AT(w.x, w.y + wcount);
  161.                 putchar(186);
  162.                 AT(tempx, w.y + wcount);
  163.                 putchar(186);
  164.         }
  165.  
  166.         AT(w.x, w.y + 2);
  167.         putchar(199);
  168.         for (wcount = 0; wcount < w.w; wcount++)
  169.         {
  170.                 putchar(196);
  171.         }
  172.         putchar(182);
  173.  
  174.         AT(tittleStart, w.y + 1);
  175.         printf("%s", w.tittle);
  176. }
  177.  
  178. void fatalError(unsigned char *message)
  179. {
  180.         strcpy(cw.tittle, "FATAL ERROR!");
  181.  
  182.         if (strlen(message) > strlen(cw.tittle))
  183.         {
  184.                 cw.w = strlen(message) + 2;
  185.         }
  186.         else
  187.                 cw.w = strlen(cw.tittle) + 2;
  188.         cw.x = 80 / 2 - cw.w / 2;
  189.         cw.y = 11;
  190.         cw.h = 4;
  191.         cw.text = 97;
  192.         cw.back = 41;
  193.  
  194.         drawWindow(cw);
  195.         AT(cw.x + 2, cw.y + 3);
  196.         printf("%s", message);
  197.         AT(1, 1);
  198.         getchar();
  199.         exit(0);
  200. }
  201.  
  202. void infoBox(unsigned char *message)
  203. {
  204.         strcpy(cw.tittle, "nedoOS system updater ");
  205.         strcat(cw.tittle, uVer);
  206.  
  207.         if (strlen(message) > strlen(cw.tittle))
  208.         {
  209.                 cw.w = strlen(message) + 2;
  210.         }
  211.         else
  212.                 cw.w = strlen(cw.tittle) + 2;
  213.         cw.x = 80 / 2 - cw.w / 2;
  214.         cw.y = 15;
  215.         cw.h = 4;
  216.         cw.text = 97;
  217.         cw.back = 42;
  218.  
  219.         drawWindow(cw);
  220.         AT(cw.x + 2, cw.y + 3);
  221.         printf("%s", message);
  222.         AT(1, 1);
  223. }
  224.  
  225. unsigned char OS_SHELL(unsigned char *command)
  226. {
  227.         unsigned char fileName[] = "bin/cmd.com";
  228.         unsigned char appCmd[127] = "cmd.com ";
  229.         unsigned int shellSize, loaded, loop, adr;
  230.         unsigned char pgbak;
  231.         union APP_PAGES shell_pg;
  232.         union APP_PAGES main_pg;
  233.         FILE *fp3;
  234.         main_pg.l = OS_GETMAINPAGES();
  235.         pgbak = main_pg.pgs.window_3;
  236.         OS_GETPATH((unsigned int)&curPath);
  237.         strcat(appCmd, command);
  238.         fp3 = OS_OPENHANDLE(fileName, 0x80);
  239.         if (((int)fp3) & 0xff)
  240.         {
  241.                 clearStatus();
  242.                 AT(1, 24);
  243.                 printf("%s", fileName);
  244.                 printf(" not found.");
  245.                 getchar();
  246.                 exit(0);
  247.         }
  248.         shellSize = OS_GETFILESIZE(fp3);
  249.  
  250.         OS_NEWAPP((unsigned int)&shell_pg);
  251.  
  252.         shell_pg.l = OS_GETAPPMAINPAGES(shell_pg.pgs.pId);
  253.  
  254.         SETPG32KHIGH(shell_pg.pgs.window_0);
  255.  
  256.         memcpy((unsigned char *)(0xC080), (unsigned char *)(&appCmd), strlen(appCmd) + 1);
  257.  
  258.         loop = 0;
  259.         while (loop < shellSize)
  260.         {
  261.                 loaded = OS_READHANDLE(netbuf, fp3, sizeof(netbuf));
  262.                 adr = 0xC100 + loop;
  263.                 memcpy((unsigned char *)(adr), &netbuf, loaded);
  264.                 loop = loop + loaded;
  265.         }
  266.         OS_CLOSEHANDLE(fp3);
  267.         SETPG32KHIGH(pgbak);
  268.         clearStatus();
  269.         AT(1, 24);
  270.         printf("Shell [pId:%u][%s][%s]", shell_pg.pgs.pId, curPath, appCmd);
  271.         AT(1, 24);
  272.         delay(300);
  273.         OS_RUNAPP(shell_pg.pgs.pId);
  274.         AT(1, 4);
  275.         OS_WAITPID(shell_pg.pgs.pId);
  276.         return shell_pg.pgs.pId;
  277. }
  278. //////////////// NETWORK PART //////////////////////
  279. // #include <network.c>
  280. void errorPrint(unsigned int error)
  281. {
  282.         clearStatus();
  283.         switch (error)
  284.         {
  285.         case 2:
  286.                 printf("02 SHUT_RDWR");
  287.                 break;
  288.         case 4:
  289.                 printf("04 ERR_INTR");
  290.                 break;
  291.         case 23:
  292.                 printf("23 ERR_NFILE");
  293.                 break;
  294.         case 35:
  295.                 printf("35 ERR_EAGAIN");
  296.                 break;
  297.         case 37:
  298.                 printf("37 ERR_ALREADY");
  299.                 break;
  300.         case 38:
  301.                 printf("38 ERR_NOTSOCK");
  302.                 break;
  303.         case 40:
  304.                 printf("40 ERR_EMSGSIZE");
  305.                 break;
  306.         case 41:
  307.                 printf("41 ERR_PROTOTYPE");
  308.                 break;
  309.         case 47:
  310.                 printf("47 ERR_AFNOSUPPORT");
  311.                 break;
  312.         case 53:
  313.                 printf("53 ERR_ECONNABORTED");
  314.                 break;
  315.         case 54:
  316.                 printf("54 ERR_CONNRESET");
  317.                 break;
  318.         case 57:
  319.                 printf("57 ERR_NOTCONN");
  320.                 break;
  321.         case 65:
  322.                 printf("65 ERR_HOSTUNREACH");
  323.                 break;
  324.         default:
  325.                 printf("[%u] UNKNOWN ERROR", error);
  326.                 break;
  327.         }
  328.         YIELD();
  329.         do
  330.         {
  331.                 key = _low_level_get();
  332.         } while (key == 0);
  333. }
  334.  
  335. unsigned int httpError(void)
  336. {
  337.         unsigned char *httpRes;
  338.         unsigned int httpErr;
  339.         httpRes = strstr(netbuf, "HTTP/1.1 ");
  340.  
  341.         if (httpRes != NULL)
  342.         {
  343.                 httpErr = atol(httpRes + 9);
  344.                 if (httpErr == 200)
  345.                 {
  346.                         return httpErr;
  347.                 }
  348.         }
  349.  
  350.         BOX(1, 1, 80, 25, 40, 32);
  351.         AT(1, 1);
  352.         printf("HTTP ERROR %u", httpErr);
  353.  
  354.         fatalError("Server response error!");
  355.         return httpErr;
  356. }
  357.  
  358. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  359. {
  360.         unsigned char socket;
  361.         unsigned int todo;
  362.         todo = OS_NETSOCKET((family << 8) + protocol);
  363.         if (todo > 32767)
  364.         {
  365.                 clearStatus();
  366.                 AT(1, 24);
  367.                 printf("OS_NETSOCKET: ");
  368.                 errorPrint(todo & 255);
  369.                 exit(0);
  370.         }
  371.         else
  372.         {
  373.                 socket = ((todo & 65280) >> 8);
  374.         }
  375.         return socket;
  376. }
  377.  
  378. signed char netShutDown(signed char socket, unsigned char type)
  379. {
  380.         unsigned int todo;
  381.         todo = OS_NETSHUTDOWN(socket, type);
  382.         if (todo > 32767)
  383.         {
  384.                 clearStatus();
  385.                 printf("OS_NETSHUTDOWN: [ERROR:");
  386.                 errorPrint(todo & 255);
  387.                 printf("] Press any key.");
  388.                 return -1;
  389.         }
  390.         else
  391.         {
  392.                 // printf("Socket #%d closed.\n\r", socket);
  393.         }
  394.         return 1;
  395. }
  396.  
  397. unsigned char netConnect(unsigned char socket)
  398. {
  399.         unsigned int todo;
  400.         unsigned char retry = 10;
  401.  
  402.         targetadr.family = AF_INET;
  403.         targetadr.porth = 00;
  404.         targetadr.portl = 80;
  405.         targetadr.b1 = 31;
  406.         targetadr.b2 = 31;
  407.         targetadr.b3 = 65;
  408.         targetadr.b4 = 35;
  409.  
  410.         while (retry > 0)
  411.         {
  412.                 todo = OS_NETCONNECT(socket, &targetadr);
  413.  
  414.                 if (todo > 32767)
  415.                 {
  416.                         retry--;
  417.                         clearStatus();
  418.                         printf("OS_NETCONNECT [ERROR:");
  419.                         errorPrint(todo & 255);
  420.                         printf("] [Retry:%u]", retry);
  421.                         YIELD();
  422.                         netShutDown(socket, 0);
  423.                         socket = OpenSock(AF_INET, SOCK_STREAM);
  424.                 }
  425.                 else
  426.                 {
  427.                         // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  428.                         return 1;
  429.                 }
  430.         }
  431.         getchar();
  432.         exit(0);
  433.         return 0;
  434. }
  435.  
  436. unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
  437. {
  438.         if (operation == 00)
  439.         {
  440.                 unsigned char fileName[255];
  441.                 strcpy(fileName, fileNamePtr);
  442.                 fp2 = OS_CREATEHANDLE(fileName, 0x80);
  443.                 if (((int)fp2) & 0xff)
  444.                 {
  445.                         clearStatus();
  446.                         AT(1, 24);
  447.                         printf("%s", fileName);
  448.                         printf(" creating error.");
  449.                         exit(0);
  450.                 }
  451.                 OS_CLOSEHANDLE(fp2);
  452.  
  453.                 fp2 = OS_OPENHANDLE(fileName, 0x80);
  454.                 if (((int)fp2) & 0xff)
  455.                 {
  456.                         clearStatus();
  457.                         AT(1, 24);
  458.                         printf("%s", fileName);
  459.                         printf(" opening error.");
  460.  
  461.                         exit(0);
  462.                 }
  463.                 AT(1, 24);
  464.                 return 0;
  465.         }
  466.  
  467.         if (operation == 01)
  468.         {
  469.                 OS_WRITEHANDLE(netbuf + headlng, fp2, sizeOfBuf);
  470.                 downloaded = downloaded + sizeOfBuf;
  471.                 return 0;
  472.         }
  473.  
  474.         if (operation == 02)
  475.         {
  476.                 OS_CLOSEHANDLE(fp2);
  477.                 return 0;
  478.         }
  479.         return 0;
  480. }
  481.  
  482. void cancel(void)
  483. {
  484.         key = _low_level_get();
  485.         if (key == 27)
  486.         {
  487.                 fatalError("File download aborted!");
  488.         }
  489. }
  490.  
  491. unsigned int tcpRead(unsigned char socket)
  492. {
  493.         unsigned char retry = 20;
  494.         unsigned int todo;
  495.         readStruct.socket = socket;
  496.         readStruct.BufAdr = (unsigned int)&netbuf;
  497.         readStruct.bufsize = bufSize;
  498.         readStruct.protocol = SOCK_STREAM;
  499.  
  500.         while (retry > 0)
  501.         {
  502.                 todo = OS_WIZNETREAD(&readStruct);
  503.  
  504.                 if (todo > 32767)
  505.                 {
  506.                         if ((todo & 255) != ERR_EAGAIN)
  507.                         {
  508.                                 clearStatus();
  509.                                 printf("OS_WIZNETREAD: [ERROR:");
  510.                                 errorPrint(todo & 255);
  511.                                 printf("] [Retry:%u]", retry);
  512.                                 YIELD();
  513.                                 retry--;
  514.                         }
  515.                 }
  516.                 else
  517.                 {
  518.                         // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  519.                         return todo;
  520.                 }
  521.         }
  522.         getchar();
  523.         exit(0);
  524.         return todo;
  525. }
  526.  
  527. unsigned int cutHeader(unsigned int todo)
  528. {
  529.         unsigned int q;
  530.         unsigned char *count;
  531.  
  532.         count = strstr(netbuf, "Content-Length:");
  533.         if (count == NULL)
  534.         {
  535.                 clearStatus();
  536.                 AT(1, 24);
  537.                 printf("Content-Length:  not found.");
  538.                 contLen = 0;
  539.         }
  540.         else
  541.         {
  542.                 contLen = atol(count + 15);
  543.                 //    AT (1,24);
  544.                 //      printf("=> Dlinna  soderzhimogo = %lu \n\r", contLen);
  545.         }
  546.  
  547.         count = strstr(netbuf, "\r\n\r\n");
  548.         headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  549.         q = todo - headlng;
  550.         return q;
  551. }
  552.  
  553. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  554. {
  555.         unsigned char retry = 20;
  556.         unsigned int todo;
  557.         readStruct.socket = socket;
  558.         readStruct.BufAdr = messageadr;
  559.         readStruct.bufsize = size;
  560.         readStruct.protocol = SOCK_STREAM;
  561.  
  562. wizwrite:
  563.         todo = OS_WIZNETWRITE(&readStruct);
  564.         if (todo > 32767)
  565.         {
  566.                 clearStatus();
  567.                 AT(1, 24);
  568.                 printf("OS_WIZNETWRITE: ");
  569.                 errorPrint(todo & 255);
  570.                 if (retry == 0)
  571.                 {
  572.                         exit(0);
  573.                 }
  574.                 retry--;
  575.                 cancel();
  576.                 delay(100);
  577.                 goto wizwrite;
  578.         }
  579.  
  580.         return todo;
  581. }
  582. unsigned char getFile(unsigned char *fileLink, unsigned char *fileNamePtr)
  583. {
  584.         unsigned int todo;
  585.         unsigned char cmdlist1[] = " HTTP/1.1\r\nHost: nedoos.ru\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  586.         unsigned char socket;
  587.         unsigned int headskip;
  588.         unsigned long bytecount;
  589.         unsigned long fileSize1;
  590.         unsigned char fileName[255];
  591.         unsigned int httpErr;
  592.         strcpy(netbuf, "GET ");
  593.         strcat(netbuf, fileLink);
  594.         strcat(netbuf, cmdlist1);
  595.         clearStatus();
  596.         socket = OpenSock(AF_INET, SOCK_STREAM);
  597.         todo = netConnect(socket);
  598.         todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  599.  
  600.         headskip = 0;
  601.         bytecount = 255;
  602.         downloaded = 0;
  603.         strcpy(fileName, fileNamePtr);
  604.         AT(1, 24);
  605.         printf("%s", fileName);
  606.  
  607.         while (bytecount != 0)
  608.         {
  609.                 headlng = 0;
  610.                 todo = tcpRead(socket);
  611.                 if (todo == 0)
  612.                 {
  613.                         break;
  614.                 }
  615.                 if (headskip == 0)
  616.                 {
  617.                         httpErr = httpError();
  618.                         headskip = 1;
  619.                         todo = cutHeader(todo);
  620.                         fileSize1 = contLen / 1024;
  621.                         bytecount = contLen;
  622.                         saveBuf(fileName, 00, 0);
  623.                 }
  624.                 AT(32, 24);
  625.                 printf("%lu of %lu kb  ", downloaded / 1024, fileSize1);
  626.  
  627.                 saveBuf(fileName, 01, todo);
  628.                 bytecount = bytecount - todo;
  629.  
  630.                 cancel();
  631.         }
  632.         netShutDown(socket, 0);
  633.         saveBuf(fileName, 02, 00);
  634.         if (downloaded != contLen)
  635.         {
  636.                 fatalError("File download error!");
  637.         }
  638.         return 0;
  639. }
  640. ////////////////////////////////////////////////////
  641.  
  642. unsigned char getConfig(void)
  643. {
  644.         config.is_atm = (unsigned char)OS_GETCONFIG();
  645.         // H=system drive, L= 1-Evo 2-ATM2 3-ATM3 6-p2.666 ;E=pgsys(system page) D= TR-DOS page
  646.         switch ((config.is_atm))
  647.         {
  648.         case 1:
  649.                 strcpy(config.machineName, "ZX-Evolution");
  650.                 strcpy(config.kernelName, "sd_boot.$C");
  651.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/sd_boot.%24C");
  652.                 break;
  653.         case 2:
  654.                 strcpy(config.machineName, "TURBO 2+");
  655.                 strcpy(config.kernelName, "osatm2hd.$C");
  656.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osatm2hd.%24C");
  657.  
  658.                 break;
  659.  
  660.         case 3: // SD HDD versions
  661.                 strcpy(config.machineName, "TURBO 3 [SD]");
  662.                 strcpy(config.kernelName, "osatm3hd.$C");
  663.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osatm3hd.%24C");
  664.                 break;
  665.         case 6: // SD HDD versions
  666.                 strcpy(config.machineName, "P2.666 [SD]");
  667.                 strcpy(config.kernelName, "osp26sd.$C");
  668.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osp26sd.%24C");
  669.                 break;
  670.  
  671.         default:
  672.                 strcpy(config.machineName, "NOT DETECED (ZX-Evo)");
  673.                 strcpy(config.kernelName, "sd_boot.$C");
  674.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/sd_boot.%24C");
  675.                 break;
  676.         }
  677.         return config.is_atm;
  678. }
  679. // Downloading minimal tools for updating/boot
  680. void getTools(void)
  681. {
  682.         unsigned char pkunzipLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/pkunzip.com";
  683.         unsigned char tarLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/tar.com";
  684.         unsigned char cmdLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/cmd.com";
  685.         unsigned char termLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/term.com";
  686.         unsigned char updLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/updater.com";
  687.         unsigned char newsLink[] = "/svn/dl.php?repname=NedoOS&path=/release/doc/updater.new";
  688.         unsigned char wizNetLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/wizcfg.com";
  689.         unsigned char netIniLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/net.ini";
  690.  
  691.         OS_MKDIR("bin"); // Create if not exist
  692.         ATRIB(cw.text);
  693.         ATRIB(cw.back);
  694.         getFile(wizNetLink, "bin/wizcfg.com");
  695.         getFile(newsLink, "updater.new");
  696.         getFile(pkunzipLink, "bin/pkunzip.com");
  697.         getFile(tarLink, "bin/tar.com");
  698.         getFile(cmdLink, "bin/cmd.com");
  699.         getFile(termLink, "bin/term.com");
  700.         getFile(updLink, "bin/updater.com");
  701.         getFile(netIniLink, "bin/net_.ini");
  702. }
  703.  
  704. void deleteWorkFiles(void)
  705. {
  706.         OS_DELETE("bin.zip");
  707.         OS_DELETE("bin.tar");
  708. }
  709.  
  710. unsigned char ren2old(unsigned char *name)
  711. {
  712.         unsigned char *oldName = "0000000000000000000000000000000000";
  713.         unsigned char counter = 255;
  714.         OS_MKDIR((void *)name);
  715.         sprintf(oldName, "%s.old", name);
  716.         while (OS_RENAME((void *)name, (void *)oldName) != 0)
  717.         {
  718.                 counter++;
  719.                 if (counter == 255)
  720.                 {
  721.                         fatalError("Unable to rename old folder");
  722.                 }
  723.  
  724.                 sprintf(oldName, "%s.%u", name, counter);
  725.         }
  726.         return counter;
  727. }
  728.  
  729. void ren2tar(void)
  730. {
  731.         unsigned char *name = "0000000000000000000000000000000000";
  732.         unsigned int counter = 2500;
  733.         errn = 255;
  734.         sprintf(name, "bin.r%u", counter);
  735.         while (errn != 0)
  736.         {
  737.                 errn = OS_RENAME((void *)name, "bin.tar");
  738.                 counter--;
  739.                 sprintf(name, "bin.r%u", counter);
  740.                 if (counter < 1800)
  741.                 {
  742.                         fatalError("Unable to rename TAR file");
  743.                 }
  744.         }
  745. }
  746.  
  747. void ren2bin(void)
  748. {
  749.         unsigned char *name = "0000000000000000000000000000000000";
  750.         unsigned char counter = 17;
  751.         errn = 255;
  752.         sprintf(name, "bin.r%u", counter);
  753.         while (errn != 0)
  754.         {
  755.                 errn = OS_RENAME((void *)name, "bin");
  756.                 counter++;
  757.                 sprintf(name, "bin.r%u", counter);
  758.                 if (counter > 99)
  759.                 {
  760.                         fatalError("Unable to rename BIN folder");
  761.                 }
  762.         }
  763. }
  764.  
  765. void restoreConfig(unsigned char oldBinExt)
  766. {
  767.         unsigned char *name = "0000000000000000000000000000000000";
  768.         unsigned char count;
  769.         errn = OS_CHDIR("/");
  770.         errn = OS_RENAME("bin/autoexec.bat", "bin/autoexec.new");
  771.         errn = OS_RENAME("bin/net.ini", "bin/net.new");
  772.         errn = OS_RENAME("bin/nv.ext", "bin/nv.new");
  773.         errn = OS_RENAME("bin/gp/gp.ini", "bin/gp/gpini.new");
  774.         errn = OS_RENAME("/bin/browser/index.gph", "/bin/browser/index.gph.new");
  775.  
  776.         errn = OS_CHDIR("/");
  777.  
  778.         if (oldBinExt == 255)
  779.         {
  780.                 errn = OS_SHELL("copy bin.old/autoexec.bat bin/autoexec.bat");
  781.  
  782.                 errn = OS_SHELL("copy bin.old/net.ini bin/net.ini");
  783.  
  784.                 errn = OS_SHELL("copy bin.old/nv.ext bin/nv.ext");
  785.  
  786.                 errn = OS_SHELL("copy bin.old/nv.pth bin/nv.pth");
  787.  
  788.                 errn = OS_SHELL("copy bin.old/gp/gp.ini bin/gp/gp.ini");
  789.                 errn = OS_SHELL("copy bin.old/browser/index.gph bin/browser/index.gph");
  790.         }
  791.         else
  792.         {
  793.                 sprintf(name, "copy bin.%u/autoexec.bat bin/autoexec.bat", oldBinExt);
  794.                 OS_SHELL((void *)name);
  795.  
  796.                 sprintf(name, "copy bin.%u/net.ini bin/net.ini", oldBinExt);
  797.                 OS_SHELL((void *)name);
  798.  
  799.                 sprintf(name, "copy bin.%u/nv.ext bin/nv.ext", oldBinExt);
  800.                 OS_SHELL((void *)name);
  801.                 sprintf(name, "copy bin.%u/nv.pth bin/nv.pth", oldBinExt);
  802.                 OS_SHELL((void *)name);
  803.                 sprintf(name, "copy bin.%u/gp/gp.ini bin/gp/gp.ini", oldBinExt);
  804.                 OS_SHELL((void *)name);
  805.                 sprintf(name, "copy bin.%u/browser/index.gph bin/browser/index.gph", oldBinExt);
  806.                 OS_SHELL((void *)name);
  807.         }
  808.         AT(1, 4);
  809.         for (count = 0; count < 15; count++)
  810.         {
  811.                 putchar(176);
  812.         }
  813.         errn = OS_RENAME("bin/autoexec.new", "bin/autoexec.bat"); // If file already exist we dont rename
  814.         errn = OS_RENAME("bin/net.new", "bin/net.ini");
  815.         errn = OS_RENAME("bin/nv.new", "bin/nv.ext");
  816.         errn = OS_RENAME("bin/gp/gpini.new", "bin/gp/gp.ini");
  817.         errn = OS_RENAME("bin/browser/index.gph.new", "bin/browser/index.gph");
  818. }
  819.  
  820. // Download, backup, unpack release.bin
  821. void fullUpdate(void)
  822. {
  823.         unsigned char relLink[] = "http://nedoos.ru/images/release.zip";
  824.  
  825.         BOX(1, 1, 80, 25, 40, 176);
  826.         cw.x = 20;
  827.         cw.y = 5;
  828.         cw.w = 40;
  829.         cw.h = 7;
  830.         cw.text = 97;
  831.         cw.back = 45;
  832.  
  833.         AT(1, 1);
  834.         ATRIB(cw.text);
  835.         ATRIB(cw.back);
  836.         printf("                   [FULL UPDATE - UPDATING ALL SYSTEM FILES]                    ");
  837.  
  838.         strcpy(cw.tittle, "nedoOS FULL updater ");
  839.         strcat(cw.tittle, uVer);
  840.  
  841.         getConfig();
  842.  
  843.         OS_GETPATH((unsigned int)&curPath);
  844.         curLetter = curPath[0];
  845.         errn = OS_CHDIR("/");
  846.  
  847.         strcat(cw.tittle, " (");
  848.         strcat(cw.tittle, config.machineName);
  849.         strcat(cw.tittle, ")");
  850.         drawWindow(cw);
  851.  
  852.         OS_DELETE("release.zip");
  853.  
  854.         clearStatus();
  855.         AT(cw.x + 2, cw.y + 3);
  856.         printf("1.Downloading release.zip...");
  857.  
  858.         errn = getFile(relLink, "release.zip"); //  Downloading the file
  859.  
  860.         clearStatus();
  861.  
  862.         clearStatus();
  863.         AT(cw.x + 2, cw.y + 4);
  864.         printf("2.Backuping old system...\r\n");
  865.  
  866.         oldBinExt = ren2old("bin");
  867.         ren2old("doc");
  868.         ren2old("nedodemo");
  869.         ren2old("nedogame");
  870.  
  871.         AT(cw.x + 2, cw.y + 5);
  872.         printf("3.Downloading tools...\r\n");
  873.  
  874.         getTools();
  875.  
  876.         BOX(1, 1, 80, 25, 40, 32);
  877.         AT(1, 1);
  878.         printf("Depacking release. Its take about 10 hours. Please wait.\r\n");
  879.         printf("First hours going without signs of life.\r\n");
  880.  
  881.         printNews();
  882.  
  883.         YIELD();
  884.         OS_SHELL("pkunzip.com release.zip");
  885.         BOX(1, 1, 80, 25, 40, 176);
  886.         drawWindow(cw);
  887.         AT(cw.x + 2, cw.y + 3);
  888.         ATRIB(cw.text);
  889.         ATRIB(cw.back);
  890.         printf("Restoring configs...");
  891. }
  892. // Updating only BIN folders, where is OS lives.
  893. void binUpdate(void)
  894. {
  895.         unsigned char binLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/&isdir=1";
  896.         BOX(1, 1, 80, 25, 40, 176);
  897.         cw.x = 20;
  898.         cw.y = 5;
  899.         cw.w = 40;
  900.         cw.h = 11;
  901.         cw.text = 97;
  902.         cw.back = 44;
  903.  
  904.         AT(1, 1);
  905.         ATRIB(cw.text);
  906.         ATRIB(cw.back);
  907.         printf("                  [STANDART UPDATE - UPDATING ONLY BIN FOLDER]                  ");
  908.  
  909.         strcpy(cw.tittle, "nedoOS BIN updater ");
  910.         strcat(cw.tittle, uVer);
  911.         getConfig();
  912.         strcat(cw.tittle, " (");
  913.         strcat(cw.tittle, config.machineName);
  914.         strcat(cw.tittle, ")");
  915.         drawWindow(cw);
  916.  
  917.         OS_CHDIR("/");
  918.         OS_GETPATH((unsigned int)&curPath);
  919.         curLetter = curPath[0];
  920.  
  921.         deleteWorkFiles();
  922.  
  923.         clearStatus();
  924.  
  925.         AT(cw.x + 2, cw.y + 10);
  926.         printf(">To full update start 'updater.com F'<");
  927.  
  928.         AT(cw.x + 2, cw.y + 3);
  929.         printf("1.Downloading bin.zip...");
  930.  
  931.         getFile(binLink, "bin.zip"); //  Downloading the file
  932.  
  933.         clearStatus();
  934.         AT(cw.x + 2, cw.y + 4);
  935.         printf("2.Downloading tools...");
  936.  
  937.         getTools();
  938.  
  939.         BOX(1, 1, 80, 25, 40, 32);
  940.         AT(1, 1);
  941.         printf("Please, make sure you don't have bin.r* folder on disk!!!\r\n");
  942.         printf("Depacking release. Its take about 10 minutes. Please wait...\r\n");
  943.  
  944.         printNews();
  945.         YIELD();
  946.  
  947.         OS_SHELL("pkunzip.com bin.zip");
  948.         BOX(1, 1, 80, 25, 40, 176);
  949.         drawWindow(cw);
  950.         clearStatus();
  951.         AT(cw.x + 2, cw.y + 3);
  952.         ATRIB(cw.text);
  953.         ATRIB(cw.back);
  954.  
  955.         printf("3.Renaming bin.r?? to bin.tar...");
  956.  
  957.         ren2tar();
  958.  
  959.         AT(cw.x + 2, cw.y + 4);
  960.         ATRIB(cw.text);
  961.         ATRIB(cw.back);
  962.         printf("4.Untaring bin.tar, please wait...");
  963.         clearStatus();
  964.         OS_SHELL("tar.com bin.tar");
  965.  
  966.         AT(cw.x + 2, cw.y + 5);
  967.         ATRIB(cw.text);
  968.         ATRIB(cw.back);
  969.         printf("5.Backuping old bin to bin.old...");
  970.  
  971.         oldBinExt = ren2old("bin");
  972.  
  973.         AT(cw.x + 2, cw.y + 6);
  974.         ATRIB(cw.text);
  975.         ATRIB(cw.back);
  976.         printf("6.Renaming NEW BIN...");
  977.  
  978.         ren2bin();
  979.  
  980.         AT(cw.x + 2, cw.y + 7);
  981.         ATRIB(cw.text);
  982.         ATRIB(cw.back);
  983.         printf("7.Deleting zip & tar...");
  984.  
  985.         AT(cw.x + 2, cw.y + 8);
  986.         ATRIB(cw.text);
  987.         ATRIB(cw.back);
  988.         printf("8.Downloading kernel [%s]...", config.machineName);
  989.         errn = OS_CHDIR("/");
  990.         errn = getFile(config.kernelLink, config.kernelName); //  Downloading the file
  991.         AT(cw.x + 2, cw.y + 9);
  992.         ATRIB(cw.text);
  993.         ATRIB(cw.back);
  994.         printf("9.Restoring configs...");
  995. }
  996.  
  997. C_task main(int argc, char *argv[])
  998. {
  999.         os_initstdio();
  1000.  
  1001.         if (argc > 1)
  1002.         {
  1003.                 if (argv[1][0] == 'F')
  1004.                 {
  1005.                         fullUpdate();
  1006.                 }
  1007.                 else
  1008.                 {
  1009.                         AT(1, 1);
  1010.                         // printTable();
  1011.                         // printNews();
  1012.                         // getchar();
  1013.                         fatalError("Use 'F' key to FULL update");
  1014.                         exit(0);
  1015.                 }
  1016.         }
  1017.         else
  1018.         {
  1019.                 binUpdate();
  1020.         }
  1021.         restoreConfig(oldBinExt);
  1022.         deleteWorkFiles();
  1023.         clearStatus();
  1024.         infoBox("System Updated successfully!");
  1025.         getchar();
  1026.         OS_DELETE("release.zip");
  1027.         ATRIB(40);
  1028.         ATRIB(32);
  1029.         exit(0);
  1030. }
  1031.