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

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