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