home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / E2CD2.C < prev    next >
C/C++ Source or Header  |  1995-05-10  |  2KB  |  89 lines

  1. /***************************************************************************
  2.  * e2cd2.c - generate .BAT file for environment change
  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 <string.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12.  
  13. #include "ext2_fs.h"
  14. #include "ext2fs/ext2fs.h"
  15. #include "ldisk.h"
  16. #include "istat.h"
  17. #include "e2err.h"
  18.  
  19. extern io_manager msdos_io_manager;
  20.  
  21.  
  22. /**********************************************************************
  23.  * usage prints usage information and exits
  24.  **********************************************************************/
  25.  
  26. void
  27. usage()
  28. {
  29.     fprintf(stderr, "usage: e2cd directory\n");
  30.     exit(1);
  31. }
  32.  
  33.  
  34. /**********************************************************************
  35.  * main routine
  36.  **********************************************************************/
  37.  
  38. main(int argc, char **argv)
  39. {
  40.     int err;
  41.     char env[80], *pos1, *pos2;
  42.     ext2_filsys fs;
  43.     ino_t ino;
  44.     struct ext2_inode e2ino;
  45.     FILE *ofile;
  46.  
  47.     if (argc!=2)
  48.         usage();
  49.  
  50.     /* Open file system */
  51.     err = ext2fs_open(0, 0, 0, 0, msdos_io_manager, &fs);
  52.     if (err)
  53.         e2_err("Cannot open ext2 file system",err);
  54.  
  55.     /* Lookup specified name */
  56.     err = ext2fs_namei(fs, 2, cwdino, argv[1], &ino);
  57.     if (err)
  58.         e2_err("Cannot find file",err);
  59.  
  60.     /* Read specified inode */
  61.     err = ext2fs_read_inode(fs, ino, &e2ino);
  62.     if (err)
  63.         e2_err("Cannot read inode information",err);
  64.  
  65.     /* Is it a directory? */
  66.     if (!S_ISDIR(e2ino.i_mode)) {
  67.         fprintf(stderr,"%s is not a directory\n",argv[1]);
  68.         exit(1);
  69.     }
  70.  
  71.     ofile=fopen("___e2cd.bat","w");
  72.     if (!ofile) {
  73.         fprintf(stderr,"Cannot create ___e2cd.bat\n");
  74.         exit(1);
  75.     }
  76.  
  77.     /* We know E2CWD exists, ext2fs_open already checked that */
  78.     strcpy(env,getenv("E2CWD")); /* We are going to modify it, so we make a copy */
  79.     pos1=strchr(env,':');
  80.     pos2=strchr(pos1+1,':');
  81.     if (pos2)
  82.         *pos2=0;
  83.  
  84.     fprintf(ofile,"SET E2CWD=%s:%d\n",env,ino);
  85.     fclose(ofile);
  86.     
  87.     return 0;
  88. }
  89.