Task
Implement the function run_exit
- Check if the
exitcommand is called correctly (with no arguments), then callexit(EXIT_SUCCESS)to terminate the program - If arguments are provided, print
usage: exittostderrand return-1(do NOT exit) - Takes
argc(number of arguments) and return0on success,-1on error
IMPORTANT
When the exit command is correct, your function should call
exit(EXIT_SUCCESS)and never return
Function Signature
// Handle the exit command. Should have argc == 1 (just "exit", no args).
// If correct, call exit(EXIT_SUCCESS). If wrong, print usage error and return -1.
int run_exit(int argc);Example
$ gcc run_exit.c -o run_exit
$ ./run_exit
exit
$ echo $?
0
$ ./run_exit
exit arg1
usage: exit
$ ./run_exit
exit
<program exits immediately>Code
#include <stdlib.h>
#include <stdio.h>
int run_exit(int argc)
{
if(argc != 1){
fprintf(stderr, "usage: exit");
return -1;
}
exit(EXIT_SUCCESS);
}