advanced programming in the unix example ls(1) command error

Viewed 361
#include "apue.h"
#include <dirent.h>

int main(int argc, char *argv[])
{
    DIR     *dp;
    struct dirent *dirp;

    if(argc != 2)
        err_quit("usage: ls directory_name");

    if((dp=opendir(argv[1])) == NULL)
        err_sys("can't open %s", argv[1]);

    while((dirp = readdir(dp)) !=NULL)
        printf("%s\n", dirp->d_name);

    closedir(dp);

    exit(0);

}

I downloaded apue.h file and it is in the current directory as my source file. when I try to compile with clang and OS X the following error occurs.

    1 warning generated.
    Undefined symbols for architecture x86_64:
        "_err_quit", referenced from:
            _main in lsnew-cbcc3e.o
        "_err_sys", referenced from:
            _main in lsnew-cbcc3e.o
    ld: symbol(s) not found for architecture x86_64
    clang error: linker command failed with exit code 1 (use -v to see invocation)

Can someone steer me in the right direction as to what is wrong? does this code only work on 32bit systems?

thanks.

2 Answers

Both undefined symbols can be replaced with own implementation.

Lines

if(argc != 2)
    err_quit("usage: ls directory_name");

can be written as:

if(argc != 2) {
    printf("usage: ls directory_name");
    return 1;
}

Lines

if((dp=opendir(argv[1])) == NULL)
    err_sys("can't open %s", argv[1]);

can be written as:

if((dp=opendir(argv[1])) == NULL) {
    printf("can't open %s", argv[1]);
    return 1;
}
Related