Enlightenment CVS committal

Author  : kwo
Project : e16
Module  : e

Dir     : e16/e/src


Modified Files:
        actions.c file.c file.h main.c menus-misc.c menus.c theme.c 


Log Message:
Refactor various file property test functions.

===================================================================
RCS file: /cvs/e/e16/e/src/actions.c,v
retrieving revision 1.212
retrieving revision 1.213
diff -u -3 -r1.212 -r1.213
--- actions.c   13 Jan 2007 19:14:26 -0000      1.212
+++ actions.c   15 Jan 2007 02:43:53 -0000      1.213
@@ -67,11 +67,8 @@
 
    sh = usershell(getuid());
 
-   path = pathtoexec(exe);
-   if (path)
+   if (path_canexec(exe))
      {
-       Efree(path);
-
        real_exec = Emalloc(strlen(params) + 6);
        if (!real_exec)
           return -1;
@@ -83,7 +80,7 @@
 
    if (!Mode.wm.startup)
      {
-       path = pathtofile(exe);
+       path = path_test(exe, EFILE_ANY);
        if (!path)
          {
             /* absolute path */
===================================================================
RCS file: /cvs/e/e16/e/src/file.c,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -3 -r1.77 -r1.78
--- file.c      13 Jan 2007 19:14:27 -0000      1.77
+++ file.c      15 Jan 2007 02:43:53 -0000      1.78
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
+ * Copyright (C) 2007 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -123,113 +124,41 @@
    unlink(s);
 }
 
-#if 0                          /* Unused */
-void
-E_cp(const char *s, const char *ss)
-{
-   int                 i;
-   FILE               *f, *ff;
-   unsigned char       buf[1];
-
-   if ((!s) || (!ss) || (!*s) || (!*ss))
-      return;
-   if (!exists(s))
-      return;
-   i = filesize(s);
-   f = fopen(s, "r");
-   if (!f)
-      return;
-   ff = fopen(ss, "w");
-   if (!ff)
-     {
-       fclose(f);
-       return;
-     }
-   while (fread(buf, 1, 1, f))
-      fwrite(buf, 1, 1, ff);
-   fclose(f);
-   fclose(ff);
-}
-#endif
-
-#if 0                          /* Unused */
-char               *
-cwd(void)
-{
-   char               *s;
-   char                ss[FILEPATH_LEN_MAX];
-
-   getcwd(ss, FILEPATH_LEN_MAX);
-   s = Estrdup(ss);
-   return s;
-}
-#endif
-
 int
-exists(const char *s)
+file_test(const char *s, unsigned int test)
 {
    struct stat         st;
+   int                 mode;
 
-   if ((!s) || (!*s))
-      return 0;
-   if (stat(s, &st) < 0)
-      return 0;
-   return 1;
-}
-
-int
-isdir(const char *s)
-{
-   struct stat         st;
-
-   if ((!s) || (!*s))
-      return 0;
-   if (stat(s, &st) < 0)
-      return 0;
-   if (S_ISDIR(st.st_mode))
-      return 1;
-   return 0;
-}
-
-int
-isfile(const char *s)
-{
-   struct stat         st;
-
-   if ((!s) || (!*s))
-      return 0;
-   if (stat(s, &st) < 0)
-      return 0;
-   if (S_ISREG(st.st_mode))
-      return 1;
-   return 0;
-}
-
-int
-canread(const char *s)
-{
-   if ((!s) || (!*s))
-      return 0;
-
-   return 1 + access(s, R_OK);
-}
-
-int
-canwrite(const char *s)
-{
-   if ((!s) || (!*s))
+   if (!s || !*s)
       return 0;
 
-   return 1 + access(s, W_OK);
-}
-
-int
-canexec(const char *s)
-{
-   if ((!s) || (!*s))
-      return 0;
+#define EFILE_ALL (EFILE_ANY | EFILE_REG | EFILE_DIR)
+   if (test & EFILE_ALL)
+     {
+       if (stat(s, &st) < 0)
+          return 0;
+       if ((test & EFILE_REG) && !S_ISREG(st.st_mode))
+          return 0;
+       if ((test & EFILE_DIR) && !S_ISDIR(st.st_mode))
+          return 0;
+     }
+
+#define EPERM_ALL (EPERM_R | EPERM_W | EPERM_X)
+   if (test & EPERM_ALL)
+     {
+       mode = 0;
+       if (test & EPERM_R)
+          mode |= R_OK;
+       if (test & EPERM_W)
+          mode |= W_OK;
+       if (test & EPERM_X)
+          mode |= X_OK;
+       if (access(s, mode))
+          return 0;
+     }
 
-   return 1 + access(s, X_OK);
+   return 1;
 }
 
 time_t
@@ -312,7 +241,7 @@
 }
 
 const char         *
-FileExtension(const char *file)
+fileext(const char *file)
 {
    const char         *p;
 
@@ -348,115 +277,57 @@
 }
 
 char               *
-pathtoexec(const char *file)
+path_test(const char *file, unsigned int test)
 {
-   char               *p, *cp, *ep;
-   char               *s;
-   int                 len, exelen;
+   char               *cp, *ep;
+   char               *s, *p;
+   unsigned int        len, exelen;
+
+   if (!file)
+      return NULL;
 
    if (isabspath(file))
      {
-       if (canexec(file))
+       if (file_test(file, test))
           return Estrdup(file);
        return NULL;
      }
-   p = getenv("PATH");
-   if (!p)
+   cp = getenv("PATH");
+   if (!cp)
       return Estrdup(file);
-   if (!file)
-      return NULL;
 
-   cp = p;
    exelen = strlen(file);
-   while ((ep = strchr(cp, ':')) != NULL)
-     {
-       len = ep - cp;
-       s = Emalloc(len + 1);
-       if (s)
-         {
-            strncpy(s, cp, len);
-            s[len] = 0;
-            s = Erealloc(s, len + 2 + exelen);
-            if (!s)
-               return NULL;
-            strcat(s, "/");
-            strcat(s, file);
-            if (canexec(s))
-               return s;
-            Efree(s);
-         }
-       cp = ep + 1;
-     }
-   len = strlen(cp);
-   s = Emalloc(len + 1);
-   if (s)
-     {
-       strncpy(s, cp, len);
-       s[len] = 0;
-       s = Erealloc(s, len + 2 + exelen);
-       if (!s)
-          return NULL;
-       strcat(s, "/");
-       strcat(s, file);
-       if (canexec(s))
+   s = NULL;
+   ep = cp;
+   for (; ep; cp = ep + 1)
+     {
+       ep = strchr(cp, ':');
+       len = (ep) ? (unsigned int)(ep - cp) : strlen(cp);
+       if (len == 0)
+          continue;
+       p = Erealloc(s, len + exelen + 2);
+       if (!p)
+          break;
+       s = p;
+       memcpy(s, cp, len);
+       s[len] = '/';
+       memcpy(s + len + 1, file, exelen + 1);
+       if (file_test(s, test))
           return s;
-       Efree(s);
      }
+   if (s)
+      Efree(s);
    return NULL;
 }
 
-char               *
-pathtofile(const char *file)
+int
+path_canexec(const char *file)
 {
-   char               *p, *cp, *ep;
    char               *s;
-   int                 len, exelen;
 
-   if (isabspath(file))
-     {
-       if (exists(file))
-          return Estrdup(file);
-     }
-   p = getenv("PATH");
-   if (!p)
-      return Estrdup(file);
-   if (!file)
-      return NULL;
-   cp = p;
-   exelen = strlen(file);
-   while ((ep = strchr(cp, ':')) != NULL)
-     {
-       len = ep - cp;
-       s = Emalloc(len + 1);
-       if (s)
-         {
-            strncpy(s, cp, len);
-            s[len] = 0;
-            s = Erealloc(s, len + 2 + exelen);
-            if (!s)
-               return NULL;
-            strcat(s, "/");
-            strcat(s, file);
-            if (exists(s))
-               return s;
-            Efree(s);
-         }
-       cp = ep + 1;
-     }
-   len = strlen(cp);
-   s = Emalloc(len + 1);
-   if (s)
-     {
-       strncpy(s, cp, len);
-       s[len] = 0;
-       s = Erealloc(s, len + 2 + exelen);
-       if (!s)
-          return NULL;
-       strcat(s, "/");
-       strcat(s, file);
-       if (exists(s))
-          return s;
-       Efree(s);
-     }
-   return NULL;
+   s = path_test(file, EFILE_REG | EPERM_X);
+   if (!s)
+      return 0;
+   Efree(s);
+   return 1;
 }
===================================================================
RCS file: /cvs/e/e16/e/src/file.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -3 -r1.3 -r1.4
--- file.h      13 Jan 2007 19:14:27 -0000      1.3
+++ file.h      15 Jan 2007 02:43:54 -0000      1.4
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various 
contributors
+ * Copyright (C) 2007 Kim Woelders
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -30,22 +31,34 @@
 void                E_rm(const char *s);
 char              **E_ls(const char *dir, int *num);
 
-int                 exists(const char *s);
-int                 isdir(const char *s);
-int                 isfile(const char *s);
-int                 canread(const char *s);
-int                 canwrite(const char *s);
-int                 canexec(const char *s);
+#define EFILE_ANY   0x01
+#define EFILE_REG   0x02
+#define EFILE_DIR   0x04
+
+#define EPERM_R     0x10
+#define EPERM_W     0x20
+#define EPERM_X     0x40
+#define EPERM_RWX   (EPERM_R | EPERM_W | EPERM_X)
+
+int                 file_test(const char *s, unsigned int test);
+
+#define exists(s)   file_test(s, EFILE_ANY)
+#define isdir(s)    file_test(s, EFILE_DIR)
+#define isfile(s)   file_test(s, EFILE_REG)
+#define canread(s)  file_test(s, EFILE_REG | EPERM_R)
+#define canexec(s)  file_test(s, EFILE_REG | EPERM_X)
+
 time_t              moddate(const char *s);
 int                 fileinode(const char *s);
 int                 filedev_map(int dev);
 int                 filedev(const char *s);
 
 int                 isabspath(const char *s);
-const char         *FileExtension(const char *file);
+const char         *fileext(const char *s);
 char               *fileof(const char *s);
 char               *fullfileof(const char *s);
-char               *pathtoexec(const char *file);
-char               *pathtofile(const char *file);
+
+char               *path_test(const char *file, unsigned int test);
+int                 path_canexec(const char *file);
 
 #endif /* _FILE_H_ */
===================================================================
RCS file: /cvs/e/e16/e/src/main.c,v
retrieving revision 1.155
retrieving revision 1.156
diff -u -3 -r1.155 -r1.156
--- main.c      13 Jan 2007 19:14:28 -0000      1.155
+++ main.c      15 Jan 2007 02:43:54 -0000      1.156
@@ -453,7 +453,7 @@
    Esnprintf(buf, sizeof(buf), "%s/edox", EDirBin());
    if (!canexec(buf))
       return;
-   Esnprintf(buf, sizeof(buf), "%s/E-docs", EDirRoot());
+   Esnprintf(buf, sizeof(buf), "%s/E-docs/MAIN", EDirRoot());
    if (!canread(buf))
       return;
 
@@ -577,36 +577,12 @@
 static void
 EDirCheck(const char *dir)
 {
-   if (!isdir(dir))
-     {
-       Alert(_("The directory %s is apparently not a directory\n"
-               "This is a fatal condition.\n" "Please remove this file\n"),
-             dir);
-       EExit(1);
-     }
-   if (!canexec(dir))
-     {
-       Alert(_("Do not have execute access to %s\n"
-               "This is a fatal condition.\n"
-               "Please check the ownership and permissions of this\n"
-               "directory and take steps to rectify this.\n"), dir);
-       EExit(1);
-     }
-   if (!canread(dir))
-     {
-       Alert(_("Do not have read access to %s\n" "This is a fatal condition.\n"
-               "Please check the ownership and permissions of this\n"
-               "directory and take steps to rectify this.\n"), dir);
-       EExit(1);
-     }
-   if (!canwrite(dir))
-     {
-       Alert(_("Do not have write access to %s\n"
-               "This is a fatal condition.\n"
-               "Please check the ownership and permissions of this\n"
-               "directory and take steps to rectify this.\n"), dir);
-       EExit(1);
-     }
+   if (file_test(dir, EFILE_DIR | EPERM_RWX))
+      return;
+
+   Alert(_("%s must be a directory in which you have\n"
+          "read, write, and execute permission.\n"));
+   EExit(1);
 }
 
 void
===================================================================
RCS file: /cvs/e/e16/e/src/menus-misc.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -3 -r1.38 -r1.39
--- menus-misc.c        13 Jan 2007 19:14:28 -0000      1.38
+++ menus-misc.c        15 Jan 2007 02:43:54 -0000      1.39
@@ -175,7 +175,7 @@
        if ((*(list[i]) == '.') || (stat(ss, &st) < 0))
           continue;
 
-       ext = FileExtension(ss);
+       ext = fileext(ss);
        if (S_ISDIR(st.st_mode))
          {
             Esnprintf(s, sizeof(s), "%s/%s:%s", dir, list[i], MenuGetName(m));
@@ -323,7 +323,7 @@
        else
          {
             char               *txt = NULL, *icon = NULL, *act = NULL;
-            char               *params = NULL, *tmp = NULL, wd[4096];
+            char               *params = NULL, wd[4096];
 
             MenuItem           *mi;
             ImageClass         *icc = NULL;
@@ -333,7 +333,6 @@
             icon = field(s, 1);
             act = field(s, 2);
             params = field(s, 3);
-            tmp = NULL;
             if (icon && exists(icon))
               {
                  Esnprintf(wd, sizeof(wd), "__FM.%s", icon);
@@ -344,10 +343,8 @@
             if ((act) && (!strcmp(act, "exec")) && (params))
               {
                  word(params, 1, wd);
-                 tmp = pathtoexec(wd);
-                 if (tmp)
+                 if (path_canexec(wd))
                    {
-                      Efree(tmp);
                       Esnprintf(s, sizeof(s), "exec %s", params);
                       mi = MenuItemCreate(txt, icc, s, NULL);
                       MenuAddItem(m, mi);
@@ -490,7 +487,6 @@
                  if (f)
                    {
                       char               *iname, *exec, *texec, *en_name;
-                      char               *tmp;
 
                       iname = exec = texec = en_name = NULL;
 
@@ -522,14 +518,12 @@
                       fclose(f);
                       if ((iname) && (exec))
                         {
-                           tmp = NULL;
+                           int                 ok = 1;
+
                            if (texec)
-                              tmp = pathtoexec(texec);
-                           if ((tmp) || (!texec))
+                              ok = path_canexec(texec);
+                           if (ok)
                              {
-                                if (tmp)
-                                   Efree(tmp);
-
                                 Esnprintf(s, sizeof(s), "exec %s", exec);
                                 mi = MenuItemCreate(iname, NULL, s, NULL);
                                 MenuAddItem(m, mi);
===================================================================
RCS file: /cvs/e/e16/e/src/menus.c,v
retrieving revision 1.267
retrieving revision 1.268
diff -u -3 -r1.267 -r1.268
--- menus.c     13 Jan 2007 19:14:28 -0000      1.267
+++ menus.c     15 Jan 2007 02:43:54 -0000      1.268
@@ -1898,17 +1898,12 @@
                  if (!strcmp(s2, "exec"))
                    {
                       char                buf[1024];
-                      char               *path;
 
                       params = atword(s, 3);
                       if (params)
                         {
                            sscanf(atword(s, 3), "%1000s", buf);
-                           path = pathtoexec(buf);
-                           if (path)
-                              Efree(path);
-                           else
-                              ok = 0;
+                           ok = path_canexec(buf);
                         }
                    }
                  if (ok)
===================================================================
RCS file: /cvs/e/e16/e/src/theme.c,v
retrieving revision 1.60
retrieving revision 1.61
diff -u -3 -r1.60 -r1.61
--- theme.c     13 Jan 2007 19:14:28 -0000      1.60
+++ theme.c     15 Jan 2007 02:43:54 -0000      1.61
@@ -142,7 +142,7 @@
               }
             else if (isfile(ss))
               {
-                 if (!FileExtension(ss) || strcmp(FileExtension(ss), "etheme"))
+                 if (!fileext(ss) || strcmp(fileext(ss), "etheme"))
                     continue;
               }
 



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
enlightenment-cvs mailing list
enlightenment-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-cvs

Reply via email to