home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE3.TAR / xarchie-2.0.1 / tilde.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  1.3 KB  |  57 lines

  1. /*
  2.  * tilde.c : Tilde expansion for filenames
  3.  *
  4.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <pwd.h>
  11. #include "config.h"
  12. #ifdef HAVE_SYS_PARAM_H
  13. # include <sys/param.h>
  14. #endif
  15. #include "alert.h"
  16. extern uid_t getuid();        /* not in stdlib.h, but is in unistd.h */
  17. extern char *getenv();
  18.  
  19. char *
  20. tildeExpand(file)
  21. char *file;
  22. {
  23.     static char filename[MAXPATHLEN];
  24.     struct passwd *pwe;
  25.     char *name,*home;
  26.  
  27.     /* Must start with tilde */
  28.     if (*file != '~')
  29.     return(file);
  30.     /* Set default return value in case tilde expansion fails */
  31.     strcpy(filename,file);
  32.     /* Skip tilde */
  33.     name = ++file;
  34.     /* Gather name following tilde (if any) */
  35.     while (*file != '\0' && *file != '/')
  36.     file += 1;
  37.     if (*file != '\0')
  38.     *file++ = '\0';
  39.     if (*name == '\0') {                /* ~/... */
  40.     if ((pwe=getpwuid(getuid())) != NULL) {
  41.         home = pwe->pw_dir;
  42.     } else if ((home=getenv("HOME")) == NULL) {
  43.         alert1("Couldn't find homedir, you should set $HOME");
  44.         return(filename);
  45.     }
  46.     } else {                        /* ~user/... */
  47.     if ((pwe=getpwnam(name)) != NULL) {
  48.         home = pwe->pw_dir;
  49.     } else {
  50.         alert1("Couldn't find homedir for \"%s\"",name);
  51.         return(filename);
  52.     }
  53.     }
  54.     sprintf(filename,"%s/%s",home,file);
  55.     return(filename);
  56. }
  57.