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

Annotation of src/usr.bin/dc/dc.c, Revision 1.15

1.15    ! deraadt     1: /*     $OpenBSD: dc.c,v 1.14 2015/10/03 18:39:13 deraadt Exp $ */
1.1       otto        2:
                      3: /*
                      4:  * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
1.8       otto       19: #include <sys/stat.h>
1.1       otto       20: #include <err.h>
1.8       otto       21: #include <errno.h>
1.5       otto       22: #include <stdlib.h>
1.6       otto       23: #include <string.h>
1.4       otto       24: #include <unistd.h>
1.1       otto       25:
                     26: #include "extern.h"
                     27:
                     28: static __dead void     usage(void);
                     29:
1.2       deraadt    30: extern char            *__progname;
1.1       otto       31:
                     32: static __dead void
                     33: usage(void)
                     34: {
1.10      sobrado    35:        (void)fprintf(stderr, "usage: %s [-x] [-e expression] [file]\n",
                     36:            __progname);
1.1       otto       37:        exit(1);
                     38: }
                     39:
                     40: int
                     41: main(int argc, char *argv[])
                     42: {
1.4       otto       43:        int             ch;
                     44:        bool            extended_regs = false;
1.1       otto       45:        FILE            *file;
                     46:        struct source   src;
1.6       otto       47:        char            *buf, *p;
1.8       otto       48:        struct stat     st;
                     49:
1.15    ! deraadt    50:        if (pledge("stdio rpath", NULL) == -1)
        !            51:                err(1, "pledge");
1.1       otto       52:
1.6       otto       53:        if ((buf = strdup("")) == NULL)
                     54:                err(1, NULL);
1.4       otto       55:        /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
1.6       otto       56:        while ((ch = getopt(argc, argv, "e:x-")) != -1) {
1.4       otto       57:                switch (ch) {
1.6       otto       58:                case 'e':
                     59:                        p = buf;
                     60:                        if (asprintf(&buf, "%s %s", buf, optarg) == -1)
                     61:                                err(1, NULL);
                     62:                        free(p);
                     63:                        break;
1.4       otto       64:                case 'x':
                     65:                        extended_regs = true;
                     66:                        break;
                     67:                case '-':
                     68:                        break;
                     69:                default:
                     70:                        usage();
                     71:                }
                     72:        }
                     73:        argc -= optind;
                     74:        argv += optind;
1.1       otto       75:
1.4       otto       76:        init_bmachine(extended_regs);
1.13      millert    77:        (void)setvbuf(stdout, NULL, _IOLBF, 0);
                     78:        (void)setvbuf(stderr, NULL, _IOLBF, 0);
1.4       otto       79:
                     80:        if (argc > 1)
                     81:                usage();
1.6       otto       82:        if (buf[0] != '\0') {
                     83:                src_setstring(&src, buf);
                     84:                reset_bmachine(&src);
                     85:                eval();
                     86:                free(buf);
                     87:                if (argc == 0)
                     88:                        return (0);
                     89:        }
                     90:        if (argc == 1) {
1.9       otto       91:                file = fopen(argv[0], "r");
                     92:                if (file == NULL)
                     93:                        err(1, "cannot open file %s", argv[0]);
1.15    ! deraadt    94:                if (pledge("stdio", NULL) == -1)
        !            95:                        err(1, "pledge");
1.9       otto       96:                if (fstat(fileno(file), &st) == -1)
1.8       otto       97:                        err(1, "%s", argv[0]);
1.12      guenther   98:                if (S_ISDIR(st.st_mode))
                     99:                        errc(1, EISDIR, "%s", argv[0]);
1.1       otto      100:                src_setstream(&src, file);
                    101:                reset_bmachine(&src);
                    102:                eval();
1.7       otto      103:                (void)fclose(file);
1.6       otto      104:                /*
                    105:                 * BSD and Solaris dc(1) continue with stdin after processing
                    106:                 * the file given as the argument. We follow GNU dc(1).
                    107:                 */
                    108:                 return (0);
1.1       otto      109:        }
1.15    ! deraadt   110:        if (pledge("stdio", NULL) == -1)
        !           111:                err(1, "pledge");
1.1       otto      112:        src_setstream(&src, stdin);
                    113:        reset_bmachine(&src);
                    114:        eval();
                    115:
1.6       otto      116:        return (0);
1.1       otto      117: }