home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / h / if / Misc / c / ARC next >
Encoding:
Text File  |  1995-01-18  |  12.5 KB  |  547 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.         os_gbpbstr filedir;
  156.         os_regset  regs;
  157.         char Buffer[61];
  158.         int  File_Type;
  159.         int  Length;
  160.         char Text_Type[11];
  161.         char Time5[10];
  162.         char Date_Buffer[31];
  163.         char Size_Buffer[21];
  164.         char Text_Buffer[81];
  165.         char Permissions[5];
  166.  
  167.         filedir.action      = 11;
  168.         filedir.file_handle = (int)path;
  169.         filedir.data_addr   = Buffer;
  170.         filedir.number      = 1;
  171.         filedir.seq_point   = 0;
  172.         filedir.buf_len     = 60;
  173.         filedir.wild_fld    = "*";
  174.  
  175.         if (os_gbpb(&filedir) != (os_error *)NULL)
  176.                 return(0);
  177.  
  178.         while (filedir.seq_point > 0)
  179.         {
  180.                 if (wildmat(Buffer + 29, pattern))
  181.                 {
  182.                         if (Buffer[16] == 1)
  183.                         {
  184.                                 memcpy(&File_Type, Buffer + 1, sizeof(int));
  185.                                 File_Type &= 0x0FFF;
  186.  
  187.                                 regs.r[0] = 18;
  188.                                 regs.r[2] = File_Type;
  189.  
  190.                                 if (os_swix(OS_FSControl, ®s) != (os_error *)NULL)
  191.                                         return(0);
  192.  
  193.                                 memcpy(Text_Type + 0, ®s.r[2], 4);
  194.                                 memcpy(Text_Type + 4, ®s.r[3], 4);
  195.                                 Text_Type[8] = '\0';
  196.  
  197.                                 *Permissions = '\0';
  198.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  199.                                 if (Buffer[12] & 0x02) strcat(Permissions, "W");
  200.                                 if (Buffer[12] & 0x01) strcat(Permissions, "R");
  201.                         }
  202.                         else
  203.                         {
  204.                                 strcpy(Text_Type, "Directory");
  205.                                 strcpy(Permissions, "D");
  206.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  207.                         }
  208.  
  209.                         memcpy(Time5 + 0, Buffer + 4, 4);
  210.                         memcpy(Time5 + 4, Buffer + 0, 1);
  211.                         memcpy(&Length, Buffer + 8, 4);
  212.  
  213.                         regs.r[0] = (int)Time5;
  214.                         regs.r[1] = (int)Date_Buffer;
  215.                         regs.r[2] = 30;
  216.                         regs.r[3] = (int)"%24:%MI:%SE %DY-%M3-%CE%YR";
  217.  
  218.                         if (os_swix(OS_ConvertDateAndTime, ®s) != (os_error *)NULL)
  219.                                 return(0);
  220.  
  221.                         regs.r[0] = Length;
  222.                         regs.r[1] = (int)Size_Buffer;
  223.                         regs.r[2] = 20;
  224.  
  225.                         if (os_swix(OS_ConvertFixedFileSize, ®s) != (os_error *)NULL)
  226.                                 return(0);
  227.  
  228.                         sprintf(Text_Buffer, "%-11s%-4s%-9s %s %s\n",
  229.                                Buffer + 29, Permissions, Text_Type,
  230.                                Date_Buffer, Size_Buffer);
  231.                         strcat(data, Text_Buffer);
  232.                 }
  233.  
  234.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  235.                         return(0);
  236.         }
  237.  
  238.         return(1);
  239. }
  240.  
  241. static int create_ls(char *path, char *pattern, char *data)
  242. {
  243.         os_gbpbstr filedir;
  244.         char Buffer[61];
  245.  
  246.         filedir.action      = 9;
  247.         filedir.file_handle = (int)path;
  248.         filedir.data_addr   = Buffer;
  249.         filedir.number      = 1;
  250.         filedir.seq_point   = 0;
  251.         filedir.buf_len     = 60;
  252.         filedir.wild_fld    = "*";
  253.  
  254.         if (os_gbpb(&filedir) != (os_error *)NULL)
  255.                 return(0);
  256.  
  257.         while (filedir.seq_point > 0)
  258.         {
  259.                 if (wildmat(Buffer, pattern))
  260.                 {
  261.                         strcat(data, Buffer);
  262.                         strcat(data, "\n");
  263.                 }
  264.  
  265.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  266.                         return(0);
  267.         }
  268.  
  269.         return(1);
  270. }
  271.  
  272. static int isdir(char *name)
  273. {
  274.         os_regset regs;
  275.  
  276.         regs.r[0] = 13;
  277.         regs.r[1] = (int)"*";
  278.         regs.r[4] = (int)name;
  279.  
  280.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  281.                 return(0);
  282.  
  283.         if (regs.r[0] == 2)
  284.                 return(1);
  285.  
  286.         return(0);
  287. }
  288.  
  289. static int Star(register char *s, register char *p)
  290. {
  291.     while (wildmat(s, p) == 0)
  292.         if (*++s == '\0')
  293.             return(0);
  294.     return(1);
  295. }
  296.  
  297. static int wildmat(register char *s, register char *p)
  298. {
  299.     register int last;
  300.     register int matched;
  301.     register int reverse;
  302.     char name[21];
  303.     char *t;
  304.  
  305.     for (t = name; (*t = tolower(*s)) != '\0'; t++, s++);
  306.     s = name;
  307.  
  308.     for ( ; *p; s++, p++)
  309.         switch (*p) {
  310.             case '\\':
  311.                 /* Literal match with following character; fall through. */
  312.                 p++;
  313.             default:
  314.                 if (*s != *p)
  315.                     return(0);
  316.                 continue;
  317.             case '?':
  318.                 /* Match anything. */
  319.                 if (*s == '\0')
  320.                     return(0);
  321.                 continue;
  322.             case '*':
  323.                 /* Trailing star matches everything. */
  324.                 return(*++p ? Star(s, p) : 1);
  325.             case '[':
  326.                 /* [^....] means inverse character class. */
  327.                 reverse = p[1] == '^';
  328.                 if (reverse)
  329.                     p++;
  330.                 for (last = 0400, matched = 0; *++p && *p != ']'; last = *p)
  331.                     /* This next line requires a good C compiler. */
  332.                     if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
  333.                         matched = 1;
  334.                 if (matched == reverse)
  335.                     return(0);
  336.                 continue;
  337.         }
  338.  
  339.     /* For "tar" use, matches that end at a slash also work. --hoptoad!gnu */
  340.     return(*s == '\0' || *s == '/');
  341. }
  342.  
  343. int mkdir(char *path)
  344. {
  345.         os_regset regs;
  346.  
  347.         regs.r[0] = 8;
  348.         regs.r[1] = (int)path;
  349.         regs.r[4] = 0;
  350.  
  351.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  352.                  return(-1);
  353.  
  354.         return(0); 
  355. }
  356.  
  357. int rmdir(char *path)
  358. {
  359.         os_regset regs;
  360.  
  361.         regs.r[0] = 6;
  362.         regs.r[1] = (int)path;
  363.  
  364.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  365.                   return(-1);
  366.  
  367.         return(0);
  368. }
  369.  
  370. int access(char *name, int modes)
  371. {
  372.        os_regset regs;
  373.        int  Attributes;
  374.        char full_name[81];
  375.  
  376.        regs.r[0] = (int)name;
  377.        regs.r[1] = (int)full_name;
  378.        regs.r[2] = 80;
  379.  
  380.        if (os_swix(OS_GSTrans, ®s) != (os_error *)NULL)
  381.                return(-1);
  382.  
  383.        regs.r[0] = 13;
  384.        regs.r[1] = (int)"";
  385.        regs.r[4] = (int)full_name;
  386.  
  387.        if (os_swix(OS_File, ®s) != (os_error *)NULL)
  388.                 return(-1);
  389.  
  390.        if (regs.r[0] == 0)
  391.                return(-1);
  392.  
  393.        Attributes = regs.r[5] & 0xFF;
  394.  
  395.        switch (modes)
  396.        {
  397.              case 0: return(0);
  398.              case 2: return((Attributes & 0x02) ? 0 : -1);
  399.              case 4: return((Attributes & 0x01) ? 0 : -1);
  400.              case 6: return(((Attributes & 0x01) && (Attributes & 0x02)) ? 0 : -1);
  401.        }
  402.  
  403.        return(-1); /* !!! */
  404. }
  405.  
  406. /* Some useful routins for ensureing the presence of a path
  407.    when creating a file.
  408.  */
  409.  
  410. static os_error* file_create(char *f, int t, int len)
  411. {
  412.   os_filestr file;
  413.  
  414.   file.action = (t==0x1000 || t==0x2000)?8:11;
  415.   file.name = f;
  416.   file.loadaddr = (t==0x1000 || t==0x2000)?0:t;
  417.   file.execaddr = 0;
  418.   file.start = 0;
  419.   file.end = (t==0x1000 || t==0x2000)?0:len;
  420.   return os_file(&file);
  421. }
  422.  
  423. static os_error *file_objtype(char *f, int *t)
  424. {
  425.   os_error *e;
  426.   os_filestr file;
  427.  
  428.   file.action = 20;
  429.   file.name = f;
  430.   file.loadaddr = 0;
  431.   file.execaddr = 0;
  432.   file.start = 0;
  433.   file.end = 0;
  434.   e = os_file(&file);
  435.  
  436.   if (e==NULL)
  437.     *t = file.action;
  438.  
  439.   return e;
  440. }
  441.  
  442. int check_path(char *path)
  443. {
  444.   int t;
  445.   os_error *e;
  446.  
  447.   if (e = file_objtype(path, &t), e!=NULL)
  448.     return (int)e;
  449.   return t;
  450. }
  451.  
  452. os_error* file_settype(char *f, int t)
  453. {
  454.   os_filestr file;
  455.  
  456.   file.action = 18;
  457.   file.name = f;
  458.   file.loadaddr = t;
  459.   return os_file(&file);
  460. }
  461.  
  462. os_error *create_path(char *path, int size, int type)
  463. {
  464.   os_error *e = NULL;
  465.   char buf[256];
  466.   char *p = buf;
  467.   char *s = path;
  468.   int t;
  469.   
  470.   os_error file_in_path = 
  471.   { 0, "File in specified path" };
  472.  
  473.   os_error obj_is_dir = 
  474.   { 0, "Specified file is a directory" };
  475.  
  476.   while (*s>' ')
  477.   {
  478.     while (*s!='.' && *s>' ')
  479.       *p++ = *s++;
  480.  
  481.     *p = '\0';
  482.  
  483.     if (e = file_objtype(buf, &t), e!=NULL)
  484.       return e;
  485.  
  486.     if (t==0)
  487.     {
  488.       if (e = file_create(buf, (*s>' ')?0x1000:type, (*s>' ')?0:size), e!=NULL)
  489.         return e;
  490.     }
  491.     else if (*s>' ' && t==1)
  492.       return &file_in_path;
  493.     else if (*s<=' ' && t==2)
  494.       return &obj_is_dir;
  495.  
  496.     if (*s=='.')
  497.       *p++ = *s++;
  498.   }
  499.  
  500.   if (e==NULL)
  501.     file_settype(path, type);
  502.  
  503.   return e;
  504. }    
  505.  
  506. int atoft(char *s)
  507. {
  508.   os_regset r;
  509.  
  510.   r.r[0] = 31;
  511.   r.r[1] = (int)s;
  512.   if (os_swix(XOS_FSControl, &r))
  513.     return -1;
  514.   return r.r[2];
  515. }
  516.  
  517. char *fttoa(int n)
  518. {
  519.   static char s[9];
  520.   char *p;
  521.  
  522.   os_regset r;
  523.  
  524.   r.r[0] = 18;
  525.   r.r[2] = n;
  526.   if (n<0 || os_swix(XOS_FSControl, &r))
  527.     return "Unknown";
  528.  
  529.   *(int *)s = r.r[2];
  530.   *(int *)(s+4) = r.r[3];
  531.   s[8] = '\0';
  532.  
  533.   if (p = strchr(s, ' '), p!=NULL)
  534.     *p = '\0';
  535.  
  536.   return s;
  537. }
  538.  
  539. char *fttola(int n)
  540. {
  541.   char *p;
  542.   char *s = fttoa(n);
  543.   for (p=s; *p; p++)
  544.     *p = tolower(*p);
  545.   return s;
  546. }
  547.