/* vi:set shiftwidth=2 cindent tabstop=2: */ /* * vxtools - Tools for accessing Veritas Journaled FileSystem (VxFS) * Copyright (c) 1999 Martin Hinner * * vxls.c: List a directory contents * * 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; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will 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 to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id$ */ #include #include #include #include #include char *usage = "Usage: %s [-dhiFR] [name...]\n" "\n" "Report bugs to \n"; int printinode; int classify; int recursive; void listdir (char *dirname, char *name) { int ino; struct vxfs_inode *inode, *dino; VXDIR *dir; struct vxdirent *dirent; char *rpath1, *rpath2; /* Paths for recursive ls */ if (name) printf ("%s:\n", name); ino = vxlookup (dirname); if (ino < 0) { printf ("%s: No such file or directory\n", dirname); return; } inode = vxiget (ino); if (inode == NULL) { printf ("%s: Can't get inode %u\n", dirname, ino); return; } dir = vxopendir (inode); if (dir == NULL) { printf ("%s: vxopendir failed\n", dirname); return; } while ((dirent = vxreaddir (dir)) != NULL) { dino = vxiget(dirent->inode); if (dino == NULL) { fprintf(stderr,"Can't get inode %u\n",dirent->inode); return; } if (printinode) printf("%8u ",dirent->inode); printf ("%s", dirent->name); if (classify) { if (VXFS_ISDIR(dino)) printf("/"); if (VXFS_ISLNK(dino)) printf("@"); if (VXFS_ISSOC(dino)) printf("="); if (VXFS_ISFIFO(dino)) printf("|"); if (VXFS_ISREG(dino) && (dino->mode & 0555)) printf("*"); } printf ("\n"); vxiput(dino); } if (recursive) { printf("\n"); vxrewinddir(dir); while ((dirent = vxreaddir (dir)) != NULL) if (strcmp(dirent->name,"..")) { dino = vxiget(dirent->inode); if (dino == NULL) { fprintf(stderr,"Can't get inode %u\n",dirent->inode); return; } if (VXFS_ISDIR(dino)) { rpath1 = (char *)malloc(strlen(dirname)+strlen(dirent->name)+2); strcpy(rpath1,dirname); strcat(rpath1,"/"); strcat(rpath1,dirent->name); if (name) { rpath2 = (char *)malloc(strlen(name)+strlen(dirent->name)+2); strcpy(rpath2,name); if (rpath2[strlen(rpath2)-1] != '/') strcat(rpath2,"/"); strcat(rpath2,dirent->name); }else rpath2 = strdup(dirent->name); listdir(rpath1,rpath2); /* Recursive ls */ free(rpath1); free(rpath2); } vxiput(dino); } } vxiput (inode); vxclosedir (dir); } int main (int argc, char **argv) { int ch; char *argv0 = argv[0]; char device[0x100], cwd[0x100]; int subpart; char path[0x100]; int printname; vxverboseon (); printinode = 0; classify = 0; recursive = 0; while ((ch = getopt (argc, argv, "dhiFR")) != -1) { switch (ch) { case 'd': vxdebugon (); break; case 'h': fprintf (stderr, usage, argv0); exit (0); case 'i': printinode = 1; break; case 'F': classify = 1; break; case 'R': recursive = 1; break; default: fprintf (stderr, usage, argv0); exit (1); } } argc -= optind; argv += optind; if (vxreadcwd (device, &subpart, cwd) < 0) { fprintf (stderr, "%s: No VxFS filesystem mounted, use vxmount(8)\n", argv0); exit (1); } if (vx_open (device, subpart) < 0) { fprintf (stderr, "%s: Can't mount VxFS %s\n", argv0, device); exit (1); } printname = argc > 1; if (argc) { while (argc--) { if (argv[0][0] == '/') strcpy (path, argv[0]); else { strcpy (path, cwd); strcat (path, "/"); strcat (path, argv[0]); } listdir (path, printname ? argv[0] : 0); argv++; } } else { listdir (cwd, 0); } return 0; }