1 #include "mk.h" 2 3 void 4 initenv(void) 5 { 6 char *ss, *sn; 7 int envf, f; 8 Dir e[20]; 9 char nam[NAMELEN+5]; 10 int i, n, len; 11 Word *w; 12 13 envf = open("/env", OREAD); 14 if(envf < 0) 15 return; 16 while((n = dirread(envf, e, sizeof e)) > 0){ 17 n /= sizeof e[0]; 18 for(i = 0; i < n; i++){ 19 len = e[i].length; 20 if(*shname(e[i].name) != '\0' /* reject funny names */ 21 || len <= 0) /* and small variables */ 22 continue; 23 sprint(nam, "/env/%s", e[i].name); 24 f = open(nam, OREAD); 25 if(f < 0) 26 continue; 27 ss = Malloc(len+1); 28 if(read(f, ss, len) != len){ 29 perror(nam); 30 close(f); 31 continue; 32 } 33 close(f); 34 if (ss[len-1] == 0) 35 len--; 36 else 37 ss[len] = 0; 38 w = encodenulls(ss, len); 39 free(ss); 40 if (internalvar(e[i].name, w)) 41 free(w); /* dump only first word */ 42 else { 43 sn = strdup(e[i].name); 44 setvar(sn, (char *) w); 45 symlook(sn, S_EXPORTED, "")->value = ""; 46 } 47 } 48 } 49 close(envf); 50 } 51 52 void 53 exportenv(Envy *env, int n) 54 { 55 int hasvalue; 56 Symtab *sy; 57 char nam[NAMELEN+5]; 58 59 USED(n); 60 for (; env->name; env++) { 61 sy = symlook(env->name, S_VAR, 0); 62 if (env->values == 0 || env->values->s == 0 63 || env->values->s[0] == 0) 64 hasvalue = 0; 65 else 66 hasvalue = 1; 67 if(sy==0 && !hasvalue) /* non-existant null symbol */ 68 continue; 69 sprint(nam, "/env/%s", env->name); 70 if (sy != 0 && !hasvalue) { /* Remove from environment */ 71 /* we could remove it from the symbol table 72 * too, but we're in the child copy, and it 73 * would still remain in the parent's table. 74 */ 75 remove(nam); 76 env->values = 0; /* memory leak */ 77 continue; 78 } 79 export(nam, env->values); 80 } 81 } 82 83 /* change any nulls in the first n characters of s to 01's */ 84 Word * 85 encodenulls(char *s, int n) 86 { 87 Word *w, *head; 88 char *cp; 89 90 head = w = 0; 91 while (n-- > 0) { 92 for (cp = s; *cp && *cp != '\01'; cp++) 93 n--; 94 *cp = 0; 95 if (w) { 96 w->next = newword(s); 97 w = w->next; 98 } else 99 head = w = newword(s); 100 s = cp+1; 101 } 102 if (!head) 103 head = newword(""); 104 return head; 105 } 106 107 /* as well as 01's, change blanks to nulls, so that rc will 108 * treat the words as separate arguments 109 */ 110 void 111 export(char *name, Word *values) 112 { 113 int f; 114 int n; 115 116 f = create(name, OWRITE, 0666L); 117 if(f < 0) { 118 fprint(2, "can't create %s, f=%d\n", name, f); 119 perror(name); 120 return; 121 } 122 while (values) { 123 n = strlen(values->s); 124 if (n) { 125 if (write(f, values->s, n) != n) 126 perror(name); 127 if (write (f, "\0", 1) != 1) 128 perror(name); 129 } 130 values = values->next; 131 } 132 close(f); 133 } 134 135 void 136 dirtime(char *dir, char *path) 137 { 138 int i, fd, n; 139 char *t; 140 Dir db[32]; 141 char buf[4096]; 142 143 fd = open(dir, OREAD); 144 if(fd >= 0) { 145 while((n = dirread(fd, db, sizeof db)) > 0){ 146 n /= sizeof(Dir); 147 for(i = 0; i < n; i++){ 148 t = (char *)db[i].mtime; 149 if (!t) /* zero mode file */ 150 continue; 151 sprint(buf, "%s%s", path, db[i].name); 152 if(symlook(buf, S_TIME, 0)) 153 continue; 154 symlook(strdup(buf), S_TIME, t)->value = t; 155 } 156 } 157 close(fd); 158 } 159 } 160 161 getuid(void) 162 { 163 return 0; /* for now */ 164 } 165 166 getgid(void) 167 { 168 return 0; /* for now */ 169 } 170 171 int 172 waitfor(char *msg) 173 { 174 Waitmsg wm; 175 int pid; 176 177 pid = wait(&wm); 178 if(pid > 0) 179 strncpy(msg, wm.msg, ERRLEN); 180 return pid; 181 } 182 183 void 184 expunge(int pid, char *msg) 185 { 186 postnote(PNPROC, pid, msg); 187 } 188