home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / tel_2_2_ / source / pcutil.c < prev    next >
C/C++ Source or Header  |  1988-07-15  |  5KB  |  281 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. #include "stdio.h"
  17. #include "whatami.h"
  18. unsigned char *malloc();
  19.  
  20. /**********************************************************************/
  21. /*
  22. *   Find directory name -- return a code that indicates whether the
  23. *   directory exists or not.
  24. *   0 = dir name ok
  25. *   -1 = error
  26. *   > 0 = dos error code, no dir by this name
  27. *
  28. *   Accept certain unix conventions, like '/' for separator
  29. *
  30. *   Also, append a '\' to the name before returning
  31. *  Note:  There must be enough room in the string to append the '\'
  32. */
  33.  
  34. struct dosdta {
  35.     char junk[21];
  36.     char att;
  37.     int time,date;
  38.     long int size;
  39.     char name[13];
  40. };
  41.  
  42. extern struct dosdta *dtaptr;
  43.  
  44. direxist(dirname)
  45.     char dirname[];
  46.     {
  47.     int i,ret;
  48.     char *p;
  49.  
  50.     if (!strcmp(dirname,".") || !dirname[0]) {
  51.         dirname[0] = '\0';
  52.         return(0);
  53.     }
  54.  
  55.     if (!strcmp(dirname,"\\"))
  56.         return(0);
  57.  
  58.     p = dirname;
  59.     while (*p) {
  60.         switch (*p) {
  61.             case '*':
  62.             case '?':
  63.                 return(-1);
  64.             case '/':
  65.                 *p = '\\';
  66.             default:
  67.                 break;
  68.         }
  69.         p++;
  70.     }
  71.  
  72. /*
  73. *  n_findfirst  will return normal files AND directories
  74. *  must check attribute to see if it is really a directory
  75. */
  76.     ret = n_findfirst(dirname,0x10);        /* find name */
  77.  
  78.     if (ret)
  79.         return(ret);
  80.  
  81.     if (!(dtaptr->att & 0x10))
  82.         return(-2);                            /* is a normal file */
  83.  
  84.     i = strlen(dirname);
  85.     dirname[i] = '\\';                        /* extend with '\' */
  86.     dirname[++i] = '\0';
  87.     
  88.     return(0);
  89. }
  90.  
  91. /**********************************************************************/
  92. /* firstname
  93. *  find the first name in the given directory which matches the wildcard
  94. *  specification
  95. *
  96. *  must malloc enough space for the path plus a full filename
  97. *
  98. *  expand '*' (unix) to '*.*' (dos)
  99. */
  100. static char *savepath;
  101. static int rootlen;
  102.  
  103. char *firstname(path)
  104.     char path[];
  105.     {
  106.     int i,len;
  107.     char *p,*q;
  108.  
  109.     if (!*path)
  110.         return(NULL);
  111.  
  112.     len = strlen(path);
  113.     savepath = malloc(len+16);
  114.  
  115.     i = 0;
  116.     rootlen = 0;
  117.     q = savepath;
  118.     p = path;
  119.     while (*q = *p) {                /* basic string copy with extras */
  120.         if (*p == '\\')
  121.             rootlen = i+1;            /* rootlen = position of last \ */
  122.         p++;
  123.         q++;
  124.         i++;
  125.     }
  126.  
  127.     if (savepath[len-1] == '*' && rootlen == len-1) {
  128.         savepath[len++] = '.';
  129.         savepath[len++] = '*';
  130.         savepath[len++] = '\0';
  131.     }
  132.  
  133.     if (n_findfirst(savepath,0))
  134.         return(NULL);
  135.  
  136. /*
  137. *  copy file name, translate to lower case 
  138. */
  139.     q = &savepath[rootlen];
  140.     p = dtaptr->name;
  141.     while (*p) {
  142.         if (*p >= 'A' && *p <= 'Z')
  143.             *q++ = *p++ + 32;
  144.         else
  145.             *q++ = *p++;
  146.     }
  147.     *q = '\0';
  148.  
  149.     return(savepath);
  150. }
  151.  
  152. /**********************************************************************/
  153. /* nextname
  154. *  modify the path spec to contain the next file name in the
  155. *  sequence as given by DOS
  156. *
  157. *  if at the end of the sequence, return NULL
  158. */
  159. char *nextname()
  160.     {
  161.     char *p,*q;
  162.  
  163.     if (NULL == savepath)
  164.         return(NULL);
  165.  
  166.     if (n_findnext()) {
  167.         free(savepath);
  168.         savepath = NULL;
  169.         return(NULL);
  170.     }
  171.  
  172. /*
  173. *  copy file name, translate to lower case 
  174. */
  175.     q = &savepath[rootlen];
  176.     p = dtaptr->name;
  177.     while (*p) {
  178.         if (*p >= 'A' && *p <= 'Z')
  179.             *q++ = *p++ + 32;
  180.         else
  181.             *q++ = *p++;
  182.     }
  183.     *q = '\0';
  184.  
  185.  
  186.     return(savepath);
  187.  
  188. }
  189.  
  190. /**********************************************************************/
  191. /*  dopwd
  192. *   get the current directory, including disk drive letter
  193. */
  194. dopwd(p,l)
  195.     char *p;
  196.     int l;
  197.     {
  198.     *p++ = 'A'+getdsk();        /* current disk drive */
  199.     *p++ = ':';
  200.     *p++ = '\\';
  201.     getcwd(p,l-2);                /* get dir */
  202.  
  203. }
  204.  
  205. /**********************************************************************/
  206. /*  chgdir
  207. *  change directory, including disk drive letter
  208. */
  209. #include <string.h>
  210.  
  211. chgdir(d)
  212.     char *d;
  213.     {
  214.  
  215.     while (*d && *d < 33)
  216.         d++;
  217.  
  218.     if (!(*d))
  219.         return(-1);
  220.  
  221.     *d = toupper(*d);
  222.  
  223.     if (*(d+1) == ':') {
  224.         chgdsk((*d)-'A');
  225.         d += 2;
  226.     }
  227.  
  228.     if (!(*d))
  229.         return(0);                /* just changed disk */
  230.  
  231.     return(chdir(d));
  232.  
  233. }
  234.  
  235. /**********************************************************************/
  236. /*  Scolorset
  237. *  setup the color value from the config file string
  238. */
  239. Scolorset(thecolor,st)
  240.     int thecolor[3];
  241.     char *st;
  242.     {
  243.     thecolor[0] = lookcolor(st);
  244.  
  245.     return(1);
  246. }
  247.  
  248. /**********************************************************************/
  249. /* lookcolor
  250. *  search a list for the given color name
  251. */
  252.  
  253. static char *colist[] = {
  254.     "black",
  255.     "blue",
  256.     "green",
  257.     "cyan",
  258.     "red",
  259.     "magenta",
  260.     "yellow",
  261.     "white" };
  262.  
  263. lookcolor(s)
  264.     char *s;
  265.     {
  266.     int i;
  267.  
  268.     for (i=0; i<7; i++)
  269.         if (!ncstrcmp(colist[i],s))
  270.             return(i);
  271.  
  272.     return(7);
  273. }
  274.  
  275. char *colorlook(cl)
  276.     int cl;
  277.     {
  278.     return(colist[cl]);
  279. }
  280.