home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / dosutils / ext2tool / src / e2pwd.c < prev    next >
C/C++ Source or Header  |  1995-05-10  |  3KB  |  126 lines

  1. /***************************************************************************
  2.  * e2pwd - Prints current directory
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14.  
  15. #include "ext2_fs.h"
  16. #include "ext2fs/ext2fs.h"
  17. #include "ldisk.h"
  18. #include "istat.h"
  19. #include "e2err.h"
  20.  
  21. extern io_manager msdos_io_manager;
  22.  
  23. int aflag, dflag, iflag, lflag, tflag, rflag;
  24.  
  25. struct fileinfo {
  26.     ino_t    inode;
  27.     u_char    name[80];
  28.     struct ext2_inode e2ino;
  29. } *list;
  30. int listix;
  31. int maxlist;
  32.  
  33. char directory[256];
  34. char dir2[256];
  35.  
  36. ino_t ino;
  37.  
  38. /**********************************************************************
  39.  * myproc is a callback routine which is called once for each entry
  40.  * in the directory
  41.  **********************************************************************/
  42.  
  43. static int myproc(struct ext2_dir_entry *dirent,
  44.                   int    offset,
  45.                   int    blocksize,
  46.                   char    *buf,
  47.                   void    *private)
  48. {
  49.     if (dirent->inode == ino) {
  50.         if (directory[0]==0) {
  51.             strncpy(directory, dirent->name, dirent->name_len);
  52.             directory[dirent->name_len]=0;
  53.         }
  54.         else {
  55.             strncpy(dir2, dirent->name, dirent->name_len);
  56.             dir2[dirent->name_len]=0;
  57.             strcat(dir2, "/");
  58.             strcat(dir2,directory);
  59.             strcpy(directory,dir2);
  60.         }
  61.         return DIRENT_ABORT;
  62.     }
  63.  
  64.     return 0;
  65. }
  66.  
  67.  
  68. /**********************************************************************
  69.  * usage prints usage information and exits
  70.  **********************************************************************/
  71.  
  72. void
  73. usage()
  74. {
  75.     fprintf(stderr, "usage: e2pwd\n");
  76.     exit(1);
  77. }
  78.  
  79.  
  80. /**********************************************************************
  81.  * main routine
  82.  **********************************************************************/
  83.  
  84. main(int argc, char **argv)
  85. {
  86.     int err, i;
  87.     ext2_filsys fs;
  88.     ino_t dotdotino;
  89.     struct ext2_inode e2ino;
  90.     char *filename;
  91.  
  92.     if (argc!=1)
  93.         usage();
  94.  
  95.     /* Open file system */
  96.     err = ext2fs_open(0, 0, 0, 0, msdos_io_manager, &fs);
  97.     if (err)
  98.         e2_err("Cannot open ext2 file system",err);
  99.  
  100.  
  101.     /* Loop until we have the root */
  102.     for (ino=cwdino; ino!=2; ino=dotdotino) {
  103.         /* Look up '..' */
  104.         err = ext2fs_namei(fs, 2, ino, "..", &dotdotino);
  105.         if (err)
  106.             e2_err("Cannot find '..'",err);
  107.  
  108.         err = ext2fs_read_inode(fs, dotdotino, &e2ino);
  109.         if (err)
  110.             e2_err("Cannot read '..'", err);
  111.  
  112.         if (!S_ISDIR(e2ino.i_mode)) {
  113.             fprintf(stderr,".. is not a directory\n");
  114.             exit(1);
  115.         }
  116.  
  117.         /* Loop through all directory entries */
  118.         err = ext2fs_dir_iterate(fs, dotdotino, 0, 0, myproc, 0);
  119.         if (err)
  120.             e2_err("Cannot read directory",err);
  121.     }
  122.  
  123.     printf("/%s\n",directory);
  124. }        
  125.  
  126.