links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 56ee8dbab37db5dd303e2d8d898fa3b6cdd7626e
Parent: 19e5302d9792cc057803889849f7155692f170f3
Author: opask
Date:   Fri,  7 Dec 2018 21:42:53 -0700

file.c: style

Diffstat:
Mfile.c | 119+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 68 insertions(+), 51 deletions(-)

diff --git a/file.c b/file.c @@ -16,6 +16,7 @@ static void setrwx(unsigned m, unsigned char *p) static void stat_mode(unsigned char **p, int *l, struct stat *stp) { unsigned char c = '?'; + unsigned char rwx[10] = "---------"; if (stp) { if (0) {} #ifdef S_ISBLK @@ -40,23 +41,21 @@ static void stat_mode(unsigned char **p, int *l, struct stat *stp) #endif } add_chr_to_str(p, l, c); - { - unsigned char rwx[10] = "---------"; - if (stp) { - unsigned mode = stp->st_mode; - setrwx(mode << 0, &rwx[0]); - setrwx(mode << 3, &rwx[3]); - setrwx(mode << 6, &rwx[6]); - } - add_to_str(p, l, rwx); + if (stp) { + unsigned mode = stp->st_mode; + setrwx(mode << 0, &rwx[0]); + setrwx(mode << 3, &rwx[3]); + setrwx(mode << 6, &rwx[6]); } + add_to_str(p, l, rwx); add_chr_to_str(p, l, ' '); } static void stat_links(unsigned char **p, int *l, struct stat *stp) { unsigned char lnk[64]; - if (!stp) add_to_str(p, l, cast_uchar " "); + if (!stp) + add_to_str(p, l, cast_uchar " "); else { sprintf(cast_char lnk, "%3ld ", (unsigned long)stp->st_nlink); add_to_str(p, l, lnk); @@ -75,7 +74,7 @@ static void stat_user(unsigned char **p, int *l, struct stat *stp, int g) struct group *grp; int id; unsigned char *pp; - int i; + size_t i; if (!stp) { add_to_str(p, l, cast_uchar " "); return; @@ -95,9 +94,10 @@ static void stat_user(unsigned char **p, int *l, struct stat *stp, int g) else sprintf(cast_char pp, "%.8s", grp->gr_name); last_gid = id; } - a: + a: add_to_str(p, l, pp); - for (i = (int)strlen(cast_const_char pp); i < 8; i++) add_chr_to_str(p, l, ' '); + for (i = strlen(cast_const_char pp); i < 8; i++) + add_chr_to_str(p, l, ' '); add_chr_to_str(p, l, ' '); } @@ -105,13 +105,12 @@ static void stat_size(unsigned char **p, int *l, struct stat *stp) { unsigned char num[64]; const int digits = 8; - int i; - if (!stp) { + size_t i; + if (!stp) num[0] = 0; - } else { + else snzprint(num, sizeof num, stp->st_size); - } - for (i = (int)strlen(cast_const_char num); i < digits; i++) + for (i = strlen(cast_const_char num); i < digits; i++) add_chr_to_str(p, l, ' '); add_to_str(p, l, num); add_chr_to_str(p, l, ' '); @@ -126,7 +125,7 @@ static void stat_date(unsigned char **p, int *l, struct stat *stp) unsigned char str[13]; static unsigned char fmt1[] = "%b %e %Y"; static unsigned char fmt2[] = "%b %e %H:%M"; - int wr; + size_t wr; EINTRLOOPX(current_time, time(NULL), (time_t)-1); if (!stp) { wr = 0; @@ -134,11 +133,13 @@ static void stat_date(unsigned char **p, int *l, struct stat *stp) } when = stp->st_mtime; when_local = localtime(&when); - if ((ulonglong)current_time > (ulonglong)when + 6L * 30L * 24L * 60L * 60L || - (ulonglong)current_time < (ulonglong)when - 60L * 60L) fmt = fmt1; - else fmt = fmt2; -again: - wr = (int)strftime(cast_char str, 13, cast_const_char fmt, when_local); + if ((ulonglong)current_time > (ulonglong)when + 6L * 30L * 24L * 60L * 60L + || (ulonglong)current_time < (ulonglong)when - 60L * 60L) + fmt = fmt1; + else + fmt = fmt2; + again: + wr = strftime(cast_char str, 13, cast_const_char fmt, when_local); if (wr && strstr(cast_const_char str, " e ") && ((e = cast_uchar strchr(cast_const_char fmt, 'e')))) { *e = 'd'; goto again; @@ -181,7 +182,7 @@ static int comp_de(const void *d1_, const void *d2_) void file_func(struct connection *c) { struct cache_entry *e; - unsigned char *file, *name, *head = NULL; /* against warning */ + unsigned char *file, *name, *head = NULL; int fl, flo; DIR *d; int h, r; @@ -193,16 +194,22 @@ void file_func(struct connection *c) return; } if (!(name = get_filename(c->url))) { - setcstate(c, S_OUT_OF_MEM); abort_connection(c); return; + setcstate(c, S_OUT_OF_MEM); + abort_connection(c); + return; } EINTRLOOP(rs, stat(cast_const_char name, &stt)); if (rs) { free(name); - setcstate(c, get_error_from_errno(errno)); abort_connection(c); return; + setcstate(c, get_error_from_errno(errno)); + abort_connection(c); + return; } if (!S_ISDIR(stt.st_mode) && !S_ISREG(stt.st_mode)) { free(name); - setcstate(c, S_FILE_TYPE); abort_connection(c); return; + setcstate(c, S_FILE_TYPE); + abort_connection(c); + return; } h = c_open(name, O_RDONLY | O_NOCTTY); if (h == -1) { @@ -225,9 +232,11 @@ void file_func(struct connection *c) EINTRLOOP(rs, close(h)); if (!d) { free(name); - setcstate(c, get_error_from_errno(er)); abort_connection(c); return; + setcstate(c, get_error_from_errno(er)); + abort_connection(c); + return; } - dir: + dir: dir = NULL; dirl = 0; if (name[0] && !dir_sep(name[strlen(cast_const_char name) - 1])) { @@ -235,7 +244,9 @@ void file_func(struct connection *c) if (get_connection_cache_entry(c)) { free(name); closedir(d); - setcstate(c, S_OUT_OF_MEM); abort_connection(c); return; + setcstate(c, S_OUT_OF_MEM); + abort_connection(c); + return; } c->cache->refcount--; } @@ -293,54 +304,56 @@ void file_func(struct connection *c) stat_date(p, &l, stp); } closedir(d); - if (dirl) qsort(dir, dirl, sizeof(struct dirs), (int (*)(const void *, const void *))comp_de); + if (dirl) + qsort(dir, dirl, sizeof(struct dirs), + (int (*)(const void *, const void *))comp_de); for (i = 0; i < dirl; i++) { - unsigned char *lnk = NULL; + char *lnk = NULL; if (dir[i].s[0] == 'l') { - unsigned char *buf = NULL; - int size = 0; + char *buf = NULL; + size_t size = 0; int r; - unsigned char *n = stracpy(name); - add_to_strn(&n, dir[i].f); + char *n = (char *)stracpy(name); + add_to_strn((unsigned char **)&n, dir[i].f); do { free(buf); size += ALLOC_GR; - if ((unsigned)size > INT_MAX) overalloc(); + if (size > INT_MAX) + overalloc(); buf = xmalloc(size); - EINTRLOOP(r, (int)readlink(cast_const_char n, cast_char buf, size)); + EINTRLOOP(r, readlink(n, buf, size)); } while (r == size); if (r == -1) goto yyy; buf[r] = 0; lnk = buf; goto xxx; - yyy: + yyy: free(buf); - xxx: + xxx: free(n); } - /*add_to_str(&file, &fl, cast_uchar " ");*/ add_to_str(&file, &fl, dir[i].s); add_to_str(&file, &fl, cast_uchar "<a href=\"./"); add_conv_str(&file, &fl, dir[i].f, (int)strlen(cast_const_char dir[i].f), 1); - if (dir[i].s[0] == 'd') add_to_str(&file, &fl, cast_uchar "/"); + if (dir[i].s[0] == 'd') + add_to_str(&file, &fl, cast_uchar "/"); else if (lnk) { struct stat st; unsigned char *n = stracpy(name); add_to_strn(&n, dir[i].f); EINTRLOOP(rs, stat(cast_const_char n, &st)); - if (!rs) if (S_ISDIR(st.st_mode)) add_to_str(&file, &fl, cast_uchar "/"); + if (!rs && S_ISDIR(st.st_mode)) + add_to_str(&file, &fl, cast_uchar "/"); free(n); } add_to_str(&file, &fl, cast_uchar "\">"); - /*if (dir[i].s[0] == 'd') add_to_str(&file, &fl, cast_uchar "<font color=\"yellow\">");*/ flo = fl; add_conv_str(&file, &fl, dir[i].f, (int)strlen(cast_const_char dir[i].f), 0); convert_file_charset(&file, &fl, flo); - /*if (dir[i].s[0] == 'd') add_to_str(&file, &fl, cast_uchar "</font>");*/ add_to_str(&file, &fl, cast_uchar "</a>"); if (lnk) { add_to_str(&file, &fl, cast_uchar " -> "); - add_to_str(&file, &fl, lnk); + add_to_str(&file, &fl, cast_uchar lnk); free(lnk); } add_to_str(&file, &fl, cast_uchar "\n"); @@ -357,7 +370,8 @@ void file_func(struct connection *c) free(name); if (stt.st_size < 0 || stt.st_size > INT_MAX) { EINTRLOOP(rs, close(h)); - setcstate(c, S_LARGE_FILE); abort_connection(c); + setcstate(c, S_LARGE_FILE); + abort_connection(c); return; } /* + !stt.st_size is there because of bug in Linux. Read returns @@ -372,7 +386,8 @@ void file_func(struct connection *c) free(file); EINTRLOOP(rs, close(h)); setcstate(c, r == -1 ? get_error_from_errno(errno) : S_FILE_ERROR); - abort_connection(c); return; + abort_connection(c); + return; } fl = r; EINTRLOOP(rs, close(h)); @@ -382,7 +397,9 @@ void file_func(struct connection *c) if (get_connection_cache_entry(c)) { free(file); free(head); - setcstate(c, S_OUT_OF_MEM); abort_connection(c); return; + setcstate(c, S_OUT_OF_MEM); + abort_connection(c); + return; } c->cache->refcount--; } @@ -397,7 +414,7 @@ void file_func(struct connection *c) } truncate_entry(e, fl, 1); free(file); - end: + end: c->cache->incomplete = 0; setcstate(c, S__OK); abort_connection(c);