home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 4029 < prev    next >
Encoding:
Text File  |  1992-07-30  |  1.4 KB  |  50 lines

  1. Path: sparky!uunet!mcsun!uknet!warwick!coventry!ccx018
  2. From: ccx018@cch.coventry.ac.uk (Leslie Griffiths (Griff))
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: expanding ~'s in a directory name.
  5. Message-ID: <Bs8u5J.J6n@cck.coventry.ac.uk>
  6. Date: 31 Jul 92 07:58:30 GMT
  7. References: <1992Jul29.012629.14000@gucis.cit.gu.edu.au>
  8. Sender: news@cck.coventry.ac.uk (news user)
  9. Organization: Coventry University
  10. Lines: 37
  11. Nntp-Posting-Host: cc_sysh
  12.  
  13. In article <1992Jul29.012629.14000@gucis.cit.gu.edu.au> aroll@gucis.cit.gu.edu.au (Ashley Roll) writes:
  14.  
  15. >My problem is how do I expand ~'s. I want to expand then the way that csh does.
  16. >
  17. >Has anyone written or got code that will do this that I can use? Or is there
  18. >a library function that will do this.
  19.  
  20. Well, all you need to do is to find out the users home directory, which can
  21. be obtained from the "struct passwd *" returned by a function such as 
  22. getpwuid, or getpwnam i.e.
  23.  
  24. #include <pwd.h>
  25.  
  26. main()
  27. {
  28.   struct passwd *pass;
  29.  
  30.     
  31.   if ((pass = getpwuid(getuid())) == NULL)/* Not sure if getpwuid sets errno */
  32.        {
  33.      fprintf(stderr, "Could not get passwd struct\n"); 
  34.      exit(1);
  35.        }
  36.  
  37.   printf("Users home area is %s\n", pass->pw_dir);
  38. }
  39.  
  40. To see what else is contained within the struct passwd, see /usr/include/pwd.h
  41.  
  42. Cheers
  43.  
  44.  
  45.   
  46. -- 
  47. Griff                \\             ccx018@uk.ac.cov
  48. ^^^^^                        :-=           ^^^^^^^^^^^^^^^^
  49.                     //
  50.