#ifndef lint
#if 0
static const char sccsid[] = "@(#)gen_subs.c 8.1 (Berkeley) 5/31/93";
#else
static const char rcsid[] __attribute__((__unused__)) = "$OpenBSD: gen_subs.c,v 1.17 2003/06/13 17:51:14 millert Exp $";
#endif
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdio.h>
#include <tzfile.h>
#include <utmp.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>
#include "pax.h"
#include "extern.h"
#define MODELEN 20
#define DATELEN 64
#define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY)
#define CURFRMT "%b %e %H:%M"
#define OLDFRMT "%b %e %Y"
#define NAME_WIDTH 8
static void
format_date(char * f_date, time_t file_time, time_t current)
{
const char *timefrmt;
if (ltmfrmt == NULL) {
if ((file_time + SIXMONTHS) <= current || file_time > current)
timefrmt = OLDFRMT;
else
timefrmt = CURFRMT;
} else
timefrmt = ltmfrmt;
if (strftime(f_date,DATELEN,timefrmt,localtime((const time_t *)&(file_time))) == 0)
*f_date = '\0';
}
void
ls_list(ARCHD *arcn, time_t now, FILE *fp)
{
struct stat *sbp;
char f_mode[MODELEN];
char f_date[DATELEN];
int term;
term = zeroflag ? '\0' : '\n';
if (!vflag) {
if (zeroflag)
(void)fputs(arcn->name, fp);
else
safe_print(arcn->name, fp);
(void)putc(term, fp);
(void)fflush(fp);
return;
}
if (pax_list_opt_format) {
pax_format_list_output(arcn, now, fp, term);
return;
}
sbp = &(arcn->sb);
strmode(sbp->st_mode, f_mode);
format_date(&f_date[0], sbp->st_mtime, now);
(void)fprintf(fp, "%s%2u %-*.*s %-*.*s ", f_mode, sbp->st_nlink,
NAME_WIDTH, UT_NAMESIZE, name_uid(sbp->st_uid, 1),
NAME_WIDTH, UT_NAMESIZE, name_gid(sbp->st_gid, 1));
if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK))
# ifdef LONG_OFF_T
(void)fprintf(fp, "%4u,%4u ", MAJOR(sbp->st_rdev),
# else
(void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev),
# endif
(unsigned long)MINOR(sbp->st_rdev));
else {
# ifdef LONG_OFF_T
(void)fprintf(fp, "%9lu ", sbp->st_size);
# else
(void)fprintf(fp, "%9qu ", sbp->st_size);
# endif
}
(void)fputs(f_date, fp);
(void)putc(' ', fp);
safe_print(arcn->name, fp);
if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) {
fputs(" == ", fp);
safe_print(arcn->ln_name, fp);
} else if (arcn->type == PAX_SLK) {
fputs(" => ", fp);
safe_print(arcn->ln_name, fp);
}
(void)putc(term, fp);
(void)fflush(fp);
return;
}
void
ls_tty(ARCHD *arcn)
{
char f_date[DATELEN];
char f_mode[MODELEN];
format_date(&f_date[0], arcn->sb.st_mtime, time(NULL));
strmode(arcn->sb.st_mode, f_mode);
tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name);
return;
}
void
safe_print(const char *str, FILE *fp)
{
char visbuf[5];
const char *cp;
if (isatty(fileno(fp))) {
for (cp = str; *cp; cp++) {
(void)vis(visbuf, cp[0], VIS_CSTYLE, cp[1]);
(void)fputs(visbuf, fp);
}
} else {
(void)fputs(str, fp);
}
}
u_long
asc_ul(char *str, int len, int base)
{
char *stop;
u_long tval = 0;
stop = str + len;
while ((str < stop) && ((*str == ' ') || (*str == '0')))
++str;
if (base == HEX) {
while (str < stop) {
if ((*str >= '0') && (*str <= '9'))
tval = (tval << 4) + (*str++ - '0');
else if ((*str >= 'A') && (*str <= 'F'))
tval = (tval << 4) + 10 + (*str++ - 'A');
else if ((*str >= 'a') && (*str <= 'f'))
tval = (tval << 4) + 10 + (*str++ - 'a');
else
break;
}
} else {
while ((str < stop) && (*str >= '0') && (*str <= '7'))
tval = (tval << 3) + (*str++ - '0');
}
return(tval);
}
int
ul_asc(u_long val, char *str, int len, int base)
{
char *pt;
u_long digit;
pt = str + len - 1;
if (base == HEX) {
while (pt >= str) {
if ((digit = (val & 0xf)) < 10)
*pt-- = '0' + (char)digit;
else
*pt-- = 'a' + (char)(digit - 10);
if ((val = (val >> 4)) == (u_long)0)
break;
}
} else {
while (pt >= str) {
*pt-- = '0' + (char)(val & 0x7);
if ((val = (val >> 3)) == (u_long)0)
break;
}
}
while (pt >= str)
*pt-- = '0';
if (val != (u_long)0)
return(-1);
return(0);
}
#ifndef LONG_OFF_T
u_quad_t
asc_uqd(char *str, int len, int base)
{
char *stop;
u_quad_t tval = 0;
stop = str + len;
while ((str < stop) && ((*str == ' ') || (*str == '0')))
++str;
if (base == HEX) {
while (str < stop) {
if ((*str >= '0') && (*str <= '9'))
tval = (tval << 4) + (*str++ - '0');
else if ((*str >= 'A') && (*str <= 'F'))
tval = (tval << 4) + 10 + (*str++ - 'A');
else if ((*str >= 'a') && (*str <= 'f'))
tval = (tval << 4) + 10 + (*str++ - 'a');
else
break;
}
} else {
while ((str < stop) && (*str >= '0') && (*str <= '7'))
tval = (tval << 3) + (*str++ - '0');
}
return(tval);
}
int
uqd_asc(u_quad_t val, char *str, int len, int base)
{
char *pt;
u_quad_t digit;
pt = str + len - 1;
if (base == HEX) {
while (pt >= str) {
if ((digit = (val & 0xf)) < 10)
*pt-- = '0' + (char)digit;
else
*pt-- = 'a' + (char)(digit - 10);
if ((val = (val >> 4)) == (u_quad_t)0)
break;
}
} else {
while (pt >= str) {
*pt-- = '0' + (char)(val & 0x7);
if ((val = (val >> 3)) == (u_quad_t)0)
break;
}
}
while (pt >= str)
*pt-- = '0';
if (val != (u_quad_t)0)
return(-1);
return(0);
}
#endif