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;
}
Related
Let's say I have a file test.c which accepts a text file and starts as follows:
int main(int argc, char **argv)
{
...
return 0;
}
I will typically compile the file and execute the binary as follows: ./test input.txt.
Now, I wish to call the main function programmatically in another function in the same file. How should I proceed?
You can do this as follows:
#include <stdio.h>
int main(int argc, char *argv[]);
void func()
{
char *argv[] = { "./test", "another.txt", NULL };
main(2, argv);
}
int main(int argc, char *argv[])
{
if (argc > 1) {
printf("Processing %s...\n", argv[1]);
}
/* ... */
func();
return 0;
}
This should output something like:
Processing input.txt...
Processing another.txt...
Beware of endless recursion!
Does someone know why the following lines of code throws a *** stack smashing detected *** error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char x[16];
strcpy(x,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
but the following code does not throw it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char x[16];
x[17] = 'a';
}
Thank you!!
Overwriting x[17] doesn't overwite the canary-value put before the return address by gcc.
I'm trying to write a program that takes a string as a command line argument and then runs said argument through a function (str_to_int) that takes a string as an input. However, when I try to compile the program, I get a warning saying
initializing 'char *' with an expression of type 'int' [-Wint
conversion]
char* str = atoi(argv[1]);
^ ~~~~~~~~~~~~~
And when I run the program I get a segmentation fault
I've tested the str_to_int a lot so I'm pretty sure that the issue lies with the command line program. Here's the code for it.
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = atoi(argv[1]);
printf("%d\n", str_to_int(str));
return 0;
}
Can anyone tell me what I'm doing wrong? Thanks.
This is all you need, though it will crash if you leave out the command-line argument.
{
printf("%d\n", str_to_int(argv[1]));
return 0;
}
This is more robust:
int main(int argc, char *argv[])
{
if (argc == 1)
printf("missing parameter.");
else
printf("%d\n", str_to_int(argv[1]));
return 0;
}
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = argv[1];
printf("%d\n", str_to_int(str));
return 0;
}
just remove atoi function invocation and it should work
i want write program in C via bluez API
I have used this site for tutorial :
and this is my code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
char *name;
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,&name);
printf("count After : %i\n",count);
printf("%s\n",&name);
}
}
and out console :
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86
why count value after ba2str(&(device_info+count)->bdaddr,&name); get random value ?
in that source i linked this issue wont occur !?
instead of
char *name;
...
printf("%s\n",&name);
use
char name[248] = { 0 };
...
printf("%s\n",name);
You need allocate memory before pass the variable as reference, and the best option is to do that out of the loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
char *name = (char *) malloc(248*sizeof(char));
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,name);
printf("count After : %i\n",count);
printf("%s\n",name);
}
free(name);
}
doing that your code will be faster because you will allocate memory only one time.
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;
}