Skip to content

Instantly share code, notes, and snippets.

@faif
Created January 29, 2012 16:27
Show Gist options
  • Save faif/1699511 to your computer and use it in GitHub Desktop.
Save faif/1699511 to your computer and use it in GitHub Desktop.
plan9 chmod
#include <u.h>
#include <libc.h>
const int STDERR = 2;
static void
usage (void)
{
fprint (STDERR, "usage: %s 0644 file1 [file2] ...\n", argv0);
exits ("usage");
}
void
mychmod (const ulong nmode, const char* fname)
{
Dir d;
nulldir (&d);
d.mode = nmode;
if (-1 == dirwstat (fname, &d))
{
sysfatal ("dirwstat: %r");
}
}
void
main (int argc, char* argv[])
{
ARGBEGIN
{
default:
usage ();
}
ARGEND;
if (argc < 2)
{
usage ();
}
const int MASK_BASE = 8;
ulong perm_mask = strtoul (argv[0], nil, MASK_BASE);
if (0 == perm_mask)
{
sysfatal ("invalid mode: '%s'", argv[0]);
}
for (int i = 1; i != argc; ++i)
{
mychmod (perm_mask, argv[i]);
}
exits (nil);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment