// Copyright (c) 2015, Samvel Khalatyan. All rights reserved.

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

int main(int, char* argv[]) {
    struct stat buf;
    for (char **fname {argv + 1}, type; *fname; ++fname) {
        memset(&buf, 0, sizeof(buf));
        // use lstat to keep links
        if (-1 == lstat(*fname, &buf)) {
            fprintf(stderr, "%s: %s %s\n", *argv, *fname, strerror(errno));
            continue;
        }
        if (S_ISREG(buf.st_mode)) {
            type = 'f';
        } else if (S_ISDIR(buf.st_mode)) {
            type = 'd';
        } else if (S_ISLNK(buf.st_mode)) {
            type = 'l';
        } else if (S_ISBLK(buf.st_mode)) {
            type = 'b';
        } else if (S_ISCHR(buf.st_mode)) {
            type = 'c';
        } else if (S_ISFIFO(buf.st_mode)) {
            type = 'p';
        } else if (S_ISSOCK(buf.st_mode)) {
            type = 's';
        } else {
            fprintf(stderr, "%s: unknown type of %s\n", *argv, *fname);
            continue;
        }
        printf("%c %s\n", type, *fname);
    }
    return 0;
}
