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

  1. /*
  2.  * newdir.c --- create a new directory block
  3.  * 
  4.  * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13.  
  14. #include <linux/ext2_fs.h>
  15.  
  16. #include "ext2fs.h"
  17.  
  18. /*
  19.  * Create new directory block
  20.  */
  21. errcode_t ext2fs_new_dir_block(ext2_filsys fs, ino_t dir_ino, ino_t parent_ino,
  22.                    char **block)
  23. {
  24.     char    *buf;
  25.     struct ext2_dir_entry *dir = NULL;
  26.     int    rec_len;
  27.  
  28.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  29.  
  30.     buf = malloc(fs->blocksize);
  31.     if (!buf)
  32.         return ENOMEM;
  33.     memset(buf, 0, fs->blocksize);
  34.     dir = (struct ext2_dir_entry *) buf;
  35.     dir->rec_len = fs->blocksize;
  36.  
  37.     if (dir_ino) {
  38.         /*
  39.          * Set up entry for '.'
  40.          */
  41.         dir->inode = dir_ino;
  42.         dir->name_len = 1;
  43.         dir->name[0] = '.';
  44.         rec_len = dir->rec_len - EXT2_DIR_REC_LEN(dir->name_len);
  45.         dir->rec_len = EXT2_DIR_REC_LEN(dir->name_len);
  46.  
  47.         /*
  48.          * Set up entry for '..'
  49.          */
  50.         dir = (struct ext2_dir_entry *) (buf + dir->rec_len);
  51.         dir->rec_len = rec_len;
  52.         dir->inode = parent_ino;
  53.         dir->name_len = 2;
  54.         dir->name[0] = '.';
  55.         dir->name[1] = '.';
  56.         
  57.     }
  58.     *block = buf;
  59.     return 0;
  60. }
  61.