picolisp

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/picolisp.git/
Log | Files | Refs | README | LICENSE

sysdefs.c (5628B)


      1 /* 07jan13abu
      2  * (c) Software Lab. Alexander Burger
      3  */
      4 
      5 #include <stdio.h>
      6 #include <stdint.h>
      7 #include <stdlib.h>
      8 #include <unistd.h>
      9 #include <limits.h>
     10 #include <errno.h>
     11 #include <fcntl.h>
     12 #include <dirent.h>
     13 #include <signal.h>
     14 #include <dlfcn.h>
     15 #include <time.h>
     16 #include <poll.h>
     17 #include <termios.h>
     18 #include <sys/types.h>
     19 #include <sys/wait.h>
     20 #include <sys/stat.h>
     21 #include <sys/time.h>
     22 #include <sys/times.h>
     23 #include <sys/resource.h>
     24 #include <netdb.h>
     25 #include <sys/socket.h>
     26 #include <netinet/in.h>
     27 
     28 static int SigNums[] = {
     29    SIGHUP, SIGINT, SIGUSR1, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM,
     30    SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGIO
     31 };
     32 
     33 static char *SigNames[] = {
     34    "SIGHUP", "SIGINT", "SIGUSR1", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM",
     35    "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGIO"
     36 };
     37 
     38 static void comment(char *s) {
     39    printf("\n# %s\n", s);
     40 }
     41 
     42 static void equ(char *sym, long val) {
     43    printf("(equ %s %ld)\n", sym, val);
     44 }
     45 
     46 int main(void) {
     47    int i, n;
     48    struct flock fl;
     49    struct stat st;
     50    struct tms tim;
     51    struct termios term;
     52    struct sigaction act;
     53    fd_set rdSet;
     54    struct tm tm;
     55    struct dirent dir;
     56    struct sockaddr_in6 addr;
     57    struct addrinfo ai;
     58 
     59    i = 1;
     60    printf("# Endianess\n%c\n# Wordsize\n%d\n",
     61       *(char*)&i == 1? 'L' : 'B', (int)sizeof(char*) * 8 );
     62 
     63    comment("errno");
     64    equ("ENOENT", ENOENT);
     65    equ ("EINTR", EINTR);
     66    equ ("EBADF", EBADF);
     67    equ ("EAGAIN", EAGAIN);
     68    equ ("EACCES", EACCES);
     69    equ ("EPIPE", EPIPE);
     70    equ ("ECONNRESET", ECONNRESET);
     71 
     72    comment("open/fcntl");
     73    equ ("O_RDONLY", O_RDONLY);
     74    equ ("O_WRONLY", O_WRONLY);
     75    equ ("O_RDWR", O_RDWR);
     76    equ ("O_CREAT", O_CREAT);
     77    equ ("O_EXCL", O_EXCL);
     78    equ ("O_TRUNC", O_TRUNC);
     79    equ ("O_APPEND", O_APPEND);
     80    equ ("F_GETFD", F_GETFD);
     81    equ ("F_SETFD", F_SETFD);
     82    equ ("FD_CLOEXEC", FD_CLOEXEC);
     83 
     84    comment ("stdio");
     85    equ("BUFSIZ", BUFSIZ);
     86    equ("PIPE_BUF", PIPE_BUF);
     87    equ("MAXPATHLEN", 0);  // getcwd(NULL,0)
     88 
     89    comment ("dlfcn");
     90    equ("RTLD_LAZY", RTLD_LAZY);
     91    equ("RTLD_GLOBAL", RTLD_GLOBAL);
     92 
     93    comment ("fcntl");
     94    equ("FLOCK", sizeof(fl));
     95    equ("L_TYPE", (char*)&fl.l_type - (char*)&fl);
     96    equ("L_WHENCE", (char*)&fl.l_whence - (char*)&fl);
     97    equ("L_START", (char*)&fl.l_start - (char*)&fl);
     98    equ("L_LEN", (char*)&fl.l_len - (char*)&fl);
     99    equ("L_PID", (char*)&fl.l_pid - (char*)&fl);
    100    equ("SEEK_SET", SEEK_SET);
    101    equ("SEEK_CUR", SEEK_CUR);
    102    equ("F_RDLCK", F_RDLCK);
    103    equ("F_WRLCK", F_WRLCK);
    104    equ("F_UNLCK", F_UNLCK);
    105    equ("F_GETFL", F_GETFL);
    106    equ("F_SETFL", F_SETFL);
    107    equ("F_GETLK", F_GETLK);
    108    equ("F_SETLK", F_SETLK);
    109    equ("F_SETLKW", F_SETLKW);
    110    equ("F_SETOWN", F_SETOWN);
    111    equ("O_NONBLOCK", O_NONBLOCK);
    112    equ("O_ASYNC", O_ASYNC);
    113 
    114    comment ("stat");
    115    equ("STAT", sizeof(st));
    116    equ("ST_MODE", (char*)&st.st_mode - (char*)&st);
    117    equ("ST_SIZE", (char*)&st.st_size - (char*)&st);
    118    equ("ST_MTIME", (char*)&st.st_mtime - (char*)&st);
    119    equ("S_IFMT", S_IFMT);
    120    equ("S_IFDIR", S_IFDIR);
    121 
    122    comment ("times");
    123    equ("TMS", sizeof(tim));
    124    equ("TMS_UTIME", (char*)&tim.tms_utime - (char*)&tim);
    125    equ("TMS_STIME", (char*)&tim.tms_stime - (char*)&tim);
    126 
    127    comment ("termios");
    128    equ("TERMIOS", sizeof(term));
    129    equ("C_IFLAG", (char*)&term.c_iflag - (char*)&term);
    130    equ("C_LFLAG", (char*)&term.c_lflag - (char*)&term);
    131    equ("C_CC", (char*)&term.c_cc - (char*)&term);
    132    equ("ISIG", ISIG);
    133    equ("VMIN", VMIN);
    134    equ("VTIME", VTIME);
    135    equ("TCSADRAIN", TCSADRAIN);
    136 
    137    comment ("signal");
    138    equ("SIGACTION", sizeof(act));
    139    equ("SIGSET_T", sizeof(sigset_t));
    140    equ("SA_HANDLER", (char*)&act.sa_handler - (char*)&act);
    141    equ("SA_MASK", (char*)&act.sa_mask - (char*)&act);
    142    equ("SA_FLAGS", (char*)&act.sa_flags - (char*)&act);
    143 
    144    equ("SIG_DFL", (long)SIG_DFL);
    145    equ("SIG_IGN", (long)SIG_IGN);
    146    equ("SIG_UNBLOCK", SIG_UNBLOCK);
    147 
    148    for (i = n = 0; i < sizeof(SigNums)/sizeof(int); ++i) {
    149       equ(SigNames[i], SigNums[i]);
    150       if (SigNums[i] > n)
    151          n = SigNums[i];
    152    }
    153    equ("SIGNALS", n + 1);  // Highest used signal number plus 1
    154 
    155    comment ("wait");
    156    equ("WNOHANG", WNOHANG);
    157    equ("WUNTRACED", WUNTRACED);
    158 
    159    comment ("select");
    160    equ("FD_SET", sizeof(rdSet));
    161 
    162    comment ("time");
    163    equ("TM_SEC", (char*)&tm.tm_sec - (char*)&tm);
    164    equ("TM_MIN", (char*)&tm.tm_min - (char*)&tm);
    165    equ("TM_HOUR", (char*)&tm.tm_hour - (char*)&tm);
    166    equ("TM_MDAY", (char*)&tm.tm_mday - (char*)&tm);
    167    equ("TM_MON", (char*)&tm.tm_mon - (char*)&tm);
    168    equ("TM_YEAR", (char*)&tm.tm_year - (char*)&tm);
    169 
    170    comment ("dir");
    171    equ("D_NAME", (char*)&dir.d_name - (char*)&dir);
    172 
    173    comment ("Sockets");
    174    equ("SOCK_STREAM", SOCK_STREAM);
    175    equ("SOCK_DGRAM", SOCK_DGRAM);
    176    equ("AF_UNSPEC", AF_UNSPEC);
    177    equ("AF_INET6", AF_INET6);
    178    equ("SOL_SOCKET", SOL_SOCKET);
    179    equ("SO_REUSEADDR", SO_REUSEADDR);
    180    equ("IPPROTO_IPV6", IPPROTO_IPV6);
    181    equ("IPV6_V6ONLY", IPV6_V6ONLY);
    182    equ("INET6_ADDRSTRLEN", INET6_ADDRSTRLEN);
    183 
    184    equ("NI_MAXHOST", NI_MAXHOST);
    185    equ("NI_NAMEREQD", NI_NAMEREQD);
    186 
    187    equ("SOCKADDR_IN6", sizeof(addr));
    188    equ("SIN6_FAMILY", (char*)&addr.sin6_family - (char*)&addr);
    189    equ("SIN6_PORT", (char*)&addr.sin6_port - (char*)&addr);
    190    equ("SIN6_ADDR", (char*)&addr.sin6_addr - (char*)&addr);
    191 
    192    equ("ADDRINFO", sizeof(ai));
    193    equ("AI_FAMILY", (char*)&ai.ai_family - (char*)&ai);
    194    equ("AI_SOCKTYPE", (char*)&ai.ai_socktype - (char*)&ai);
    195    equ("AI_ADDRLEN", (char*)&ai.ai_addrlen - (char*)&ai);
    196    equ("AI_ADDR", (char*)&ai.ai_addr - (char*)&ai);
    197    equ("AI_NEXT", (char*)&ai.ai_next - (char*)&ai);
    198 }