i have a code to write in C where i have to implement the mkdir, rm, rmdir and touch functions using double linked lists...im haveing trouble with it...here is what i have so far:
Expand|Select|Wrap|Line Numbers
- #ifndef _DIRECTORY_H
- #define _DIRECTORY_H
- /* tree node data structure declaration */
- struct entryNode {
- char * name;
- struct entryNode * next; /* sibling */
- struct entryNode * previous; /* sibling */
- int isDirectory;
- struct entryNode * parent;
- union {
- char * contents; /* if this node is a file, this contains the content of the file */
- struct entryNode * entryList; /* if this node is a directory,
- this contains a pointer to the first child inside the directory */
- } entry;
- };
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "directory.h"
- #define TRUE 1
- #define FALSE 0
- #define MAX_TEXT_SIZE 8192
- /* used as a temp storage for the text content of a file */
- char tmpbuf[MAX_TEXT_SIZE];
- /* pointer to the root node of the file system */
- struct entryNode * root;
- /* Helper functions */
- void pwdHelper (struct entryNode *); /* don't worry about this one */
- /* Return a pointer to the entry with the given name in the given list,
- or NULL if no such entry exists. */
- /* basically it searches for the file/dir name at the current level:
- * it does not go into directories */
- struct entryNode * located (char * name, struct entryNode * list) {
- if (list == NULL) {
- return NULL;
- } else if (strcmp (list->name, name) == 0) {
- return list;
- } else {
- return located (name, list->next);
- }
- }
- /* implements the "touch" command (one argument; not in standard UNIX) */
- /* wd is a pointer to the current working directory */
- void touchFile (struct entryNode * wd, char * fileName)
- {
- struct entryNode * newFile;
- if (located (fileName, wd->entry.entryList))
- {
- printf ("touch: %s: File exists\n", fileName);
- return;
- }
- /* initialize temporary text buffer to zeros */
- bzero(tmpbuf,MAX_TEXT_SIZE);
- /* HELP NEEDED HERE*/
- }
- /* implements the "mkdir" command (one argument; no options) */
- void createDir (struct entryNode * wd, char * dirName) {
- struct entryNode * newDir;
- if (located (dirName, wd->entry.entryList))
- {
- printf ("mkdir: %s: File exists\n", dirName);
- return;
- }
- /*struct entryNode * newDir = (char *) malloc(sizeof(newDir));*/
- newDir->entry.entryList;
- newDir->next = NULL;
- /*newDir->previous = dirName;*/
- /*dirName->next = newDir;*/
- struct entryNode * entryList = NULL;
- /*HELP NEEDED WITH BOLDED TEXT*/
- }
- /* implements the "rm" command (one argument, unlike standard UNIX; no options) */
- void removeFile (struct entryNode * wd, char * fileName) {
- struct entryNode * file;
- file = located (fileName, wd->entry.entryList);
- if (file == NULL)
- {
- printf ("rm: %s: No such file or directory.\n", fileName);
- return;
- }
- else if (file->isDirectory)
- {
- printf ("rm: %s: is a directory.\n", fileName);
- return;
- }
- /*HELP NEEDED HERE*/
- }
- /* implements the "rmdir" command (one argument, unlike standard UNIX; no options) */
- void removeDir (struct entryNode * wd, char * dirName) {
- struct entryNode * dir;
- struct entryNode * tmp;
- dir = located (dirName, wd->entry.entryList);
- if (dir == NULL)
- {
- printf ("rmdir: %s: No such file or directory.\n", dirName);
- return;
- }
- else if (!dir->isDirectory)
- {
- printf ("rmdir: %s: Not a directory.\n", dirName);
- return;
- }
- else if (dir->name[0] =='/')
- {
- printf("rmdir: Cannot delete root directory.\n");
- return;
- }
- [b]else
- {
- previousfile = file->previous;
- nextfile = file->next;
- if (previousfile==NULL)
- {
- printf("%s: No Previous File");
- if (nextfile == NULL)
- {
- printf("%s: No Next File");
- }
- free(file);
- wd->entry.entrylit = NULL;
- }
- else if
- {
- if (nextfile == NULL)
- {
- printf("%s: No Next File");
- if (previousfile==NULL)
- {
- printf("%s: No Previous File");
- }
- free(file);
- wd->entry.entrylit = NULL;
- }
- }
- else
- {
- free(file);
- wd->entry.entrylit = NULL;
- }
- }
- /* HELP NEEDED WITH BOLDED TEXT */
- /* implements the "mv" command (two arguments, unlike standard UNIX; no options) */
- void moveEntry (struct entryNode * wd, char * from, char * to) {
- struct entryNode * fromNode;
- struct entryNode * toNode;
- fromNode = located (from, wd->entry.entryList);
- if (fromNode == NULL)
- {
- printf ("mv: %s: No such file or directory.\n", from);
- return;
- }
- /*HELP NEEDED HERE */
- }