/* 
 * bog@deol.ru 2000716
 *
 */
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>

extern char *optarg;
extern int optind,opterr;

#define MAXLENG 1024

int main( int argc, char **argv)
{
  char line[MAXLENG], rootpath [MAXLENG], prefpath [MAXLENG], fpath [MAXLENG], fowner [32], fgroup [32], link[MAXLENG];
  struct stat statbuf;
  struct group *groupbuf;
  struct passwd *userbuf;
  int c, link_flag = 0, rdev_flag = 0, path_flag=0, err, l;
  
  rootpath[0] = 0;
  opterr=0;
  while ((c = getopt(argc,argv,"lrp:")) != EOF)
	  switch (c) {
		  case 'l': link_flag=1; break;
		  case 'r': rdev_flag=1; break;
		  case 'p': path_flag=1; strcpy( rootpath, optarg ); break;
	  }
  while( fgets( line, MAXLENG, stdin)) {
    if( sscanf( line, "%s\n", prefpath ) < 1 ) {
	    fprintf( stderr, "input error: '%s'\n", line );
	    exit( 1 );
    }
    if( path_flag ) {
      strcpy( fpath, rootpath );
/*      strcat( fpath, "/" ); */
      if( strlen( fpath ) + strlen( prefpath ) >= MAXLENG ) {
	fprintf( stderr, "input error: '%s'\n", line );
	exit( 1 );
      }
      strcat( fpath, prefpath );
    } else {
      strcpy( fpath, prefpath);
    }
    if( link_flag ) {
	    err = lstat ( fpath, &statbuf);
            if( err == 0 ) {
		    l = readlink( fpath, link, MAXLENG );
		    if( l > 0) link[l] = 0; else err = -1;
	    }
    } else
      err = stat ( fpath, &statbuf);
    if( err < 0 ) {
        printf( "%s error\n", fpath );
    } else {
        if( (groupbuf = getgrgid( statbuf.st_gid )) == NULL ) {
	      sprintf( fgroup, "%d", statbuf.st_gid );
        } else {
	      strcpy( fgroup, groupbuf->gr_name );
        }
        if( (userbuf = getpwuid( statbuf.st_uid )) == NULL ) {
	      sprintf( fowner, "%d", statbuf.st_uid );
        } else {
	      strcpy( fowner, userbuf->pw_name );
        }
        if( link_flag )
	  printf( "%s ok %s %s %s\n", 
	    prefpath, fowner, fgroup, link );
        else if( rdev_flag )
	  printf( "%s ok 0%o %s %s %d\n",
	    prefpath, statbuf.st_mode, fowner, fgroup, statbuf.st_rdev );
        else
          printf( "%s ok %d 0%o %s %s\n", 
	    prefpath, statbuf.st_mtime, statbuf.st_mode, fowner, fgroup );
    }
  }
}

