Annotation of cci/usr/src/ucb/rtar/tarrmt.c, revision 1.1.1.1

1.1       root        1: static char *sccsid = "@(#)dumprmt.c   1.6 (Berkeley) 7/1/83";
                      2: 
                      3: #include <sys/param.h>
                      4: #include <sys/mtio.h>
                      5: #include <sys/ioctl.h>
                      6: 
                      7: #include <netinet/in.h>
                      8: 
                      9: #include <stdio.h>
                     10: #include <netdb.h>
                     11: #include <pwd.h>
                     12: 
                     13: #define        TS_CLOSED       0
                     14: #define        TS_OPEN         1
                     15: 
                     16: static int rmtstate = TS_CLOSED;
                     17: int    rmtape;
                     18: int    rmtconnaborted();
                     19: char   *rmtpeer;
                     20: struct         passwd  *getpwuid();
                     21: 
                     22: /*---------------------------------------------------------------------
                     23:  *     Changes to this file:
                     24:  *
                     25:  *     08/15/85 - Add rmtcreat to implement remote tape dev creation.
                     26:  *              - Get user's name instead of assuming that he is root.
                     27:  *                This modification is neccessary when this program is
                     28:  *                used by rtar.  (Peter Pham)
                     29:  *--------------------------------------------------------------------*/
                     30: 
                     31: rmthost(host)
                     32:        char *host;
                     33: {
                     34: 
                     35:        rmtpeer = host;
                     36:        signal(SIGPIPE, rmtconnaborted);
                     37:        rmtgetconn();
                     38:        if (rmtape < 0)
                     39:                exit(1);
                     40: }
                     41: 
                     42: rmtconnaborted()
                     43: {
                     44: 
                     45:        fprintf(stderr, "Lost connection to remote host.\n");
                     46:        exit(1);
                     47: }
                     48: 
                     49: rmtgetconn()
                     50: {
                     51:        static struct servent *sp = 0;
                     52:        struct passwd *pwd;
                     53: 
                     54:        if (sp == 0) {
                     55:                sp = getservbyname("shell", "tcp");
                     56:                if (sp == 0) {
                     57:                        fprintf(stderr, "rdump: shell/tcp: unknown service\n");
                     58:                        exit(1);
                     59:                }
                     60:        }
                     61:        pwd = (struct passwd *)getpwuid(getuid());
                     62:        if (pwd == 0) {
                     63:                fprintf(stderr, "Who are you?\n");
                     64:                exit(1);
                     65:        }
                     66:         rmtape = rcmd(&rmtpeer, sp->s_port, pwd->pw_name,
                     67:                        pwd->pw_name, "/etc/rmt", 0);
                     68: }
                     69: 
                     70: rmtopen(tape, mode)
                     71:        char *tape;
                     72:        int mode;
                     73: {
                     74:        char buf[256];
                     75: 
                     76:        sprintf(buf, "O%s\n%d\n", tape, mode);
                     77:        rmtcall(tape, buf);
                     78:        rmtstate = TS_OPEN;
                     79: }
                     80: 
                     81: rmtclose()
                     82: {
                     83: 
                     84:        if (rmtstate != TS_OPEN)
                     85:                return;
                     86:        rmtcall("close", "C\n");
                     87:        rmtstate = TS_CLOSED;
                     88: }
                     89: 
                     90: rmtcreat(tape, mode)
                     91:        char *tape;
                     92:        int mode;
                     93: {
                     94:        char buf[256];
                     95: 
                     96:        sprintf(buf, "N%s\n%d\n", tape, mode);
                     97:        rmtcall(tape, buf);
                     98:        rmtstate = TS_OPEN;
                     99: }
                    100: 
                    101: rmtread(buf, count)
                    102:        char *buf;
                    103:        int count;
                    104: {
                    105:        char line[30];
                    106:        int n, i, cc;
                    107:        extern errno;
                    108: 
                    109:        sprintf(line, "R%d\n", count);
                    110:        n = rmtcall("read", line);
                    111:        if (n < 0) {
                    112:                errno = n;
                    113:                return (-1);
                    114:        }
                    115:        for (i = 0; i < n; i += cc) {
                    116:                cc = read(rmtape, buf+i, n - i);
                    117:                if (cc <= 0) {
                    118:                        rmtconnaborted();
                    119:                }
                    120:        }
                    121:        return (n);
                    122: }
                    123: 
                    124: rmtwrite(buf, count)
                    125:        char *buf;
                    126:        int count;
                    127: {
                    128:        char line[30];
                    129: 
                    130:        sprintf(line, "W%d\n", count);
                    131:        write(rmtape, line, strlen(line));
                    132:        write(rmtape, buf, count);
                    133:        return (rmtreply("write"));
                    134: }
                    135: 
                    136: rmtwrite0(count)
                    137:        int count;
                    138: {
                    139:        char line[30];
                    140: 
                    141:        sprintf(line, "W%d\n", count);
                    142:        write(rmtape, line, strlen(line));
                    143: }
                    144: 
                    145: rmtwrite1(buf, count)
                    146:        char *buf;
                    147:        int count;
                    148: {
                    149: 
                    150:        write(rmtape, buf, count);
                    151: }
                    152: 
                    153: rmtwrite2()
                    154: {
                    155:        int i;
                    156: 
                    157:        return (rmtreply("write"));
                    158: }
                    159: 
                    160: rmtseek(offset, pos)
                    161:        int offset, pos;
                    162: {
                    163:        char line[80];
                    164: 
                    165:        sprintf(line, "L%d\n%d\n", offset, pos);
                    166:        return (rmtcall("seek", line));
                    167: }
                    168: 
                    169: struct mtget mts;
                    170: 
                    171: struct mtget *
                    172: rmtstatus()
                    173: {
                    174:        register int i;
                    175:        register char *cp;
                    176: 
                    177:        if (rmtstate != TS_OPEN)
                    178:                return (0);
                    179:        if (rmtcall("status", "S\n") < 0)
                    180:                return (0);
                    181:        for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
                    182:                *cp++ = rmtgetb();
                    183:        return (&mts);
                    184: }
                    185: 
                    186: rmtioctl(cmd, count)
                    187:        int cmd, count;
                    188: {
                    189:        char buf[256];
                    190: 
                    191:        if (count < 0)
                    192:                return (-1);
                    193:        sprintf(buf, "I%d\n%d\n", cmd, count);
                    194:        return (rmtcall("ioctl", buf));
                    195: }
                    196: 
                    197: rmtcall(cmd, buf)
                    198:        char *cmd, *buf;
                    199: {
                    200:        if (write(rmtape, buf, strlen(buf)) != strlen(buf))
                    201:                rmtconnaborted();
                    202:        return (rmtreply(cmd));
                    203: }
                    204: 
                    205: rmtreply(cmd)
                    206:        char *cmd;
                    207: {
                    208:        register int c;
                    209:        char code[30], emsg[BUFSIZ];
                    210: 
                    211:        rmtgets(code, sizeof (code));
                    212:        if (*code == 'E' || *code == 'F') {
                    213:                rmtgets(emsg, sizeof (emsg));
                    214:                msg("rmpreply: %s: %s - %d\n", cmd, emsg, atoi(code + 1));
                    215:                if (*code == 'F') {
                    216:                        rmtstate = TS_CLOSED;
                    217:                        return (-1);
                    218:                }
                    219:                return (-1);
                    220:        }
                    221:        if (*code != 'A') {
                    222:                msg("Protocol to remote tape server botched (code %s?).\n",
                    223:                    code);
                    224:                rmtconnaborted();
                    225:        }
                    226:        return (atoi(code + 1));
                    227: }
                    228: 
                    229: rmtgetb()
                    230: {
                    231:        char c;
                    232: 
                    233:        if (read(rmtape, &c, 1) != 1) 
                    234:                rmtconnaborted();
                    235:        return (c);
                    236: }
                    237: 
                    238: rmtgets(cp, len)
                    239:        char *cp;
                    240:        int len;
                    241: {
                    242: 
                    243:        while (len > 1) {
                    244:                *cp = rmtgetb();
                    245:                if (*cp == '\n') {
                    246:                        cp[1] = 0;
                    247:                        return;
                    248:                }
                    249:                cp++;
                    250:                len--;
                    251:        }
                    252:        msg("Protocol to remote tape server botched (in rmtgets).\n");
                    253:        rmtconnaborted();
                    254: }
                    255: 
                    256: #ifdef RTAR
                    257: msg(cp, a1, a2, a3)
                    258:        char *cp;
                    259: {
                    260: 
                    261:        fprintf(stderr, cp, a1, a2, a3);
                    262: }
                    263: #endif RTAR

unix.superglobalmegacorp.com

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