Posted to tcl by lmcvoy at Fri Oct 19 17:17:55 GMT 2007view raw
- /*
- * stdio stuff (some above because we don't have {*} yet).
- */
- typedef string FILE;
- FILE stdin = "stdin";
- FILE stderr = "stderr";
- FILE stdout = "stdout";
- string stdio_lasterr;
- FILE
- fopen(string path, string mode)
- {
- FILE f;
- string err;
- int v = 0;
- /* new mode, v, means be verbose w/ errors */
- if (mode =~ /v/) {
- mode =~ s/v//;
- v = 1;
- }
- if (catch("set f [open {${path}} ${mode}]", &err)) {
- stdio_lasterr = err;
- if (v) fprintf(stderr, "fopen(%s, %s) = %s\n", path, mode, err);
- return (0);
- } else {
- return (f);
- }
- }
- FILE
- popen(string cmd, string mode)
- {
- FILE f;
- string err;
- int v = 0;
- if (mode =~ /v/) {
- mode =~ s/v//;
- v = 1;
- }
- if (catch("set f [open {|${cmd}} ${mode}]", &err)) {
- stdio_lasterr = err;
- if (v) fprintf(stderr, "popen(%s, %s) = %s\n", cmd, mode, err);
- return (0);
- } else {
- return (f);
- }
- }
- int
- fclose(FILE f)
- {
- string err = "";
- if (f eq "") return (0);
- if (catch("close ${f}", &err)) {
- stdio_lasterr = err;
- return (-1);
- } else {
- return (0);
- }
- }
- int pclose(FILE f) { return (fclose(f)); }
- int
- fgetline(FILE f, string &buf)
- {
- return (gets(f, &buf));
- }
- string
- stdio_getLastError()
- {
- return (stdio_lasterr);
- }
- /*
- * string functions
- */
- int
- streq(string a, string b)
- {
- return (string("compare", a, b));
- }
- int
- strneq(string a, string b, int n)
- {
- return (string("compare", length: n, a, b));
- }
- string
- strchr(string s, string c)
- {
- return (string("first", c, s));
- }
- string
- strrchr(string s, string c)
- {
- return (string("last", c, s));
- }
- int
- strlen(string s)
- {
- return (string("length", s));
- }
- void
- chomp(string &s)
- {
- s = string("trimright", s, "\r\n");
- }
- ===============================================
- Simplistic cat.
- main(int ac, string av[])
- {
- string buf;
- FILE f;
- int i;
- for (i = 1; i < ac; ++i) {
- unless (f = fopen(av[i], "rv")) continue;
- while (fgetline(f, &buf) >= 0) {
- printf("%s\n", buf);
- }
- fclose(f);
- }
- }