lstat64:: use Posix st_mode defines
[xfstests-dev.git] / src / lstat64.c
index 891fc08c1c70c9eb44be249f1ab1b11c7b5d40f1..c8a0cc6c05b129bde1ce039b8ed9a624a4086f41 100644 (file)
@@ -1,35 +1,22 @@
 /*
- * Copyright (c) 2000-2002 Silicon Graphics, Inc.  All Rights Reserved.
- * 
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
+ * Copyright (c) 2000-2002 Silicon Graphics, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
  * published by the Free Software Foundation.
- * 
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * 
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- * 
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- * 
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- * 
- * http://www.sgi.com 
- * 
- * For further information regarding this notice, see: 
- * 
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
+#include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -59,43 +46,69 @@ timesince(long timesec)
                        d_since, h_since, m_since, s_since);
 }
 
+void
+usage(void)
+{
+       fprintf(stderr, "Usage: lstat64 [-t] filename ...\n");
+       exit(1);
+}
+
 int
 main(int argc, char **argv)
 {
        struct stat64   sbuf;
-       char            mode[10];
-       int             i;
+       int             i, c;
+       int             terse_flag = 0;
+
+       while ((c = getopt(argc, argv, "t")) != EOF) {
+               switch (c) {
+                       case 't':
+                               terse_flag = 1;
+                               break;
+
+                       case '?':
+                               usage();
+               }
+       }
+       if (optind == argc) {
+               usage();
+       }
 
        time(&timebuf);
 
-       for (i = 1; i < argc; i++) {
+       for (i = optind; i < argc; i++) {
+               char mode[] = "----------";
 
                if( lstat64(argv[i], &sbuf) < 0) {
                        perror(argv[i]);
                        continue;
                }
 
-               printf("  File: \"%s\"\n", argv[i]);
-               printf("  Size: %-10llu", (unsigned long long)sbuf.st_size);
+               if (terse_flag) {
+                       printf("%s %llu ", argv[i], (unsigned long long)sbuf.st_size);
+               }
+               else {
+                       printf("  File: \"%s\"\n", argv[i]);
+                       printf("  Size: %-10llu", (unsigned long long)sbuf.st_size);
+               }
 
-               strcpy(mode,"----------");
-               if (sbuf.st_mode & (S_IEXEC>>6))
+               if (sbuf.st_mode & S_IXOTH)
                        mode[9] = 'x';
-               if (sbuf.st_mode & (S_IWRITE>>6))
+               if (sbuf.st_mode & S_IWOTH)
                        mode[8] = 'w';
-               if (sbuf.st_mode & (S_IREAD>>6))
+               if (sbuf.st_mode & S_IROTH)
                        mode[7] = 'r';
-               if (sbuf.st_mode & (S_IEXEC>>3))
+               if (sbuf.st_mode & S_IXGRP)
                        mode[6] = 'x';
-               if (sbuf.st_mode & (S_IWRITE>>3))
+               if (sbuf.st_mode & S_IWGRP)
                        mode[5] = 'w';
-               if (sbuf.st_mode & (S_IREAD>>3))
+               if (sbuf.st_mode & S_IRGRP)
                        mode[4] = 'r';
-               if (sbuf.st_mode & S_IEXEC)
+               if (sbuf.st_mode & S_IXUSR)
                        mode[3] = 'x';
-               if (sbuf.st_mode & S_IWRITE)
+               if (sbuf.st_mode & S_IWUSR)
                        mode[2] = 'w';
-               if (sbuf.st_mode & S_IREAD)
+               if (sbuf.st_mode & S_IRUSR)
                        mode[1] = 'r';
                if (sbuf.st_mode & S_ISVTX)
                        mode[9] = 't';
@@ -104,41 +117,55 @@ main(int argc, char **argv)
                if (sbuf.st_mode & S_ISUID)
                        mode[3] = 's';
 
-               printf("   Filetype: ");
+               if (!terse_flag)
+                       printf("   Filetype: ");
                switch (sbuf.st_mode & S_IFMT) {
                case S_IFSOCK:  
-                       puts("Socket");
+                       if (!terse_flag)
+                               puts("Socket");
                        mode[0] = 's';
                        break;
                case S_IFDIR:   
-                       puts("Directory");
+                       if (!terse_flag)
+                               puts("Directory");
                        mode[0] = 'd';
                        break;
                case S_IFCHR:   
-                       puts("Character Device");
+                       if (!terse_flag)
+                               puts("Character Device");
                        mode[0] = 'c';
                        break;
                case S_IFBLK:   
-                       puts("Block Device");
+                       if (!terse_flag)
+                               puts("Block Device");
                        mode[0] = 'b';
                        break;
                case S_IFREG:   
-                       puts("Regular File");
+                       if (!terse_flag)
+                               puts("Regular File");
                        mode[0] = '-';
                        break;
                case S_IFLNK:   
-                       puts("Symbolic Link");
+                       if (!terse_flag)
+                               puts("Symbolic Link");
                        mode[0] = 'l';
                        break;
                case S_IFIFO:   
-                       puts("Fifo File");
+                       if (!terse_flag)
+                               puts("Fifo File");
                        mode[0] = 'f';
                        break;
                default:        
-                       puts("Unknown");
+                       if (!terse_flag)
+                               puts("Unknown");
                        mode[0] = '?';
                }
 
+               if (terse_flag) {
+                       printf("%s %d,%d\n", mode, (int)sbuf.st_uid, (int)sbuf.st_gid);
+                       continue;
+               }
+
                printf("  Mode: (%04o/%s)", (unsigned int)(sbuf.st_mode & 07777), mode);
                printf("         Uid: (%d)", (int)sbuf.st_uid);
                printf("  Gid: (%d)\n", (int)sbuf.st_gid);
@@ -164,9 +191,5 @@ main(int argc, char **argv)
                if (i+1 < argc)
                        printf("\n");
        }
-       if (i == 1) {
-               fprintf(stderr, "Usage: lstat64 filename...\n");
-               exit(1);
-       }
        exit(0);
 }