Sponsored Content
Full Discussion: Equivalent command to 'stat'
Top Forums Shell Programming and Scripting Equivalent command to 'stat' Post 302277045 by fpmurphy on Thursday 15th of January 2009 09:04:27 AM
Old 01-15-2009
Solaris does not have a stat(1) utility. Unless you are using the lastest version of Solaris, which contains ksh93t with a custom stat builtin developed by the porting team, you are out of luck.

If you want to add your own stat builtin into ksh93, here is some code to do it
Code:
#pragma prototyped

#include "defs.h"
#include "builtins.h"
#include "path.h"
#include <tm.h>

/* macro to create subvariables */
#define CREATE_CVE(X,Y,Z) \
   strcpy(b,(X)); \
   np = nv_open(buf, shp->var_tree, NV_NOASSIGN|NV_VARNAME); \
   nv_putval(np, (char*)(Y), (Z) ); \
   nv_close(np)

static char strperms_buf[30];

static const
char sh_optstat[] =
   "[-?\n@(#)$Id: stat 2009-01-03 $\n]"
   "[-author?fpmurphy ]"
   "[-license?http://www.opensource.org/licenses/cpl1.0.txt]"
   "[+NAME? stat - get file status]"
   "[+DESCRIPTION?\bstat\b creates the compound variable \avar\a corresponding"
       " to the file given by the pathname \afile\a.  The elements of \avar\a"
       " are the names of fields in the \astat\a structure with the \bst_\b"
       " prefix removed, together with the basename of \afile\a.]"
   "\n"
   "\nvar file\n"
   "\n"
   "[+EXIT STATUS?]{"
       "[+0?Success.]"
       "[+>0?An error occurred.]"
   "}"
   "[+SEE ALSO?\bstat\b(1),\bstat\b(2)]"
;

/* stringify the permission bits */
static char *
strperms(char * p, mode_t mode)
{
   char ftype = '?';

   if (S_ISBLK(mode))  ftype = 'b';
   if (S_ISCHR(mode))  ftype = 'c';
   if (S_ISDIR(mode))  ftype = 'd';
   if (S_ISFIFO(mode)) ftype = '|';
   if (S_ISLNK(mode))  ftype = 'l';
   if (S_ISREG(mode))  ftype = '-';

   sfsprintf(p, 30, "\\0%010lo %c%c%c%c%c%c%c%c%c%c %c%c%c\0",
   mode, ftype,
   mode & S_IRUSR ? 'r' : '-',
   mode & S_IWUSR ? 'w' : '-',
   mode & S_IXUSR ? 'x' : '-',
   mode & S_IRGRP ? 'r' : '-',
   mode & S_IWGRP ? 'w' : '-',
   mode & S_IXGRP ? 'x' : '-',
   mode & S_IROTH ? 'r' : '-',
   mode & S_IWOTH ? 'w' : '-',
   mode & S_IXOTH ? 'x' : '-',
   mode & S_ISUID ? 'U' : '-',
   mode & S_ISGID ? 'G' : '-',
   mode & S_ISVTX ? 'S' : '-');

   return(p);
}

