Annotation of researchv8dc/cmd/asd/path.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *     getpath (file) - read a path name
        !             3:  *     putpath (file, path) - write a path name
        !             4:  *
        !             5:  *     These subroutines cater to the possibility of unprintable
        !             6:  *     characters in the path names being read or written, by
        !             7:  *     using the same sort of \ conventions commonly found in
        !             8:  *     C character constants.  The result of getpath is a pointer
        !             9:  *     to a static buffer whose contents will stay around no longer
        !            10:  *     than the next call to getpath.  When getpath is called, the
        !            11:  *     character about to be read from the input file must be the
        !            12:  *     first character of the path name.
        !            13:  */
        !            14: 
        !            15: #include <stdio.h>
        !            16: #include <ctype.h>
        !            17: #include "asd.h"
        !            18: 
        !            19: #define CHUNK 64
        !            20: 
        !            21: static char *r;
        !            22: static unsigned size;
        !            23: 
        !            24: char *
        !            25: getpath (file)
        !            26:        register FILE *file;
        !            27: {
        !            28:        register int c;
        !            29:        register int len = 0;
        !            30: 
        !            31:        c = getc (file);
        !            32: 
        !            33:        while (!isspace(c) && c != EOF) {
        !            34:                register int i = 0, n = 0;
        !            35: 
        !            36:                /* determine the next input character */
        !            37:                if (c == '\\') {
        !            38:                        c = getc (file);
        !            39:                        switch (c) {
        !            40: 
        !            41:                        case '\\':
        !            42:                                break;
        !            43:                        
        !            44:                        case 'n':
        !            45:                                c = '\n';
        !            46:                                break;
        !            47: 
        !            48:                        case 'r':
        !            49:                                c = '\r';
        !            50:                                break;
        !            51: 
        !            52:                        case 't':
        !            53:                                c = '\t';
        !            54:                                break;
        !            55: 
        !            56:                        case 'b':
        !            57:                                c = '\b';
        !            58:                                break;
        !            59:                        
        !            60:                        case 'f':
        !            61:                                c = '\f';
        !            62:                                break;
        !            63:                        
        !            64:                        case ' ':
        !            65:                             /* c = ' '; */
        !            66:                                break;
        !            67: 
        !            68:                        default:
        !            69:                                while (c >= '0' && c <= '7' && i < 3) {
        !            70:                                        n = (n << 3) + c - '0';
        !            71:                                        i++;
        !            72:                                        c = getc (file);
        !            73:                                }
        !            74:                                ungetc (c, file);
        !            75:                                c = n;
        !            76:                                break;
        !            77:                        }
        !            78:                }
        !            79: 
        !            80:                /* ensure there's room in the buffer */
        !            81:                if (len >= size)
        !            82:                        r = ralloc (r, size += CHUNK);
        !            83: 
        !            84:                /* put the character in the buffer */
        !            85:                r[len++] = c;
        !            86: 
        !            87:                /* read the next character */
        !            88:                c = getc (file);
        !            89:        }
        !            90: 
        !            91:        /* unless we hit eof, we read one character too far. */
        !            92:        if (c != EOF)
        !            93:                ungetc (c, file);
        !            94:        
        !            95:        /* put a final null into the buffer */
        !            96:        if (len >= size)
        !            97:                r = ralloc (r, size += CHUNK);
        !            98:        r[len] = '\0';
        !            99: 
        !           100:        return r;
        !           101: }
        !           102: 
        !           103: void
        !           104: putpath (file, path)
        !           105:        register FILE *file;
        !           106:        char *path;
        !           107: {
        !           108:        register char *p = path;
        !           109:        register int c;
        !           110:        
        !           111:        while ((c = *p++) != NULL) {
        !           112:                switch (c) {
        !           113: 
        !           114:                case '\n':
        !           115:                        fprintf (file, "\\n");
        !           116:                        break;
        !           117: 
        !           118:                case '\r':
        !           119:                        fprintf (file, "\\r");
        !           120:                        break;
        !           121: 
        !           122:                case '\b':
        !           123:                        fprintf (file, "\\b");
        !           124:                        break;
        !           125: 
        !           126:                case '\t':
        !           127:                        fprintf (file, "\\t");
        !           128:                        break;
        !           129: 
        !           130:                case '\f':
        !           131:                        fprintf (file, "\\f");
        !           132:                        break;
        !           133:                
        !           134:                case '\\':
        !           135:                        fprintf (file, "\\\\");
        !           136:                        break;
        !           137:                
        !           138:                case ' ':
        !           139:                        fprintf (file, "\\ ");
        !           140:                        break;
        !           141:                
        !           142:                default:
        !           143:                        if (iscntrl (c))
        !           144:                                fprintf (file,
        !           145:                                    *p >= '0' && *p <= '7'? "\\%.3o": "\\%o",
        !           146:                                    c);
        !           147:                        else
        !           148:                                putc (c, file);
        !           149:                        break;
        !           150:                }
        !           151:        }
        !           152: }

unix.superglobalmegacorp.com

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