allow the specification of multiple filenames (with or without
[rsync/rsync.git] / util.c
diff --git a/util.c b/util.c
index 346d58a..1fdeee6 100644 (file)
--- a/util.c
+++ b/util.c
@@ -483,3 +483,92 @@ int name_to_gid(char *name, gid_t *gid)
 }
 
 
+/****************************************************************************
+check if a process exists. 
+****************************************************************************/
+int process_exists(int pid)
+{
+       return(kill(pid,0) == 0 || errno != ESRCH);
+}
+
+/* lock a byte range in a open file */
+int lock_range(int fd, int offset, int len)
+{
+       struct flock lock;
+
+       lock.l_type = F_WRLCK;
+       lock.l_whence = SEEK_SET;
+       lock.l_start = offset;
+       lock.l_len = len;
+       lock.l_pid = 0;
+       
+       return fcntl(fd,F_SETLK,&lock) == 0;
+}
+
+
+static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
+{
+#ifndef HAVE_GLOB
+       argv[*argc] = strdup(s);
+       (*argc)++;
+       return;
+#else
+       glob_t globbuf;
+       int i;
+
+       argv[*argc] = strdup(s);
+
+       memset(&globbuf, 0, sizeof(globbuf));
+       glob(argv[*argc], 0, NULL, &globbuf);
+       if (globbuf.gl_pathc == 0) {
+               (*argc)++;
+               globfree(&globbuf);
+               return;
+       }
+       for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
+               if (i == 0) free(argv[*argc]);
+               argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
+               if (!argv[(*argc) + i]) out_of_memory("glob_expand");
+       }
+       globfree(&globbuf);
+       (*argc) += i;
+#endif
+}
+
+void glob_expand(char *base, char **argv, int *argc, int maxargs)
+{
+       char *s = argv[*argc];
+       char *p, *q;
+
+       if (!s || !*s) return;
+
+       s = strdup(s);
+       if (!s) out_of_memory("glob_expand");
+
+       q = s;
+       while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
+               if (p != q && *(p-1) == ' ' && p[strlen(base)] == '/') {
+                       /* split it at this point */
+                       *(p-1) = 0;
+                       glob_expand_one(q, argv, argc, maxargs);
+                       q = p+strlen(base);
+               } else {
+                       q++;
+               }
+       }
+
+       if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
+
+       free(s);
+}
+
+/*******************************************************************
+  convert a string to lower case
+********************************************************************/
+void strlower(char *s)
+{
+       while (*s) {
+               if (isupper(*s)) *s = tolower(*s);
+               s++;
+       }
+}