[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.30

1.30    ! florian     1: /*     $OpenBSD: dir.c,v 1.29 2016/09/12 18:32:54 millert 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.28      bcallah    12: #include <sys/queue.h>
1.20      jasper     13: #include <sys/stat.h>
1.28      bcallah    14: #include <signal.h>
                     15: #include <stdio.h>
                     16: #include <string.h>
                     17: #include <unistd.h>
1.20      jasper     18:
1.1       deraadt    19: #include "def.h"
                     20:
1.17      kjell      21: static char     mgcwd[NFILEN];
1.1       deraadt    22:
                     23: /*
1.12      db         24:  * Initialize anything the directory management routines need.
1.1       deraadt    25:  */
1.5       art        26: void
1.8       vincent    27: dirinit(void)
1.1       deraadt    28: {
1.17      kjell      29:        mgcwd[0] = '\0';
1.29      millert    30:        if (getcwd(mgcwd, sizeof(mgcwd)) == NULL)
1.10      vincent    31:                ewprintf("Can't get current directory!");
1.29      millert    32:        if (mgcwd[0] != '\0' && !(mgcwd[0] == '/' && mgcwd[1] == '\0'))
1.19      kjell      33:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
1.1       deraadt    34: }
                     35:
                     36: /*
1.12      db         37:  * Change current working directory.
1.1       deraadt    38:  */
1.3       millert    39: /* ARGSUSED */
                     40: int
1.8       vincent    41: changedir(int f, int n)
1.1       deraadt    42: {
1.17      kjell      43:        char    bufc[NFILEN], *bufp;
1.1       deraadt    44:
1.17      kjell      45:        (void)strlcpy(bufc, mgcwd, sizeof(bufc));
                     46:        if ((bufp = eread("Change default directory: ", bufc, NFILEN,
1.18      kjell      47:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.12      db         48:                return (ABORT);
1.14      kjell      49:        else if (bufp[0] == '\0')
                     50:                return (FALSE);
1.18      kjell      51:        /* Append trailing slash */
1.1       deraadt    52:        if (chdir(bufc) == -1) {
1.25      lum        53:                dobeep();
1.1       deraadt    54:                ewprintf("Can't change dir to %s", bufc);
1.3       millert    55:                return (FALSE);
1.1       deraadt    56:        }
1.29      millert    57:        if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL) {
                     58:                if (bufc[0] == '/')
                     59:                        (void)strlcpy(mgcwd, bufc, sizeof(mgcwd));
                     60:                else
                     61:                        (void)strlcat(mgcwd, bufc, sizeof(mgcwd));
                     62:        }
1.18      kjell      63:        if (mgcwd[strlen(mgcwd) - 1] != '/')
                     64:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
1.29      millert    65:        ewprintf("Current directory is now %s", mgcwd);
1.18      kjell      66:        return (TRUE);
1.1       deraadt    67: }
                     68:
                     69: /*
1.12      db         70:  * Show current directory.
1.1       deraadt    71:  */
1.3       millert    72: /* ARGSUSED */
                     73: int
1.8       vincent    74: showcwdir(int f, int n)
1.1       deraadt    75: {
1.17      kjell      76:        ewprintf("Current directory: %s", mgcwd);
                     77:        return (TRUE);
                     78: }
                     79:
                     80: int
                     81: getcwdir(char *buf, size_t len)
                     82: {
                     83:        if (strlcpy(buf, mgcwd, len) >= len)
                     84:                return (FALSE);
                     85:
1.20      jasper     86:        return (TRUE);
                     87: }
                     88:
                     89: /* Create the directory and it's parents. */
                     90: /* ARGSUSED */
                     91: int
                     92: makedir(int f, int n)
                     93: {
1.27      lum        94:        return (ask_makedir());
1.23      lum        95: }
                     96:
                     97: int
1.27      lum        98: ask_makedir(void)
1.23      lum        99: {
                    100:
1.22      lum       101:        char             bufc[NFILEN];
1.27      lum       102:        char            *path;
1.20      jasper    103:
                    104:        if (getbufcwd(bufc, sizeof(bufc)) != TRUE)
                    105:                return (ABORT);
1.22      lum       106:        if ((path = eread("Make directory: ", bufc, NFILEN,
1.20      jasper    107:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
                    108:                return (ABORT);
1.22      lum       109:        else if (path[0] == '\0')
1.20      jasper    110:                return (FALSE);
1.27      lum       111:
                    112:        return (do_makedir(path));
                    113: }
                    114:
                    115: int
                    116: do_makedir(char *path)
                    117: {
                    118:        struct stat      sb;
                    119:        int              finished, ishere;
1.30    ! florian   120:        mode_t           dir_mode, f_mode, oumask;
1.27      lum       121:        char            *slash;
1.20      jasper    122:
1.22      lum       123:        if ((path = adjustname(path, TRUE)) == NULL)
1.20      jasper    124:                return (FALSE);
                    125:
1.24      florian   126:        /* Remove trailing slashes */
                    127:        slash = strrchr(path, '\0');
                    128:        while (--slash > path && *slash == '/')
                    129:                *slash = '\0';
1.26      lum       130:
                    131:        slash = path;
1.20      jasper    132:
                    133:        oumask = umask(0);
1.30    ! florian   134:        f_mode = 0777 & ~oumask;
        !           135:        dir_mode = f_mode | S_IWUSR | S_IXUSR;
1.20      jasper    136:
                    137:        for (;;) {
                    138:                slash += strspn(slash, "/");
                    139:                slash += strcspn(slash, "/");
                    140:
                    141:                finished = (*slash == '\0');
                    142:                *slash = '\0';
                    143:
                    144:                ishere = !stat(path, &sb);
1.21      lum       145:                if (finished && ishere) {
1.25      lum       146:                        dobeep();
1.21      lum       147:                        ewprintf("Cannot create directory %s: file exists",
                    148:                             path);
                    149:                        return(FALSE);
                    150:                } else if (!finished && ishere && S_ISDIR(sb.st_mode)) {
1.20      jasper    151:                        *slash = '/';
                    152:                        continue;
                    153:                }
                    154:
1.30    ! florian   155:                if (mkdir(path, finished ? f_mode : dir_mode) == 0) {
        !           156:                        if (f_mode > 0777 && chmod(path, f_mode) < 0) {
1.20      jasper    157:                                umask(oumask);
                    158:                                return (ABORT);
                    159:                        }
                    160:                } else {
                    161:                        if (!ishere || !S_ISDIR(sb.st_mode)) {
1.25      lum       162:                                if (!ishere) {
                    163:                                        dobeep();
1.20      jasper    164:                                        ewprintf("Creating directory: "
                    165:                                            "permission denied, %s", path);
1.25      lum       166:                                } else
1.20      jasper    167:                                        eerase();
                    168:
                    169:                                umask(oumask);
                    170:                                return (FALSE);
                    171:                        }
                    172:                }
                    173:
                    174:                if (finished)
                    175:                        break;
                    176:
                    177:                *slash = '/';
                    178:        }
                    179:
                    180:        eerase();
                    181:        umask(oumask);
1.3       millert   182:        return (TRUE);
1.1       deraadt   183: }