int
b_stat(int argc, char *argv[], void *extra)
{
   register Shell_t *shp = ((Shbltin_t*)extra)->shp;
   register Namval_t *np;
   register int n;
   struct stat statb;
   char buf[100];
   char *b;

   while (n = optget(argv, sh_optstat)) switch (n) {
      case ':':
         errormsg(SH_DICT, 2, "%s", opt_info.arg);
         break;
      case '?':
         errormsg(SH_DICT, ERROR_usage(2), "%s", opt_info.arg);
         break;
   }

   argc -= opt_info.index;
   argv += opt_info.index;
   if (argc!=2)
       errormsg(SH_DICT, ERROR_usage(2), optusage((char*)0));

   /* stat the file */
   if (stat(argv[1], &statb) < 0)
       errormsg(SH_DICT, ERROR_system(1), "%s: stat failed", argv[1]);

   strcpy(buf, argv[0]);
   b = buf;
   while (*b) b++;

   /* create compound variable */
   np = nv_open(buf, shp->var_tree, NV_NOASSIGN|NV_VARNAME|NV_ARRAY );
   if (!nv_isnull(np))
      nv_unset(np);
   nv_setvtree(np);
   nv_close(np);

   /* create compound variable elements */
   CREATE_CVE(".name", path_basename(argv[1]) , NV_RDONLY);
   CREATE_CVE(".atime", &statb.st_atime, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".ctime", &statb.st_ctime, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".mtime", &statb.st_mtime, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".uid", &statb.st_uid, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".gid", &statb.st_gid, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".size", &statb.st_size, NV_RDONLY|NV_INTEGER|NV_LONG);
   CREATE_CVE(".dev", &statb.st_dev, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".ino", &statb.st_ino, NV_RDONLY|NV_INTEGER|NV_LONG);
   CREATE_CVE(".nlink", &statb.st_nlink, NV_RDONLY|NV_INTEGER);
   CREATE_CVE(".mode", strperms(strperms_buf, statb.st_mode), NV_RDONLY);

   return(0);
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

stat command

how can ý use "stat command"????.. (2 Replies)
Discussion started by: emreatlier
2 Replies

2. Shell Programming and Scripting

stat command with ls -l

If i do ls -l i get the result rwx-rw-r ...... ............... file. How can i get the result in octal format. All other output will be the same as ls -l shows. The rwx-rw-r would be like 755 etc. (7 Replies)
Discussion started by: cola
7 Replies

3. Shell Programming and Scripting

hp-unix stat command to get last change date of file

I'm on hp-unix. I would like a variable to hold the last change date of a file. I looked at the man pages for stat, but I don't see any examples and can't get the syntax right. Can anyone help me? Thank you. (2 Replies)
Discussion started by: sboxtops
2 Replies

4. Windows & DOS: Issues & Discussions

Does PowerSHell have *stat command analogs?

I.e. like vmstat, cpustat, iostat, fsstat, kstat ..etc? (1 Reply)
Discussion started by: Xcislav
1 Replies

5. Shell Programming and Scripting

Need file timestamp without stat command

Hi all, I want to check whether a file is not updated in last 15 minutes, for this i need to get timestamp of file, (yyyy:mm:dd:hh:mi:ss). I dont have access to STAT command :(. Please suggest a program or command to do this . Thanks, Saravana (1 Reply)
Discussion started by: sam_1210
1 Replies

6. AIX

smitty equivalent command line command

i know after you do something in smitty via the gui, you can click something in smitty that will show you how to do the same thing via the command line, (not using the gui) can anyone tell me how (2 Replies)
Discussion started by: asyed123
2 Replies

7. Shell Programming and Scripting

AIX and HP-UX equivalent of Linux stat command

To list file permission/access right in octal format, linux has a command 'stat'. For example, we can use the followin - stat -c %a `find . -type f Is there any equivalent command in AIX and HP-UX to give the same result as linux 'stat' command? Please advice. (3 Replies)
Discussion started by: atanubanerji
3 Replies

8. UNIX for Dummies Questions & Answers

Stat command

i know this command does not exist in solaris. however, i read somewhere on this forum that basically everything the stat command provides in other oses can be obtained in solaris using the ls command. i've searched the forum for a while now and i cant find the thread. does anyone know about... (1 Reply)
Discussion started by: SkySmart
1 Replies

9. UNIX for Dummies Questions & Answers

Help with stat command

Hi Experts, I am here with very simple request: #!bin/bash a=`stat -c %y log1.csv` echo $a and this stat command returning value as 2013-08-11 05:42:10.000000000 -0400: But I want to see in mm/dd/yyyy format? any help is highly appreciated thank you ---------- Post... (9 Replies)
Discussion started by: parpaa
9 Replies

10. UNIX for Dummies Questions & Answers

Question on stat command

hello, I wanted to know which is the output of the stat command with a file, for example if I write on the terminal: stat ./unix.pdf i get the output: 754974726 6915670 -rwxrwxrwx 1 mbruno106 staff 0 90501 "Aug 13 13:26:02 2013" "Aug 13 13:26:02 2013" "Aug 13 13:26:02 2013" "Aug 13... (1 Reply)
Discussion started by: Marina2013
1 Replies
All times are GMT -4. The time now is 01:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy