Thread: Obtaining Linux system 'whoami' output for display in Apache module

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    Atlanta, GA
    Posts
    3

    Obtaining Linux system 'whoami' output for display in Apache module

    I'm a C newbie. My eventual goal is to create an Apache module that returns the Apache user account's crontab as JSON.

    Baby steps first though. I've successfully followed couple of online tutorials to output "hello world" from an Apache module, and I actually own Nick Kew's book. I've modified the examples I've found to successfully compile and run code that emits json output as follows:

    Code:
    ap_rputs("{'hello': {'to': 'world', 'from': '?'}}", r);
    I'd like to substitute the '?' above with output from the Linux system's 'whoami' command (eventually I want to run the linux command 'crontab -lu username'). As a C newbie though I am overwhelmed by the choices as to how to go about this, I've tried a few things, and don't seem to be close to getting anything right. I do seem to be able to trap the output from whoami, or at least my code compiles and runs

    Code:
    FILE *sysp = popen("whoami","r");
    But am I even doing the above right? And what is a good next step? I thought I might try to determine the length of the output from above, then create a char array of the same length, rewind the file handle, and grab the output. But I don't seem to be obtaining the length properly, and maybe this is a sub-optimal approach? When I run the following (I've left out a couple of lines I know might be necessary, i.e. rewind, fclose) the output I get is -1:

    Code:
    fseek(sysp, 0L, SEEK_END);
    long len = ftell(sysp);
    char buf[2];
    sprintf(buf, "%d", (int)len);
    ap_rputs(buf, r);
    Any pointers specifically as to how to better approach outputting the result from the system command "whoami" would be appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Don't use a linux command. Look up getpwuid and getuid. That should be all you need.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    Atlanta, GA
    Posts
    3
    Quote Originally Posted by EVOEx View Post
    Don't use a linux command. Look up getpwuid and getuid. That should be all you need.
    Thanks for reminding me, I came across those googling last night. I still think there will be value for me in solving it with a linux command though. Especially because I don't see any way around using a linux command for getting the crontab output. So I think I need to build up my experience with the repetoire of various io and string functions.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Grab output of popen() in a char array with a call to fgets().
    The char array is supplied as the last argument to fgets().

  5. #5
    Registered User
    Join Date
    Jan 2011
    Location
    Atlanta, GA
    Posts
    3
    Quote Originally Posted by itCbitC View Post
    Grab output of popen() in a char array with a call to fgets().
    The char array is supplied as the last argument to fgets().
    Ok, and since whoami's output will be on one line, and I presume terminated with a newline, and my understanding is that fgets() will preserve the newline, I will need to replace the new line with a trailing \0 to treat it as a string, correct? If so, what is "the best", or at least "an idiomatic" way, of doing so?

    Thanks

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use strchr. If it returns non-null, turn the char it points to into a '\0'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux gcc file printer output
    By ed bitwise in forum C Programming
    Replies: 1
    Last Post: 05-17-2006, 01:05 AM
  2. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  3. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. system command output.
    By joshk in forum C Programming
    Replies: 3
    Last Post: 08-21-2002, 01:49 PM

Tags for this Thread