Annotation of 43BSDReno/contrib/mh/uip/rmf.c, revision 1.1.1.1

1.1       root        1: /* rmf.c - remove a folder */
                      2: 
                      3: #include "../h/mh.h"
                      4: #include <stdio.h>
                      5: #include <sys/types.h>
                      6: #ifndef        BSD42
                      7: #ifndef        SYS5
                      8: #include <ndir.h>
                      9: #else  SYS5
                     10: #include <dir.h>
                     11: #endif SYS5
                     12: #else  BSD42
                     13: #include <sys/dir.h>
                     14: #endif BSD42
                     15: 
                     16: /*  */
                     17: 
                     18: static struct swit switches[] = {
                     19: #define        INTRSW  0
                     20:     "interactive", 0,
                     21: #define        NINTRSW 1
                     22:     "nointerative", 0,
                     23: 
                     24: #define        HELPSW  2
                     25:     "help", 4,
                     26: 
                     27:     NULL, NULL
                     28: };
                     29: 
                     30: 
                     31: static int rmf();
                     32: static void rma();
                     33: 
                     34: /*  */
                     35: 
                     36: /* ARGSUSED */
                     37: 
                     38: main(argc, argv)
                     39:        int argc;
                     40:        char *argv[];
                     41: {
                     42:     int     defolder = 0,
                     43:             interactive = -1;
                     44:     char   *cp,
                     45:            *folder = NULL,
                     46:             newfolder[BUFSIZ],
                     47:             buf[100],
                     48:           **ap,
                     49:           **argp,
                     50:            *arguments[MAXARGS];
                     51: 
                     52:     invo_name = r1bindex (argv[0], '/');
                     53:     if ((cp = m_find (invo_name)) != NULL) {
                     54:        ap = brkstring (cp = getcpy (cp), " ", "\n");
                     55:        ap = copyip (ap, arguments);
                     56:     }
                     57:     else
                     58:        ap = arguments;
                     59:     (void) copyip (argv + 1, ap);
                     60:     argp = arguments;
                     61: 
                     62: /*  */
                     63: 
                     64:     while (cp = *argp++) {
                     65:        if (*cp == '-')
                     66:            switch (smatch (++cp, switches)) {
                     67:                case AMBIGSW:
                     68:                    ambigsw (cp, switches);
                     69:                    done (1);
                     70:                case UNKWNSW:
                     71:                    adios (NULLCP, "-%s unknown", cp);
                     72:                case HELPSW:
                     73:                    (void) sprintf (buf, "%s [+folder] [switches]", invo_name);
                     74:                    help (buf, switches);
                     75:                    done (1);
                     76: 
                     77:                case INTRSW:
                     78:                    interactive = 1;
                     79:                    continue;
                     80:                case NINTRSW:
                     81:                    interactive = 0;
                     82:                    continue;
                     83:            }
                     84:        if (*cp == '+' || *cp == '@') {
                     85:            if (folder)
                     86:                adios (NULLCP, "only one folder at a time!");
                     87:            else
                     88:                folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
                     89:        }
                     90:        else
                     91:            adios (NULLCP, "usage: %s [+folder] [switches]", invo_name);
                     92:     }
                     93: 
                     94: /*  */
                     95: 
                     96:     if (!m_find ("path"))
                     97:        free (path ("./", TFOLDER));
                     98:     if (!folder) {
                     99:        folder = m_getfolder ();
                    100:        defolder++;
                    101:     }
                    102:     if (strcmp (m_mailpath (folder), pwd ()) == 0)
                    103:        adios (NULLCP, "sorry, you can't remove the current working directory");
                    104: 
                    105:     if (interactive == -1)
                    106:        interactive = defolder;
                    107: 
                    108:     if (index (folder, '/') && (*folder != '/') && (*folder != '.')) {
                    109:        for (cp = copy (folder, newfolder); cp > newfolder && *cp != '/'; cp--)
                    110:            continue;
                    111:        if (cp > newfolder)
                    112:            *cp = NULL;
                    113:        else
                    114:            (void) strcpy (newfolder, defalt);
                    115:     }
                    116:     else
                    117:        (void) strcpy (newfolder, defalt);
                    118: 
                    119:     if (interactive) {
                    120:        cp = concat ("Remove folder \"", folder, "\"? ", NULLCP);
                    121:        if (!getanswer (cp))
                    122:            done (0);
                    123:        free (cp);
                    124:     }
                    125: 
                    126:     if (rmf (folder) == OK && strcmp (m_find (pfolder), newfolder)) {
                    127:        printf ("[+%s now current]\n", newfolder);
                    128:        m_replace (pfolder, newfolder);
                    129:     }
                    130:     m_update ();
                    131: 
                    132:     done (0);
                    133: }
                    134: 
                    135: /*  */
                    136: 
                    137: static int
                    138: rmf(folder)
                    139:        register char *folder;
                    140: {
                    141:     int     i,
                    142:             j,
                    143:             others;
                    144:     register char  *maildir;
                    145:     char    cur[BUFSIZ];
                    146:     register struct direct *dp;
                    147:     register    DIR * dd;
                    148: 
                    149: #ifdef COMPAT
                    150:     (void) m_delete (concat (current, "-", m_mailpath (folder), NULLCP));
                    151: #endif COMPAT
                    152:     switch (i = chdir (maildir = m_maildir (folder))) {
                    153:        case OK:
                    154:            if (access (".", 2) != NOTOK && access ("..", 2) != NOTOK)
                    155:                break;          /* fall otherwise */
                    156: 
                    157:        case NOTOK:
                    158:            (void) sprintf (cur, "atr-%s-%s", current, m_mailpath (folder));
                    159:            if (!m_delete (cur)) {
                    160:                printf ("[+%s de-referenced]\n", folder);
                    161:                return OK;
                    162:            }
                    163:            advise (NULLCP, "you have no profile entry for the %s folder +%s",
                    164:                    i == NOTOK ? "unreadable" : "read-only", folder);
                    165:            return NOTOK;
                    166:     }
                    167: 
                    168:     if ((dd = opendir (".")) == NULL)
                    169:        adios (NULLCP, "unable to read folder +%s", folder);
                    170:     others = 0;
                    171: 
                    172:     j = strlen (SBACKUP);
                    173:     while (dp = readdir (dd)) {
                    174:        switch (dp -> d_name[0]) {
                    175:            case '.':
                    176:                if (strcmp (dp -> d_name, ".") == 0
                    177:                        || strcmp (dp -> d_name, "..") == 0)
                    178:                    continue;   /* else fall */
                    179: 
                    180:            case ',':
                    181: #ifdef MHE
                    182:            case '+':
                    183: #endif MHE
                    184: #ifdef UCI
                    185:            case '_':
                    186:            case '#':
                    187: #endif UCI
                    188:                break;
                    189: 
                    190:            default:
                    191:                if (m_atoi (dp -> d_name))
                    192:                    break;
                    193: #ifdef COMPAT
                    194:                if (strcmp (dp -> d_name, current) == 0)
                    195:                    break;
                    196: #endif COMPAT
                    197:                if (strcmp (dp -> d_name, LINK) == 0
                    198:                        || strncmp (dp -> d_name, SBACKUP, j) == 0)
                    199:                    break;
                    200: 
                    201:                admonish (NULLCP, "file \"%s/%s\" not deleted",
                    202:                        folder, dp -> d_name);
                    203:                others++;
                    204:                continue;
                    205:        }
                    206:        if (unlink (dp -> d_name) == NOTOK) {
                    207:            admonish (dp -> d_name, "unable to unlink %s:", folder);
                    208:            others++;
                    209:        }
                    210:     }
                    211: 
                    212:     closedir (dd);
                    213: 
                    214:     rma (folder);
                    215: 
                    216:     (void) chdir ("..");
                    217:     if (others == 0 && remdir (maildir))
                    218:        return OK;
                    219: 
                    220:     advise (NULLCP, "folder +%s not removed", folder);
                    221:     return NOTOK;
                    222: }
                    223: 
                    224: /*  */
                    225: 
                    226: static void
                    227: rma(folder)
                    228:        register char *folder;
                    229: {
                    230:     register int    alen,
                    231:                     j,
                    232:                     plen;
                    233:     register char  *cp;
                    234:     register struct node   *np,
                    235:                            *pp;
                    236: 
                    237:     alen = strlen ("atr-");
                    238:     plen = strlen (cp = m_mailpath (folder)) + 1;
                    239: 
                    240:     m_getdefs ();
                    241:     for (np = m_defs, pp = NULL; np; np = np -> n_next)
                    242:        if (ssequal ("atr-", np -> n_name)
                    243:                && (j = strlen (np -> n_name) - plen) > alen
                    244:                && *(np -> n_name + j) == '-'
                    245:                && strcmp (cp, np -> n_name + j + 1) == 0) {
                    246:            if (!np -> n_context)
                    247:                admonish (NULLCP, "bug: m_delete(key=\"%s\")", np -> n_name);
                    248:            if (pp) {
                    249:                pp -> n_next = np -> n_next;
                    250:                np = pp;
                    251:            }
                    252:            else
                    253:                m_defs = np -> n_next;
                    254:            ctxflags |= CTXMOD;
                    255:        }
                    256:        else
                    257:            pp = np;
                    258: }

unix.superglobalmegacorp.com

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