|
|
1.1 ! root 1: /* file.c - DS.INI file and path stuff */ ! 2: ! 3: #include <direct.h> ! 4: #include <dos.h> ! 5: #include <stdio.h> ! 6: #include <stdlib.h> ! 7: ! 8: #include "ds.h" ! 9: #include "vars.h" ! 10: ! 11: ! 12: /* External procedures */ ! 13: ! 14: extern newDir (Directory **, char *, Directory *); ! 15: ! 16: ! 17: /* Local variables */ ! 18: ! 19: static char initFile[MAX_PATH_LEN]; /* Initialization file */ ! 20: static FILE *iF; /* Init file handle */ ! 21: ! 22: static int i_level; /* Directory level */ ! 23: static char i_name[FILE_NAME_LEN]; /* Directory name */ ! 24: ! 25: ! 26: /*** fileInit - initialize file stuff ! 27: * ! 28: * ! 29: */ ! 30: fileInit () ! 31: { ! 32: char pLastChar; ! 33: int pLast; ! 34: char *iPath; ! 35: ! 36: RootPath = getcwd (NULL,MAX_PATH_LEN); /* Get current working directory */ ! 37: if (!RootPath) { ! 38: perror ("\nCouldn't get current working directory"); ! 39: exitError (2); ! 40: } ! 41: strcpy (IntRootPath,RootPath); ! 42: pLast = strlen(IntRootPath) - 1; /* Index of last character */ ! 43: pLastChar = IntRootPath[pLast]; /* Last character of root path */ ! 44: if ((pLastChar == '\\') || (pLastChar == '/')) ! 45: IntRootPath[pLast] = '\0'; /* Get rid of path separator */ ! 46: ! 47: iPath = getenv (INIT_SYMBOL); ! 48: if (!iPath) { ! 49: fprintf (stderr, "\n%s symbol not set in environment.\n",INIT_SYMBOL); ! 50: fprintf (stderr, ! 51: "Put SET %s=drive:path in your AUTOEXEC.BAT.\n",INIT_SYMBOL); ! 52: exitError (2); ! 53: } ! 54: strcpy (initFile,iPath); /* Copy path */ ! 55: pLast = strlen(initFile) - 1; /* Index of last character */ ! 56: pLastChar = initFile[pLast]; /* Last character of root path */ ! 57: if ((pLastChar != '\\') && (pLastChar != '/')) ! 58: strcat (initFile,"\\"); /* Append path separator */ ! 59: strcat (initFile,INIT_FILE); /* Append file name and extension */ ! 60: } /* fileInit */ ! 61: ! 62: ! 63: /*** saveState - save options to init file ! 64: * ! 65: * ! 66: */ ! 67: saveState () ! 68: { ! 69: int i; ! 70: ! 71: iF = fopen (initFile, "w"); ! 72: if (!iF) { ! 73: fprintf (stderr, "\n%s", initFile); ! 74: perror ("\nCannot write to initialization file"); ! 75: exitError (2); ! 76: } ! 77: ! 78: fprintf (iF,INIT_FILE_HEADER); ! 79: for (i=0; i<N_COLORS; i++) ! 80: fprintf (iF, " %x", color[i]); ! 81: fprintf (iF, "\n"); ! 82: ! 83: fprintf (iF, "%s\n", IntRootPath); /* Write out root directory */ ! 84: writeTree (root->d_child, 1); /* Write out tree */ ! 85: fprintf (iF, "0 The_End\n"); /* Terminate tree */ ! 86: ! 87: if (fclose (iF) == EOF) { ! 88: fprintf (stderr,"\nCannot close initialization file"); ! 89: exitError (2); ! 90: } ! 91: ! 92: /* Return to original directory */ ! 93: ! 94: if (chdir(RootPath) == -1) { ! 95: fprintf (stderr, "\nDirectory %s", RootPath); ! 96: perror ("\nCannot change directory"); ! 97: exitError (2); ! 98: } ! 99: } /* saveState */ ! 100: ! 101: ! 102: /*** writeTree - write out directory tree ! 103: * ! 104: * ! 105: */ ! 106: writeTree (root, level) ! 107: Directory *root; /* Root of some subtree */ ! 108: int level; /* Distance of this root to top root */ ! 109: { ! 110: Directory *p; /* Used to traverse tree */ ! 111: ! 112: p = root; ! 113: while (p) { /* Process directories at this level */ ! 114: writeNode (p,level); /* Write out node information */ ! 115: if (p->d_child) /* Process subtree */ ! 116: writeTree (p->d_child, level+1); ! 117: p = p->d_next; ! 118: } /* while */ ! 119: } /* writeTree */ ! 120: ! 121: ! 122: /*** writeNode - write out a directory node ! 123: * ! 124: * ! 125: */ ! 126: writeNode (p, level) ! 127: Directory *p; /* Directory node to write out */ ! 128: int level; /* Distance of this node to top root */ ! 129: { ! 130: fprintf (iF, "%d %s\n", level, p->d_name); ! 131: ! 132: } /* writeNode */ ! 133: ! 134: ! 135: /*** getState - get options from init file ! 136: * ! 137: * EXIT: rCode = true, tree WAS read from init file ! 138: * rCode = false, tree NOT read from init file ! 139: * ! 140: */ ! 141: getState () ! 142: { ! 143: int i; ! 144: int rCode; ! 145: ! 146: rCode = TRUE; ! 147: iF = fopen (initFile, "r"); ! 148: if (!iF) /* File not opened */ ! 149: return (FALSE); ! 150: ! 151: if (fscanf(iF,INIT_FILE_HEADER) == EOF) ! 152: return (FALSE); ! 153: ! 154: for (i=0; i<N_COLORS; i++) { ! 155: rCode = fscanf(iF, " %x", &color[i]); ! 156: if (rCode != 1) /* Bad data */ ! 157: i = N_COLORS; /* Exit loop */ ! 158: } ! 159: ! 160: if (fscanf(iF,"%s\n", IntRootPath) == EOF) ! 161: return (FALSE); ! 162: ! 163: newDir (&root,IntRootPath,0); /* Make root node */ ! 164: rCode = readTree (root); ! 165: ! 166: if (fclose (iF) == EOF) { ! 167: fprintf (stderr,"\nCannot close initialization file\n"); ! 168: exitError (2); ! 169: } ! 170: ! 171: return (rCode); ! 172: ! 173: } /* getState */ ! 174: ! 175: ! 176: /*** readTree - read in directory tree ! 177: * ! 178: * EXIT: rCode = true, tree WAS read from init file ! 179: * rCode = false, tree NOT read from init file ! 180: * ! 181: */ ! 182: readTree (root) ! 183: Directory *root; /* Root of some subtree */ ! 184: { ! 185: int level; /* Current level */ ! 186: Directory *p; /* Used to build tree */ ! 187: Directory *n; /* Used to build tree */ ! 188: int rCode; /* Return code */ ! 189: ! 190: p = root; /* Root is parent node */ ! 191: level = 0; /* Level of root */ ! 192: if (!readNode()) /* Failed to read first node */ ! 193: return (FALSE); ! 194: ! 195: while (i_level) { ! 196: if (i_level > level ) { /* Subtree */ ! 197: level++; /* Increase level */ ! 198: newDir (&n, i_name, p); /* Create new node */ ! 199: p->d_child = n; /* Point parent to child */ ! 200: } ! 201: else { /* Sibling or higher up tree */ ! 202: while (i_level < level) { /* Backup, if necessary */ ! 203: level--; /* Decrease level */ ! 204: p = p->d_parent; /* Backup to parent */ ! 205: } ! 206: newDir (&n, i_name, p->d_parent); /* Create new node */ ! 207: p->d_next = n; /* Point previous to new */ ! 208: n->d_prev = p; /* Point new to previous */ ! 209: } ! 210: p = n; /* New parent node */ ! 211: if (!readNode()) /* Failed to read next node */ ! 212: return (FALSE); ! 213: } /* while */ ! 214: return (TRUE); /* Read entire tree successfully */ ! 215: } /* readTree */ ! 216: ! 217: ! 218: /*** readNode - read in a directory node ! 219: * ! 220: * ! 221: */ ! 222: readNode () ! 223: { ! 224: int rCode; ! 225: ! 226: rCode = fscanf (iF, "%d %s\n", &i_level, i_name); ! 227: if (rCode == 2) /* Read succeeded */ ! 228: rCode = TRUE; ! 229: else /* Read failed */ ! 230: rCode = FALSE; ! 231: ! 232: return (rCode); /* Return status */ ! 233: } /* readNode */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.