[BACK]Return to dir.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/dir.c, Revision 1.20

1.20    ! jasper      1: /*     $OpenBSD: dir.c,v 1.19 2008/06/13 20:07:40 kjell Exp $  */
1.13      kjell       2:
                      3: /* This file is in the public domain. */
1.4       niklas      4:
1.1       deraadt     5: /*
                      6:  * Name:       MG 2a
                      7:  *             Directory management functions
                      8:  * Created:    Ron Flax (ron@vsedev.vse.com)
                      9:  *             Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
                     10:  */
                     11:
1.20    ! jasper     12: #include <sys/stat.h>
        !            13:
1.1       deraadt    14: #include "def.h"
                     15:
1.17      kjell      16: static char     mgcwd[NFILEN];
1.1       deraadt    17:
                     18: /*
1.12      db         19:  * Initialize anything the directory management routines need.
1.1       deraadt    20:  */
1.5       art        21: void
1.8       vincent    22: dirinit(void)
1.1       deraadt    23: {
1.17      kjell      24:        mgcwd[0] = '\0';
                     25:        if (getcwd(mgcwd, sizeof(mgcwd)) == NULL) {
1.10      vincent    26:                ewprintf("Can't get current directory!");
                     27:                chdir("/");
                     28:        }
1.19      kjell      29:        if (!(mgcwd[0] == '/' && mgcwd [1] == '\0'))
                     30:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
1.1       deraadt    31: }
                     32:
                     33: /*
1.12      db         34:  * Change current working directory.
1.1       deraadt    35:  */
1.3       millert    36: /* ARGSUSED */
                     37: int
1.8       vincent    38: changedir(int f, int n)
1.1       deraadt    39: {
1.17      kjell      40:        char    bufc[NFILEN], *bufp;
1.1       deraadt    41:
1.17      kjell      42:        (void)strlcpy(bufc, mgcwd, sizeof(bufc));
                     43:        if ((bufp = eread("Change default directory: ", bufc, NFILEN,
1.18      kjell      44:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.12      db         45:                return (ABORT);
1.14      kjell      46:        else if (bufp[0] == '\0')
                     47:                return (FALSE);
1.18      kjell      48:        /* Append trailing slash */
1.1       deraadt    49:        if (chdir(bufc) == -1) {
                     50:                ewprintf("Can't change dir to %s", bufc);
1.3       millert    51:                return (FALSE);
1.1       deraadt    52:        }
1.18      kjell      53:        if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL)
                     54:                panic("Can't get current directory!");
                     55:        if (mgcwd[strlen(mgcwd) - 1] != '/')
                     56:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
                     57:        ewprintf("Current directory is now %s", bufp);
                     58:        return (TRUE);
1.1       deraadt    59: }
                     60:
                     61: /*
1.12      db         62:  * Show current directory.
1.1       deraadt    63:  */
1.3       millert    64: /* ARGSUSED */
                     65: int
1.8       vincent    66: showcwdir(int f, int n)
1.1       deraadt    67: {
1.17      kjell      68:        ewprintf("Current directory: %s", mgcwd);
                     69:        return (TRUE);
                     70: }
                     71:
                     72: int
                     73: getcwdir(char *buf, size_t len)
                     74: {
                     75:        if (strlcpy(buf, mgcwd, len) >= len)
                     76:                return (FALSE);
                     77:
1.20    ! jasper     78:        return (TRUE);
        !            79: }
        !            80:
        !            81: /* Create the directory and it's parents. */
        !            82: /* ARGSUSED */
        !            83: int
        !            84: makedir(int f, int n)
        !            85: {
        !            86:        struct stat      sb;
        !            87:        int              finished, ishere;
        !            88:        mode_t           dir_mode, mode, oumask;
        !            89:        char             bufc[NFILEN], path[MAXPATHLEN];
        !            90:        char            *slash, *tpath, *fpath;
        !            91:
        !            92:        if (getbufcwd(bufc, sizeof(bufc)) != TRUE)
        !            93:                return (ABORT);
        !            94:        if ((tpath = eread("Make directory: ", bufc, NFILEN,
        !            95:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
        !            96:                return (ABORT);
        !            97:        else if (tpath[0] == '\0')
        !            98:                return (FALSE);
        !            99:
        !           100:        if ((fpath = expandtilde(tpath)) == NULL)
        !           101:                return (FALSE);
        !           102:
        !           103:        (void)strlcpy(path, fpath, sizeof(path));
        !           104:        free(fpath);
        !           105:
        !           106:        slash = path;
        !           107:        oumask = umask(0);
        !           108:        mode = 0777 & ~oumask;
        !           109:        dir_mode = mode | S_IWUSR | S_IXUSR;
        !           110:
        !           111:        for (;;) {
        !           112:                slash += strspn(slash, "/");
        !           113:                slash += strcspn(slash, "/");
        !           114:
        !           115:                finished = (*slash == '\0');
        !           116:                *slash = '\0';
        !           117:
        !           118:                ishere = !stat(path, &sb);
        !           119:                if (!finished && ishere && S_ISDIR(sb.st_mode)) {
        !           120:                        *slash = '/';
        !           121:                        continue;
        !           122:                }
        !           123:
        !           124:                if (mkdir(path, finished ? mode : dir_mode) == 0) {
        !           125:                        if (mode > 0777 && chmod(path, mode) < 0) {
        !           126:                                umask(oumask);
        !           127:                                return (ABORT);
        !           128:                        }
        !           129:                } else {
        !           130:                        if (!ishere || !S_ISDIR(sb.st_mode)) {
        !           131:                                if (!ishere)
        !           132:                                        ewprintf("Creating directory: "
        !           133:                                            "permission denied, %s", path);
        !           134:                                else
        !           135:                                        eerase();
        !           136:
        !           137:                                umask(oumask);
        !           138:                                return (FALSE);
        !           139:                        }
        !           140:                }
        !           141:
        !           142:                if (finished)
        !           143:                        break;
        !           144:
        !           145:                *slash = '/';
        !           146:        }
        !           147:
        !           148:        eerase();
        !           149:        umask(oumask);
1.3       millert   150:        return (TRUE);
1.1       deraadt   151: }