2 * $OpenBSD: dc.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $
3 * $DragonFly: src/usr.bin/dc/dc.c,v 1.2 2005/04/21 18:50:50 swildner Exp $
7 * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 static __dead2 void usage(void);
33 extern char *__progname;
38 fprintf(stderr, "usage: %s [-x] [-e expr] [file]\n", __progname);
43 main(int argc, char *argv[])
46 bool extended_regs = false;
52 if ((buf = strdup("")) == NULL)
54 /* accept and ignore a single dash to be 4.4BSD dc(1) compatible */
55 while ((ch = getopt(argc, argv, "e:x-")) != -1) {
59 if (asprintf(&buf, "%s %s", buf, optarg) == -1)
75 init_bmachine(extended_regs);
82 src_setstring(&src, buf);
90 file = fopen(argv[0], "r");
92 err(1, "cannot open file %s", argv[0]);
93 if (fstat(fileno(file), &st) == -1)
94 err(1, "%s", argv[0]);
95 if (S_ISDIR(st.st_mode)) {
97 err(1, "%s", argv[0]);
100 src_setstream(&src, file);
101 reset_bmachine(&src);
105 * BSD and Solaris dc(1) continue with stdin after processing
106 * the file given as the argument. We follow GNU dc(1).
110 src_setstream(&src, stdin);
111 reset_bmachine(&src);