I have a bug that I've found boils down to this:
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <stdlib.h>
int main () {
char *LOG_ROOT = "/var/log";
FTS *ftsp;
FTSENT *p, *chp;
int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR;
char *paths[] = { LOG_ROOT };
fts_open(paths, fts_options, NULL);
}
Why does this segfault?
The first arg. is expected to be a NULL terminated array of character pointers.
char *paths[] = { LOG_ROOT, NULL};
Related
So I have a string like "11111 & 11111" and i need to write this char in the middle in (char operation), but i have Segmentation Fault.
main.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char getChar(char *a);
int main(){
char *str = NULL;
size_t n = 0;
int a = getline(&str,&n,stdin);
printf("%c",getChar(str));
char operation = getChar(str);
free(str);
return 0;
}
bin.c(compiling with main.c)
#include <stdio.h>
#include <string.h>
char getChar(char *a){
char *p = strtok(a," ");
while (p != NULL){
p = strtok(NULL," ");
break;
}
return p[0];
}
in main.c this one
printf("%c",getChar(str));
is works well
but when i write this one
char operation = getChar(str);
I get Segmentation Fault
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
void *process(char **nbE)
{
char buffer[8] = "test";
*nbE = &buffer[0];
printf("%s\n", *nbE);
}
int main(int argc, char **argv)
{
char *str;
process(&str);
printf("%s\n", str);
}
I'm trying to get the value of *nbE in main() by making it points to the address of first char in my array.
But it returns something not encoded, why?
What would be a way for me to do this way?
Note: I know I can do it simpler, I have a more complex code and this is a mini example
Basically I have something interesting in my array and want to pass it to my main function through a char* variable
char buffer[8] = "test";
creates a string that is local to the function, it is destroyed once you return from that function. Do this
static char buffer[8] = "test";
or
char * buffer = strdup("test");
you have to release the string when you have finsihed with it in the second case
I want to mprotect the data section. The following program will not run correctly. I understand the first argument of mprotect() should be aligned. But how to get an aligned memory address for the data section?
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
char s[] = "Hello World!";
int main() {
if(mprotect(s, strlen(s) + 1, PROT_EXEC) == -1) {
perror("mprotect()");
return 1;
}
}
$ ./mprotect_prog
mprotect(): Invalid argument
EDIT: I use the following code to get the page size.
{
builtin printf %s '#define PAGESIZE '
getconf PAGESIZE
} > pagesize.h
Then the C code is changed to the following.
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include "pagesize.h"
char s[] __attribute__((aligned(PAGESIZE))) = "Hello World!";
int main() {
if(mprotect(s, strlen(s) + 1, PROT_EXEC) == -1) {
perror("mprotect()");
return 1;
}
}
Then, I get a segmentation fault. Can anybody reproduce this error? What is wrong with it?
$ ./mprotect_prog
Segmentation fault
EDIT2: I have to add the following line below the 's' line to make sure s occupies a whole page on its own. Then, the program works.
char r[] __attribute__((aligned(PAGESIZE))) = "Hello World!";
{
builtin printf %s '#define PAGESIZE '
getconf PAGESIZE
} > pagesize.h
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include "pagesize.h"
char s[] __attribute__((aligned(PAGESIZE))) = "Hello World!";
char r[] __attribute__((aligned(PAGESIZE))) = "Hello World!";
int main() {
if(mprotect(s, strlen(s) + 1, PROT_EXEC) == -1) {
perror("mprotect()");
return 1;
}
}
all this in Linux not windows
hello i want to know how i can change the color of xeyes like we can do in terminal like
xeyes -fg blue
now i want to to do this in c program using path
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <malloc.h>
//#inlcude <windows.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[0], fullPathName);
myArgv[1] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
if i simply call /usr/bin/X11/xeyes it just show eyes
now i am trying to add command like /usr/bin/X11/xeyes-fg but its not working
any suggestion?
You can add onto the argument vector, like this:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
int n = 0;
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[n++], fullPathName);
myArgv[n++] = "-fg";
myArgv[n++] = "blue";
myArgv[n] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
Here is a picture of the result:
Offhand, I would have expected strace to show the file rgb.txt being opened, but do not see this using -f option (assume it happens in the server). The "blue" does show up in a trace, but only in the exec call, e.g.,
execve("/usr/bin/X11/xeyes", ["/usr/bin/X11/xeyes", "-fg", "blue"], [/* 62 vars */]) = 0
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.