public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* man-page code doesn't compile with `-std=c99'
@ 2016-08-22 12:12 Yubin Ruan
  2016-08-22 12:27 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Yubin Ruan @ 2016-08-22 12:12 UTC (permalink / raw)
  To: linux-mm; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: man-page code doesn't compile with `-std=c99'
  2016-08-22 12:12 man-page code doesn't compile with `-std=c99' Yubin Ruan
@ 2016-08-22 12:27 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2016-08-22 12:27 UTC (permalink / raw)
  To: linux-mm, gcc-help

On 22 August 2016 at 13:12, Yubin Ruan wrote:
> The man page doesn't mention standard issue about these S_IF* flags. Is it a man
> page issue or a gcc issue ?

It seems that you need _XOPEN_SOURCE >= 500 but it has nothing to do with GCC.

The example comes from the linux man pages project and the headers
come from glibc.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-22 12:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-22 12:12 man-page code doesn't compile with `-std=c99' Yubin Ruan
2016-08-22 12:27 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).