Error with file pointer not defined - c

I have a file a.c, a.h which acts as a supporting module with functions from other files.
In those functions I have to open a file and do functions like reading data, writing data, closing it.
For these purposes I gave a global declaration for file pointer in a.c by
static FILE* pFile;
and used pFile directly.
The error that the compiler was throwing error is like :
"pFile" not defined in the function
What's wrong here?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>
#define path "/tmp/diag.log"
FILE* pFile;
void a(bool value) {
if (value) {
pFile = fopen (path,"a");
return;
}
else if (!value) {
fclose (pFile);
}
}
void b(bool value) {
if(value)
{
fprintf (pFile,"%s",message);
}
}
Update:
The error was my makefile was taking the files from build directory and the changes were not getting reflected.Thanks a lot for your help

If you try to use this static FILE* pFile from another compilation units remove the static
in other file add:
extern FILE *pfile;

Related

modlib3.c:21:9: error: invalid use of incomplete typedef 'DIR {aka struct _dirstream}

The problem is I need to use LD_PRELOAD to modify the opendir() function in that is part of the ls command to restrict the opening of directories when the target path is not within the /home directory. The error I am getting is with the return line that says:
return((*original_opendir)(_name));
In that the original_opendir is bolded and it says error: invalid use of incomplete typedef 'DIR{aka struct _distream)'
I have attached my code below. Please let me know if you have any ideas I would really appreciate it![enter image description here][1]
The file name was called modlib3.c and I compiled it with this when I got the errors:
//gcc -o modlib3.so -shared -fPIC -D_GNU_SOURCE modlib3.c -ldl
Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dlfcn.h>
#include <dlfcn.h>
DIR *opendir(const char *_name) {
DIR (*original_opendir)(const char *);
*(void **)(&original_opendir) = dlsym(RTLD_NEXT, "*opendir");
if (strcmp(_name, "/home") <0 || strcmp(_name, "/home")>0){
syslog(LOG_EMERG, "Cannot open! ");
exit(1);
}
return((*original_opendir)(_name));
}
Fixed some of the problems in the code, please check the differences carefully
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <dirent.h>
#include <dlfcn.h>
DIR *opendir(const char *_name) {
DIR *(*original_opendir)(const char *);
*(void **)(&original_opendir) = dlsym(RTLD_NEXT, "opendir");
if (strncmp(_name, "/home", 5)!=0) {
syslog(LOG_EMERG, "Cannot open!");
exit(1);
}
return((*original_opendir)(_name));
}

Unable to retrieve a value inside a c header file

I have a header file where a few functions needs to be implemented in the header file and included in a main.c file to be tested. These are some library functions of String and encoding.
Once these methods are implemented in the header file I should be able to include this file in another c file and execute these methods.
#ifndef ABSTRING_H
#define ABSTRING_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define ABSBLOCK 4096
typedef struct _abstring
{
unsigned char* val;
size_t length;
size_t space;
}absval;
abstring absval;
//Initialize string
abstring* initAbs() {
printf("%s", abval.val);
printf("%zu", abval.val);
abval.length = sizeof(abval.val);
abval.space = ABSBLOCK - sizeof(abval.val);
return &abval;
}
------------------ End of the header file (abString.h ) ------------------------
main.c file
#include "abString.h"
int main()
{
abstring absinit;
absinit.val = "abString";
printf("ABSBLOCK block size : %d .\n", ABSBLOCK);
initAbs();
return 0;
}
The issue I'm having is once I define a val in the main c file I'm not able to retrieve that value inside my header file in order to initialize the length and space.

Setting Immutable Flag using ioctl() in C

