home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / NEXTARCH / FTPPARSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-25  |  5.7 KB  |  259 lines

  1. #import <string.h>
  2. #import <time.h>
  3. #import "DirEntry.h"
  4.  
  5. enum {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
  6. static const char *monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  7.                 "Aug", "Sep", "Oct", "Nov", "Dec", ""};
  8. static const char *monthFirstLetters = "JFMASOND";
  9. static int thisYear;
  10.  
  11. char *NextEntry(char *buffer)
  12. {
  13. static char *searchBuffer;
  14. char *tmp,tmp_c;
  15. int count;
  16.     if(buffer != NULL)
  17.         searchBuffer = buffer;
  18.     /* Find the next "\n[d-][rwxs-]x9" sequence in buffer */
  19.     tmp = index(searchBuffer,'\n');
  20.     if(tmp == NULL)
  21.         return (char *) -1;
  22.     searchBuffer = ++tmp;
  23.     /* Check next 10 characters */
  24.     if(strlen(tmp) < 11)
  25.         return NULL;
  26.     tmp_c = tmp[10];
  27.     tmp[10] = '\0';
  28.     count = strspn(tmp,"drwxs-");
  29.     tmp[10] = tmp_c;
  30.     if(count == 10)
  31.         return tmp;
  32.     return NULL;
  33. }
  34.  
  35. /* Search the line from the ftp dir output and return the file information.
  36.     Returns 0 on sucess with buffer set to the file name, -1 on failure. */
  37. int ParseAttributes(char **buffer,int *size,int *month,int *day,int *year,int *hr,int *min)
  38. {
  39. char *tmpPtr,*datePtr,*dayPtr,*yr_timePtr;
  40. int success;
  41.  
  42.     /* Find the start of the date string by locating its 3 letter month code */
  43.     datePtr = tmpPtr = strpbrk(*buffer,monthFirstLetters);
  44.     if(tmpPtr == NULL)
  45.         return -1;
  46.     /* Make sure this is a month name */
  47.     switch(tmpPtr[0])
  48.     {
  49.         case 'J' :
  50.             success = !strncmp(tmpPtr,monthNames[Jan],3);
  51.             if(success)
  52.             {
  53.                 *month = 1;
  54.                 break;
  55.             }
  56.             success = !strncmp(tmpPtr,monthNames[Jun],3);
  57.             if(success)
  58.             {
  59.                 *month = 6;
  60.                 break;
  61.             }
  62.             success = !strncmp(tmpPtr,monthNames[Jul],3);
  63.             if(success)
  64.             {
  65.                 *month = 7;
  66.                 break;
  67.             }
  68.             return -1;
  69.             break;
  70.         case 'F' :
  71.             if(strncmp(tmpPtr,monthNames[Feb],3) != 0)
  72.                 return -1;
  73.             *month = 2;
  74.             break;
  75.         case 'M' :
  76.             success = !strncmp(tmpPtr,monthNames[Mar],3);
  77.             if(success)
  78.             {
  79.                 *month = 3;
  80.                 break;
  81.             }
  82.             success = !strncmp(tmpPtr,monthNames[May],3);
  83.             if(success)
  84.             {
  85.                 *month = 5;
  86.                 break;
  87.             }
  88.             return -1;
  89.             break;
  90.         case 'A' :
  91.             success = !strncmp(tmpPtr,monthNames[Apr],3);
  92.             if(success)
  93.             {
  94.                 *month = 4;
  95.                 break;
  96.             }
  97.             success = !strncmp(tmpPtr,monthNames[Aug],3);
  98.             if(success)
  99.             {
  100.                 *month = 8;
  101.                 break;
  102.             }
  103.             return -1;
  104.             break;
  105.         case 'S' :
  106.             if(strncmp(tmpPtr,monthNames[Sep],3) != 0)
  107.                 return -1;
  108.             *month = 9;
  109.             break;
  110.         case 'O' :
  111.             if(strncmp(tmpPtr,monthNames[Oct],3) != 0)
  112.                 return -1;
  113.             *month = 10;
  114.             break;
  115.         case 'N' :
  116.             if(strncmp(tmpPtr,monthNames[Nov],3) != 0)
  117.                 return -1;
  118.             *month = 11;
  119.             break;
  120.         case 'D' :
  121.             if(strncmp(tmpPtr,monthNames[Dec],3) != 0)
  122.                 return -1;
  123.             *month = 12;
  124.             break;
  125.         default :
  126.             return -1;    // Never should happen
  127.     }
  128.     /* Now backup to the start of the size field */
  129.     while( isdigit(*tmpPtr) == 0)
  130.         tmpPtr --;
  131.     /* And to the start of the date */
  132.     while( isdigit(*tmpPtr) != 0)
  133.         tmpPtr --;
  134.     sscanf(++tmpPtr,"%d",size);
  135.     /* Get the day */
  136.     datePtr += 4;
  137.     while( isdigit(*datePtr) == 0)
  138.         datePtr ++;
  139.     dayPtr = datePtr;    // A pointer to the date
  140.     while( isdigit(*datePtr) != 0)
  141.         datePtr ++;
  142.     *datePtr = '\0';
  143.     *day = atoi(dayPtr);
  144.     /* Get the year or time */
  145.     datePtr ++;
  146.     while( isdigit(*datePtr) == 0)
  147.         datePtr ++;
  148.     yr_timePtr = datePtr;    // A pointer to the year or time
  149.     while( isdigit(*datePtr) != 0)
  150.         datePtr ++;
  151.     if( *datePtr == ':')
  152.     {
  153.         sscanf(yr_timePtr,"%d:%d",hr,min);
  154.         *year = thisYear;
  155.         datePtr += 3;
  156.     }
  157.     else
  158.     {
  159.         *datePtr = '\0';
  160.         *year = atoi(yr_timePtr);
  161.         *hr = *min = 0;
  162.         datePtr ++;
  163.     }
  164.     /* Lastly set the buffer to the file name */
  165.     while( isspace(*datePtr) != 0)
  166.         datePtr ++;
  167.     *buffer = datePtr;
  168.     return 0;
  169. }
  170.  
  171.  
  172. DirEntryPtr ParseBuffer(char *buffer)
  173. {
  174. DirEntryPtr tmp,list;
  175. char lineBuffer[128],*tmpPtr,*linkName;
  176. int size,month,day,year,hr,min;
  177. struct tm *timeStruct;
  178. time_t now;
  179.  
  180.     time(&now);
  181.     timeStruct = localtime(&now);
  182.     thisYear = timeStruct->tm_year + 1900;
  183.  
  184.     tmpPtr = buffer;
  185.     list = NULL;
  186.     while( (tmpPtr = NextEntry(tmpPtr)) != (char *) -1 )
  187.     {
  188.         if(tmpPtr != NULL)
  189.         {
  190.             sscanf(tmpPtr,"%[^\n]",lineBuffer);
  191.             tmpPtr = lineBuffer + 10;
  192.             if(ParseAttributes(&tmpPtr,&size,&month,&day,&year,&hr,&min) == 0)
  193.             {
  194.                 /* Don't display the '.' or '..' files */
  195.                 if(strcmp(tmpPtr,".") == 0 || strcmp(tmpPtr,"..") == 0)
  196.                 {
  197.                     tmpPtr = NULL;
  198.                     continue;
  199.                 }
  200.  
  201.                 if(list == NULL)
  202.                     tmp = list = (DirEntryPtr) malloc(sizeof(DirEntry));
  203.                 else
  204.                 {
  205.                     tmp->next = (DirEntryPtr) malloc(sizeof(DirEntry));
  206.                     tmp = tmp->next;
  207.                 }
  208.                 strncpy(tmp->mode,lineBuffer,10);
  209.                 sprintf(tmp->date,"%.4d%.2d%.2d%.2d%.2d",
  210.                     year,month,day,hr,min);
  211.                 tmp->bytes = size;
  212.                 /* Handle links - Ignore them for now
  213.                 if(lineBuffer[0] == 'l')
  214.                 {
  215.                     linkName = index(tmpPtr,'>');
  216.                     linkName -= 2;    // move back before "->"
  217.                     while( isspace(*linkName) != 0)
  218.                         linkName --;
  219.                     *(++linkName) = '\0';
  220.                 } */
  221.                 tmp->name = (char *) malloc(strlen(tmpPtr)+1);
  222.                 strcpy(tmp->name,tmpPtr);
  223.                 tmp->next = NULL;
  224.             }
  225.             tmpPtr = NULL;
  226.         }
  227.     }
  228.     return list;
  229. }
  230.  
  231. void PrintDirList(DirEntryPtr list)
  232. {
  233.     while(list != NULL)
  234.     {
  235.         fprintf(stderr,"%s %d %s %s\n",list->mode,list->bytes,list->date,list->name);
  236.         list = list->next;
  237.     }
  238. }
  239.  
  240. #ifdef FTPPARSE_MAIN
  241. #import <stdio.h>
  242. #import <sys/file.h>
  243.  
  244. char buffer[4096];
  245. main(int argc,char **argv)
  246. {
  247. int fd,n;
  248. DirEntryPtr list;
  249.  
  250.     fd = open((argc == 1 ? "./ftp_dir.out" : argv[1]),O_RDONLY);
  251.     n = read(fd,buffer,4095);
  252.     printf("Read %d bytes\n",n);
  253.     
  254.     /* Parse the file entries */
  255.     list = ParseBuffer(buffer);
  256.     PrintDirList(list);
  257. }
  258. #endif //FTPPARSE_MAIN
  259.