xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/string/strtape.c (revision 9b664393d4fdda96221e6ea9ea95790d3c15be70)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * local device pathname for portable tape unit specification is returned
25  * if e is non-null then it is set to the next unused char in s
26  *
27  *	<unit><density>[<no-rewind>]
28  *	{0-7}[l,m,h,u,c][n]
29  */
30 
31 #include <ast.h>
32 
33 char*
34 strtape(register const char* s, register char** e)
35 {
36 	int		mtunit = '0';
37 	int		mtdensity = 0;
38 	char		mtrewind[2];
39 	char		mtbehavior[2];
40 
41 	static char	tapefile[sizeof("/dev/Xrmt/123456789")];
42 
43 	mtrewind[0] = mtrewind[1] = mtbehavior[0] = mtbehavior[1] = 0;
44 	for (;;)
45 	{
46 		switch (*s)
47 		{
48 		case '0':
49 		case '1':
50 		case '2':
51 		case '3':
52 		case '4':
53 		case '5':
54 		case '6':
55 		case '7':
56 			mtunit = *s++;
57 			continue;
58 		case 'b':
59 		case 'v':
60 			mtbehavior[0] = *s++;
61 			continue;
62 		case 'l':
63 		case 'm':
64 		case 'h':
65 		case 'u':
66 		case 'c':
67 			mtdensity = *s++;
68 			continue;
69 		case 'n':
70 			mtrewind[0] = *s++;
71 			continue;
72 		}
73 		break;
74 	}
75 	if (e) *e = (char*)s;
76 	if (!access("/dev/rmt/.", F_OK))
77 	{
78 		/*
79 		 * system V
80 		 */
81 
82 		if (!mtdensity) mtdensity = 'm';
83 		sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt/ctape%c%s", mtunit, mtrewind);
84 		if (!access(tapefile, F_OK)) return(tapefile);
85 		for (;;)
86 		{
87 			sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt/%c%c%s%s", mtunit, mtdensity, mtbehavior, mtrewind);
88 			if (!access(tapefile, F_OK)) return(tapefile);
89 			if (!mtbehavior[0]) break;
90 			mtbehavior[0] = 0;
91 		}
92 	}
93 	else if (!access("/dev/nst0", F_OK))
94 	{
95 		/*
96 		 * linux
97 		 */
98 
99 		sfsprintf(tapefile, sizeof(tapefile), "/dev/%sst%c", mtrewind, mtunit);
100 	}
101 	else if (!access("/dev/nrmt0", F_OK))
102 	{
103 		/*
104 		 * 9th edition
105 		 */
106 
107 		switch (mtdensity)
108 		{
109 		case 'l':
110 			mtunit = '0';
111 			break;
112 		case 'm':
113 			mtunit = '1';
114 			break;
115 		case 'h':
116 			mtunit = '2';
117 			break;
118 		}
119 		sfsprintf(tapefile, sizeof(tapefile), "/dev/%srmt%c", mtrewind, mtunit);
120 	}
121 	else
122 	{
123 		/*
124 		 * BSD
125 		 */
126 
127 		mtunit -= '0';
128 		switch (mtdensity)
129 		{
130 		case 'l':
131 			break;
132 		case 'h':
133 			mtunit |= 020;
134 			break;
135 		default:
136 			mtunit |= 010;
137 			break;
138 		}
139 		switch (mtrewind[0])
140 		{
141 		case 'n':
142 			mtunit |= 040;
143 			break;
144 		}
145 		sfsprintf(tapefile, sizeof(tapefile), "/dev/rmt%d", mtunit);
146 	}
147 	return(tapefile);
148 }
149