Israel's Profile
Reputation: 4
Apprentice
- Group:
- Authors
- Active Posts:
- 788 (0.25 per day)
- Joined:
- 22-November 04
- Profile Views:
- 8,241
- Last Active:
Oct 23 2012 08:19 AM- Currently:
- Offline
Previous Fields
- Country:
- Who Cares
- OS Preference:
- Linux
- Favorite Browser:
- FireFox
- Favorite Processor:
- Intel
- Favorite Gaming Platform:
- Playstation
- Your Car:
- Who Cares
- Dream Kudos:
- 200
Latest Visitors
-
trevster344 
13 Nov 2012 - 18:50 -
coldbreeze16 
19 Oct 2012 - 22:48 -
AKMafia001 
19 Oct 2012 - 08:22 -
modi123_1 
19 Oct 2012 - 08:17 -
pokiaka 
06 May 2012 - 00:36
Posts I've Made
-
In Topic: getenv returning null in C
Posted 22 Oct 2012
Sorry I didn't respond sooner, but working 3rd shift makes me keep strange hours.
But I finally figured this out without having to use Bash!!!
I added to #define's code with the strstr() function and was finally able to trim out the user name:
#include <stdlib.h> #include <string.h> #include <stdio.h> #define USRTRIM 6 int main (void) { // testing example char test[] = "/home/Israel/this/dir"; char *Left; char *Right; char *usr; usr = malloc(strlen(test)); if(usr) { if(strlen(test) > USRTRIM){ strcpy(usr, test + USRTRIM); } else { printf("cannot allocate memory for usr string\n"); } } else { printf("Fail...\n"); } // first make a copy Left = strdup(usr); // second locate the desired text Right = strstr(Left,"/"); // third split the string *(Right - 0) = '\0'; // print the results printf("User is: %s\n",Left); // clean up the mess free(Left); free(usr); // and exit return 0; }
I apologize for not giving more details. I thought the length of the code I had may have been daunting and a boring overkill for what I needed. So I just stuck to the part that was problematic.
If anyone does figure out how to pass the environment variable back to C, like in my first attempt, please let me know.
Even if it's 5 years from now inbox me. This has been bothering me for way too long.
Thank you again to all those who kept me going!
-
In Topic: getenv returning null in C
Posted 21 Oct 2012
That was an awesome suggestion that I must praise you for.
Unfortunately, I neglected to mention that I need to run this as root.
If root is the user $HOME will always equal the root directory:
# echo $HOME /root
It looks like you may have tried to get the user directory too, however, this did not return anything on my system:
# ./home value of HOME is: /root value of USR is:
I saw you had "Windows" in your comments.
Perhaps this is a difference between how the Power Shell and Bash work as I'm on Linux.
Regardless, thank you for the valiant effort. -
In Topic: getenv returning null in C
Posted 19 Oct 2012
I remembered that about the variable but left out "export" in my original post which would cover the problem of being in a different shell.
That line should've been like this:
system("export SK=`pwd | cut -d '/' -f3`;)/>;
After the program would exit I could use "echo $SK" from my command-line and the variable was still set.
I would unset the variable, run the program again and it would still work from the command-line.
I'm pretty sure this was just not being called correctly from my program.
I've looked high and low for a way to do this in C without using some Bash. I've found how to get the directory in C, but I haven't found how to cut the user name from the directory. There are ways to do it if you know the length of your user name. However, I need to do this on a server with many users of different character lengths.
I didn't want to, but I've been playing with writing to a temp file and letting C pick it up from there.
Here's my latest failure:
#include <stdlib.h> #include <string.h> #include <stdio.h> #include <unistd.h> int main() { system("export SK=`pwd | cut -d '/' -f3`"); char ch[60]; FILE *fp; fp = fopen("/tmp/test", "r"); if (fp == NULL) { perror("Error opening /tmp/test"); exit(EXIT_FAILURE); } while (!feof(fp)) { if (fscanf(fp, "%s", ch) != 1){ printf("%s", ch); } else{ printf("Fail..."); } fclose(fp); system("rm /tmp/test"); return 0; } }
I've tried a couple different functions to try and get the username out of this temp file. However, fscanf is a little new or maybe forgotten to me. This writes the temp file fine, but I get a segmentation fault with fscanf. I looked for examples but haven't seen it used with just copying one string.
I'm still open to suggestions with or without a temp file or any other work-around. Just been getting a little delirious from looking at this so long... -
In Topic: create own malloc,free,realloc,calloc
Posted 22 Feb 2012
Not sure where or what you would've used printf for, but documentation for the write function and be found here:
http://gd.tuwien.ac....brown/c_075.htm
Hope that helps you out. -
In Topic: Waiting For Key Press: Console App
Posted 13 Nov 2011
Nice! I'd like to see this in more languages.
My Information
- Member Title:
- D.I.C Addict
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
- Location:
- Hell
- Years Programming:
- 5
- Programming Languages:
- C, Java, html, asm,
Contact Information
- E-mail:
- Click here to e-mail me
Friends
|
|


Find Topics
Find Posts
View Reputation Given

|
Comments
Israel has no profile comments yet. Why not say hello?