home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5422 < prev    next >
Encoding:
Text File  |  1992-08-21  |  4.3 KB  |  183 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!merlin.dev.cdx.mot.com!lezz
  3. From: lezz@merlin.dev.cdx.mot.com (Lezz Giles)
  4. Subject: Re: Filename expansion ("~user")
  5. Message-ID: <1992Aug21.183318.11200@merlin.dev.cdx.mot.com>
  6. Keywords: ~user filename expansion
  7. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  8. Nntp-Posting-Host: monarch.dev.cdx.mot.com
  9. Reply-To: lezz@merlin.dev.cdx.mot.com (Lezz Giles)
  10. Organization: Motorola Codex, Canton, MA
  11. References:  <1992Aug20.153727.167@cbnews.cb.att.com>
  12. Distribution: usa
  13. Date: Fri, 21 Aug 1992 18:33:18 GMT
  14. Lines: 167
  15.  
  16. In article <1992Aug20.153727.167@cbnews.cb.att.com>, daleske@cbnews.cb.att.com (John D. Daleske) writes:
  17. |>I'm relatively new to Perl and love it.  I've read the recent notes in this
  18. |>notes group and have not found how do one capability that I am trying to
  19. |>use in a Perl script which is the ~user filename expansion.  Following is a test
  20. |>script:
  21. |>
  22. |>$tst1="~daleske";
  23. |>die "${tst1} not a directory" if !(-d ${tst1});
  24. |>opendir(DH, ${tst1});
  25. |>@allfiles = readdir(DH);
  26. |>closedir(DH);
  27. |>print "@allfiles\n";
  28. |>
  29. |>If I set $tst1 to the full pathname it works fine.  I'd like to use the common
  30. |>facility of ~user to avoid needing to know the full path name.  (User home
  31. |>directories do get moved around.)
  32.  
  33. You'll certainly get the standard response about "~ is only a shell construct
  34. and so you'll need to use a shell to expand it", which is valid enough and
  35. leads to various solutions.  However, some time ago I was working in an
  36. environment where we wanted to write /bin/sh scripts which could access
  37. people's home directories - so we had a semi-standard tool called 'logdir'
  38. which takes a username and returns the home directory, e.g.
  39.  
  40. $ logdir lezz
  41. /usr/u/a1/lezz
  42.  
  43. Once you've written this tool, you can add lines like these to your perl prog:
  44.  
  45. ($homedir = `logdir lezz`) || die "Can't find home dir for lezz\n";
  46.  
  47. $homebin = `logdir lezz` . "/bin";
  48.  
  49. The source for logdir is:
  50.  
  51. =================CUT HERE================
  52. /*
  53.  * Program:
  54.  *  logdir
  55.  *
  56.  * Function:
  57.  *  returns the HOME directory of the specified user.
  58.  *
  59.  * Author:
  60.  *  Lezz Giles
  61.  *
  62.  * History:
  63.  *  0.1   4/30  First release.
  64.  */
  65.  
  66. /*
  67.  * First of all, define all the constants
  68.  */
  69. #define OPTSTRING "HV"
  70. #define HELPSTRING "logdir will return the HOME directory of the given user,\n\
  71. default is your own HOME directory.\n\
  72. Usage: where [-H|V] [user]\n\
  73. \n\
  74. \tOptions:\n\
  75. \t\t-H\tPrint this message\n\
  76. \t\t-V\tPrint version number\n\
  77. \n\
  78. Examples:\n\
  79. \tlogdir jdoe\n\
  80. \t\treturns the HOME directory of jdoe\n\
  81. \n\
  82. \tlogdir\n\
  83. \t\treturns your HOME directory\n"
  84. #define USAGE "[-H|V] [user]"
  85. #define VERSID "0.1"
  86. #define STRLEN 100
  87.  
  88. /*
  89.  * Next all the include files
  90.  */
  91. #include <errno.h>
  92. #include <stdio.h>
  93. #include <pwd.h>
  94. #include <string.h>
  95.  
  96. /*
  97.  * Any extern declarations needed...
  98.  */
  99. extern char *getlogin();
  100.  
  101. /*
  102.  * Now any typedefs
  103.  */
  104. typedef enum {
  105.   FALSE=0,
  106.   TRUE=1
  107. } boolean;
  108.  
  109. /*
  110.  * Then any global variables
  111.  */
  112. char  *cmd_name;
  113.  
  114. main(argc,argv)
  115. int argc;
  116. char **argv;
  117.  
  118. {
  119.   char *path=NULL;
  120.   char option;
  121.   extern int optind, opterr;
  122.   extern char *optarg;
  123.   struct passwd *pwd_entry;
  124.  
  125.   /*
  126.    * Remember the command name for error messages
  127.    */
  128.   cmd_name = argv[0];
  129.  
  130.   /*
  131.    * Parse the options...
  132.    */
  133.   while ((option = getopt(argc, argv, OPTSTRING)) != EOF)
  134.     switch (option) {
  135.     case 'H':
  136.       printf("%s\n",HELPSTRING);
  137.       exit(0);
  138.     case 'V':
  139.       printf("%s: Version %s\n",cmd_name, VERSID);
  140.       exit(0);
  141.     }
  142.  
  143.   /*
  144.    * Now if there is one parameter...
  145.    */
  146.   if (optind == argc-1) {
  147.     if ((pwd_entry = getpwnam(argv[optind])) == NULL) {
  148.       fprintf(stderr,"%s: Cannot find entry for %s\n",cmd_name,argv[optind]);
  149.       exit(1);
  150.     }
  151.   } else if (optind == argc) {
  152.   /*
  153.    * else if there is no parameter...
  154.    */
  155.     char *username;
  156.     if ((username = getlogin()) != NULL) {
  157.       if ((pwd_entry = getpwnam(username)) == NULL) {
  158.     fprintf(stderr,"%s: Cannot find entry for %s\n",cmd_name,username);
  159.     exit(1);
  160.       }
  161.     } else {
  162.       if ((pwd_entry = getpwent(getuid())) == NULL) {
  163.     fprintf(stderr,"%s: Cannot find entry for you\n",cmd_name);
  164.     exit(1);
  165.       }
  166.     }
  167.   } else {
  168.   /*
  169.    * Error condition if there is more than one parameter...
  170.    */
  171.     fprintf(stderr,"%s: Only one username please!\n",cmd_name);
  172.     exit(1);
  173.   }
  174.   printf("%s\n",pwd_entry->pw_dir);
  175.   exit(0);
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.