/[mailutils]/mailutils/mailbox/mutil.c
ViewVC logotype

Diff of /mailutils/mailbox/mutil.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32 by sroberts, Thu May 9 02:16:54 2002 UTC revision 1.33 by gray, Thu Jun 27 09:13:45 2002 UTC
# Line 973  mu_unroll_symlink (char *out, size_t out Line 973  mu_unroll_symlink (char *out, size_t out
973    
974    return 0;    return 0;
975  }  }
976    
977    /* Expand a PATTERN to the pathname. PATTERN may contain the following
978       macro-notations:
979       ---------+------------
980       notation |  expands to
981       ---------+------------
982       %u         user name
983       %h         user's home dir
984       ---------+------------
985    
986       Allocates memory.
987    */  
988    char *
989    mu_expand_path_pattern (const char *pattern, const char *username)
990    {
991      char *homedir = NULL;
992      const char *p, *startp;
993      char *q;
994      char *path;
995      int len = 0;
996      
997      for (p = pattern; *p; p++)
998        {
999          if (*p == '%')
1000            switch (*++p)
1001              {
1002              case 'u':
1003                len += strlen (username);
1004                break;
1005                
1006              case 'h':
1007                if (!homedir)
1008                  {
1009                    struct passwd *pwd = mu_getpwnam (username);
1010                    if (!pwd)
1011                      return NULL;
1012                    homedir = pwd->pw_dir;
1013                  }
1014                len += strlen (homedir);
1015                break;
1016                
1017              case '%':
1018                len++;
1019                break;
1020                
1021              default:
1022                len += 2;
1023              }
1024          else
1025            len++;
1026        }
1027      
1028      path = malloc (len + 1);
1029      if (!path)
1030        return NULL;
1031    
1032      startp = pattern;
1033      q = path;
1034      while (*startp && (p = strchr (startp, '%')) != NULL)
1035        {
1036          memcpy (q, startp, p - startp);
1037          q += p - startp;
1038          switch (*++p)
1039            {
1040            case 'u':
1041              strcpy (q, username);
1042              q += strlen (username);
1043              break;
1044              
1045            case 'h':
1046              strcpy (q, homedir);
1047              q += strlen (homedir);
1048              break;
1049              
1050            case '%':
1051              *q++ = '%';
1052              break;
1053              
1054            default:
1055              *q++ = '%';
1056              *q++ = *p;
1057            }
1058          startp = p + 1;
1059        }
1060      if (*startp)
1061        {
1062          strcpy (q, startp);
1063          q += strlen (startp);
1064        }
1065      *q = 0;
1066      return path;
1067    }

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.33

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26