From: Sean W. <se...@se...> - 2011-01-23 21:20:45
|
I am trying my hand at writing a callback function based on Chapter 11 of the manual but am coming up short. The function generates a seg fault and I'm not sure why. (I think I may need to pre-allocate space for the returned string, but I'm not sure what the best way to do that would be. I tried declaring a parameter as an output variable in the callback table, but am unsure of how to pass that reference to my C program.) Any help would be greatly appreciated. Thanks, Sean This program uses the same function and exists normally, printing the appropriate data: swoods@sachem:~/mumps/src$ cat stat.c #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /* GTM Operating System Library */ char* fileinfo (char *filename) { struct stat sb; printf("FN: %s\n", filename); if (stat(filename, &sb) == -1) { return "1,File not found!"; } return ctime(&sb.st_mtime); } void main(int argc, char *argv[]) { struct stat sb; if (stat(argv[1], &sb) == 1) { printf("Not found!\n"); exit(1); } printf("%s\n\n", ctime(&sb.st_mtime)); printf("%s\n", fileinfo(argv[1])); return; } Please see the console snippet below for an idea of what I'm doing from a GTM perspective: swoods@sachem:~/mumps/src$ echo $GTMXC_os /home/swoods/mumps/src/os.xc swoods@sachem:~/mumps/src$ cat $GTMXC_os /home/swoods/mumps/bin/os.so fileinfo: xc_char_t* fileinfo(I:gtm_char_t*) swoods@sachem:~/mumps/src$ cat os.c #include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <string.h> #include <stdio.h> #include <stdlib.h> /* GTM Operating System Library */ char* fileinfo (char *filename) { struct stat sb; printf("FN: %s", filename); if (stat(filename, &sb) == -1) { return "1,File not found!"; } return ctime(&sb.st_mtime); } swoods@sachem:~/mumps/src$ ../bin/fe GTM>w $&os.fileinfo("/tmp/s") %GTM-F-KILLBYSIGSINFO1, GT.M process 9365 has been killed by a signal 11 at address 0x005E0B33 (vaddr 0x00000001) %GTM-F-SIGMAPERR, Signal was caused by an address not mapped to an object FN: Just to show you that the file exists and can be stat'd : swoods@sachem:~/mumps/src$ stat /tmp/s File: `/tmp/s' Size: 5 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 5505048 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ swoods) Gid: ( 1000/ swoods) Access: 2011-01-23 15:51:56.865610008 -0500 Modify: 2011-01-23 15:51:56.865610008 -0500 Change: 2011-01-23 15:51:56.865610008 -0500 swoods@sachem:~/mumps/src$ |