This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

man-page code doesn't compile with `-std=c99'


Hi,
the code below is from linux man-page:
  <http://man7.org/linux/man-pages/man2/stat.2.html>
however it doesn't compile against gcc's `-std=c99' flag.


#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    struct stat sb;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (stat(argv[1], &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    printf("File type:                ");

    switch (sb.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            break;
    case S_IFCHR:  printf("character device\n");        break;
    case S_IFDIR:  printf("directory\n");               break;
    case S_IFIFO:  printf("FIFO/pipe\n");               break;
    case S_IFLNK:  printf("symlink\n");                 break;
    case S_IFREG:  printf("regular file\n");            break;
    case S_IFSOCK: printf("socket\n");                  break;
    default:       printf("unknown?\n");                break;
    }

    printf("I-node number:            %ld\n", (long) sb.st_ino);

    printf("Mode:                     %lo (octal)\n",
            (unsigned long) sb.st_mode);

    printf("Link count:               %ld\n", (long) sb.st_nlink);
    printf("Ownership:                UID=%ld   GID=%ld\n",
            (long) sb.st_uid, (long) sb.st_gid);

    printf("Preferred I/O block size: %ld bytes\n",
            (long) sb.st_blksize);
    printf("File size:                %lld bytes\n",
            (long long) sb.st_size);
    printf("Blocks allocated:         %lld\n",
            (long long) sb.st_blocks);

    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));

    exit(EXIT_SUCCESS);
}

With `gcc -std=c99 test.c -o test' it throw error:

  test.c: In function ‘main’:
  test.c:24:26: error: ‘S_IFMT’ undeclared (first use in this function)
       switch (sb.st_mode & S_IFMT) {
                            ^
  test.c:24:26: note: each undeclared identifier is reported only once for each function it appears in
  test.c:25:10: error: ‘S_IFBLK’ undeclared (first use in this function)
       case S_IFBLK:  printf("block device\n");            break;
            ^
  test.c:26:10: error: ‘S_IFCHR’ undeclared (first use in this function)
       case S_IFCHR:  printf("character device\n");        break;
            ^
  test.c:27:10: error: ‘S_IFDIR’ undeclared (first use in this function)
       case S_IFDIR:  printf("directory\n");               break;
            ^
  test.c:28:10: error: ‘S_IFIFO’ undeclared (first use in this function)
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
            ^
  test.c:29:10: error: ‘S_IFLNK’ undeclared (first use in this function)
       case S_IFLNK:  printf("symlink\n");                 break;
            ^
  test.c:30:10: error: ‘S_IFREG’ undeclared (first use in this function)
       case S_IFREG:  printf("regular file\n");            break;
            ^
  test.c:31:10: error: ‘S_IFSOCK’ undeclared (first use in this function)
       case S_IFSOCK: printf("socket\n");                  break;
            ^

But without the "-std=c99" it work fine. 
The man page doesn't mention standard issue about these S_IF* flags. Is it a man
page issue or a gcc issue ?

thanks,
ruan


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]