hautils

[hare] Set of POSIX utilities
Log | Files | Refs | README | LICENSE

pwd.ha (880B)


      1 use errors;
      2 use fmt;
      3 use fs;
      4 use getopt;
      5 use main;
      6 use os;
      7 use path;
      8 use strings;
      9 
     10 type mode = enum {
     11 	NORM,
     12 	REAL,
     13 };
     14 
     15 export fn utilmain() (main::error | void) = {
     16 	const help: []getopt::help = [
     17 		"prints the working directory",
     18 		('L', "print normalized path"),
     19 		('P', "print normalized path without links"),
     20 	];
     21 	const cmd = getopt::parse(os::args, help...);
     22 	defer getopt::finish(&cmd);
     23 
     24 	let mode = mode::NORM;
     25 	for (let i = 0z; i < len(cmd.opts); i += 1) {
     26 		const opt = cmd.opts[i];
     27 		switch (opt.0) {
     28 		case 'L' =>
     29 			mode = mode::NORM;
     30 		case 'P' =>
     31 			mode = mode::REAL;
     32 		case => abort();
     33 		};
     34 	};
     35 
     36 	// TODO: Update me following https://todo.sr.ht/~sircmpwn/hare/540
     37 	const cwd = os::tryenv("PWD", os::getcwd());
     38 	const path = switch (mode) {
     39 	case mode::NORM =>
     40 		yield os::resolve(cwd);
     41 	case mode::REAL =>
     42 		yield os::realpath(cwd)?;
     43 	};
     44 	fmt::println(path)?;
     45 };