Getting the user's local username

This is a discussion on Getting the user's local username within the C Programming forums, part of the General Programming Boards category; Hi all, I'm trying to write a program in a Linux enviroment that parses through some xml files, that reside ...

  1. #1
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12

    Question Getting the user's local username

    Hi all,

    I'm trying to write a program in a Linux enviroment that parses through some xml files, that reside in the user's home dir. The problem is getting that user's username, so I can put together the path to those files (e.g. /home/username/.hiddendir/file.xml).

    I've looked as much as I can, but can't find a way to dynamically, during program execution, get the user's username on the OS. Does anyone know how this could be done?
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    use the path ~/.hiddendir/file.xml

    ~ is a symbolic link to the user's home directory.

  3. #3
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12

    Thumbs up

    Of course. Stupid me. Thanks, works nicely now
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    I don't think fopen() handles ~ expansion:
    Code:
    itsme@itsme:~/C$ cat homediropen.c
    #include <stdio.h>
    
    int main(void)
    {
      FILE *fp;
    
      if(!(fp = fopen("~/garbage.txt", "r")))
        puts("Couldn't open file");
      else
      {
        puts("File opened successfully");
        fclose(fp);
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ls ~/garbage.txt
    /home/itsme/garbage.txt
    itsme@itsme:~/C$ ./homediropen
    Couldn't open file
    itsme@itsme:~/C$
    This does seem to work though:
    Code:
    itsme@itsme:~/C$ cat homediropen.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      FILE *fp;
      char *homedir;
      char filename[BUFSIZ];
    
      if(!(homedir = getenv("HOME")))
      {
        puts("Environment variable HOME not available");
        return 1;
      }
    
      sprintf(filename, "%s/garbage.txt", homedir);
    
      if(!(fp = fopen(filename, "r")))
        puts("Couldn't open file");
      else
      {
        puts("File opened successfully");
        fclose(fp);
      }
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./homediropen
    File opened successfully
    itsme@itsme:~/C$
    Last edited by itsme86; 11-23-2005 at 09:15 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by Datamike
    Of course. Stupid me. Thanks, works nicely now
    Using ~ worked for you?
    If you understand what you're doing, you're not learning anything.

  6. #6
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12
    Quote Originally Posted by itsme86
    Using ~ worked for you?
    Actually, no, it didn't. Thought at first that it had worked, since I got no errors from my program. But that just turned out to be a bug in my code. The code you posted, worked though, so I've been able to continue my project. Thank you for that.
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Oh yeah ~ must be a shell thing. It's been ages since I've used Unix.

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Yes, it's only the shell that knows what to do with ~.

    What you need to do is use getuid() to find out the user id number of the person running the program, then getpwuid to grab the attributes including home directory:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <pwd.h>
    
    int main(void)
    {
        struct passwd *p;
        p = getpwuid(getuid());
        printf("Your home dir is %s\n", p->pw_dir);
        return 0;
    }
    
    /* my output:
    Your home dir is /usr/home/cwr
    */
    P.S. The Linux Programming forum is an excellent place for questions related to Linux programming.

  9. #9
    The 7th Coder Datamike's Avatar
    Join Date
    Jan 2002
    Posts
    12
    Quote Originally Posted by cwr
    P.S. The Linux Programming forum is an excellent place for questions related to Linux programming.
    Cool
    Thanks for the advice, I'll try it out
    developer & programmer
    me tomi kaistila
    gnupg 0x6D58CC04
    home http://www.datamike.org


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Windows ntfs perms
    By wp_x in forum Tech Board
    Replies: 3
    Last Post: 03-04-2003, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21