home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / thesrc15.zip / directry.c < prev    next >
C/C++ Source or Header  |  1993-11-17  |  11KB  |  406 lines

  1. /***********************************************************************/
  2. /* DIRECTRY.C - Directory routines                                     */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991-1993 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                     email: M.Hessling@gu.edu.au
  33.  * 36 David Road                     Phone: +61 7 849 7731
  34.  * Holland Park                      Fax:   +61 7 875 5314
  35.  * QLD 4121
  36.  * Australia
  37.  */
  38.  
  39. /*
  40. $Header: C:\THE\RCS\directry.c 1.4 1993/09/01 16:26:06 MH Interim MH $
  41. */
  42.  
  43. #ifdef __OS2__
  44. #   define INCL_DOS
  45. #endif
  46. #include "the.h"
  47. #include "directry.h"
  48. #include "fnmatch.h"
  49. #include "proto.h"
  50.  
  51. static ATTR_TYPE curr_dirtype = (F_DI | F_AR);
  52. /*********************************************************************/
  53. #ifdef PROTO
  54. char *make_full(char *path, char *file)
  55. #else
  56. char *make_full(path, file)
  57. char *path, *file;
  58. #endif
  59. /*********************************************************************/
  60. {
  61.  static char filebuf[BUFSIZ];
  62.  int pathlen=strlen(path);
  63.  
  64.  if (pathlen+1+strlen(file)+1 > BUFSIZ)
  65.     return(NULL);
  66.  if (!strcmp(path, "") || !strcmp(path, "."))
  67.    {
  68.     (void) strcpy(filebuf, file);
  69.     return(filebuf);
  70.    }
  71.  (void) strcpy(filebuf, path);
  72.  if (*(path+(pathlen - 1)) != ISLASH && *file != ISLASH)
  73.     (void) strcat(filebuf, ISTR_SLASH);
  74.  (void) strcat(filebuf, file);
  75.  return(filebuf);
  76. }
  77. /*********************************************************************/
  78. #if defined(UNIX)
  79. #ifdef PROTO
  80. int getfiles(char *path,char *files,struct dirfile **dpfirst,
  81.                                     struct dirfile **dplast)
  82. #else
  83. int getfiles(path,files,dpfirst,dplast)
  84. char *path;
  85. char *files;
  86. struct dirfile **dpfirst;
  87. struct dirfile **dplast;
  88. #endif
  89. /*********************************************************************/
  90. {
  91.  DIR *dirp;
  92.  struct stat sp;
  93.  struct dirent *direntp;
  94.  struct dirfile *dp;
  95.  char *full_name;
  96.  int entries = 10;
  97.  
  98.  dirp = opendir(path);
  99.  if (dirp == NULL)
  100.     return(10);
  101.  
  102.  dp = *dpfirst = (struct dirfile *)calloc(entries, sizeof(struct dirfile));
  103.  if (dp == NULL)
  104.     return(30);
  105.  *dplast = *dpfirst + entries;
  106.  
  107.  for (direntp = readdir(dirp);direntp != NULL;direntp = readdir(dirp))
  108.     {
  109.      if (fnmatch(files,direntp->d_name,0) == 0)
  110.        {
  111.         if ((full_name = make_full(path,(char *)direntp->d_name)) == NULL)
  112.            return(30);
  113.         if (stat(full_name,&sp) != 0)
  114.            continue;
  115.         if ((dp->fname = (char *)calloc(strlen(direntp->d_name)+1,sizeof(char))) == NULL)
  116.            return(30);
  117.         strcpy(dp->fname,direntp->d_name);
  118.         dp->fattr = sp.st_mode;
  119.         dp->ftime = sp.st_mtime;
  120.         dp->fsize = sp.st_size;
  121.     dp->fdate = 0;
  122.         dp++;
  123.         if (dp == *dplast)
  124.           {
  125.            *dpfirst = (struct dirfile *)realloc((char *)*dpfirst,
  126.                       2 * entries * sizeof (struct dirfile));
  127.            if (*dpfirst == NULL)
  128.               return(30);
  129.            dp = *dpfirst + entries;
  130.            *dplast = dp + entries;
  131.            entries *= 2;
  132.           }
  133.        }
  134.     }
  135.  closedir(dirp);
  136.  *dplast = dp;
  137.  return(0);
  138. }
  139. #else
  140. /*********************************************************************/
  141. #ifdef PROTO
  142. int getfiles(char *path,char *files,struct dirfile **dpfirst,
  143.                                     struct dirfile **dplast)
  144. #else
  145. int getfiles(path,files,dpfirst,dplast)
  146. char *path;
  147. char *files;
  148. struct dirfile **dpfirst;
  149. struct dirfile **dplast;
  150. #endif
  151. /*********************************************************************/
  152. {
  153.  struct dirfile *dp;
  154. #ifdef OS2
  155. #  ifdef __32BIT__
  156.  ULONG matches=1L;
  157.  ULONG rsvrd=FIL_STANDARD;
  158.  FILEFINDBUF3 ffblk;
  159. #  else
  160.  USHORT matches=1;
  161.  ULONG rsvrd=0;
  162.  FILEFINDBUF ffblk;
  163. #  endif
  164.  HDIR hdir=HDIR_SYSTEM;
  165. #else
  166.  FSTR_TYPE ffblk;
  167. #endif
  168.  ATTR_TYPE attrs = curr_dirtype;
  169.  DONE_TYPE done;
  170.  char *full_path;
  171.  char str_attr[11];
  172.  char str_date[10];
  173.  char str_time[6];
  174.  int entries = 10;
  175.  
  176.  if ((full_path = make_full(path,"*.*")) == NULL)
  177.        return(RC_FILE_NOT_FOUND);
  178.  
  179. #if defined(DOS) && defined(TC)
  180.  done = findfirst(full_path,&ffblk,attrs);
  181. #endif
  182. #if defined (DOS) && defined(MSC)
  183.  done = _dos_findfirst(full_path,attrs,&ffblk);
  184. #endif
  185. #if defined(DOS) && defined(GO32)
  186.  done = findfirst(full_path,&ffblk,attrs);
  187. #endif
  188. #ifdef OS2
  189. #  ifdef __32BIT__
  190.  done = DosFindFirst((PSZ) full_path, (PHDIR)&hdir, (ULONG)attrs,
  191.              (PVOID)&ffblk, (ULONG)sizeof(ffblk), (PULONG)&matches,
  192.              (ULONG)rsvrd);
  193. #  else
  194.  done = DosFindFirst((PSZ) full_path, (PHDIR)&hdir, (USHORT)attrs,
  195.              (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk), (PUSHORT)&matches,
  196.              (ULONG)rsvrd);
  197. #  endif
  198. #endif
  199.  if (done != 0)
  200.     return(RC_FILE_NOT_FOUND);
  201.  
  202.  dp = *dpfirst = (struct dirfile *)calloc(entries, sizeof (struct dirfile));
  203.  *dplast = *dpfirst + entries;
  204.  
  205.  
  206.  while(!done)
  207.     {
  208.      if (fnmatch(files,ffblk.NAME_NAME,FNM_IGNORECASE) == 0)
  209.        {
  210.     if ((dp->fname = (char *)calloc(strlen(ffblk.NAME_NAME)+1,sizeof(char))) == NULL)
  211.        return(RC_OUT_OF_MEMORY);
  212.     strcpy(dp->fname,ffblk.NAME_NAME);
  213.     dp->fattr = ffblk.ATTR_NAME;
  214.     dp->ftime = ffblk.TIME_NAME;
  215.     dp->fsize = ffblk.SIZE_NAME;
  216.     dp->fdate = ffblk.DATE_NAME;
  217.     dp++;
  218.     if (dp == *dplast)
  219.       {
  220.            *dpfirst = (struct dirfile *)realloc((char *)*dpfirst,
  221.                       2 * entries * sizeof (struct dirfile));
  222.            if (*dpfirst == NULL)
  223.               return(RC_FILE_NOT_FOUND);
  224.            dp = *dpfirst + entries;
  225.            *dplast = dp + entries;
  226.            entries *= 2;
  227.           }
  228.        }
  229. #if defined(DOS) && defined(TC)
  230.      done = findnext(&ffblk);
  231. #endif
  232. #if defined(DOS) && defined(MSC)
  233.      done = _dos_findnext(&ffblk);
  234. #endif
  235. #if defined(DOS) && defined(GO32)
  236.      done = findnext(&ffblk);
  237. #endif
  238. #ifdef OS2
  239. #  ifdef __32BIT__
  240.      done = DosFindNext((HDIR)hdir, (PVOID)&ffblk, (ULONG)sizeof(ffblk),
  241.             (PULONG)&matches);
  242. #  else
  243.      done = DosFindNext((HDIR)hdir, (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk),
  244.             (PUSHORT)&matches);
  245. #  endif
  246. #endif
  247.     }
  248.  *dplast = dp;
  249.  return(RC_OK);
  250. }
  251. #endif
  252. /*********************************************************************/
  253. #ifdef PROTO
  254. int fcomp(struct dirfile *first,struct dirfile* next)
  255. #else
  256. int fcomp(first,next)
  257. struct dirfile *first;
  258. struct dirfile* next;
  259. #endif
  260. /*********************************************************************/
  261. {
  262. #ifdef OS2
  263.  return(stricmp(first->fname,next->fname));
  264. #else
  265.  return(strcmp(first->fname,next->fname));
  266. #endif
  267. }
  268. /*********************************************************************/
  269. #ifdef PROTO
  270. char *file_date(D_TYPE date,char *str_date)
  271. #else
  272. char *file_date(date,str_date)
  273. D_TYPE date;
  274. char *str_date;
  275. #endif
  276. /*********************************************************************/
  277. {
  278.  static char mon[12][4] = {
  279.  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  280.  sprintf(str_date,"%2d-%3.3s-%2.2d",DAYS_MASK,MONT_MASK,YEAR_MASK);
  281.  return(str_date);
  282. }
  283. /*********************************************************************/
  284. #ifdef PROTO
  285. char *file_time(T_TYPE time,char *str_time)
  286. #else
  287. char *file_time(time,str_time)
  288. T_TYPE time;
  289. char *str_time;
  290. #endif
  291. /*********************************************************************/
  292. {
  293.  sprintf(str_time,"%2d:%2.2d",HOUR_MASK,MINU_MASK);
  294.  return(str_time);
  295. }
  296. /*********************************************************************/
  297. #ifdef PROTO
  298. char *file_attrs(ATTR_TYPE attrs,char *str_attr)
  299. #else
  300. char *file_attrs(attrs,str_attr)
  301. ATTR_TYPE attrs;
  302. char *str_attr;
  303. #endif
  304. /*********************************************************************/
  305. {
  306. #ifdef UNIX
  307.  ATTR_TYPE ftype=(attrs & S_IFMT);
  308.  
  309.  str_attr[10] = '\0';
  310.  switch(ftype)
  311.    {
  312.     case S_IFDIR:  str_attr[0] = 'd'; break;
  313.     case S_IFCHR:  str_attr[0] = 'c'; break;
  314.     case S_IFIFO:  str_attr[0] = 'p'; break;
  315. #if !defined(ATT) && !defined(M_XENIX)
  316.     case S_IFSOCK: str_attr[0] = 's'; break;
  317.     case S_IFLNK:  str_attr[0] = 'l'; break;
  318. #endif
  319.     default:       str_attr[0] = '-'; break;
  320.    }
  321.  str_attr[1] = (attrs & S_IRUSR) ? 'r' : '-';
  322.  str_attr[2] = (attrs & S_IWUSR) ? 'w' : '-';
  323.  str_attr[3] = (attrs & S_IXUSR) ? 'x' : '-';
  324.  str_attr[3] = (attrs & S_ISUID) ? 's' : str_attr[3];
  325.  str_attr[4] = (attrs & S_IRGRP) ? 'r' : '-';
  326.  str_attr[5] = (attrs & S_IWGRP) ? 'w' : '-';
  327.  str_attr[6] = (attrs & S_IXGRP) ? 'x' : '-';
  328.  str_attr[6] = (attrs & S_ISGID) ? 's' : str_attr[6];
  329.  str_attr[7] = (attrs & S_IROTH) ? 'r' : '-';
  330.  str_attr[8] = (attrs & S_IWOTH) ? 'w' : '-';
  331.  str_attr[9] = (attrs & S_IXOTH) ? 'x' : '-';
  332. #else
  333.  strcpy(str_attr,".... ");
  334.  if ((attrs & F_RO) == F_RO)
  335.    str_attr[0] = 'r';
  336.  if ((attrs & F_AR) == F_AR)
  337.    str_attr[1] = 'a';
  338.  if ((attrs & F_SY) == F_SY)
  339.    str_attr[2] = 's';
  340.  if ((attrs & F_HI) == F_HI)
  341.    str_attr[3] = 'h';
  342.  str_attr[4] = '\0';
  343.  if ((attrs & F_DI) == F_DI)
  344.    strcat(str_attr,"(dir) ");
  345.  else
  346.    strcat(str_attr,"      ");
  347. #endif
  348.  return(str_attr);
  349. }
  350. /*********************************************************************/
  351. #ifdef PROTO
  352. int set_dirtype(char *params)
  353. #else
  354. int set_dirtype(params)
  355. char *params;
  356. #endif
  357. /***********************************************************************/
  358. {
  359. /*--------------------------- local data ------------------------------*/
  360. #define NUM_DIRTYPE 5
  361.  static char *dirtype[NUM_DIRTYPE] =
  362.          {(char *)"normal",
  363.           (char *)"readonly",
  364.           (char *)"system",
  365.           (char *)"hidden",
  366.           (char *)"directory"};
  367.  static ATTR_TYPE att[NUM_DIRTYPE] =
  368.          {0,F_RO,F_SY,F_HI,F_DI};
  369.  char *p;
  370.  register int i;
  371.  ATTR_TYPE attribs=0;
  372.  bool found;
  373. /*--------------------------- processing ------------------------------*/
  374.  if (strcmp(params,"") == 0)                 /* error if no paramaters */
  375.    {
  376.     display_error(3,(char *)"");
  377.     return(RC_INVALID_OPERAND);
  378.    }
  379.  if (strcmp(params,"*") == 0)                            /* set to ALL */
  380.    {
  381.     curr_dirtype = (F_RO | F_HI | F_SY | F_DI | F_AR);
  382.     return(RC_OK);
  383.    }
  384.  p = (char *)strtok(params," ");
  385.  while(p != NULL)
  386.    {
  387.     found = FALSE;
  388.     for (i=0;i<NUM_DIRTYPE;i++)
  389.        {
  390.         if (equal(dirtype[i],p,1))
  391.           {
  392.            found = TRUE;
  393.            attribs |= att[i];
  394.           }
  395.        }
  396.     if (!found)
  397.       {
  398.        display_error(1,(char *)p);
  399.        return(RC_INVALID_OPERAND);
  400.       }
  401.     p = (char *)strtok(NULL," ");
  402.    }
  403.  curr_dirtype = attribs;
  404.  return(RC_OK);
  405.  }
  406.