Lines Matching refs:ef

115 static int ef_record_pathname(ExpandFile *ef, const char *pathname,
117 static char *ef_cache_pathname(ExpandFile *ef, const char *pathname,
119 static void ef_clear_files(ExpandFile *ef);
121 static DirNode *ef_open_dir(ExpandFile *ef, const char *pathname);
122 static DirNode *ef_close_dir(ExpandFile *ef, DirNode *node);
123 static char *ef_expand_special(ExpandFile *ef, const char *path, int pathlen);
124 static int ef_match_relative_pathname(ExpandFile *ef, DirReader *dr,
164 ExpandFile *ef; /* The object to be returned */
168 ef = (ExpandFile *) malloc(sizeof(ExpandFile));
169 if(!ef) {
178 ef->err = NULL;
179 ef->sg = NULL;
180 ef->cache.mem = NULL;
181 ef->cache.head = NULL;
182 ef->cache.next = NULL;
183 ef->cache.tail = NULL;
184 ef->path = NULL;
185 ef->home = NULL;
186 ef->result.files = NULL;
187 ef->result.nfile = 0;
188 ef->usrnam[0] = '\0';
189 ef->envnam[0] = '\0';
193 ef->err = _new_ErrMsg();
194 if(!ef->err)
195 return del_ExpandFile(ef);
199 ef->sg = _new_StringGroup(_pu_pathname_dim());
200 if(!ef->sg)
201 return del_ExpandFile(ef);
205 ef->cache.mem = _new_FreeList(sizeof(DirNode), DIR_CACHE_BLK);
206 if(!ef->cache.mem)
207 return del_ExpandFile(ef);
211 ef->path = _new_PathName();
212 if(!ef->path)
213 return del_ExpandFile(ef);
217 ef->home = _new_HomeDir();
218 if(!ef->home)
219 return del_ExpandFile(ef);
223 ef->files_dim = MATCH_BLK_FACT;
224 ef->result.files = (char **) malloc(sizeof(ef->result.files[0]) *
225 ef->files_dim);
226 if(!ef->result.files) {
228 return del_ExpandFile(ef);
230 return ef;
237 * ef ExpandFile * The object to be deleted.
241 ExpandFile *del_ExpandFile(ExpandFile *ef)
243 if(ef) {
248 ef->sg = _del_StringGroup(ef->sg);
252 for(dnode=ef->cache.head; dnode; dnode=dnode->next)
258 ef->cache.mem = _del_FreeList(ef->cache.mem, 1);
259 ef->cache.head = ef->cache.tail = ef->cache.next = NULL;
263 ef->path = _del_PathName(ef->path);
267 ef->home = _del_HomeDir(ef->home);
271 if(ef->result.files) {
272 free(ef->result.files);
273 ef->result.files = NULL;
278 ef->err = _del_ErrMsg(ef->err);
282 free(ef);
321 * ef ExpandFile * The pathname expansion resource object.
348 FileExpansion *ef_expand_file(ExpandFile *ef, const char *path, int pathlen)
357 if(!ef || !path) {
358 if(ef) {
359 _err_record_msg(ef->err, "ef_expand_file: NULL path argument",
374 ef_clear_files(ef);
377 * using ef->path as a work directory and returning a pointer to
380 path = ef_expand_special(ef, path, pathlen);
386 _pn_clear_path(ef->path);
406 if(ef_record_pathname(ef, path, 1))
411 ef->result.exists = _pu_file_exists(ef->result.files[0]);
420 ef->result.exists = 1;
426 if(ef_record_pathname(ef, FS_ROOT_DIR, 0))
434 if(!_pn_append_to_path(ef->path, FS_ROOT_DIR, -1, 0)) {
435 _err_record_msg(ef->err, "Insufficient memory to record path",
446 dnode = ef_open_dir(ef, dirname);
452 if(ef_match_relative_pathname(ef, dnode->dr, path, 0)) {
453 dnode = ef_close_dir(ef, dnode);
459 dnode = ef_close_dir(ef, dnode);
464 if(ef->result.nfile < 1) {
465 _err_record_msg(ef->err, "No files match", END_ERR_MSG);
471 qsort(ef->result.files, ef->result.nfile, sizeof(ef->result.files[0]),
477 return &ef->result;
485 * ef ExpandFile * The pathname expansion resource object.
491 * directory to ef->pathname, insert a directory
498 static int ef_match_relative_pathname(ExpandFile *ef, DirReader *dr,
505 int pathlen; /* The length of ef->pathname[] on entry to this */
509 * ef->pathname[].
511 pathlen = strlen(ef->path->name);
533 if((separate && _pn_append_to_path(ef->path, FS_DIR_SEP, -1, 0)==NULL) ||
534 _pn_append_to_path(ef->path, file, -1, 0)==NULL) {
535 _err_record_msg(ef->err, "Insufficient memory to record path",
544 if(ef_record_pathname(ef, ef->path->name, 0))
552 } else if(_pu_path_is_dir(ef->path->name) &&
559 if(ef_record_pathname(ef, ef->path->name, 0))
565 DirNode *subdnode = ef_open_dir(ef, ef->path->name);
567 if(ef_match_relative_pathname(ef, subdnode->dr,
569 subdnode = ef_close_dir(ef, subdnode);
572 subdnode = ef_close_dir(ef, subdnode);
580 ef->path->name[pathlen] = '\0';
590 * ef ExpandFile * The filename-match resource object.
596 * 1 - Error (ef->err will contain a
599 static int ef_record_pathname(ExpandFile *ef, const char *pathname,
606 copy = ef_cache_pathname(ef, pathname, remove_escapes);
613 if(ef->result.nfile + 1 > ef->files_dim) {
614 int files_dim = ef->files_dim + MATCH_BLK_FACT;
615 char **files = (char **) realloc(ef->result.files,
618 _err_record_msg(ef->err,
624 ef->result.files = files;
625 ef->files_dim = files_dim;
630 ef->result.files[ef->result.nfile++] = copy;
638 * ef ExpandFile * The filename-match resource object.
645 * of the error is left in ef->err.
647 static char *ef_cache_pathname(ExpandFile *ef, const char *pathname,
650 char *copy = _sg_store_string(ef->sg, pathname, remove_escapes);
652 _err_record_msg(ef->err, "Insufficient memory to store pathname",
662 * ef ExpandFile * The pathname expansion resource object.
664 static void ef_clear_files(ExpandFile *ef)
666 _clr_StringGroup(ef->sg);
667 _pn_clear_path(ef->path);
668 ef->result.exists = 0;
669 ef->result.nfile = 0;
670 _err_clear_msg(ef->err);
678 * ef ExpandFile * The pathname expansion resource object.
682 * or NULL on error. On error, ef->err will
685 static DirNode *ef_open_dir(ExpandFile *ef, const char *pathname)
692 DirCache *cache = &ef->cache;
699 _err_record_msg(ef->err, "Insufficient memory to open a new directory",
714 _err_record_msg(ef->err, "Insufficient memory to open a new directory",
737 _err_record_msg(ef->err, errmsg, END_ERR_MSG);
765 * ef ExpandFile * The pathname expansion resource object.
771 static DirNode *ef_close_dir(ExpandFile *ef, DirNode *node)
776 DirCache *cache = &ef->cache;
1032 * ef->path as a work buffer, then copy the result into a cache entry,
1036 * ef ExpandFile * The resource object of the file matcher.
1041 * of the error is left in ef->err.
1043 static char *ef_expand_special(ExpandFile *ef, const char *path, int pathlen)
1054 _pn_clear_path(ef->path);
1073 if(spos < ppos && _pn_append_to_path(ef->path, path + spos, ppos-spos, 0)
1075 _err_record_msg(ef->err, "Insufficient memory to expand path",
1085 * ef->envnam[], stopping if a directory separator or end of string
1090 ef->envnam[envlen] = path[ppos++];
1097 _err_record_msg(ef->err, "Environment variable name too long",
1104 ef->envnam[envlen] = '\0';
1108 value = getenv(ef->envnam);
1110 _err_record_msg(ef->err, "No expansion found for: $", ef->envnam,
1117 if(_pn_append_to_path(ef->path, value, -1, 0) == NULL) {
1118 _err_record_msg(ef->err, "Insufficient memory to expand path",
1131 if(spos < ppos && _pn_append_to_path(ef->path, path + spos, ppos-spos, 0)
1133 _err_record_msg(ef->err, "Insufficient memory to expand path", END_ERR_MSG);
1140 pptr = ef->path->name;
1150 plen = strlen(ef->path->name);
1156 * Copy the optional username that follows the tilde into ef->usrnam[].
1160 ef->usrnam[usrlen] = *pptr++;
1167 _err_record_msg(ef->err, "Username too long", END_ERR_MSG);
1173 ef->usrnam[usrlen] = '\0';
1177 homedir = _hd_lookup_home_dir(ef->home, ef->usrnam);
1179 _err_record_msg(ef->err, _hd_last_home_dir_error(ef->home), END_ERR_MSG);
1199 if(_pn_resize_path(ef->path, plen - usrlen - 1 - skip + homelen)==NULL) {
1200 _err_record_msg(ef->err, "Insufficient memory to expand filename",
1208 memmove(ef->path->name + homelen,
1209 ef->path->name + 1 + usrlen + skip, plen - usrlen - 1 - skip+1);
1214 ef->path->name[i] = homedir[i];
1219 return ef_cache_pathname(ef, ef->path->name, 0);
1226 * ef ExpandFile * The path-expansion resource object.
1230 const char *ef_last_error(ExpandFile *ef)
1232 return ef ? _err_get_msg(ef->err) : "NULL ExpandFile argument";