1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdio.h>
  27 #include <errno.h>
  28 #include <unistd.h>
  29 #include <fcntl.h>
  30 #include <sys/stat.h>
  31 
  32 /**
  33  * Generates sun.nio.fs.UnixConstants
  34  */
  35 
  36 static void out(char* s) {
  37     printf("%s\n", s);
  38 }
  39 
  40 static void emit(char* name, int value) {
  41     printf("    static final int %s = %d;\n", name, value);
  42 }
  43 
  44 static void emitX(char* name, int value) {
  45     printf("    static final int %s = 0x%x;\n", name, value);
  46 }
  47 
  48 #define DEF(X) emit(#X, X);
  49 #define DEFX(X) emitX(#X, X);
  50 
  51 int main(int argc, const char* argv[]) {
  52     out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT                                  ");
  53     out("package sun.nio.fs;                                                            ");
  54     out("class UnixConstants {                                                          ");
  55     out("    private UnixConstants() { }                                                ");
  56 
  57     // open flags
  58     DEF(O_RDONLY);
  59     DEF(O_WRONLY);
  60     DEF(O_RDWR);
  61     DEFX(O_APPEND);
  62     DEFX(O_CREAT);
  63     DEFX(O_EXCL);
  64     DEFX(O_TRUNC);
  65     DEFX(O_SYNC);
  66 #ifndef O_DSYNC
  67     // At least FreeBSD doesn't define O_DSYNC
  68     emit("O_DSYNC", O_SYNC);
  69 #else
  70     DEFX(O_DSYNC);
  71 #endif
  72 #ifdef O_NOFOLLOW
  73     DEFX(O_NOFOLLOW);
  74 #else
  75     // not supported (dummy values will not be used at runtime).
  76     emitX("O_NOFOLLOW", 0x0);
  77 #endif
  78 
  79     // mode masks
  80     emitX("S_IAMB",
  81          (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH));
  82     DEF(S_IRUSR);
  83     DEF(S_IWUSR);
  84     DEF(S_IXUSR);
  85     DEF(S_IRGRP);
  86     DEF(S_IWGRP);
  87     DEF(S_IXGRP);
  88     DEF(S_IROTH);
  89     DEF(S_IWOTH);
  90     DEF(S_IXOTH);
  91     DEFX(S_IFMT);
  92     DEFX(S_IFREG);
  93     DEFX(S_IFDIR);
  94     DEFX(S_IFLNK);
  95     DEFX(S_IFCHR);
  96     DEFX(S_IFBLK);
  97     DEFX(S_IFIFO);
  98 
  99     // access modes
 100     DEF(R_OK);
 101     DEF(W_OK);
 102     DEF(X_OK);
 103     DEF(F_OK);
 104 
 105     // errors
 106     DEF(ENOENT);
 107     DEF(EACCES);
 108     DEF(EEXIST);
 109     DEF(ENOTDIR);
 110     DEF(EINVAL);
 111     DEF(EXDEV);
 112     DEF(EISDIR);
 113     DEF(ENOTEMPTY);
 114     DEF(ENOSPC);
 115     DEF(EAGAIN);
 116     DEF(EWOULDBLOCK);
 117     DEF(ENOSYS);
 118     DEF(ELOOP);
 119     DEF(EROFS);
 120 #ifndef ENODATA
 121     // Only used in Linux java source, provide any value so it compiles
 122     emit("ENODATA", ELAST);
 123 #else
 124     DEF(ENODATA);
 125 #endif
 126     DEF(ERANGE);
 127     DEF(EMFILE);
 128 
 129     // flags used with openat/unlinkat/etc.
 130 #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_REMOVEDIR)
 131     DEFX(AT_SYMLINK_NOFOLLOW)
 132     DEFX(AT_REMOVEDIR);
 133 #else
 134     // not supported (dummy values will not be used at runtime).
 135     emitX("AT_SYMLINK_NOFOLLOW", 0x0);
 136     emitX("AT_REMOVEDIR", 0x0);
 137 #endif
 138 
 139     out("}                                                                              ");
 140 
 141     return 0;
 142 }