>>11Step one:
Devise a function prototype for your subprogram and decide whether to return something and/or pass something by reference. Some return types, like pointers or non-zero integers, can be abused to signal an error. Otherwise you should return a non-zero integer on error.
Step two:
Depending on your function layout, either return early on error or return early on success.
Step three:
React to the error in your program context.
Example:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int parse_int(char *p, size_t n, int *result) {
size_t i;
int j;
for (i = j = 0; i != n; i++) {
if (p[i] < '0' || p[i] > '9')
return 1;
j = j*10 + p[i] - '0';
}
*result = j;
return 0;
}
int main(int argc, char **argv) {
int fd, i;
for (i = 1; i != argc; i++) {
if (parse_int(argv[i], strlen(argv[i]), &fd)) {
write(1, "Usage: poke fd ...\n", 19);
exit(100);
}
if (write(fd, "", 1) < 0)
dprintf(1, "%s: %s\n", argv[i], strerror(errno));
}
}