Home > Software design >  compilation warnings in the program that uses lseek for reading
compilation warnings in the program that uses lseek for reading

Time:05-03

My program opens a read-write file with the append flag to read from any position specified with lseek. But I'm getting some compilation warnings and I'd like you to help me remove them. What is the problem?

#include "lab.h"
#include <fcntl.h>

#define BUFFERSIZE 1024

int main(int argc, const char *argv[])
{
    int fd;
    char buffer[BUFFERSIZE];
    char dx[] = "C is awesome";

    if ((fd = open("test", O_APPEND)) < 0)
    {
        err_sys("open error");
    }
    
    if (write(fd, dx, sizeof(dx)) != sizeof(dx))    {
        err_sys("write error");
    }

    if(lseek(fd, 0, SEEK_END) == -1)    
    {
        err_sys("lseek error1");
    }
    if (read(fd, buffer, BUFFERSIZE) > 0)
    {
        puts(buffer);
    }
    if(write(fd, dx, sizeof(dx)) != sizeof(dx))
    {
        err_sys("write error1");
    }
    
    if (lseek(fd, 0, SEEK_CUR) == -1)   /* EOF */
    {
        err_sys("lseek error2");
    }
    if (read(fd, buffer, BUFFERSIZE) > 0)
    {
        puts(buffer);
    }
    if (write(fd, dx, sizeof(dx)) != sizeof(dx))
    {
        err_sys("write error2");
    }

    if(lseek(fd, 0, SEEK_SET) == -1)    /* the beginning of file */
    {
        err_sys("lseek error3");
    }
    if (read(fd, buffer, BUFFERSIZE) > 0)
    {
        puts(buffer);
    }
    if ((write(fd, dx, sizeof(dx))) != sizeof(dx))
    {
        err_sys("write error3");
    }
    close(fd);
    return 0;
}

header:

#ifndef __LAB_H__
#define __LAB_H__

#include    <sys/types.h>   /* required for some of our prototypes */
#include    <stdio.h>       /* for convenience */
#include    <stdlib.h>      /* for convenience */
#include    <string.h>      /* for convenience */
#include    <unistd.h>      /* for convenience */

#define MAXLINE 4096            /* max line length */

#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
                    /* default file access permissions for new files */
#define DIR_MODE    (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
                    /* default permissions for new directories */


                    /* prototypes for our own functions */
char    *path_alloc(int *);         /* {Prog pathalloc} */
int      open_max(void);            /* {Prog openmax} */
void     clr_fl(int, int);          /* {Prog setfl} */
void     set_fl(int, int);          /* {Prog setfl} */

void    err_dump(const char *, ...);    /* {App misc_source} */
void    err_msg(const char *, ...);
void    err_quit(const char *, ...);
void    err_ret(const char *, ...);
void    err_sys(const char *, ...);

#endif  /* __LAB_H__ */

warnings receive:

gcc -Wall -Wextra -O -g -D_FORTIFY_SOURCE=2 -I../exemple/   append.c  ../exemple/liblab.a -o append
append.c: In function ‘main’:
append.c:6:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
    6 | int main(int argc, const char *argv[])
      |          ~~~~^~~~
append.c:6:32: warning: unused parameter ‘argv’ [-Wunused-parameter]
    6 | int main(int argc, const char *argv[])
      |                    ~~~~~~~~~~~~^~~~~~

CodePudding user response:

You aren't using the argc and argv variables and when you compile the solution, the compiler displays a warning for each of these.

Change the signature of int main(int argc, const char *argv[]) to plain int main() and it should compile without warnings.

CodePudding user response:

-Wall -Wextra is a very high warning level to use, and some of the sub-options it includes produce warnings that are not helpful in all situations. It is okay to compile without -Wextra or to turn off specific options as by using -Wno-unused-parameter to turn off the unused-parameter warning.

Often, any parameter declared in a function prototype is needed to perform the function’s purpose. So, if the parameter does not appear anywhere in the function definition, it could be a sign that some mistake has been made. This is not always true. For example, a program could have a class of functions that all have the same parameters because they are used in some common way, but some of the functions in the class do not need all the parameters. So it is not always a mistake not to use all parameters.

In addition to turning off the specific warning, you can tell the compiler the parameters are deliberately unused by inserting these lines in your main routine:

(void) argc;
(void) argv;

These are expressions that use the parameters argc and argv, but the void cast tells the compiler to discard the values of the expressions. It tells the compiler you are deliberately ignoring these values, and then the compiler will not warn that the parameters are not used.

Because main is a special function, it may be declared as int main(void) or int main(int argc, char *argv[]) (or equivalent). So another solution is to use the alternate declaration for main, int main(void). When this warning occurs with ordinary functions, an option might be to remove the unused parameter from the declaration of the function, as long as the function does not need to have that parameter for some other reason.

  • Related