home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / ARC next >
Text File  |  1993-04-13  |  10KB  |  378 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "werr.h"
  7. #include "os.h"
  8. #include "swis.h"
  9. #include "global.h"
  10. #include "cmdparse.h"
  11. #include "iface.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.  
  21. static int Star(char *, char *);
  22. static int wildmat(char *, char *);
  23. static int create_dir(char *, char *, char *);
  24. static int create_ls(char *, char *, char *);
  25. static int isdir(char *);
  26.  
  27. extern struct cmds attab[];
  28. extern char nospace[];
  29.  
  30. void check_time(void)
  31. {
  32.         static int clockstart = 0;
  33.         static clock_t clkval = 0;
  34.         clock_t nticks;
  35.         clock_t clkadj;
  36.  
  37.         if (!clockstart)
  38.         {
  39.                 clockstart = 1;
  40.                 clkval = clock();
  41.                 return;
  42.         }
  43.                               
  44.         clkadj = clock();
  45.  
  46.         nticks = clkadj - clkval;
  47.         clkval = clkadj;
  48.  
  49.         while (nticks-- > 0)
  50.         {
  51.                 tick();
  52.                 iss();
  53.         }
  54. }
  55.  
  56. void filedir (char *name, int times, char *ret_str)
  57. {
  58.         static int count;
  59.         os_regset regs;
  60.         char Buffer[21];
  61.  
  62.         if (times == 0) count = 0;
  63.  
  64.         /*
  65.          * Make sure that the NULL is there incase we don't find anything
  66.          */
  67.         ret_str[0] = '\0';
  68.  
  69.         regs.r[0] = 9;
  70.         regs.r[1] = (int)name;
  71.         regs.r[2] = (int)Buffer;
  72.         regs.r[3] = 1;
  73.         regs.r[4] = count;
  74.         regs.r[5] = 20;
  75.         regs.r[6] = (int)"*";
  76.  
  77.         if (os_swix(OS_GBPB, ®s) != (os_error *)NULL)
  78.                return;
  79.  
  80.         if (regs.r[4] <= 0) return;
  81.  
  82.         count = regs.r[4];
  83.  
  84.         strcpy(ret_str, Buffer);
  85. }
  86.  
  87. char *dir(char *path, int full)
  88. {       
  89.         char *data;
  90.         char *name;
  91.         char *s;
  92.  
  93.         if ((data = malloc(80 * 80)) == NULL)
  94.         {
  95.                 werr(0,nospace);
  96.                 return(NULL);
  97.         }
  98.  
  99.         *data = '\0';
  100.         name = "*";
  101.  
  102.         /* Root directory is a special case */
  103.         if (path == NULLCHAR || *path == '\0')
  104.         {
  105.                 path = "$";
  106.         }
  107.         else
  108.         {
  109.                 if (strpbrk(path, "*?[]") != NULLCHAR || !isdir(path))
  110.                 {
  111.                         s = strrchr(path, '.');
  112.                         name = s + 1;
  113.                         *s = '\0';
  114.                 }
  115.         }
  116.  
  117.         if (full)
  118.               create_dir(path, name, data);
  119.         else
  120.               create_ls(path, name, data);
  121.  
  122.         return(data);
  123. }
  124.  
  125. static int create_dir(char *path, char *pattern, char *data)
  126. {
  127.         os_gbpbstr filedir;
  128.         os_regset  regs;
  129.         char Buffer[61];
  130.         int  File_Type;
  131.         int  Length;
  132.         char Text_Type[11];
  133.         char Time5[10];
  134.         char Date_Buffer[31];
  135.         char Size_Buffer[21];
  136.         char Text_Buffer[81];
  137.         char Permissions[5];
  138.  
  139.         filedir.action      = 11;
  140.         filedir.file_handle = (int)path;
  141.         filedir.data_addr   = Buffer;
  142.         filedir.number      = 1;
  143.         filedir.seq_point   = 0;
  144.         filedir.buf_len     = 60;
  145.         filedir.wild_fld    = "*";
  146.  
  147.         if (os_gbpb(&filedir) != (os_error *)NULL)
  148.                 return(0);
  149.  
  150.         while (filedir.seq_point > 0)
  151.         {
  152.                 if (wildmat(Buffer + 29, pattern))
  153.                 {
  154.                         if (Buffer[16] == 1)
  155.                         {
  156.                                 memcpy(&File_Type, Buffer + 1, sizeof(int));
  157.                                 File_Type &= 0x0FFF;
  158.  
  159.                                 regs.r[0] = 18;
  160.                                 regs.r[2] = File_Type;
  161.  
  162.                                 if (os_swix(OS_FSControl, ®s) != (os_error *)NULL)
  163.                                         return(0);
  164.  
  165.                                 memcpy(Text_Type + 0, ®s.r[2], 4);
  166.                                 memcpy(Text_Type + 4, ®s.r[3], 4);
  167.                                 Text_Type[8] = '\0';
  168.  
  169.                                 *Permissions = '\0';
  170.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  171.                                 if (Buffer[12] & 0x02) strcat(Permissions, "W");
  172.                                 if (Buffer[12] & 0x01) strcat(Permissions, "R");
  173.                         }
  174.                         else
  175.                         {
  176.                                 strcpy(Text_Type, "Directory");
  177.                                 strcpy(Permissions, "D");
  178.                                 if (Buffer[12] & 0x08) strcat(Permissions, "L");
  179.                         }
  180.  
  181.                         memcpy(Time5 + 0, Buffer + 4, 4);
  182.                         memcpy(Time5 + 4, Buffer + 0, 1);
  183.                         memcpy(&Length, Buffer + 8, 4);
  184.  
  185.                         regs.r[0] = (int)Time5;
  186.                         regs.r[1] = (int)Date_Buffer;
  187.                         regs.r[2] = 30;
  188.                         regs.r[3] = (int)"%24:%MI:%SE %DY-%M3-%CE%YR";
  189.  
  190.                         if (os_swix(OS_ConvertDateAndTime, ®s) != (os_error *)NULL)
  191.                                 return(0);
  192.  
  193.                         regs.r[0] = Length;
  194.                         regs.r[1] = (int)Size_Buffer;
  195.                         regs.r[2] = 20;
  196.  
  197.                         if (os_swix(OS_ConvertFixedFileSize, ®s) != (os_error *)NULL)
  198.                                 return(0);
  199.  
  200.                         sprintf(Text_Buffer, "%-11s%-4s%-9s %s %s\n",
  201.                                Buffer + 29, Permissions, Text_Type,
  202.                                Date_Buffer, Size_Buffer);
  203.                         strcat(data, Text_Buffer);
  204.                 }
  205.  
  206.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  207.                         return(0);
  208.         }
  209.  
  210.         return(1);
  211. }
  212.  
  213. static int create_ls(char *path, char *pattern, char *data)
  214. {
  215.         os_gbpbstr filedir;
  216.         char Buffer[61];
  217.  
  218.         filedir.action      = 9;
  219.         filedir.file_handle = (int)path;
  220.         filedir.data_addr   = Buffer;
  221.         filedir.number      = 1;
  222.         filedir.seq_point   = 0;
  223.         filedir.buf_len     = 60;
  224.         filedir.wild_fld    = "*";
  225.  
  226.         if (os_gbpb(&filedir) != (os_error *)NULL)
  227.                 return(0);
  228.  
  229.         while (filedir.seq_point > 0)
  230.         {
  231.                 if (wildmat(Buffer, pattern))
  232.                 {
  233.                         strcat(data, Buffer);
  234.                         strcat(data, "\n");
  235.                 }
  236.  
  237.                 if (os_gbpb(&filedir) != (os_error *)NULL)
  238.                         return(0);
  239.         }
  240.  
  241.         return(1);
  242. }
  243.  
  244. static int isdir(char *name)
  245. {
  246.         os_regset regs;
  247.  
  248.         regs.r[0] = 13;
  249.         regs.r[1] = (int)"*";
  250.         regs.r[4] = (int)name;
  251.  
  252.         if (os_swix(OS_File, ®s) != (os_error *)NULL)
  253.                 return(0);
  254.  
  255.         if (regs.r[0] == 2)
  256.                 return(1);
  257.  
  258.         return(0);
  259. }
  260.  
  261. static int Star(register char *s, register char *p)
  262. {
  263.     while (wildmat(s, p) == 0)
  264.         if (*++s == '\0')
  265.             return(0);
  266.     return(1);
  267. }
  268.  
  269. static int wildmat(register char *s, register char *p)
  270. {
  271.     register int last;
  272.     register int matched;
  273.     register int reverse;
  274.     char name[21];
  275.     char *t;
  276.  
  277.     for (t = name; (*t = tolower(*s)) != '\0'; t++, s++);
  278.     s = name;
  279.  
  280.     for ( ; *p; s++, p++)
  281.         switch (*p) {
  282.             case '\\':
  283.                 /* Literal match with following character; fall through. */
  284.                 p++;
  285.             default:
  286.                 if (*s != *p)
  287.                     return(0);
  288.                 continue;
  289.             case '?':
  290.                 /* Match anything. */
  291.                 if (*s == '\0')
  292.                     return(0);
  293.                 continue;
  294.             case '*':
  295.                 /* Trailing star matches everything. */
  296.                 return(*++p ? Star(s, p) : 1);
  297.             case '[':
  298.                 /* [^....] means inverse character class. */
  299.                 reverse = p[1] == '^';
  300.                 if (reverse)
  301.                     p++;
  302.                 for (last = 0400, matched = 0; *++p && *p != ']'; last = *p)
  303.                     /* This next line requires a good C compiler. */
  304.