home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / elm-2.4-pl20.tar.Z / elm-2.4-pl20.tar / lib / expand.c < prev    next >
C/C++ Source or Header  |  1993-01-12  |  5KB  |  163 lines

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