Home > OS >  Is it possible to combine multiple exit functions in a single statement in c?
Is it possible to combine multiple exit functions in a single statement in c?

Time:08-24

I was writting a function to copy contents of a file to another file, but i still don't know how to workout this. I have two functions that are involved in exiting the program incase of any errors;

void exitErr(char *str, char *file1 char *file2, int ext_val)
void closeFile(int fd1, int fd2)

These two function are supposed to write error message to stderr - function 1 when the program experiences an error,(prints error message and exit) and function 2 in case the file fails to close, the program exits, also, with an error meaasage. If there is an error in the program (read / write) I want to exit with code 99, and when the files fails to close, I want to exit with code 100. I've tried to combine the two function on a single statement like this

/**
 *opf1 = successful open to file 1
 *opf2 = successful open to file 2
 */
closeFile(opf1, opf2) && exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);

but gcc is not compiling the code

3-cp.c:91:25: error: void value not ignored as it ought to be
   91 |                         closeFile(opf1, opf2) &&
      |                         ^~~~~~~~~~~~~~~~~~~~~
3-cp.c:92:33: error: void value not ignored as it ought to be
   92 |                                 exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);

Now, Is this possible or I'm I trying the impossible? Here are the two function:

void exitErr(char *str, char *file1 char *file2, int ext_val)
{
        if (file1 == NULL && file2 != NULL)
                dprintf(STDERR_FILENO, str, file2);
        if (file2 == NULL && file1 != NULL)
                dprintf(STDERR_FILENO, str, file1);
        if (file1 == NULL && file2 == NULL)
                dprintf(STDERR_FILENO, str);
        else
                dprintf(STDERR_FILENO, str, file1, file2);
        exit(ext_val);
}
void closeFile(int fd1, int fd2)
{
        int c1 = 0, c2 = 0;

        if (fd1 == 0 && fd2 != 0)
                c2 = close(fd2);
        if (fd2 == 0 && fd1 != 0)
                c1 = close(fd1);
        else
        {
                c1 = close(fd1);
                c2 = close(fd2);
        }
        if (c1 == -1)
                dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd1);
        if (c2 == -1)
                dprintf(STDERR_FILENO, "Error: Can't close fd %d\n", fd2);
        if (c1 == -1 || c2 == -1)
               exit(100);
}

test scenario

                if (wr < 0)
                {
                        closeFile(opf1, opf2) &&
                                exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);
                }

I want to print all errors messages...

CodePudding user response:

You can not use a logical (or a binary) operator with a function that doesn't return anything, if you want exitErr be called in this line:

closeFile(opf1, opf2) && exitErr(...);

you need to return a value other than 0 from closeFile

int closeFile(int fd1, int fd2)  
{
    ...
    return 1;
}

or return any value including 0 and use the OR operator instead of AND

closeFile(opf1, opf2) || exitErr(...);

exitErr must also return some value so that it can be evaluated as the right side of the operator.

CodePudding user response:

To call two functions, write two call statements:

closeFile(opf1, opf2)
exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);

If you need one statement that calls two functions, use a compound statement:

{
    closeFile(opf1, opf2)
    exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);
}

CodePudding user response:

Okay... Thanks guys for taking time to respond to my question and i really appreciate your help and suggestions. After following what you suggested, i changed one of the two exit function, (can't remember exactly which), but it didn't work, so i changed both functions exitErr(...) & closeFile(...) to return an int value which i assigned it into an "unused variable".

    int __attribute__ ((unused)) ret;

    if (wr < 0)
                {
                        ret = closeFile(opf1, opf2) &&
                                exitErr("Error: Can't write to %s\n", NULL, argv[2], 99);
                }

I haven't tested it on "intentional errors" environment, but it passed the compiler flags successfully.

  • Related