home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / expand.c < prev    next >
C/C++ Source or Header  |  1992-05-03  |  4KB  |  150 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 <pwd.h>
  32. #include "defs.h"
  33.  
  34. char *expand_define();
  35. static struct passwd *pass;
  36. static char *home;
  37.  
  38. int
  39. expand(filename)
  40. char *filename;
  41. {
  42.     /** Expand the filename since the first character is a meta-
  43.         character that should expand to the "maildir" variable
  44.         in the users ".elmrc" file...
  45.  
  46.         Note: this is a brute force way of getting the entry out
  47.         of the .elmrc file, and isn't recommended for the faint
  48.         of heart!
  49.     **/
  50.  
  51.     FILE *rcfile;
  52.     char  buffer[SLEN], *expanded_dir, *bufptr;
  53.     int   foundit = 0;
  54.  
  55.     bufptr = (char *) buffer;        /* same address */
  56.  
  57.     if((pass = getpwuid(getuid())) == NULL) {
  58.         printf("You have no password entry!\n");
  59.         exit(1);
  60.     }
  61.     home = pass->pw_dir;
  62.  
  63.     sprintf(buffer, "%s/%s", home, elmrcfile);
  64.  
  65.     if ((rcfile = fopen(buffer, "r")) == NULL) {
  66.       printf("Can't open your \".elmrc\" file (%s) for reading!\n",
  67.          buffer);
  68.       return(NO);
  69.     }
  70.  
  71.     while (fgets(buffer, SLEN, rcfile) != NULL && ! foundit) {
  72.       if (strncmp(buffer, "maildir", 7) == 0 ||
  73.           strncmp(buffer, "folders", 7) == 0) {
  74.         while (*bufptr != '=' && *bufptr)
  75.           bufptr++;
  76.         bufptr++;            /* skip the equals sign */
  77.         while (whitespace(*bufptr) && *bufptr)
  78.           bufptr++;
  79.         home = bufptr;        /* remember this address */
  80.  
  81.         while (! whitespace(*bufptr) && *bufptr != '\n')
  82.           bufptr++;
  83.  
  84.         *bufptr = '\0';        /* remove trailing space */
  85.         foundit++;
  86.       }
  87.     }
  88.  
  89.     fclose(rcfile);            /* be nice... */
  90.  
  91.     if (! foundit) {
  92.       printf("Couldn't find \"maildir\" in your .elmrc file!\n");
  93.       return(NO);
  94.     }
  95.  
  96.     /** Home now points to the string containing your maildir, with
  97.         no leading or trailing white space...
  98.     **/
  99.  
  100.     if ((expanded_dir = expand_define(home)) == NULL)
  101.         return(NO);
  102.  
  103.     sprintf(buffer, "%s%s%s", expanded_dir,
  104.         (expanded_dir[strlen(expanded_dir)-1] == '/' ||
  105.         filename[0] == '/') ? "" : "/", (char *) filename+1);
  106.  
  107.     strcpy(filename, buffer);
  108.     return(YES);
  109. }
  110.  
  111. char *expand_define(maildir)
  112. char *maildir;
  113. {
  114.     /** This routine expands any occurances of "~" or "$var" in
  115.         the users definition of their maildir directory out of
  116.         their .elmrc file.
  117.  
  118.         Again, another routine not for the weak of heart or staunch
  119.         of will!
  120.     **/
  121.  
  122.     static char buffer[SLEN];    /* static buffer AIEE!! */
  123.     char   name[SLEN],        /* dynamic buffer!! (?) */
  124.            *nameptr,           /*  pointer to name??     */
  125.            *value;              /* char pointer for munging */
  126.  
  127.     if (*maildir == '~')
  128.       sprintf(buffer, "%s%s", home, ++maildir);
  129.     else if (*maildir == '$') {     /* shell variable */
  130.  
  131.       /** break it into a single word - the variable name **/
  132.  
  133.       strcpy(name, (char *) maildir + 1);    /* hurl the '$' */
  134.       nameptr = (char *) name;
  135.       while (*nameptr != '/' && *nameptr) nameptr++;
  136.       *nameptr = '\0';    /* null terminate */
  137.  
  138.       /** got word "name" for expansion **/
  139.  
  140.       if ((value = getenv(name)) == NULL) {
  141.         printf("Couldn't expand shell variable $%s in .elmrc!\n", name);
  142.         return(NULL);
  143.       }
  144.       sprintf(buffer, "%s%s", value, maildir + strlen(name) + 1);
  145.     }
  146.     else strcpy(buffer, maildir);
  147.  
  148.     return( ( char *) buffer);
  149. }
  150.