]> git.cameronkatri.com Git - cgit.git/blobdiff - html.c
Use long instead of int when converting from TTL to seconds
[cgit.git] / html.c
diff --git a/html.c b/html.c
index 5780dc179e6c22070e0f3a509cafbddfabb89e13..c0b2ed48317957c6ce9c337542ac89c89ea88206 100644 (file)
--- a/html.c
+++ b/html.c
@@ -1,3 +1,11 @@
+/* html.c: helper functions for html output
+ *
+ * Copyright (C) 2006 Lars Hjemli
+ *
+ * Licensed under GNU General Public License v2
+ *   (see COPYING for full license text)
+ */
+
 #include "cgit.h"
 
 char *fmt(const char *format, ...)
@@ -20,22 +28,24 @@ char *fmt(const char *format, ...)
 
 void html(const char *txt)
 {
-       fputs(txt, stdout);
+       write(htmlfd, txt, strlen(txt));
 }
 
 void htmlf(const char *format, ...)
 {
+       static char buf[65536];
        va_list args;
 
        va_start(args, format);
-       vprintf(format, args);
+       vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
+       html(buf);
 }
 
 void html_txt(char *txt)
 {
        char *t = txt;
-       while(*t){
+       while(t && *t){
                int c = *t;
                if (c=='<' || c=='>' || c=='&') {
                        *t = '\0';
@@ -55,11 +65,39 @@ void html_txt(char *txt)
                html(txt);
 }
 
+void html_ntxt(int len, char *txt)
+{
+       char *t = txt;
+       while(t && *t && len--){
+               int c = *t;
+               if (c=='<' || c=='>' || c=='&') {
+                       *t = '\0';
+                       html(txt);
+                       *t = c;
+                       if (c=='>')
+                               html("&gt;");
+                       else if (c=='<')
+                               html("&lt;");
+                       else if (c=='&')
+                               html("&amp;");
+                       txt = t+1;
+               }
+               t++;
+       }
+       if (t!=txt) {
+               char c = *t;
+               *t = '\0';
+               html(txt);
+               *t = c;
+       }
+       if (len<0)
+               html("...");
+}
 
 void html_attr(char *txt)
 {
        char *t = txt;
-       while(*t){
+       while(t && *t){
                int c = *t;
                if (c=='<' || c=='>' || c=='\'') {
                        *t = '\0';
@@ -79,6 +117,15 @@ void html_attr(char *txt)
                html(txt);
 }
 
+void html_hidden(char *name, char *value)
+{
+       html("<input type='hidden' name='");
+       html_attr(name);
+       html("' value='");
+       html_attr(value);
+       html("'/>");
+}
+
 void html_link_open(char *url, char *title, char *class)
 {
        html("<a href='");
@@ -98,3 +145,23 @@ void html_link_close(void)
 {
        html("</a>");
 }
+
+void html_fileperm(unsigned short mode)
+{
+       htmlf("%c%c%c", (mode & 4 ? 'r' : '-'), 
+             (mode & 2 ? 'w' : '-'), (mode & 1 ? 'x' : '-'));
+}
+
+void html_filemode(unsigned short mode)
+{
+       if (S_ISDIR(mode))
+               html("d");
+       else if (S_ISLNK(mode))
+               html("l");
+       else
+               html("-");
+       html_fileperm(mode >> 6);
+       html_fileperm(mode >> 3);
+       html_fileperm(mode);
+}
+