How to print the environment variables in a C program using environ.
extern char **environ
#include <unistd.h>
#include <stdio.h>
extern char **environ;
//...
int i = 0;
while(environ[i]) {
printf("%s\n", environ[i++]); // prints in form of "variable=value"
}
Do you mean
int main(int argc, char **argv, char **envp)
{
while(*envp!=null) {
printf("%s\n", *envp);
envp++;
}
return 0;
}
Related
I need to know a way for use environment variables in the C programming language. How can I use and read them?
For example, read an environment variable or take the value of an environment variable and load it in another variable.
You can use following functions -
char * getenv (const char *name)-returns a string that is the value of the environment variable name.
char * secure_getenv (const char *name)
Read about some more functions here -http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access
Use the getenv function from stdlib.h. That's it!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("test\n");
const char* s = getenv("PATH");
// If the environment variable doesn't exist, it returns NULL
printf("PATH :%s\n", (s != NULL) ? s : "getenv returned NULL");
printf("end test\n");
}
getenv:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char* my_env_var = getenv("MY_ENV_VAR");
if(my_env_var)
printf("Var found: %s", my_env_var );
else
printf("Var not found.");
return 0;
}
On windows, you would use GetEnvironmentVariable.
#include <stdio.h>
#include <winbase.h>
int main(int argc, char *argv[])
{
TCHAR buff[100] = T("");
DWORD resultLengthInCharacters = GetEnvironmentVariable(T("USERDOMAIN"), buff, 100);
if (resultLengthInCharacters > 0 && resultLengthInCharacters < 100) {
_tprintf(T("USERDOMAIN: %s\n"), buff);
} else if ( resultLengthInCharacters > 100) {
_tprintf(T("USERDOMAIN too long to store in buffer of length 100, try again with buffer length %lu\n"), resultLengthInCharacters);
} else {
// Error handling incomplete, should use GetLastError(),
// but typically:
_tprintf(T("USERDOMAIN is empty or not set in the Environment\n"));
}
return 0;
}
But if you are trying to get a standard path variable, you should use the SHGetFolderPath function with the right CSIDL variable (like from this question: How do I get the application data path in Windows using C++?)
Another way could be to use the global variable environ.
#include <stdio.h>
extern char** environ;
void main(int argc, char* argv[])
{
int i=0;
while(environ[i]!=NULL){
printf("%s\n",environ[i++]);
}
}
#include <string.h>
void foo (char *bar)
{
char c[12];
strcpy(c, bar);
}
int main (int argc, char **argv)
{
foo(argv[1]);
return(1);
}
There are two problems :
if the program has no argument argv[1] is NULL and in foo you do strcpy(c, NULL); having an undefined behavior (typically a crash).
if the firs argument of the program has at least 12 characters strcpy(c, bar); will write out of c, again with an undefined behavior.
I do not speak about the fact the strcpy is in the best case useless because c is not used after
A secure version of your program with the minimal changes is :
#include <string.h>
void foo (char *bar)
{
char c[12];
strncpy(c, bar, sizeof(c) - 1);
c[sizeof(c) - 1] = 0;
}
int main (int argc, char **argv)
{
if (argc >= 2)
foo(argv[1]);
return(1);
}
#include <stdio.h>
int main(){
printf("asd");
return 0;
}
The task is without modifying this program, overwriting or using define, in windows environment, this program have to write out : Yourname asd Yourname.
Any idea?
You can do it like this (edited to read Yourname from stdin):
#include <stdio.h>
#include <string.h>
int main(){
printf("asd");
return 0;
}
int printf(const char *__restrict __format, ...)
{
char str[106], scs[50];
scanf("%s", &scs);
strcpy(str,scs);
puts(strcat(strcat(strcat(strcat(str," "), __format)," "),scs));
return 1;
}
Here's a working demo.
You can do it in a simple way. Global objects are created on the static memory area, they are allocated and initialized before the execution of main, and are freed after the execution of main. Here's the simple answer to your problem:
#include<iostream>
struct MyName {
public:
MyName() { printf("%s", "GaborRuszcsak\n"); }
~MyName() { printf("%s", "\nGaborRuszcsak\n"); }
};
MyName isGaborRuszcsak;
int main(int argc, char** argv){
printf("asd");
return 0;
}
Hope that helps.
I need to know a way for use environment variables in the C programming language. How can I use and read them?
For example, read an environment variable or take the value of an environment variable and load it in another variable.
You can use following functions -
char * getenv (const char *name)-returns a string that is the value of the environment variable name.
char * secure_getenv (const char *name)
Read about some more functions here -http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access
Use the getenv function from stdlib.h. That's it!
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("test\n");
const char* s = getenv("PATH");
// If the environment variable doesn't exist, it returns NULL
printf("PATH :%s\n", (s != NULL) ? s : "getenv returned NULL");
printf("end test\n");
}
getenv:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char* my_env_var = getenv("MY_ENV_VAR");
if(my_env_var)
printf("Var found: %s", my_env_var );
else
printf("Var not found.");
return 0;
}
On windows, you would use GetEnvironmentVariable.
#include <stdio.h>
#include <winbase.h>
int main(int argc, char *argv[])
{
TCHAR buff[100] = T("");
DWORD resultLengthInCharacters = GetEnvironmentVariable(T("USERDOMAIN"), buff, 100);
if (resultLengthInCharacters > 0 && resultLengthInCharacters < 100) {
_tprintf(T("USERDOMAIN: %s\n"), buff);
} else if ( resultLengthInCharacters > 100) {
_tprintf(T("USERDOMAIN too long to store in buffer of length 100, try again with buffer length %lu\n"), resultLengthInCharacters);
} else {
// Error handling incomplete, should use GetLastError(),
// but typically:
_tprintf(T("USERDOMAIN is empty or not set in the Environment\n"));
}
return 0;
}
But if you are trying to get a standard path variable, you should use the SHGetFolderPath function with the right CSIDL variable (like from this question: How do I get the application data path in Windows using C++?)
Another way could be to use the global variable environ.
#include <stdio.h>
extern char** environ;
void main(int argc, char* argv[])
{
int i=0;
while(environ[i]!=NULL){
printf("%s\n",environ[i++]);
}
}
How can I call notify-send from C code with a message stored in my string ?
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("mount something somewhere");
system("notify-send message");
return 0;
}
Just send the string as a parameter to system().
For example:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char command[100], msg[100];
strcpy(command,"notify-send ");
strcpy(msg,"\"Hello World\"");
strcat(command,msg);
system(command);
return 0;
}
#include <stdio.h>
int main(void)
{
system("notify-send Test \"Hello World\"");
return 0;
}