I have attempted to make a script that creates a file and then sets it as immutable similar to the chattr +i command for linux. The script compiles (with gcc), runs and the file is created. However the file itself is not immutable and can be removed with a simple rm -f. I have attempted to stacktrace where chattr is called and I found a function called ioctl. I then used what little information I could gather and came up with what I have below. I narrowed it down from ext2_fs.h but it just doesn't seem to work. I've clearly overlooked something.
Updates to previous entry: Compiles but returns -1 on ioctl() function. Bad address shown with perror().
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
fp = fopen("/shovel.txt", "w+");
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
ioctl(fileno(fp), FS_IOC_SETFLAGS, 0x00000010);
fclose(fp);
}
Any help appreciated.
You are using the right ioctl command, but you're passing it the wrong arguments.
The manpage for ioctl_list(2) shows that FS_IOC_SETFLAGS expects to receive a pointer to int (an int *), yet you're passing it an integer literal (hence the Bad Address error).
The fact that you don't to any error checking whatsoever is also not helping.
The correct flag to pass to FS_IOC_SETFLAGS is a pointer holding the value EXT2_IMMUTABLE_FL, which is defined in ext2fs/ext2_fs.h (some older / different Linux distributions seem to have it under linux/ext2_fs.h), so you'll need to #include <ext2fs/etx2_fs.h>. Make sure to install e2fslibs-dev (and probably you'll need linux-headers too).
This code is working:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <ext2fs/ext2_fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
if ((fp = fopen("shovel.txt", "w+")) == NULL) {
perror("fopen(3) error");
exit(EXIT_FAILURE);
}
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
int val = EXT2_IMMUTABLE_FL;
if (ioctl(fileno(fp), FS_IOC_SETFLAGS, &val) < 0)
perror("ioctl(2) error");
fclose(fp);
return 0;
}
Remember to run this as root.
UPDATE:
As Giuseppe Guerrini suggests in his answer, you might want to use FS_IMMUTABLE_FL instead, and you won't need to include ext2_fs.h:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
if ((fp = fopen("shovel.txt", "w+")) == NULL) {
perror("fopen(3) error");
exit(EXIT_FAILURE);
}
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
int val = FS_IMMUTABLE_FL;
if (ioctl(fileno(fp), FS_IOC_SETFLAGS, &val) < 0)
perror("ioctl(2) error");
fclose(fp);
return 0;
}
The main problem is that the ioctl wants a pointer to the mask, not a direct constant. You have to define a int variable, store the mask (0x10) in it and pass its address as third argument of ioctl.
Also, I'd add some hints:
other programs to change attributes are used to use low-level I/O directly (open, close...). Also, the file is usually opened with O_RDONLY.
Use FS_IMMUTABLE_FL istead the raw constant.
Get the current attribute mask first (FS_IOC_SETFLAGS) and mask it with the new flag, so other settings are not lost by the service.

Im getting the error: expected '=', ',', ';', 'asm', or ''__attribute__' before 'void'

I am experiencing this error at my preprocessText() function (below) in my .c and I'm not entirely sure why. From browsing it seems most people were missing a { or ( or ; etc somewhere, but I'm fairly certain I am not.
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "Assembler.h"
int main(int argc, char** argv) {
// ...
preprocessText(file, inter1);
// ...
}
public void preprocessText(FILE* file, FILE* file2) { //error happens at this declaration
// ...
}
My header file is:
#ifndef ASSEMBLER_H
#define ASSEMBLER_H
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
// ...
void preprocessText(FILE* file, FILE* file2);
#endif
All methods are implicitly accessible by any other piece of code, if the function name is in scope. There is no public keyword in c
You have 'public' before 'void'. Remember, this is C ;)

Implicit declaration of mkdir

For my question let's suppose I have two functions, both of them with the prototypes on a .h file in a library folder, and the implementation in a .c auxiliary file (shown below), and I will use both of them in my program.
calsis.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include "include/calsis.h" /* Extern header */
char folder_name[30] = "Information";
void no_args() /* Function 1 */
{
printf("Hello, world!\n");
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
void with_args(char *foo) /* Function 2 */
{
printf("Hello, world!\n");
printf("Name: %s\n", foo);
if ( mkdir(folder_name, S_IRWXU) == -1 )
perror("Can't create a new folder");
}
For something I will do later, I need in both functions to create a folder with mkdir, but, in the generation of the object file calsis.o by the compilation of the .c file with the implemented functions, the compilation with GCC gives me a warning that the mkdir function is implicity declared.
Any idea I can remove this warning?
You haven't included the header for mkdir:
From man(2) mkdir:
SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);

Resources