Merge remote-tracking branch 'njsf/for-steve' into for-steve
[sxemacs] / src / editfns.c
index e399a6b..4bdaf8e 100644 (file)
@@ -39,6 +39,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #include "casetab.h"
 #include "chartab.h"
 #include "line-number.h"
+#include "ent/ent.h"
 
 #include "systime.h"
 #include "sysdep.h"
@@ -607,7 +608,7 @@ On Unix it is obtained from TMPDIR, with /tmp as the default.
 
                strcpy(path, "/tmp/");
                strncat(path, user_login_name(NULL), _POSIX_PATH_MAX);
-                path[sizeof(path)-1]=0;
+               path[sizeof(path)-1]=0;
                if (lstat(path, &st) < 0 && errno == ENOENT) {
                        mkdir(path, 0700);      /* ignore retval -- checked next anyway. */
                }
@@ -615,31 +616,31 @@ On Unix it is obtained from TMPDIR, with /tmp as the default.
                    S_ISDIR(st.st_mode)) {
                        tmpdir = path;
                } else {
-                       const char* home_env = getenv("HOME");
+                       const char* home_env = getenv("HOME");
                        if ( home_env ) {
-                               strncpy(path, home_env, sizeof(path)-1);
-                               strncat(path, "/tmp/", sizeof(path)-1);
-                               if (stat(path, &st) < 0 && errno == ENOENT) {
+                               xstrncpy(path, home_env, sizeof(path));
+                               xstrncat(path, "/tmp/", sizeof(path)-1);
+                               if ( mkdir(path, 0700) >= 0 || errno == EEXIST ) {
                                        int fd;
-                                       char warnpath[ 
-                                               /* strlen(".created_by_sxemacs") */ 
+                                       char warnpath[
+                                               /* strlen(".created_by_sxemacs") */
                                                19 + _POSIX_PATH_MAX + 1];
-                                       mkdir(path, 0700);      /* ignore retvals */
-                                       strncpy(warnpath, path, _POSIX_PATH_MAX);
-                                       warnpath[sizeof(warnpath)-1]=0;
-                                       
+                                       xstrncpy(warnpath, path, sizeof(warnpath));
+
                                        /* we already are reserved these 20 bytes... */
-                                       strcat(warnpath, ".created_by_sxemacs");
+                                       xstrncat(warnpath, ".created_by_sxemacs", 
+                                                sizeof(warnpath)-1);
                                        if ((fd = open(warnpath, O_WRONLY | O_CREAT,
-                                                      0644)) > 0) {
+                                                      0644)) >= 0) {
                                                write(fd, "SXEmacs created this directory "
-                                                         "because /tmp/<yourname> "
-                                                         "was unavailable -- \nPlease check !\n",  89);
+                                                         "because /tmp/<yourname> "
+                                                         "was unavailable -- \nPlease check !\n",
+                                                     89);
                                                close(fd);
                                        }
                                }
                        }
-                       if (stat(path, &st) == 0 && st.st_uid == (uid_t) myuid 
+                       if (stat(path, &st) == 0 && st.st_uid == (uid_t) myuid
                            && S_ISDIR(st.st_mode)) {
                                tmpdir = path;
                        } else {
@@ -917,7 +918,7 @@ The time is returned as a big integer.
 
        EMACS_GET_TIME(t);
        bigz_init(btime);
-       
+
        bigz_set_long(btime, EMACS_SECS(t));
        mpz_mul_ui(btime, btime, 1000000UL);
        mpz_add_ui(btime, btime, EMACS_USECS(t));
@@ -1050,7 +1051,7 @@ time will be 0.
        return list3(make_float(user), make_float(sys), make_float(real));
 }
 
-DEFUN("uptime", Fuptime, 0, 1, "P", /* 
+DEFUN("uptime", Fuptime, 0, 1, "P", /*
 Display SXEmacs \"uptime\".
 
 When called interactively, without a prefix arg, return a list of 4
@@ -1162,7 +1163,7 @@ TIME is specified as (HIGH LOW . IGNORED) or (HIGH . LOW), as from
 `current-time' and `file-attributes'.  If TIME is not specified it
 defaults to the current time.
 
-If compiled with ENT, TIME may also be a big integer representing 
+If compiled with ENT, TIME may also be a big integer representing
 the number of microseconds since the Epoch, as output by
 `current-btime'.
 
@@ -1349,9 +1350,10 @@ If you want them to stand for years in this century, you must do that yourself.
                        tzstring = (char *)XSTRING_DATA(zone);
                } else if (INTP(zone)) {
                        int abszone = abs(XINT(zone));
-                       snprintf(tzbuf, countof(tzbuf) - 1, "XXX%s%d:%02d:%02d",
-                                "-" + (XINT(zone) < 0), abszone / (60 * 60),
-                                (abszone / 60) % 60, abszone % 60);
+                       int sz = snprintf(tzbuf, sizeof(tzbuf), "XXX%s%d:%02d:%02d",
+                                         "-" + (XINT(zone) < 0), abszone / (60 * 60),
+                                         (abszone / 60) % 60, abszone % 60);
+                       assert(sz >= 0 && (size_t)sz < sizeof(tzbuf));
                        tzstring = tzbuf;
                } else {
                        error("Invalid time zone specification");
@@ -1423,9 +1425,10 @@ Like `encode-time' but return a big integer time instead.
                        tzstring = (char *)XSTRING_DATA(zone);
                else if (INTP(zone)) {
                        int abszone = abs(XINT(zone));
-                       sprintf(tzbuf, "XXX%s%d:%02d:%02d",
-                               "-" + (XINT(zone) < 0), abszone / (60 * 60),
-                               (abszone / 60) % 60, abszone % 60);
+                       int sz = snprintf(tzbuf, sizeof(tzbuf), "XXX%s%d:%02d:%02d",
+                                         "-" + (XINT(zone) < 0), abszone / (60 * 60),
+                                         (abszone / 60) % 60, abszone % 60);
+                       assert(sz>=0 && (size_t)sz < sizeof(tzbuf));
                        tzstring = tzbuf;
                } else
                        error("Invalid time zone specification");
@@ -1561,8 +1564,10 @@ the data it can't find.
                        /* No local time zone name is available; use "+-NNNN"
                           instead.  */
                        int am = (offset < 0 ? -offset : offset) / 60;
-                       sprintf(buf, "%c%02d%02d", (offset < 0 ? '-' : '+'),
-                               am / 60, am % 60);
+                       int sz = snprintf(buf, sizeof(buf), "%c%02d%02d",
+                                         (offset < 0 ? '-' : '+'),
+                                         am / 60, am % 60);
+                       assert(sz>=0 && (size_t)sz < sizeof(buf));
                        s = buf;
                }
                return list2(make_int(offset), build_string(s));