home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / ENGINE / PCUTIL.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  12KB  |  443 lines

  1. /*  PCUTIL.C
  2. *   Utilities for the network library that are PC specific
  3. ****************************************************************************
  4. *                                                                          *
  5. *      part of:                                                            *
  6. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  7. *      by Tim Krauskopf                                                    *
  8. *                                                                          *
  9. *      National Center for Supercomputing Applications                     *
  10. *      152 Computing Applications Building                                 *
  11. *      605 E. Springfield Ave.                                             *
  12. *      Champaign, IL  61820                                                *
  13. *                                                                          *
  14. ****************************************************************************
  15. */
  16.  
  17. /*
  18. *    Includes
  19. */
  20. #ifdef __TURBOC__
  21. #include "turboc.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #ifdef MSC
  28. #ifdef __TURBOC__
  29. #include <alloc.h>
  30. #include <dir.h>
  31. #else
  32. #include <malloc.h>
  33. #include <direct.h>
  34. #include <dos.h>
  35. #endif
  36. #endif
  37. #include "whatami.h"
  38. #include "externs.h"
  39.  
  40. static char lookcolor(char *);
  41.  
  42. /**********************************************************************/
  43. /*
  44. *   Find directory name -- return a code that indicates whether the
  45. *   directory exists or not.
  46. *   0 = dir name ok
  47. *   -1 = error
  48. *   > 0 = dos error code, no dir by this name
  49. *
  50. *   Accept certain unix conventions, like '/' for separator
  51. *
  52. *   Also, append a '\' to the name before returning
  53. *  Note:  There must be enough room in the string to append the '\'
  54. */
  55. struct dosdta {
  56.     char junk[21];
  57.     char att;
  58.     int time,date;
  59.     long int size;
  60.     char name[13];
  61. };
  62.  
  63. extern struct dosdta *dtaptr;    /* declared in ncsaio.asm */
  64.  
  65. #ifndef NET14
  66. int direxist(char dirname[])
  67. {
  68.     int i,ret;
  69.     char *p;
  70.  
  71.     if(!strcmp(dirname,".") || !dirname[0]) {
  72.         dirname[0]='\0';
  73.         return(0);
  74.       }
  75.     if(!strcmp(dirname,"\\"))
  76.         return(0);
  77.     p=dirname;
  78.     while(*p) {
  79.         switch(*p) {
  80.             case '*':
  81.             case '?':
  82.                 return(-1);
  83.  
  84.             case '/':
  85.                 *p='\\';
  86.                 break;
  87.           }
  88.         p++;
  89.       }
  90. /*
  91. *  n_findfirst  will return normal files AND directories
  92. *  must check attribute to see if it is really a directory
  93. */
  94.     ret=n_findfirst(dirname,0x10);        /* find name */
  95.  
  96.     if(ret)
  97.         return(ret);
  98.     if(!(dtaptr->att&0x10))
  99.         return(-2);                            /* is a normal file */
  100.     i=strlen(dirname);
  101.     dirname[i]='\\';                        /* extend with '\' */
  102.     dirname[++i]='\0';
  103.     return(0);
  104. }
  105.  
  106. /**********************************************************************/
  107. /* firstname
  108. *  find the first name in the given directory which matches the wildcard
  109. *  specification
  110. *
  111. *  expand '*' (unix) to '*.*' (dos)
  112. */
  113. char savepath[_MAX_DIR+_MAX_FNAME+_MAX_EXT+30]; /* allocate enough room for the pathname, filename, & extension, plus a little bit of slush */
  114. int rootlen;
  115.  
  116. char *firstname(char path[],int type)
  117. {
  118.     int i,len;
  119.     char *p,*q;
  120.  
  121.     if(!*path)
  122.         return(NULL);
  123.  
  124.     len=strlen(path);
  125.     i=0;
  126.     rootlen=0;
  127.     q=savepath;
  128.     p=path;
  129.     while(*q=*p) {                /* basic string copy with extras */
  130.         if(*p=='\\')
  131.             rootlen=i+1;            /* rootlen = position of last \ */
  132.         p++;
  133.         q++;
  134.         i++;
  135.       }
  136.     if(savepath[len-1]=='*' && rootlen==len-1) {
  137.         savepath[len++]='.';
  138.         savepath[len++]='*';
  139.         savepath[len++]='\0';
  140.       }
  141.     switch(type) {
  142.         case 1:
  143.             if(n_findfirst(savepath,0x10))
  144.                 return(NULL);
  145.              break;
  146.  
  147.         default: 
  148.             if(n_findfirst(savepath,0x00))
  149.                 return(NULL);
  150.              break;
  151.  
  152.       }
  153. /*
  154. *  copy file name, translate to lower case 
  155. */
  156.     q=&savepath[rootlen];
  157.     p=dtaptr->name;
  158.     while(*p) {
  159.         if(*p>='A' && *p<='Z')
  160.             *q++= (*p++) + (char) 32;
  161.         else
  162.             *q++=*p++;
  163.       }
  164. /*
  165. *  if it is a directory then put <DIR> after it
  166. */
  167.     if(type) {
  168.         p=&savepath[rootlen+20];
  169.         for(; q!=p; *q++=' ');        
  170.         if(dtaptr->att&0x10) {
  171.             *q++=' ';
  172.             *q++='<';
  173.             *q++='D';
  174.             *q++='I';
  175.             *q++='R';
  176.             *q++='>';
  177.           }
  178.         else {
  179.             sprintf(q,"%8ld",dtaptr->size);
  180.             return(savepath);
  181.           }    /* end else */
  182.          }        
  183.     *q='\0';
  184.     return(savepath);
  185. }
  186.  
  187. /**********************************************************************/
  188. /* nextname
  189. *  modify the path spec to contain the next file name in the
  190. *  sequence as given by DOS
  191. *
  192. *  if at the end of the sequence, return NULL
  193. */
  194. char *nextname(int type)
  195. {
  196.     char *p,*q;
  197.  
  198.     if(n_findnext())    /* check if there are any more filenames */
  199.         return(NULL);
  200.  
  201. /*
  202. *  copy file name, translate to lower case 
  203. */
  204.     q=&savepath[rootlen];
  205.     p=dtaptr->name;
  206.     while(*p) {
  207.         if(*p >='A' && *p<='Z')
  208.             *q++ = (*p++) + (char)32;
  209.         else
  210.             *q++=*p++;
  211.       }
  212. /*
  213. * if it is a directory, then put <DIR> after it
  214. */
  215.     if(type) {
  216.         p=&savepath[rootlen+20];
  217.         for(; q!=p; *q++=' ');        
  218.         if(dtaptr->att&0x10) {
  219.             *q++=' ';
  220.             *q++='<';
  221.             *q++='D';
  222.             *q++='I';
  223.             *q++='R';
  224.             *q++='>';
  225.           }
  226.         else {
  227.             sprintf(q,"%8ld",dtaptr->size);
  228.             return(savepath);
  229.           }    /* end else */
  230.          }        
  231.     *q='\0';
  232.     return(savepath);
  233. }
  234. /**********************************************************************/
  235. /*  getdrive
  236. *   get the current disk drive
  237. */
  238. void getdrive(unsigned int *d)
  239. {
  240. #ifdef __TURBOC__
  241.     *d=(unsigned) getdisk();
  242. #else
  243.     _dos_getdrive(d);
  244. #endif
  245. };
  246.  
  247. void setdrive(unsigned int d)
  248. {
  249.     unsigned temp;
  250. #ifdef __TURBOC__
  251.     setdisk((int) d);
  252. #else
  253.     _dos_setdrive(d, &temp);
  254. #endif
  255. };
  256.  
  257. /**********************************************************************/
  258. /*  dopwd
  259. *   get the current directory, including disk drive letter
  260. */
  261. void dopwd(char *p,int l)
  262. {
  263.     getcwd(p,l);                /* get dir */
  264. }
  265.  
  266. /**********************************************************************
  267. *    Function    :    chgdir
  268. *    Purpose    :    change to a different drive and directory
  269. *    Parameters    :
  270. *        file_name - handle of the name of the directory to change to 
  271. *    Returns    :    0 for success, 1 for error
  272. *    Calls    :    various string routines
  273. *    Called by    :    ftp server
  274. **********************************************************************/
  275. int chgdir(char *file_name)
  276. {
  277.     unsigned old_drive,     /* the old drive we were in */
  278.         temp_val;           /* temporary value */
  279.     int ret_val=0,          /* the return value from the function */
  280.         new_drive;            /* the drive to change to */
  281.     char *name_ptr,            /* pointer to the file name data */
  282.         cwd[64],            /* handle of the current directory */
  283.         *current,            /* pointer to the place in the file name */
  284.         working[64];        /* handle of a space for doing things */
  285.  
  286.     name_ptr=file_name;    /* set the pointer to the file name data */
  287. #ifdef MSC
  288. #ifdef __TURBOC__
  289.     old_drive=getdisk();        /* get the old drive number */
  290. #else
  291.     _dos_getdrive(&old_drive);    /* get the old drive number */
  292. #endif
  293. #else
  294.     old_drive=getdsk();
  295. #endif
  296.     getcwd(cwd,64);        /* get the current directory */
  297.     memmove(cwd,cwd+2,strlen(cwd)+1);    /* get rid of the drive specifier */
  298.     if(*(name_ptr+1)==':') {        /* do we have a drive specified */
  299.         new_drive=toupper((int)*name_ptr)-'A';    /* get the new drive number */
  300. #ifdef MSC
  301. #ifdef __TURBOC__
  302.         setdisk(new_drive);
  303. #else
  304.         _dos_setdrive(new_drive+1,&temp_val);
  305. #endif
  306. #else
  307.         chgdsk(new_drive);
  308. #endif
  309.         name_ptr+=2;                /* increment the name pointer */
  310.         if(*name_ptr=='\0')            /* check for simple drive change */
  311.             name_ptr=NULL;
  312.       }    /* end if */
  313.     while(*name_ptr=='\\') {        /* check for changing to the root directory */
  314.         chdir("\\");                /* change to the root directory */
  315.         name_ptr++;                    /* advance the name pointer to the next character */
  316.         if(*name_ptr=='\0')            /* check for simple drive change */
  317.             name_ptr=NULL;
  318.       }    /* end if */
  319.     while(name_ptr!=NULL && ret_val!=1) {        /* continue until the end of the string or an error occurs */
  320. #ifdef MSC
  321.         current=strchr(name_ptr,(int)'\\');        /* find the first occurence of the SLASH character */
  322. #else
  323.         current=strchr(name_ptr,(char)'\\');    /* find the first occurence of the SLASH character */
  324. #endif
  325.         if(current!=NULL) {                            /* found the SLASH character */
  326.             temp_val=current-name_ptr;                    /* find out the length of the string */
  327.             movebytes(working,name_ptr,temp_val);        /* copy the string into the working buffer */
  328.             *(working+temp_val)=0;                        /* terminate the string */
  329.             name_ptr=current+1;                        /* advance to the next part of the path name */
  330.           }    /* end if */
  331.         else {                                        /* the SLASH character is not in the name */
  332.             strcpy(working,name_ptr);
  333.             name_ptr=NULL;
  334.           }    /* end else */
  335.         if(chdir(working))        /* change the directory, but look for an error also */
  336.             ret_val=1;
  337.       }    /* end while */
  338.     if(ret_val==1) {                    /* on error, reset the old drive */
  339. #ifdef MSC
  340. #ifdef __TURBOC__
  341.         setdisk(old_drive);
  342. #else
  343.         _dos_setdrive(old_drive,&temp_val);
  344. #endif
  345. #else
  346.         chgdsk(old_drive);
  347. #endif
  348.         chdir(cwd);                    /* fix the directory */
  349.       }    /* end if */
  350.     return(ret_val);                /* return the retuen value */
  351. }    /* end chgdir() */
  352. #endif
  353.  
  354. /**********************************************************************/
  355. /*  Scolorset
  356. *  setup the color value from the config file string
  357. */
  358. void Scolorset(char *thecolor,char *st)
  359. {
  360.     *thecolor=lookcolor(st);
  361. }
  362.  
  363. /**********************************************************************/
  364. /* lookcolor
  365. *  search a list for the given color name
  366. */
  367. static char *colist[]={
  368.     "black",
  369.     "blue",
  370.     "green",    
  371.     "cyan",
  372.     "red",
  373.     "magenta",
  374.     "yellow",
  375.     "white",
  376.     "BLACK",
  377.     "BLUE",
  378.     "GREEN",    
  379.     "CYAN",
  380.     "RED",
  381.     "MAGENTA",
  382.     "YELLOW",
  383.     "WHITE"    };
  384.  
  385. static char lookcolor(char *s)
  386. {
  387.     char i;
  388.  
  389.     for(i=0; i<15; i++)
  390.         if(!strcmp(colist[i],s))
  391.             return(i);
  392.     return(15);
  393. }
  394.  
  395. #ifndef NET14
  396. /**********************************************************************
  397. *  Function    :    octal_to_int
  398. *  Purpose    :    convert an octal string to an integer (like atoi())
  399. *  Parameters    :
  400. *            octal_str - the octal string to get the value of
  401. *  Returns    :    0 to indicate that the input cannot be converted to an integer
  402. *  Calls    :    none
  403. *  Called by    :    parse_str()
  404. **********************************************************************/
  405. int octal_to_int(char *octal_str)
  406. {
  407.     unsigned int return_value=0;    /* the value to return from the function */
  408.  
  409.     while((*octal_str)>='0' && (*octal_str)<='7') {
  410.         return_value*=(return_value*8);        /* bump up the value top return to the next multiple of eight */
  411.         return_value+=((*octal_str)-'0');        /* increment by the digit found */
  412.         octal_str++;
  413.       }    /* end while */
  414.     return((int)return_value);
  415. }    /* end octal_to_int() */
  416.  
  417. /**********************************************************************
  418. *  Function    :    hex_to_int
  419. *  Purpose    :    convert an hexadecimal string to an integer (like atoi())
  420. *  Parameters    :
  421. *            hex_str - the hexadecimal string to get the value of
  422. *  Returns    :    0 to indicate that the input cannot be converted to an integer
  423. *  Calls    :    none
  424. *  Called by    :    parse_str()
  425. **********************************************************************/
  426. int hex_to_int(char *hex_str)
  427. {
  428.     unsigned int return_value=0;    /* the value to return from the function */
  429.  
  430.     while(isxdigit((int)(*hex_str))) {
  431.         return_value*=(return_value*16);        /* bump up the value top return to the next multiple of sixteen */
  432.         if(isdigit((int)(*hex_str)))    /* check whether this digit is numeric or alphabetic */
  433.             return_value+=(unsigned int)((*hex_str)-'0');        /* increment by the digit found (for '0'-'9') */
  434.         else
  435.             return_value+=(unsigned int)(tolower((int)(*hex_str))-'a'+10);    /* increment by the digit found (for 'A'-'F' & 'a'-'f') */
  436.         hex_str++;
  437.       }    /* end while */
  438.     return((int)return_value);
  439. }    /* end hex_to_int() */
  440. #endif
  441.  
  442.