[BACK]Return to uname.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / uname

Annotation of src/usr.bin/uname/uname.c, Revision 1.7

1.7     ! deraadt     1: /*     $OpenBSD: uname.c,v 1.6 2002/09/17 19:37:40 deraadt Exp $       */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1994 Winning Strategies, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *      This product includes software developed by Winning Strategies, Inc.
                     18:  * 4. The name of Winning Strategies, Inc. may not be used to endorse or
                     19:  *    promote products derived from this software without specific prior
                     20:  *    written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     23:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     24:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     25:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     26:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     27:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     28:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     29:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     30:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     31:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     32:  */
                     33:
                     34: #ifndef lint
1.7     ! deraadt    35: static char rcsid[] = "$OpenBSD: uname.c,v 1.6 2002/09/17 19:37:40 deraadt Exp $";
1.1       deraadt    36: #endif /* not lint */
                     37:
1.3       deraadt    38: #include <sys/param.h>
1.1       deraadt    39: #include <stdio.h>
                     40: #include <locale.h>
                     41: #include <unistd.h>
                     42: #include <sys/utsname.h>
1.3       deraadt    43: #include <sys/sysctl.h>
1.1       deraadt    44: #include <err.h>
                     45:
1.7     ! deraadt    46: static void usage(void);
1.1       deraadt    47:
                     48: #define        PRINT_SYSNAME   0x01
                     49: #define        PRINT_NODENAME  0x02
                     50: #define        PRINT_RELEASE   0x04
                     51: #define        PRINT_VERSION   0x08
                     52: #define        PRINT_MACHINE   0x10
                     53: #define        PRINT_ALL       0x1f
1.3       deraadt    54: #define PRINT_PROCESSOR        0x20
1.1       deraadt    55:
                     56: int
1.6       deraadt    57: main(int argc, char *argv[])
1.1       deraadt    58: {
                     59:        struct utsname u;
                     60:        int c;
                     61:        int space = 0;
                     62:        int print_mask = 0;
                     63:
                     64:        setlocale(LC_ALL, "");
                     65:
1.3       deraadt    66:        while ((c = getopt(argc,argv,"amnrsvp")) != -1 ) {
1.1       deraadt    67:                switch ( c ) {
                     68:                case 'a':
                     69:                        print_mask |= PRINT_ALL;
                     70:                        break;
                     71:                case 'm':
                     72:                        print_mask |= PRINT_MACHINE;
                     73:                        break;
                     74:                case 'n':
                     75:                        print_mask |= PRINT_NODENAME;
                     76:                        break;
                     77:                case 'r':
                     78:                        print_mask |= PRINT_RELEASE;
                     79:                        break;
                     80:                case 's':
                     81:                        print_mask |= PRINT_SYSNAME;
                     82:                        break;
                     83:                case 'v':
                     84:                        print_mask |= PRINT_VERSION;
                     85:                        break;
1.3       deraadt    86:                case 'p':
                     87:                        print_mask |= PRINT_PROCESSOR;
                     88:                        break;
1.1       deraadt    89:                default:
                     90:                        usage();
                     91:                        /* NOTREACHED */
                     92:                }
                     93:        }
                     94:
                     95:        if (optind != argc) {
                     96:                usage();
                     97:                /* NOTREACHED */
                     98:        }
                     99:
                    100:        if (!print_mask) {
                    101:                print_mask = PRINT_SYSNAME;
                    102:        }
                    103:
                    104:        if (uname(&u)) {
                    105:                err(1, NULL);
                    106:                /* NOTREACHED */
                    107:        }
                    108:
                    109:        if (print_mask & PRINT_SYSNAME) {
                    110:                space++;
                    111:                fputs(u.sysname, stdout);
                    112:        }
                    113:        if (print_mask & PRINT_NODENAME) {
                    114:                if (space++) putchar(' ');
                    115:                fputs(u.nodename, stdout);
                    116:        }
                    117:        if (print_mask & PRINT_RELEASE) {
                    118:                if (space++) putchar(' ');
                    119:                fputs(u.release, stdout);
                    120:        }
                    121:        if (print_mask & PRINT_VERSION) {
                    122:                if (space++) putchar(' ');
                    123:                fputs(u.version, stdout);
                    124:        }
                    125:        if (print_mask & PRINT_MACHINE) {
                    126:                if (space++) putchar(' ');
                    127:                fputs(u.machine, stdout);
                    128:        }
1.3       deraadt   129:        if (print_mask & PRINT_PROCESSOR) {
                    130:                char buf[1024];
                    131:                size_t len;
                    132:                int mib[2];
                    133:
                    134:                if (space++) putchar(' ');
                    135:                mib[0] = CTL_HW;
                    136:                mib[1] = HW_MODEL;
                    137:                len = sizeof(buf);
                    138:                if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
                    139:                        err(1, "sysctl");
1.5       deraadt   140:                printf("%.*s", (int)len, buf);
1.3       deraadt   141:        }
1.1       deraadt   142:        putchar('\n');
                    143:
                    144:        exit(0);
                    145:        /* NOTREACHED */
                    146: }
                    147:
                    148: static void
1.6       deraadt   149: usage(void)
1.1       deraadt   150: {
1.4       deraadt   151:        fprintf(stderr, "usage: uname [-amnprsv]\n");
1.1       deraadt   152:        exit(1);
                    153: }