1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * sum -- list file checksum and size
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: sum (AT&T Research) 2010-07-28 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?cksum,md5sum,sum - print file checksum and block count]"
1N/A"[+DESCRIPTION?\bsum\b lists the checksum, and for most methods the block"
1N/A" count, for each file argument. The standard input is read if there are"
1N/A" no \afile\a arguments. \bgetconf UNIVERSE\b determines the default"
1N/A" \bsum\b method: \batt\b for the \batt\b universe, \bbsd\b otherwise."
1N/A" The default for the other commands is the command name itself. The"
1N/A" \batt\b method is a true sum, all others are order dependent.]"
1N/A"[+?Method names consist of a leading identifier and 0 or more options"
1N/A" separated by -.]"
1N/A"[+?\bgetconf PATH_RESOLVE\b determines how symbolic links are handled. This"
1N/A" can be explicitly overridden by the \b--logical\b, \b--metaphysical\b,"
1N/A" and \b--physical\b options below. \bPATH_RESOLVE\b can be one of:]{"
1N/A" [+logical?Follow all symbolic links.]"
1N/A" [+metaphysical?Follow command argument symbolic links,"
1N/A" otherwise don't follow.]"
1N/A" [+physical?Don't follow symbolic links.]"
1N/A"}"
1N/A
1N/A"[a:all?List the checksum for all files. Use with \b--total\b to list both"
1N/A" individual and total checksums and block counts.]"
1N/A"[b:binary?Read files in binary mode. This is the default.]"
1N/A"[B:scale?Block count scale (bytes per block) override for methods that"
1N/A" include size in the output. The default is method specific.]#[scale]"
1N/A"[c:check?Each \afile\a is interpreted as the output from a previous \bsum\b."
1N/A" If \b--header\b or \b--permissions\b was specified in the previous"
1N/A" \bsum\b then the checksum method is automatically determined,"
1N/A" otherwise \b--method\b must be specified. The listed checksum is"
1N/A" compared with the current value and a warning is issued for each file"
1N/A" that does not match. If \afile\a was generated by \b--permissions\b"
1N/A" then the file mode, user and group are also checked. Empty lines,"
1N/A" lines starting with \b#<space>\b, or the line \b#\b are ignored. Lines"
1N/A" containing no blanks are interpreted as [no]]\aname\a[=\avalue\a]]"
1N/A" options:]{"
1N/A" [+method=name?Checksum method to apply to subsequent lines.]"
1N/A" [+permissions?Subsequent lines were generated with"
1N/A" \b--permissions\b.]"
1N/A"}"
1N/A"[h:header?Print the checksum method as the first output line. Used with"
1N/A" \b--check\b and \b--permissions\b.]"
1N/A"[l:list?Each \afile\a is interpreted as a list of files, one per line,"
1N/A" that is checksummed.]"
1N/A"[p:permissions?If \b--check\b is not specified then list the file"
1N/A" mode, user and group between the checksum and path. User and group"
1N/A" matching the caller are output as \b-\b. If \b--check\b is"
1N/A" specified then the mode, user and group for each path in \afile\a"
1N/A" are updated if necessary to match those in \afile\a. A warning is"
1N/A" printed on the standard error for each changed file.]"
1N/A"[R:recursive?Recursively checksum the contents of directories.]"
1N/A"[S:silent|status?No output for \b--check\b; 0 exit status means all sums"
1N/A" matched, non-0 means at least one sum failed to match. Ignored for"
1N/A" \b--permissions\b.]"
1N/A"[t:total?List only the total checksum and block count of all files."
1N/A" \b--all\b \b--total\b lists each checksum and the total. The"
1N/A" total checksum and block count may be different from the checksum"
1N/A" and block count of the catenation of all files due to partial"
1N/A" blocks that may occur when the files are treated separately.]"
1N/A"[T:text?Read files in text mode (i.e., treat \b\\r\\n\b as \b\\n\b).]"
1N/A"[w!:warn?Warn about invalid \b--check\b lines.]"
1N/A"[x:method|algorithm?Specifies the checksum \amethod\a to"
1N/A" apply. Parenthesized method options are readonly implementation"
1N/A" details.]:[method]{\fmethods\f}"
1N/A"[L:logical|follow?Follow symbolic links when traversing directories. The"
1N/A" default is determined by \bgetconf PATH_RESOLVE\b.]"
1N/A"[H:metaphysical?Follow command argument symbolic links, otherwise don't"
1N/A" follow symbolic links when traversing directories. The default is"
1N/A" determined by \bgetconf PATH_RESOLVE\b.]"
1N/A"[P:physical?Don't follow symbolic links when traversing directories. The"
1N/A" default is determined by \bgetconf PATH_RESOLVE\b.]"
1N/A"[r:bsd?Equivalent to \b--method=bsd --scale=512\b for compatibility with"
1N/A" other \bsum\b(1) implementations.]"
1N/A"[s:sysv?Equivalent to \b--method=sys5\b for compatibility with other"
1N/A" \bsum\b(1) implementations.]"
1N/A
1N/A"\n"
1N/A"\n[ file ... ]\n"
1N/A"\n"
1N/A
1N/A"[+SEE ALSO?\bgetconf\b(1), \btw\b(1), \buuencode\b(1)]"
1N/A;
1N/A
1N/A#include <cmd.h>
1N/A#include <sum.h>
1N/A#include <ls.h>
1N/A#include <modex.h>
1N/A#include <fts_fix.h>
1N/A#include <error.h>
1N/A
1N/Atypedef struct State_s /* program state */
1N/A{
1N/A int all; /* list all items */
1N/A Sfio_t* check; /* check previous output */
1N/A int flags; /* sumprint() SUM_* flags */
1N/A gid_t gid; /* caller gid */
1N/A int header; /* list method on output */
1N/A int list; /* list file name too */
1N/A Sum_t* oldsum; /* previous sum method */
1N/A int permissions; /* include mode,uer,group */
1N/A int haveperm; /* permissions in the input */
1N/A int recursive; /* recursively descend dirs */
1N/A size_t scale; /* scale override */
1N/A unsigned long size; /* combined size of all files */
1N/A int silent; /* silent check, 0 exit if ok */
1N/A int (*sort)(FTSENT* const*, FTSENT* const*);
1N/A Sum_t* sum; /* sum method */
1N/A int text; /* \r\n == \n */
1N/A int total; /* list totals only */
1N/A uid_t uid; /* caller uid */
1N/A int warn; /* invalid check line warnings */
1N/A} State_t;
1N/A
1N/Astatic void verify(State_t*, char*, char*, Sfio_t*);
1N/A
1N/A/*
1N/A * open path for read mode
1N/A */
1N/A
1N/Astatic Sfio_t*
1N/Aopenfile(const char* path, const char* mode)
1N/A{
1N/A Sfio_t* sp;
1N/A
1N/A if (!path || streq(path, "-") || streq(path, "/dev/stdin") || streq(path, "/dev/fd/0"))
1N/A {
1N/A sp = sfstdin;
1N/A sfopen(sp, NiL, mode);
1N/A }
1N/A else if (!(sp = sfopen(NiL, path, mode)))
1N/A error(ERROR_SYSTEM|2, "%s: cannot read", path);
1N/A return sp;
1N/A}
1N/A
1N/A/*
1N/A * close an openfile() stream
1N/A */
1N/A
1N/Astatic int
1N/Aclosefile(Sfio_t* sp)
1N/A{
1N/A return sp == sfstdin ? 0 : sfclose(sp);
1N/A}
1N/A
1N/A/*
1N/A * compute and print sum on an open file
1N/A */
1N/A
1N/Astatic void
1N/Apr(State_t* state, Sfio_t* op, Sfio_t* ip, char* file, int perm, struct stat* st, Sfio_t* check)
1N/A{
1N/A register char* p;
1N/A register char* r;
1N/A register char* e;
1N/A register int peek;
1N/A struct stat ss;
1N/A
1N/A if (check)
1N/A {
1N/A state->oldsum = state->sum;
1N/A while (p = sfgetr(ip, '\n', 1))
1N/A verify(state, p, file, check);
1N/A state->sum = state->oldsum;
1N/A if (state->warn && !sfeof(ip))
1N/A error(2, "%s: last line incomplete", file);
1N/A return;
1N/A }
1N/A suminit(state->sum);
1N/A if (state->text)
1N/A {
1N/A peek = 0;
1N/A while (p = sfreserve(ip, SF_UNBOUND, 0))
1N/A {
1N/A e = p + sfvalue(ip);
1N/A if (peek)
1N/A {
1N/A peek = 0;
1N/A if (*p != '\n')
1N/A sumblock(state->sum, "\r", 1);
1N/A }
1N/A while (r = memchr(p, '\r', e - p))
1N/A {
1N/A if (++r >= e)
1N/A {
1N/A e--;
1N/A peek = 1;
1N/A break;
1N/A }
1N/A sumblock(state->sum, p, r - p - (*r == '\n'));
1N/A p = r;
1N/A }
1N/A sumblock(state->sum, p, e - p);
1N/A }
1N/A if (peek)
1N/A sumblock(state->sum, "\r", 1);
1N/A }
1N/A else
1N/A while (p = sfreserve(ip, SF_UNBOUND, 0))
1N/A sumblock(state->sum, p, sfvalue(ip));
1N/A if (sfvalue(ip))
1N/A error(ERROR_SYSTEM|2, "%s: read error", file);
1N/A sumdone(state->sum);
1N/A if (!state->total || state->all)
1N/A {
1N/A sumprint(state->sum, op, state->flags|SUM_SCALE, state->scale);
1N/A if (perm >= 0)
1N/A {
1N/A if (perm)
1N/A {
1N/A if (!st && fstat(sffileno(ip), st = &ss))
1N/A error(ERROR_SYSTEM|2, "%s: cannot stat", file);
1N/A else
1N/A sfprintf(sfstdout, " %04o %s %s",
1N/A modex(st->st_mode & S_IPERM),
1N/A (st->st_uid != state->uid && ((st->st_mode & S_ISUID) || (st->st_mode & S_IRUSR) && !(st->st_mode & (S_IRGRP|S_IROTH)) || (st->st_mode & S_IXUSR) && !(st->st_mode & (S_IXGRP|S_IXOTH)))) ? fmtuid(st->st_uid) : "-",
1N/A (st->st_gid != state->gid && ((st->st_mode & S_ISGID) || (st->st_mode & S_IRGRP) && !(st->st_mode & S_IROTH) || (st->st_mode & S_IXGRP) && !(st->st_mode & S_IXOTH))) ? fmtgid(st->st_gid) : "-");
1N/A }
1N/A if (ip != sfstdin)
1N/A sfprintf(op, " %s", file);
1N/A sfputc(op, '\n');
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * verify previous sum output
1N/A */
1N/A
1N/Astatic void
1N/Averify(State_t* state, register char* s, char* check, Sfio_t* rp)
1N/A{
1N/A register char* t;
1N/A char* e;
1N/A char* file;
1N/A int attr;
1N/A int mode;
1N/A int uid;
1N/A int gid;
1N/A Sfio_t* sp;
1N/A struct stat st;
1N/A
1N/A if (!*s || *s == '#' && (!*(s + 1) || *(s + 1) == ' ' || *(s + 1) == '\t'))
1N/A return;
1N/A if (t = strchr(s, ' '))
1N/A {
1N/A if ((t - s) > 10 || !(file = strchr(t + 1, ' ')))
1N/A file = t;
1N/A *file++ = 0;
1N/A attr = 0;
1N/A if ((mode = strtol(file, &e, 8)) && *e == ' ' && (e - file) == 4)
1N/A {
1N/A mode = modei(mode);
1N/A if (t = strchr(++e, ' '))
1N/A {
1N/A if (*e == '-' && (t - e) == 1)
1N/A uid = -1;
1N/A else
1N/A {
1N/A *t = 0;
1N/A uid = struid(e);
1N/A *t = ' ';
1N/A }
1N/A if (e = strchr(++t, ' '))
1N/A {
1N/A if (*t == '-' && (e - t) == 1)
1N/A gid = -1;
1N/A else
1N/A {
1N/A *e = 0;
1N/A gid = struid(t);
1N/A *e = ' ';
1N/A }
1N/A file = e + 1;
1N/A attr = 1;
1N/A }
1N/A }
1N/A }
1N/A if (sp = openfile(file, "rb"))
1N/A {
1N/A pr(state, rp, sp, file, -1, NiL, NiL);
1N/A if (!(t = sfstruse(rp)))
1N/A error(ERROR_SYSTEM|3, "out of space");
1N/A if (!streq(s, t))
1N/A {
1N/A if (state->silent)
1N/A error_info.errors++;
1N/A else
1N/A error(2, "%s: checksum changed", file);
1N/A }
1N/A else if (attr)
1N/A {
1N/A if (fstat(sffileno(sp), &st))
1N/A {
1N/A if (state->silent)
1N/A error_info.errors++;
1N/A else
1N/A error(ERROR_SYSTEM|2, "%s: cannot stat", file);
1N/A }
1N/A else
1N/A {
1N/A if (uid < 0 || uid == st.st_uid)
1N/A uid = -1;
1N/A else if (!state->permissions)
1N/A {
1N/A if (state->silent)
1N/A error_info.errors++;
1N/A else
1N/A error(2, "%s: uid should be %s", file, fmtuid(uid));
1N/A }
1N/A if (gid < 0 || gid == st.st_gid)
1N/A gid = -1;
1N/A else if (!state->permissions)
1N/A {
1N/A if (state->silent)
1N/A error_info.errors++;
1N/A else
1N/A error(2, "%s: gid should be %s", file, fmtgid(gid));
1N/A }
1N/A if (state->permissions && (uid >= 0 || gid >= 0))
1N/A {
1N/A if (chown(file, uid, gid) < 0)
1N/A {
1N/A if (uid < 0)
1N/A error(ERROR_SYSTEM|2, "%s: cannot change group to %s", file, fmtgid(gid));
1N/A else if (gid < 0)
1N/A error(ERROR_SYSTEM|2, "%s: cannot change user to %s", file, fmtuid(uid));
1N/A else
1N/A error(ERROR_SYSTEM|2, "%s: cannot change user to %s and group to %s", file, fmtuid(uid), fmtgid(gid));
1N/A }
1N/A else
1N/A {
1N/A if (uid < 0)
1N/A error(1, "%s: changed group to %s", file, fmtgid(gid));
1N/A else if (gid < 0)
1N/A error(1, "%s: changed user to %s", file, fmtuid(uid));
1N/A else
1N/A error(1, "%s: changed user to %s and group to %s", file, fmtuid(uid), fmtgid(gid));
1N/A }
1N/A }
1N/A if ((st.st_mode & S_IPERM) ^ mode)
1N/A {
1N/A if (state->permissions)
1N/A {
1N/A if (chmod(file, mode) < 0)
1N/A error(ERROR_SYSTEM|2, "%s: cannot change mode to %s", file, fmtmode(mode, 0));
1N/A else
1N/A error(ERROR_SYSTEM|1, "%s: changed mode to %s", file, fmtmode(mode, 0));
1N/A }
1N/A else if (state->silent)
1N/A error_info.errors++;
1N/A else
1N/A error(2, "%s: mode should be %s", file, fmtmode(mode, 0));
1N/A }
1N/A }
1N/A }
1N/A closefile(sp);
1N/A }
1N/A }
1N/A else if (strneq(s, "method=", 7))
1N/A {
1N/A s += 7;
1N/A if (state->sum != state->oldsum)
1N/A sumclose(state->sum);
1N/A if (!(state->sum = sumopen(s)))
1N/A error(3, "%s: %s: unknown checksum method", check, s);
1N/A }
1N/A else if (streq(s, "permissions"))
1N/A state->haveperm = 1;
1N/A else
1N/A error(1, "%s: %s: unknown option", check, s);
1N/A}
1N/A
1N/A/*
1N/A * sum the list of files in lp
1N/A */
1N/A
1N/Astatic void
1N/Alist(State_t* state, register Sfio_t* lp)
1N/A{
1N/A register char* file;
1N/A register Sfio_t* sp;
1N/A
1N/A while (file = sfgetr(lp, '\n', 1))
1N/A if (sp = openfile(file, state->check ? "rt" : "rb"))
1N/A {
1N/A pr(state, sfstdout, sp, file, state->permissions, NiL, state->check);
1N/A closefile(sp);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * order child entries
1N/A */
1N/A
1N/Astatic int
1N/Aorder(FTSENT* const* f1, FTSENT* const* f2)
1N/A{
1N/A return strcoll((*f1)->fts_name, (*f2)->fts_name);
1N/A}
1N/A
1N/A/*
1N/A * optget() info discipline function
1N/A */
1N/A
1N/Astatic int
1N/Aoptinfo(Opt_t* op, Sfio_t* sp, const char* s, Optdisc_t* dp)
1N/A{
1N/A if (streq(s, "methods"))
1N/A return sumusage(sp);
1N/A return 0;
1N/A}
1N/A
1N/Aint
1N/Ab_cksum(int argc, register char** argv, void* context)
1N/A{
1N/A register int flags;
1N/A char* file;
1N/A char* method;
1N/A Sfio_t* sp;
1N/A FTS* fts;
1N/A FTSENT* ent;
1N/A int logical;
1N/A Optdisc_t optdisc;
1N/A State_t state;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
1N/A memset(&state, 0, sizeof(state));
1N/A flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER;
1N/A state.flags = SUM_SIZE;
1N/A state.warn = 1;
1N/A logical = 1;
1N/A method = 0;
1N/A optinit(&optdisc, optinfo);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'a':
1N/A state.all = 1;
1N/A continue;
1N/A case 'b':
1N/A state.text = 0;
1N/A continue;
1N/A case 'B':
1N/A state.scale = opt_info.num;
1N/A continue;
1N/A case 'c':
1N/A if (!(state.check = sfstropen()))
1N/A error(3, "out of space [check]");
1N/A continue;
1N/A case 'h':
1N/A state.header = 1;
1N/A continue;
1N/A case 'l':
1N/A state.list = 1;
1N/A continue;
1N/A case 'p':
1N/A state.permissions = 1;
1N/A continue;
1N/A case 'r':
1N/A method = "bsd";
1N/A state.scale = 512;
1N/A state.flags |= SUM_LEGACY;
1N/A continue;
1N/A case 'R':
1N/A flags &= ~FTS_TOP;
1N/A state.recursive = 1;
1N/A state.sort = order;
1N/A logical = 0;
1N/A continue;
1N/A case 's':
1N/A method = "sys5";
1N/A continue;
1N/A case 'S':
1N/A state.silent = opt_info.num;
1N/A continue;
1N/A case 't':
1N/A state.total = 1;
1N/A continue;
1N/A case 'w':
1N/A state.warn = opt_info.num;
1N/A continue;
1N/A case 'x':
1N/A method = opt_info.arg;
1N/A continue;
1N/A case 'H':
1N/A flags |= FTS_META|FTS_PHYSICAL;
1N/A logical = 0;
1N/A continue;
1N/A case 'L':
1N/A flags &= ~(FTS_META|FTS_PHYSICAL);
1N/A logical = 0;
1N/A continue;
1N/A case 'P':
1N/A flags &= ~FTS_META;
1N/A flags |= FTS_PHYSICAL;
1N/A logical = 0;
1N/A continue;
1N/A case 'T':
1N/A state.text = 1;
1N/A continue;
1N/A case '?':
1N/A error(ERROR_USAGE|4, "%s", opt_info.arg);
1N/A break;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A if (error_info.errors)
1N/A error(ERROR_USAGE|4, "%s", optusage(NiL));
1N/A
1N/A /*
1N/A * check the method
1N/A */
1N/A
1N/A if (method && !(state.sum = sumopen(method)))
1N/A error(3, "%s: unknown checksum method", method);
1N/A if (!state.sum && !(state.sum = sumopen(error_info.id)) && !(state.sum = sumopen(astconf("UNIVERSE", NiL, NiL))))
1N/A state.sum = sumopen(NiL);
1N/A
1N/A /*
1N/A * do it
1N/A */
1N/A
1N/A if (logical)
1N/A {
1N/A flags &= ~(FTS_META|FTS_PHYSICAL);
1N/A flags |= FTS_SEEDOTDIR;
1N/A }
1N/A if (state.permissions)
1N/A {
1N/A state.uid = geteuid();
1N/A state.gid = getegid();
1N/A state.silent = 0;
1N/A }
1N/A if (!state.check && (state.header || state.permissions))
1N/A {
1N/A sfprintf(sfstdout, "method=%s\n", state.sum->name);
1N/A if (state.permissions)
1N/A sfprintf(sfstdout, "permissions\n");
1N/A }
1N/A if (state.list)
1N/A {
1N/A if (*argv)
1N/A {
1N/A while (file = *argv++)
1N/A if (sp = openfile(file, "rt"))
1N/A {
1N/A list(&state, sp);
1N/A closefile(sp);
1N/A }
1N/A }
1N/A else if (sp = openfile(NiL, "rt"))
1N/A {
1N/A list(&state, sp);
1N/A closefile(sp);
1N/A }
1N/A }
1N/A else if (!*argv && !state.recursive)
1N/A pr(&state, sfstdout, sfstdin, "/dev/stdin", state.permissions, NiL, state.check);
1N/A else if (!(fts = fts_open(argv, flags, state.sort)))
1N/A error(ERROR_system(1), "%s: not found", *argv);
1N/A else
1N/A {
1N/A while (!sh_checksig(context) && (ent = fts_read(fts)))
1N/A switch (ent->fts_info)
1N/A {
1N/A case FTS_SL:
1N/A if (!(flags & FTS_PHYSICAL) || (flags & FTS_META) && ent->fts_level == 1)
1N/A fts_set(NiL, ent, FTS_FOLLOW);
1N/A break;
1N/A case FTS_F:
1N/A if (sp = openfile(ent->fts_accpath, "rb"))
1N/A {
1N/A pr(&state, sfstdout, sp, ent->fts_path, state.permissions, ent->fts_statp, state.check);
1N/A closefile(sp);
1N/A }
1N/A break;
1N/A case FTS_DC:
1N/A error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_path);
1N/A break;
1N/A case FTS_DNR:
1N/A error(ERROR_system(0), "%s: cannot read directory", ent->fts_path);
1N/A break;
1N/A case FTS_DNX:
1N/A error(ERROR_system(0), "%s: cannot search directory", ent->fts_path);
1N/A break;
1N/A case FTS_NS:
1N/A error(ERROR_system(0), "%s: not found", ent->fts_path);
1N/A break;
1N/A }
1N/A fts_close(fts);
1N/A }
1N/A if (state.total)
1N/A {
1N/A sumprint(state.sum, sfstdout, state.flags|SUM_TOTAL|SUM_SCALE, state.scale);
1N/A sfputc(sfstdout, '\n');
1N/A }
1N/A sumclose(state.sum);
1N/A return error_info.errors != 0;
1N/A}