home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / elm.lzh / ELM / UTILS / EXPAND.C < prev    next >
Text File  |  1990-08-04  |  4KB  |  147 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: expand.c,v 4.1 90/04/28 22:44:37 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    expand.c,v $
  17.  * Revision 4.1  90/04/28  22:44:37  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This is a library routine for the various utilities that allows
  24.     users to have the standard 'Elm' folder directory nomenclature
  25.     for all filenames (e.g. '+', '=' or '%').  It should be compiled
  26.     and then linked in as needed.
  27.  
  28. **/
  29.  
  30. #include <stdio.h>
  31. #include "defs.h"
  32.  
  33. char *expand_define();
  34.  
  35. int
  36. expand(filename)
  37. char *filename;
  38. {
  39.     /** Expand the filename since the first character is a meta-
  40.         character that should expand to the "maildir" variable
  41.         in the users ".elmrc" file...
  42.  
  43.         Note: this is a brute force way of getting the entry out 
  44.         of the .elmrc file, and isn't recommended for the faint 
  45.         of heart!
  46.     **/
  47.  
  48.     FILE *rcfile;
  49.     char  buffer[SLEN], *expanded_dir, *home, *getenv(), *bufptr;
  50.     int   foundit = 0;
  51.  
  52.     bufptr = (char *) buffer;        /* same address */
  53.     
  54.     if ((home = getenv("HOME")) == NULL) {
  55.       printf(
  56.          "Can't expand environment variable $HOME to find .elmrc file!\n");
  57.       return(NO);
  58.     }
  59.  
  60.     sprintf(buffer, "%s/%s", home, elmrcfile);
  61.  
  62.     if ((rcfile = fopen(buffer, "r")) == NULL) {
  63.       printf("Can't open your \".elmrc\" file (%s) for reading!\n",
  64.          buffer);
  65.       return(NO);
  66.     }
  67.  
  68.     while (fgets(buffer, SLEN, rcfile) != NULL && ! foundit) {
  69.       if (strncmp(buffer, "maildir", 7) == 0 ||
  70.           strncmp(buffer, "folders", 7) == 0) {
  71.         while (*bufptr != '=' && *bufptr) 
  72.           bufptr++;
  73.         bufptr++;            /* skip the equals sign */
  74.         while (whitespace(*bufptr) && *bufptr)
  75.           bufptr++; 
  76.         home = bufptr;        /* remember this address */
  77.  
  78.         while (! whitespace(*bufptr) && *bufptr != '\n')
  79.           bufptr++;
  80.  
  81.         *bufptr = '\0';        /* remove trailing space */
  82.         foundit++;
  83.       }
  84.     }
  85.  
  86.     fclose(rcfile);            /* be nice... */
  87.  
  88.     if (! foundit) {
  89.       printf("Couldn't find \"maildir\" in your .elmrc file!\n");
  90.       return(NO);
  91.     }
  92.  
  93.     /** Home now points to the string containing your maildir, with
  94.         no leading or trailing white space...
  95.     **/
  96.  
  97.     if ((expanded_dir = expand_define(home)) == NULL)
  98.         return(NO);
  99.  
  100.     sprintf(buffer, "%s%s%s", expanded_dir, 
  101.         (expanded_dir[strlen(expanded_dir)-1] == '/' ||
  102.         filename[0] == '/') ? "" : "/", (char *) filename+1);
  103.  
  104.     strcpy(filename, buffer);
  105.     return(YES);
  106. }
  107.  
  108. char *expand_define(maildir)
  109. char *maildir;
  110. {
  111.     /** This routine expands any occurances of "~" or "$var" in
  112.         the users definition of their maildir directory out of
  113.         their .elmrc file.
  114.  
  115.         Again, another routine not for the weak of heart or staunch
  116.         of will!
  117.     **/
  118.  
  119.     static char buffer[SLEN];    /* static buffer AIEE!! */
  120.     char   name[SLEN],        /* dynamic buffer!! (?) */
  121.            *nameptr,           /*  pointer to name??     */
  122.            *value;              /* char pointer for munging */
  123.  
  124.     if (*maildir == '~') 
  125.       sprintf(buffer, "%s%s", getenv("HOME"), ++maildir);
  126.     else if (*maildir == '$') {     /* shell variable */
  127.  
  128.       /** break it into a single word - the variable name **/
  129.  
  130.       strcpy(name, (char *) maildir + 1);    /* hurl the '$' */
  131.       nameptr = (char *) name;
  132.       while (*nameptr != '/' && *nameptr) nameptr++;
  133.       *nameptr = '\0';    /* null terminate */
  134.       
  135.       /** got word "name" for expansion **/
  136.  
  137.       if ((value = getenv(name)) == NULL) {
  138.         printf("Couldn't expand shell variable $%s in .elmrc!\n", name);
  139.         return(NULL);
  140.       }
  141.       sprintf(buffer, "%s%s", value, maildir + strlen(name) + 1);
  142.     }
  143.     else strcpy(buffer, maildir);
  144.  
  145.     return( ( char *) buffer);
  146. }
  147.