Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is function err_sys() defined?

I am getting an error related to err_sys() in this code:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
    int sockfd;

    if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
        err_sys("can't create socket");

    close(sockfd);
    return 0;
}

I am getting the linker error:

/tmp/cciuUVhz.o: In function `main':
getsockopt.c:(.text+0x38): undefined reference to `err_sys'
collect2: ld returned 1 exit status

Where is the function err_sys() defined?

like image 629
subhash kumar singh Avatar asked Aug 14 '11 18:08

subhash kumar singh


2 Answers

Place this on top of your code:

void err_sys(const char* x) 
{ 
    perror(x); 
    exit(1); 
}

perror is defined in stdio.h

err_sys is used in the book "UNIX Network Programming: The sockets networking API" by Richard Stevens. It's not something common, as far as I know.

edit:fixed code error

like image 130
Patrik Avatar answered Nov 20 '22 08:11

Patrik


Is this from TCP/IP Illustrated? I remember seeing this and the definition is provided in the appendix:

#include <errno.h>
#include <stdarg.h>
/*
 * Print a message and return to caller.
 * Caller specifies "errnoflag".
 */
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
    char    buf[MAXLINE];
    vsnprintf(buf, MAXLINE, fmt, ap);
    if (errnoflag)
        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
    strerror(error));
    strcat(buf, "\n");
    fflush(stdout);     /* in case stdout and stderr are the same */
    fputs(buf, stderr);
    fflush(NULL);       /* flushes all stdio output streams */
}


/*
 * Fatal error related to a system call.
 * Print a message and terminate.
 */
void
err_sys(const char *fmt, ...)
{
    va_list     ap;
    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    exit(1);
}
like image 6
Foo Bah Avatar answered Nov 20 '22 09:11

Foo Bah