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

1.19    ! kjell       1: /*     $OpenBSD: dir.c,v 1.18 2007/05/28 17:52:17 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:
                     12: #include "def.h"
                     13:
1.17      kjell      14: static char     mgcwd[NFILEN];
1.1       deraadt    15:
                     16: /*
1.12      db         17:  * Initialize anything the directory management routines need.
1.1       deraadt    18:  */
1.5       art        19: void
1.8       vincent    20: dirinit(void)
1.1       deraadt    21: {
1.17      kjell      22:        mgcwd[0] = '\0';
                     23:        if (getcwd(mgcwd, sizeof(mgcwd)) == NULL) {
1.10      vincent    24:                ewprintf("Can't get current directory!");
                     25:                chdir("/");
                     26:        }
1.19    ! kjell      27:        if (!(mgcwd[0] == '/' && mgcwd [1] == '\0'))
        !            28:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
1.1       deraadt    29: }
                     30:
                     31: /*
1.12      db         32:  * Change current working directory.
1.1       deraadt    33:  */
1.3       millert    34: /* ARGSUSED */
                     35: int
1.8       vincent    36: changedir(int f, int n)
1.1       deraadt    37: {
1.17      kjell      38:        char    bufc[NFILEN], *bufp;
1.1       deraadt    39:
1.17      kjell      40:        (void)strlcpy(bufc, mgcwd, sizeof(bufc));
                     41:        if ((bufp = eread("Change default directory: ", bufc, NFILEN,
1.18      kjell      42:            EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
1.12      db         43:                return (ABORT);
1.14      kjell      44:        else if (bufp[0] == '\0')
                     45:                return (FALSE);
1.18      kjell      46:        /* Append trailing slash */
1.1       deraadt    47:        if (chdir(bufc) == -1) {
                     48:                ewprintf("Can't change dir to %s", bufc);
1.3       millert    49:                return (FALSE);
1.1       deraadt    50:        }
1.18      kjell      51:        if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL)
                     52:                panic("Can't get current directory!");
                     53:        if (mgcwd[strlen(mgcwd) - 1] != '/')
                     54:                (void)strlcat(mgcwd, "/", sizeof(mgcwd));
                     55:        ewprintf("Current directory is now %s", bufp);
                     56:        return (TRUE);
1.1       deraadt    57: }
                     58:
                     59: /*
1.12      db         60:  * Show current directory.
1.1       deraadt    61:  */
1.3       millert    62: /* ARGSUSED */
                     63: int
1.8       vincent    64: showcwdir(int f, int n)
1.1       deraadt    65: {
1.17      kjell      66:        ewprintf("Current directory: %s", mgcwd);
                     67:        return (TRUE);
                     68: }
                     69:
                     70: int
                     71: getcwdir(char *buf, size_t len)
                     72: {
                     73:        if (strlcpy(buf, mgcwd, len) >= len)
                     74:                return (FALSE);
                     75:
1.3       millert    76:        return (TRUE);
1.1       deraadt    77: }