home-etc: doc/home_etc_expand_tilde.3 (NEW), src/example4.c (NEW), ...
siefca
siefca at pld-linux.org
Wed May 17 11:13:05 CEST 2006
Author: siefca Date: Wed May 17 09:13:05 2006 GMT
Module: home-etc Tag: HEAD
---- Log message:
- added documentation and example apropos tile-slash handling
- added comments to code
---- Files affected:
home-etc/doc:
home_etc_expand_tilde.3 (NONE -> 1.1) (NEW)
home-etc/src:
example4.c (NONE -> 1.1) (NEW), core.c (1.18 -> 1.19)
---- Diffs:
================================================================
Index: home-etc/doc/home_etc_expand_tilde.3
diff -u /dev/null home-etc/doc/home_etc_expand_tilde.3:1.1
--- /dev/null Wed May 17 11:13:05 2006
+++ home-etc/doc/home_etc_expand_tilde.3 Wed May 17 11:13:00 2006
@@ -0,0 +1,72 @@
+.\" Copyright (c) 2003-2006 by Paweł Wilk <siefca at gnu.org>
+.\"
+.\" Permission is granted to copy, distribute and/or modify this document
+.\" under the terms of the GNU Free Documentation License, Version 1.2
+.\" or any later version published by the Free Software Foundation;
+.\" with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
+.\" A copy of the license is included in the section entitled "GNU
+.\" Free Documentation License"; if it's not, contact Free Software Foundation, Inc.
+.\" 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain one.
+.\"
+.TH home_etc_reset "3" "PLD Linux/GNU" "December 2003" "HOME-ETC"
+.SH NAME
+home_etc_expand_tilde \- toggle tilde-slash pair parsing
+.SH SYNOPSIS
+.nf
+.B #include <home_etc.h>
+.sp
+.BI "void home_etc_expand_tilde(const char);"
+.sp
+.BI _HE_LIKE_TILDE;
+.BI _HE_UNLIKE_TILDE;
+.fi
+.SH DESCRIPTION
+The function \fBhome_etc_expand_tilde(3)\fP is used to set leading
+tilde & slash parsing, while processing pathnames by \fBhome_etc_path(3)\fP.
+It takes one argument, which should be set to 0 to disable and to non-zero to
+enable parsing of a leading \fI~/\fP string found in path. When parsing
+is enabled, the path is expanded by replacing \fI~/\fP by user's home directory
+pathname while invoking internal routines.
+
+The way to obtain user's home directory depends on method choosen while
+invoking \fBhome_etc_path(3)\fP (see \fIuse_env\fP description in the
+manual).
+
+Once invoked this function keeps last setting memorized. The parsing
+by default is in state DISABLED.
+
+\fB_HE_LIKE_TILDE\fP and \fB_HE_UNLIKE_TILDE\fP are macros for this function.
+Usage is self-explanatory (but don't forget to add trailing semicolon in case of
+use, e.g. _HE_LIKE_TILDE; to keep the syntax ok).
+
+.SH "RETURN VALUE"
+This function returns no value.
+
+.SH EXAMPLES
+for HOME=\fI/home/users/siefca\fP
+and HOME_ETC=\fI/usr/siefca-configs\fP
+.sp
+the function:
+.sp
+.BI "home_etc_expand_tilde(" 1 );
+.BI "home_etc_path("""~/.muttrc """, " 1 );
+.sp
+
+will return
+.I /usr/siefca-configs/.muttrc
+
+.SH "SEE ALSO"
+.BR home_etc(3)
+.BR home_etc_path(3),
+.BR get_home_etc(3),
+.BR get_home_etc_static(3)
+
+.SH LICENSE
+The HOME-ETC library is distributed under the terms of GNU LGPL.
+This documentation is distributed under the terms of GNU FDL.
+
+.SH AUTHOR
+Pawel Wilk <siefca at gnu.org>
+There was also a lot of other people, who have contributed to this code
+and/or helped with patching applications. See the AUTHORS and the CONTRIBUTORS files.
+
================================================================
Index: home-etc/src/example4.c
diff -u /dev/null home-etc/src/example4.c:1.1
--- /dev/null Wed May 17 11:13:05 2006
+++ home-etc/src/example4.c Wed May 17 11:13:00 2006
@@ -0,0 +1,40 @@
+/* home-etc library: obtain user-decided configuration directory
+ *
+ * Copyright (C) 2003-2006 Pawel Wilk <siefca at gnu.org>,
+ *
+ * This is free software; see the GNU Lesser General Public License version 2
+ * or later for copying conditions. There is NO warranty.
+ *
+ *
+ * THIS IS AN EXAMPLE FILE,
+ * IT SHOWS HOW TO USE HOME-ETC IN APPLICATIONS BEING MODIFIED
+ *
+ * compilation: gcc example4.c -lhome_etc -o example4
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <home_etc.h>
+
+int main(int argc, char *argv[])
+{
+ const char *p;
+
+ setenv("HOME", "/tmp", 1);
+ setenv("HOME_ETC", "/tmp/myconfigs", 1);
+
+ _HE_LIKE_TILDE;
+ p = _HE("~/.myrc");
+ printf("original path: %s\n", p);
+ printf("modified path: %s\n", p);
+
+ exit(0);
+}
+
+/*
+ Local Variables:
+ mode: c
+ c-set-style: "gnu"
+ End:
+*/
================================================================
Index: home-etc/src/core.c
diff -u home-etc/src/core.c:1.18 home-etc/src/core.c:1.19
--- home-etc/src/core.c:1.18 Wed May 17 03:50:04 2006
+++ home-etc/src/core.c Wed May 17 11:13:00 2006
@@ -82,6 +82,7 @@
if (! getcwd(prev, sizeof(prev)))
return NULL;
+ /* memorize trailing slash */
s = strlen(path);
if (s > 0 && *(path+s-1) == '/')
trailslash = 1;
@@ -102,7 +103,7 @@
strcpy(buff, home_d); /* strcpy checked */
strcat(buff, path+1); /* strcat checked */
}
- else /* just copy path into buffer */
+ else /* or just copy path into buffer */
{
if (strlen(path) > sizeof(buff)-2)
{
@@ -126,7 +127,7 @@
strcat(pbuff, "/"); /* strcpy checked */
strcat(pbuff, buff); /* strcat checked */
}
- else /* if we have absolute pathname */
+ else /* or if we have absolute pathname */
{
if (strlen(buff) > sizeof(pbuff)-2)
{
@@ -146,15 +147,19 @@
&& chdir(pbuff) == -1)
*q = '\0';
- if(counter <= 0) /* do we have strange traversal loops? */
+ /* do we have strange traversal loops? */
+ if(counter <= 0)
{
chdir(prev);
return NULL;
}
- if (q == NULL || q >= pbuff+s) /* pure resolvable path? */
- q = pbuff+s; /* point it to \0 string */
+
+ /* do we have pure resolvable path? */
+ if (q == NULL || q >= pbuff+s)
+ q = pbuff+s; /* point q to \0 string */
else
- q += strlen(q);
+ q += strlen(q); /* point q to last char */
+
/* q pointer now keeps the borderline between resolvable */
/* and unresolvable part of the pathname */
@@ -179,7 +184,7 @@
*q = '\0'; /* keep split */
}
else
- *intbuf = '\0';
+ *intbuf = '\0'; /* if q was unnecessary */
/* make the buffered, resolvable path absolute */
if(absolutize_dir(pbuff, sizeof(pbuff)-1) == -1)
@@ -188,6 +193,7 @@
return NULL;
}
+ /* do we have unresolvable part of path? */
if(intbuf && *intbuf != '\0')
{
/* attach the unresolvable part to the absolutized part */
@@ -199,6 +205,7 @@
strcat(pbuff, intbuf); /* strcat checked */
}
+ /* add or remove trailing slash as memorized before */
s = strlen(pbuff);
if (s > 0)
{
@@ -211,6 +218,7 @@
*(pbuff+s) = '\0';
}
+ /* go back to the directory we were and return pointer to buffer */
chdir(prev);
return pbuff;
}
================================================================
---- CVS-web:
http://cvs.pld-linux.org/home-etc/src/core.c?r1=1.18&r2=1.19&f=u
More information about the pld-cvs-commit
mailing list