Annotation of os2sdk/demos/apps/ds/file.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.