home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / internet / tcpip / src205 / TCPIP_Src / Misc / c / ARC next >
Encoding:
Text File  |  1995-03-08  |  12.5 KB  |  549 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "os.h"
  7. #include "swis.h"
  8. #include "global.h"
  9. #include "cmdparse.h"
  10. #include "iface.h"
  11. #include "icmp.h"
  12. #include "timer.h"
  13. #include "netuser.h"
  14. #include "tcp.h"
  15. #include "smtp.h"
  16. #include "misc.h"
  17. #include "arc.h"
  18.  
  19. #define TICKSPERSEC    (1000L / MSPTICK)
  20. #define TICKLIMIT      (2 * TICKSPERSEC)
  21.  
  22. static int Star(char *, char *);
  23. static int wildmat(char *, char *);
  24. static int create_dir(char *, char *, char *);
  25. static int create_ls(char *, char *, char *);
  26. static int isdir(char *);
  27.  
  28. extern struct cmds attab[];
  29.  
  30. void time_adj(char *itime, int adj)
  31. {
  32.   BOOL carry = FALSE;
  33.   char btime[8];
  34.   int loop, temp;
  35.  
  36.  /* memcpy(&rtime[3], &itime[0], 5); */
  37.   *((int *)&btime[0]) = adj * 100;
  38.   if (adj < 0)
  39.     *((int *)&btime[4]) = -1;
  40.   else
  41.     *((int *)&btime[4]) = 0;
  42.  
  43.   for (loop = 0; loop < 5; loop++)
  44.   {
  45.     temp = itime[loop] + btime[loop] + carry;
  46.     itime[loop] = temp & 0xFF;
  47.     carry = ((temp & 0x100) != 0);
  48.   }
  49.  /* memcpy(&itime[0], &ntime[3], 5); */
  50. }
  51.  
  52. void check_time(void)
  53. {
  54.         static int clockstart = 0;
  55.         static clock_t clkval = 0;
  56.         clock_t nticks;
  57.         clock_t clkadj;
  58.  
  59.         if (!clockstart)
  60.         {
  61.                 clockstart = 1;
  62.                 clkval = clock();
  63.                 return;
  64.         }
  65.                               
  66.         clkadj = clock();
  67.         nticks = clkadj - clkval;
  68.         clkval = clkadj;
  69.         clkadj = nticks;
  70.         nticks /= 5;
  71.         clkval -= (clkadj - (nticks * 5));
  72.  
  73.         if (nticks > TICKLIMIT)
  74.                 nticks = TICKLIMIT;
  75.  
  76.         while (nticks-- > 0)
  77.         {
  78.                 icmpclk();
  79.                 tick();
  80.                 iss();
  81.         }
  82. }
  83.  
  84. void filedir (char *name, int times, char *ret_str)
  85. {
  86.         static int count;
  87.         os_regset regs;
  88.         char Buffer[21];
  89.  
  90.         if (times == 0) count = 0;
  91.  
  92.         /*
  93.          * Make sure that the NULL is there incase we don't find anything
  94.          */
  95.         ret_str[0] = '\0';
  96.  
  97.         regs.r[0] = 9;
  98.         regs.r[1] = (int)name;
  99.         regs.r[2] = (int)Buffer;
  100.         regs.r[3] = 1;
  101.         regs.r[4] = count;
  102.         regs.r[5] = 20;
  103.         regs.r[6] = (int)"*";
  104.  
  105.         if (os_swix(OS_GBPB, ®s) != (os_error *)NULL)
  106.                return;
  107.  
  108.         if (regs.r[4] <= 0) return;
  109.  
  110.         count = regs.r[4];
  111.  
  112.         strcpy(ret_str, Buffer);
  113. }
  114.  
  115. char *dir(char *path, int full)
  116. {       
  117.         char *data;
  118.         char *name;
  119.         char *s;
  120.  
  121.         if ((data = malloc(80 * 80)) == NULL)
  122.         {
  123.                 cwprintf(NULL, "Out of memory in ARC\r\n");
  124.                 return(NULL);
  125.         }
  126.  
  127.         *data = '\0';
  128.         name = "*";
  129.  
  130.         /* Root directory is a special case */
  131.         if (path == NULLCHAR || *path == '\0')
  132.         {
  133.                 path = "$";
  134.         }
  135.         else
  136.         {
  137.                 if (strpbrk(path, "*?[]") != NULLCHAR || !isdir(path))
  138.                 {
  139.                         s = strrchr(path, '.');
  140.                         name = s + 1;
  141.                         *s = '\0';
  142.                 }
  143.         }
  144.  
  145.         if (full)
  146.               create_dir(path, name, data);
  147.         else
  148.               create_ls(path, name, data);
  149.  
  150.         return(data);
  151. }
  152.  
  153. static int create_dir(char *path, char *pattern, char *data)
  154. {
  155.         char *cp;
  156.         os_gbpbstr filedir;
  157.         os_regset  regs;
  158.         char Buffer[81];
  159.         int  File_Type;
  160.         int  Length;
  161.         char Text_Type[11];
  162.         char Time5[10];
  163.         char Date_Buffer[31];
  164.         char Size_Buffer[21];
  165.         char Text_Buffer[81];
  166.         char Permissions[5];
  167.  
  168.         filedir.action      = 11;
  169.         filedir.file_handle = (int)path;
  170.         filedir.data_addr   = Buffer;
  171.         filedir.number      = 1;
  172.         filedir.seq_point   = 0;
  173.         filedir.buf_len     = 80;
  174.         filedir.wild_fld    = "*";
  175.  
  176.         if (os_gbpb(&filedir) != (os_error *)NULL)
  177.                 return(0);
  178.  
  179.         while (filedir.seq_point > 0)
  180.         {
  181.                 if (wildmat(Buffer + 29, pattern))
  182.                 {
  183.                         if (Buffer[16] == 1)
  184.                         {
  185.                                 memcpy(&File_Type, Buffer + 1, sizeof(int));
  186.                                 File_Type &= 0x0FFF;
  187.  
  188.                                 regs.r[0] = 18;
  189.                                 regs.r[2] = File_Type;
  190.  
  191.                                 if (os_swix(OS_FSControl, ®s) != (os_error *)NULL)
  192.                                         return(0);
  193.  
  194.                                 memcpy(Text_Type + 0, ®s.r[2], 4);
  195.                                 memcpy(Text_Type + 4, ®s.r[3], 4);
  196.                                 Text_Type[8] = '\0';
  197.  
  198.                                 *Permissions = '\0';
  199.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  200.                                 if (Buffer[12] & 0x02) strcat(Permissions, "W");
  201.                                 if (Buffer[12] & 0x01) strcat(Permissions, "R");
  202.                         }
  203.                         else
  204.                         {
  205.                                 strcpy(Text_Type, "Directory");
  206.                                 strcpy(Permissions, "D");
  207.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  208.                         }
  209.  
  210.                         memcpy(Time5 + 0, Buffer + 4, 4);
  211.                         memcpy(Time5 + 4, Buffer + 0, 1);
  212.                         memcpy(&Length, Buffer + 8, 4);
  213.  
  214.                         regs.r[0] = (int)Time5;
  215.                         regs.r[1] = (int)Date_Buffer;
  216.                         regs.r[2] = 30;
  217.                         regs.r[3] = (int)"%24:%MI:%SE %DY-%M3-%CE%YR";
  218.  
  219.                         if (os_swix(OS_ConvertDateAndTime, ®s) != (os_error *)NULL)
  220.                                 return(0);
  221.  
  222.                         regs.r[0] = Length;
  223.                         regs.r[1] = (int)Size_Buffer;
  224.                         regs.r[2] = 20;
  225.  
  226.                         if (os_swix(OS_ConvertFixedFileSize, ®s) != (os_error *)NULL)
  227.                                 return(0);
  228.  
  229.                         sprintf(Text_Buffer, "%-11s%-4s%-9s %s %s\n",
  230.                                Buffer + 29, Permissions, Text_Type,
  231.                                Date_Buffer, Size_Buffer);
  232.                         strcat(data, Text_Buffer);
  233.                 }
  234.  
  235.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  236.                         return(0);
  237.         }
  238.  
  239.         return(1);
  240. }
  241.  
  242. static int create_ls(char *path, char *pattern, char *data)
  243. {
  244.         char *cp;
  245.         os_gbpbstr filedir;
  246.         char Buffer[61];
  247.  
  248.         filedir.action      = 9;
  249.         filedir.file_handle = (int)path;
  250.         filedir.data_addr   = Buffer;
  251.         filedir.number      = 1;
  252.         filedir.seq_point   = 0;
  253.         filedir.buf_len     = 60;
  254.         filedir.wild_fld    = "*";
  255.  
  256.         if (os_gbpb(&filedir) != (os_error *)NULL)
  257.                 return(0);
  258.  
  259.         while (filedir.seq_point > 0)
  260.         {
  261.                 if (wildmat(Buffer, pattern))
  262.                 {
  263.                         strcat(data, Buffer);
  264.                         strcat(data, "\n");
  265.                 }
  266.  
  267.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  268.                         return(0);
  269.         }
  270.  
  271.         return(1);
  272. }
  273.  
  274. static int isdir(char *name)
  275. {
  276.         os_regset regs;
  277.  
  278.         regs.r[0] = 13;
  279.         regs.r[1] = (int)"*";
  280.         regs.r[4] = (int)name;
  281.  
  282.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  283.                 return(0);
  284.  
  285.         if (regs.r[0] == 2)
  286.                 return(1);
  287.  
  288.         return(0);
  289. }
  290.  
  291. static int Star(register char *s, register char *p)
  292. {
  293.     while (wildmat(s, p) == 0)
  294.         if (*++s == '\0')
  295.             return(0);
  296.     return(1);
  297. }
  298.  
  299. static int wildmat(register char *s, register char *p)
  300. {
  301.     register int last;
  302.     register int matched;
  303.     register int reverse;
  304.     char name[21];
  305.     char *t;
  306.  
  307.     for (t = name; (*t = tolower(*s)) != '\0'; t++, s++);
  308.     s = name;
  309.  
  310.     for ( ; *p; s++, p++)
  311.         switch (*p) {
  312.             case '\\':
  313.                 /* Literal match with following character; fall through. */
  314.                 p++;
  315.             default:
  316.                 if (*s != *p)
  317.                     return(0);
  318.                 continue;
  319.             case '?':
  320.                 /* Match anything. */
  321.                 if (*s == '\0')
  322.                     return(0);
  323.                 continue;
  324.             case '*':
  325.                 /* Trailing star matches everything. */
  326.                 return(*++p ? Star(s, p) : 1);
  327.             case '[':
  328.                 /* [^....] means inverse character class. */
  329.                 reverse = p[1] == '^';
  330.                 if (reverse)
  331.                     p++;
  332.                 for (last = 0400, matched = 0; *++p && *p != ']'; last = *p)
  333.                     /* This next line requires a good C compiler. */
  334.                     if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
  335.                         matched = 1;
  336.                 if (matched == reverse)
  337.                     return(0);
  338.                 continue;
  339.         }
  340.  
  341.     /* For "tar" use, matches that end at a slash also work. --hoptoad!gnu */
  342.     return(*s == '\0' || *s == '/');
  343. }
  344.  
  345. int mkdir(char *path)
  346. {
  347.         os_regset regs;
  348.  
  349.         regs.r[0] = 8;
  350.         regs.r[1] = (int)path;
  351.         regs.r[4] = 0;
  352.  
  353.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  354.                  return(-1);
  355.  
  356.         return(0); 
  357. }
  358.  
  359. int rmdir(char *path)
  360. {
  361.         os_regset regs;
  362.  
  363.         regs.r[0] = 6;
  364.         regs.r[1] = (int)path;
  365.  
  366.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  367.                   return(-1);
  368.  
  369.         return(0);
  370. }
  371.  
  372. int access(char *name, int modes)
  373. {
  374.        os_regset regs;
  375.        int  Attributes;
  376.        char full_name[81];
  377.  
  378.        regs.r[0] = (int)name;
  379.        regs.r[1] = (int)full_name;
  380.        regs.r[2] = 80;
  381.  
  382.        if (os_swix(OS_GSTrans, ®s) != (os_error *)NULL)
  383.                return(-1);
  384.  
  385.        regs.r[0] = 13;
  386.        regs.r[1] = (int)"";
  387.        regs.r[4] = (int)full_name;
  388.  
  389.        if (os_swix(OS_File, ®s) != (os_error *)NULL)
  390.                 return(-1);
  391.  
  392.        if (regs.r[0] == 0)
  393.                return(-1);
  394.  
  395.        Attributes = regs.r[5] & 0xFF;
  396.  
  397.        switch (modes)
  398.        {
  399.              case 0: return(0);
  400.              case 2: return((Attributes & 0x02) ? 0 : -1);
  401.              case 4: return((Attributes & 0x01) ? 0 : -1);
  402.              case 6: return(((Attributes & 0x01) && (Attributes & 0x02)) ? 0 : -1);
  403.        }
  404.  
  405.        return(-1); /* !!! */
  406. }
  407.  
  408. /* Some useful routins for ensureing the presence of a path
  409.    when creating a file.
  410.  */
  411.  
  412. os_error* file_create(char *f, int t, int len)
  413. {
  414.   os_filestr file;
  415.  
  416.   file.action = (t==0x1000 || t==0x2000)?8:11;
  417.   file.name = f;
  418.   file.loadaddr = (t==0x1000 || t==0x2000)?0:t;
  419.   file.execaddr = 0;
  420.   file.start = 0;
  421.   file.end = (t==0x1000 || t==0x2000)?0:len;
  422.   return os_file(&file);
  423. }
  424.  
  425. os_error *file_objtype(char *f, int *t)
  426. {
  427.   os_error *e;
  428.   os_filestr file;
  429.  
  430.   file.action = 20;
  431.   file.name = f;
  432.   file.loadaddr = 0;
  433.   file.execaddr = 0;
  434.   file.start = 0;
  435.   file.end = 0;
  436.   e = os_file(&file);
  437.  
  438.   if (e==NULL)
  439.     *t = file.action;
  440.  
  441.   return e;
  442. }
  443.  
  444. int check_path(char *path)
  445. {
  446.   int t;
  447.   os_error *e;
  448.  
  449.   if (e = file_objtype(path, &t), e!=NULL)
  450.     return (int)e;
  451.   return t;
  452. }
  453.  
  454. os_error* file_settype(char *f, int t)
  455. {
  456.   os_filestr file;
  457.  
  458.   file.action = 18;
  459.   file.name = f;
  460.   file.loadaddr = t;
  461.   return os_file(&file);
  462. }
  463.  
  464. os_error *create_path(char *path, int size, int type)
  465. {
  466.   os_error *e = NULL;
  467.   char buf[256];
  468.   char *p = buf;
  469.   char *s = path;
  470.   int t;
  471.   
  472.   os_error file_in_path = 
  473.   { 0, "File in specified path" };
  474.  
  475.   os_error obj_is_dir = 
  476.   { 0, "Specified file is a directory" };
  477.  
  478.   while (*s>' ')
  479.   {
  480.     while (*s!='.' && *s>' ')
  481.       *p++ = *s++;
  482.  
  483.     *p = '\0';
  484.  
  485.     if (e = file_objtype(buf, &t), e!=NULL)
  486.       return e;
  487.  
  488.     if (t==0)
  489.     {
  490.       if (e = file_create(buf, (*s>' ')?0x1000:type, (*s>' ')?0:size), e!=NULL)
  491.         return e;
  492.     }
  493.     else if (*s>' ' && t==1)
  494.       return &file_in_path;
  495.     else if (*s<=' ' && t==2)
  496.       return &obj_is_dir;
  497.  
  498.     if (*s=='.')
  499.       *p++ = *s++;
  500.   }
  501.  
  502.   if (e==NULL)
  503.     file_settype(path, type);
  504.  
  505.   return e;
  506. }    
  507.  
  508. int atoft(char *s)
  509. {
  510.   os_regset r;
  511.  
  512.   r.r[0] = 31;
  513.   r.r[1] = (int)s;
  514.   if (os_swix(XOS_FSControl, &r))
  515.     return -1;
  516.   return r.r[2];
  517. }
  518.  
  519. char *fttoa(int n)
  520. {
  521.   static char s[9];
  522.   char *p;
  523.  
  524.   os_regset r;
  525.  
  526.   r.r[0] = 18;
  527.   r.r[2] = n;
  528.   if (n<0 || os_swix(XOS_FSControl, &r))
  529.     return "Unknown";
  530.  
  531.   *(int *)s = r.r[2];
  532.   *(int *)(s+4) = r.r[3];
  533.   s[8] = '\0';
  534.  
  535.   if (p = strchr(s, ' '), p!=NULL)
  536.     *p = '\0';
  537.  
  538.   return s;
  539. }
  540.  
  541. char *fttola(int n)
  542. {
  543.   char *p;
  544.   char *s = fttoa(n);
  545.   for (p=s; *p; p++)
  546.     *p = tolower(*p);
  547.   return s;
  548. }
  549.