SUMMARY: Permissions on a file

From: Peter Olson, Delphi Information Engineer <PETER_at_delphi.com>
Date: Tue, 09 Jun 1998 16:59:23 -0400 (EDT)

I figure I owe the list a contribution in return for all the great stuff I
read here, so here is a small C program to solve SSpaldin_at_mem-ins.com's
permissions inquiry. Tested on Digital Unix 3.2c and 4.0 and 4.0B, but
it should work on most Unix systems.

Peter Olson, Delphi Information Engineer

-----------%<----------
/*
** fstatm.c
**
** fstat multiple files.
**
** Public Domain 1998
**
*/

#include <stdio.h>
#include <sys/stat.h>
#include <sys/mode.h>

int
main(int argc, char **argv)
{
int retcode = 0, showfn = 0;
if (argc > 1 && '-' == argv[1][0])
    {
    if ('n' != argv[1][1] || argv[1][2])
        {
        fprintf(stderr, "Usage: fstatm {-n} filenames\n");
        fprintf(stderr,
            " -n causes filenames to be displayed in second column\n");
        return 2;
        }
    showfn = 1;
    argv++; argc--;
    }
while (++argv, --argc > 0) /* can run against wildcards */
    {
    struct stat stbuf;
    if (0 == lstat(argv[0], &stbuf)) /* don't follow symbolic links */
        {
        if (S_ISREG(stbuf.st_mode)) /* is this a regular file? */
            stbuf.st_mode &= ~S_IFREG; /* lose the bit that says so */
        /* see /usr/include/sys/mode.h for esoteric high order bits */
        if (showfn)
            printf("%3.3lo %s\n", 0x0000FFFFL & (long) stbuf.st_mode,
                                        argv[0]);
        else printf("%3.3lo\n", 0x0000FFFFL & (long) stbuf.st_mode);
        }
    else /* placeholder for when we can't get status bits */
        {
        /* real 'no permissions' has 3 digits */
        if (showfn)
            printf("000000 %s\n", argv[0]);
        else printf("000000\n");
        retcode = 1; /* bad path name or other error */
        }
    }
return retcode; /* non-zero if one or more fstat errors */
}
Received on Wed Jun 10 1998 - 07:17:59 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:37 NZDT