1: #include "hd.h"
2: #include "mydir.h"
3: #include "strings.h"
4: #include "classify.h"
5:
6: /* File gets a file name, converts it into a full path name, and
7: selects that file. */
8:
9: file (argv) char **argv; { /* Select specific file */
10:
11: char nname [STRMAX];
12:
13: if (*argv == CNULL) putmsg ("File: ");
14: if (getfname (*argv, nname) == FAILURE) return FAILURE;
15:
16: return (enterfile (nname));
17: }
18:
19: /* Create accepts a file name, creats that file, and then enters it. */
20: create () {
21:
22: char nname [STRMAX], oname [STRMAX];
23: register char ch; char *cp; int class;
24:
25: putmsg ("Create: ");
26: if (getfname (CNULL, nname) == FAILURE) return FAILURE;
27:
28: if (!access (nname, 0)) {
29: putmsg (nname); printf (": Already exists");
30: return FAILURE;
31: }
32:
33: todotdot (nname);
34: class = classify (nname);
35:
36: if (class == CL_NULL || access (nname, 3)) {
37: myperror (nname); return FAILURE;
38: }
39: else if (class != CL_DIR) {
40: putmsg (nname); printf (": Not a directory");
41: return FAILURE;
42: }
43:
44: /* Un do effect of todotdot */
45: for (cp = nname; *cp++;);
46: *--cp = '/';
47:
48: erase (); printf ("Creating %s\n\nSelect:\n\n", nname);
49:
50: printf
51: ("1 Text\n2 Directory\n3 Copy a file\n4 Link a file\n\n");
52:
53: ch = getch (); putch (LF);
54: if (ch == '1') {
55: f_exec (EDITOR, EDITOR, nname, 0);
56: }
57: else if (ch == '2') {
58: if (f_exec ("/bin/mkdir", "mkdir", nname, 0)) getrtn ();
59: return enterdir (nname) | REPLOT;
60: }
61: else if (ch == '3' || ch == '4') {
62: printf ("From: ");
63: if (getfname (CNULL, oname) == FAILURE) return REPLOT;
64:
65: switch (classify (oname)) {
66:
67: case CL_NULL: case CL_PROTPLN:
68: myperror (oname); break;
69:
70: case CL_DIR:
71: printf ("Cannot copy or link to directories");
72: break;
73:
74: default:
75: if (ch == '3') f_exec
76: ("/bin/cp", "cp", oname, nname, 0);
77: if (ch == '4') {
78: printf ("Linking\n");
79: if (link (oname, nname))
80: myperror ("Link failed");
81: }
82: at (1501);
83: if (!access (nname, 0)) {
84: printf ("File created.\
85: Do you wish to examine it? ");
86: if (getch() == 'y') {
87: putch (LF);
88: return
89: enterfile (nname) | REPLOT;
90: }
91: return REPLOT;
92: }
93: }
94: at (1501);
95: printf ("File not created. ");
96: getrtn ();
97: }
98: return REPLOT;
99: }
100:
101: /* Getfname takes two character arrays as parameters.
102: Inname is the partial pathname of a file. If inname == CNULL,
103: getfname will instead read the partial pathname from the terminal.
104: The full pathname is returned in outname.
105: Getfname has a return value of SUCCESS or FAILURE.
106: */
107: getfname (inname, outname) char *inname, *outname; {
108: char inword [STRMAX]; int ilen;
109:
110: if (inname == CNULL) {
111: tty_push (COOKEDMODE);
112: ilen = getword (inword);
113: tty_pop ();
114: if (ilen <= 0) {
115: clearmsg (0);
116: return FAILURE;
117: }
118: inname = inword;
119: }
120: if (pathgen (wdname, inname, outname)) {
121: putmsg ("Path name too long");
122: return FAILURE;
123: }
124: return SUCCESS;
125: }
126:
127: /* If one is in dir with pathname "old", and does a chdir "change",
128: one ends up in directory "new". Exception: ".." always breaks
129: through to the root.
130: */
131: pathgen (old, change, new) char *old, *change, *new; {
132:
133: char element [DIRSIZ + 1];
134: char chgbuf [STRMAX];
135: register len;
136:
137: if (change [0] == '/') strcpy (new, SLASH);
138: else strcpy (new, old);
139:
140: strcpy (chgbuf, change); /* So change is not modified */
141:
142: while (*chgbuf) {
143:
144: extract (element, chgbuf);
145: if (compe (DOT, element));
146: else if (compe (DOTDOT, element)) todotdot (new);
147:
148: else {
149: len = strlen (new);
150: if (len > LPLEN) return 1;
151: else if (len > 1) strcat (new, SLASH);
152: strcat (new, element);
153: }
154: }
155: return 0;
156: }
157:
158: (element, path) char *element, *path; {
159:
160: register char *cp;
161: int eltlen;
162:
163: for (cp = path; *cp != 0 && *cp != '/'; cp++);
164:
165: eltlen = cp - path;
166: if (eltlen == 0) {
167: strcpy (element, DOT);
168: }
169: else {
170: strncpy (element, path, DIRSIZ);
171: element [min (eltlen, DIRSIZ)] = 0;
172: }
173: if (*cp) shift (path, eltlen + 1);
174: else path [0] = 0;
175: }
176:
177: shift (path, length) char *path; int length; {
178:
179: register char *cp;
180:
181: for (cp = path + length; cp [-1];) *path++ = *cp++;
182: }