1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992 Keith Muller. 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Keith Muller of the University of California, San Diego. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 #include <sys/types.h> 40 #include <sys/time.h> 41 #include <sys/stat.h> 42 #include <langinfo.h> 43 #include <stdint.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include "pax.h" 47 #include "extern.h" 48 49 /* 50 * a collection of general purpose subroutines used by pax 51 */ 52 53 /* 54 * constants used by ls_list() when printing out archive members 55 */ 56 #define MODELEN 20 57 #define DATELEN 64 58 #define SIXMONTHS ((365 / 2) * 86400) 59 #define CURFRMTM "%b %e %H:%M" 60 #define OLDFRMTM "%b %e %Y" 61 #define CURFRMTD "%e %b %H:%M" 62 #define OLDFRMTD "%e %b %Y" 63 64 static int d_first = -1; 65 66 /* 67 * ls_list() 68 * list the members of an archive in ls format 69 */ 70 71 void 72 ls_list(ARCHD *arcn, time_t now, FILE *fp) 73 { 74 struct stat *sbp; 75 char f_mode[MODELEN]; 76 char f_date[DATELEN]; 77 const char *timefrmt; 78 79 /* 80 * if not verbose, just print the file name 81 */ 82 if (!vflag) { 83 (void)fprintf(fp, "%s\n", arcn->name); 84 (void)fflush(fp); 85 return; 86 } 87 88 if (d_first < 0) 89 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 90 /* 91 * user wants long mode 92 */ 93 sbp = &(arcn->sb); 94 strmode(sbp->st_mode, f_mode); 95 96 /* 97 * time format based on age compared to the time pax was started. 98 */ 99 if ((sbp->st_mtime + SIXMONTHS) <= now) 100 timefrmt = d_first ? OLDFRMTD : OLDFRMTM; 101 else 102 timefrmt = d_first ? CURFRMTD : CURFRMTM; 103 104 /* 105 * print file mode, link count, uid, gid and time 106 */ 107 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0) 108 f_date[0] = '\0'; 109 (void)fprintf(fp, "%s%2ju %-12s %-12s ", f_mode, 110 (uintmax_t)sbp->st_nlink, 111 name_uid(sbp->st_uid, 1), name_gid(sbp->st_gid, 1)); 112 113 /* 114 * print device id's for devices, or sizes for other nodes 115 */ 116 if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK)) 117 (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)MAJOR(sbp->st_rdev), 118 (unsigned long)MINOR(sbp->st_rdev)); 119 else { 120 (void)fprintf(fp, "%9ju ", (uintmax_t)sbp->st_size); 121 } 122 123 /* 124 * print name and link info for hard and soft links 125 */ 126 (void)fprintf(fp, "%s %s", f_date, arcn->name); 127 if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG)) 128 (void)fprintf(fp, " == %s\n", arcn->ln_name); 129 else if (arcn->type == PAX_SLK) 130 (void)fprintf(fp, " => %s\n", arcn->ln_name); 131 else 132 (void)putc('\n', fp); 133 (void)fflush(fp); 134 return; 135 } 136 137 /* 138 * tty_ls() 139 * print a short summary of file to tty. 140 */ 141 142 void 143 ls_tty(ARCHD *arcn) 144 { 145 char f_date[DATELEN]; 146 char f_mode[MODELEN]; 147 const char *timefrmt; 148 149 if (d_first < 0) 150 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 151 152 if ((arcn->sb.st_mtime + SIXMONTHS) <= time(NULL)) 153 timefrmt = d_first ? OLDFRMTD : OLDFRMTM; 154 else 155 timefrmt = d_first ? CURFRMTD : CURFRMTM; 156 157 /* 158 * convert time to string, and print 159 */ 160 if (strftime(f_date, DATELEN, timefrmt, 161 localtime(&(arcn->sb.st_mtime))) == 0) 162 f_date[0] = '\0'; 163 strmode(arcn->sb.st_mode, f_mode); 164 tty_prnt("%s%s %s\n", f_mode, f_date, arcn->name); 165 return; 166 } 167 168 /* 169 * l_strncpy() 170 * copy src to dest up to len chars (stopping at first '\0'). 171 * when src is shorter than len, pads to len with '\0'. 172 * Return: 173 * number of chars copied. (Note this is a real performance win over 174 * doing a strncpy(), a strlen(), and then a possible memset()) 175 */ 176 177 int 178 l_strncpy(char *dest, const char *src, int len) 179 { 180 char *stop; 181 char *start; 182 183 stop = dest + len; 184 start = dest; 185 while ((dest < stop) && (*src != '\0')) 186 *dest++ = *src++; 187 len = dest - start; 188 while (dest < stop) 189 *dest++ = '\0'; 190 return(len); 191 } 192 193 /* 194 * asc_ul() 195 * convert hex/octal character string into a u_long. We do not have to 196 * check for overflow! (the headers in all supported formats are not large 197 * enough to create an overflow). 198 * NOTE: strings passed to us are NOT TERMINATED. 199 * Return: 200 * unsigned long value 201 */ 202 203 u_long 204 asc_ul(char *str, int len, int base) 205 { 206 char *stop; 207 u_long tval = 0; 208 209 stop = str + len; 210 211 /* 212 * skip over leading blanks and zeros 213 */ 214 while ((str < stop) && ((*str == ' ') || (*str == '0'))) 215 ++str; 216 217 /* 218 * for each valid digit, shift running value (tval) over to next digit 219 * and add next digit 220 */ 221 if (base == HEX) { 222 while (str < stop) { 223 if ((*str >= '0') && (*str <= '9')) 224 tval = (tval << 4) + (*str++ - '0'); 225 else if ((*str >= 'A') && (*str <= 'F')) 226 tval = (tval << 4) + 10 + (*str++ - 'A'); 227 else if ((*str >= 'a') && (*str <= 'f')) 228 tval = (tval << 4) + 10 + (*str++ - 'a'); 229 else 230 break; 231 } 232 } else { 233 while ((str < stop) && (*str >= '0') && (*str <= '7')) 234 tval = (tval << 3) + (*str++ - '0'); 235 } 236 return(tval); 237 } 238 239 /* 240 * ul_asc() 241 * convert an unsigned long into an hex/oct ascii string. pads with LEADING 242 * ascii 0's to fill string completely 243 * NOTE: the string created is NOT TERMINATED. 244 */ 245 246 int 247 ul_asc(u_long val, char *str, int len, int base) 248 { 249 char *pt; 250 u_long digit; 251 252 /* 253 * WARNING str is not '\0' terminated by this routine 254 */ 255 pt = str + len - 1; 256 257 /* 258 * do a tailwise conversion (start at right most end of string to place 259 * least significant digit). Keep shifting until conversion value goes 260 * to zero (all digits were converted) 261 */ 262 if (base == HEX) { 263 while (pt >= str) { 264 if ((digit = (val & 0xf)) < 10) 265 *pt-- = '0' + (char)digit; 266 else 267 *pt-- = 'a' + (char)(digit - 10); 268 if ((val = (val >> 4)) == (u_long)0) 269 break; 270 } 271 } else { 272 while (pt >= str) { 273 *pt-- = '0' + (char)(val & 0x7); 274 if ((val = (val >> 3)) == (u_long)0) 275 break; 276 } 277 } 278 279 /* 280 * pad with leading ascii ZEROS. We return -1 if we ran out of space. 281 */ 282 while (pt >= str) 283 *pt-- = '0'; 284 if (val != (u_long)0) 285 return(-1); 286 return(0); 287 } 288 289 /* 290 * asc_uqd() 291 * convert hex/octal character string into a u_quad_t. We do not have to 292 * check for overflow! (the headers in all supported formats are not large 293 * enough to create an overflow). 294 * NOTE: strings passed to us are NOT TERMINATED. 295 * Return: 296 * u_quad_t value 297 */ 298 299 u_quad_t 300 asc_uqd(char *str, int len, int base) 301 { 302 char *stop; 303 u_quad_t tval = 0; 304 305 stop = str + len; 306 307 /* 308 * skip over leading blanks and zeros 309 */ 310 while ((str < stop) && ((*str == ' ') || (*str == '0'))) 311 ++str; 312 313 /* 314 * for each valid digit, shift running value (tval) over to next digit 315 * and add next digit 316 */ 317 if (base == HEX) { 318 while (str < stop) { 319 if ((*str >= '0') && (*str <= '9')) 320 tval = (tval << 4) + (*str++ - '0'); 321 else if ((*str >= 'A') && (*str <= 'F')) 322 tval = (tval << 4) + 10 + (*str++ - 'A'); 323 else if ((*str >= 'a') && (*str <= 'f')) 324 tval = (tval << 4) + 10 + (*str++ - 'a'); 325 else 326 break; 327 } 328 } else { 329 while ((str < stop) && (*str >= '0') && (*str <= '7')) 330 tval = (tval << 3) + (*str++ - '0'); 331 } 332 return(tval); 333 } 334 335 /* 336 * uqd_asc() 337 * convert an u_quad_t into a hex/oct ascii string. pads with LEADING 338 * ascii 0's to fill string completely 339 * NOTE: the string created is NOT TERMINATED. 340 */ 341 342 int 343 uqd_asc(u_quad_t val, char *str, int len, int base) 344 { 345 char *pt; 346 u_quad_t digit; 347 348 /* 349 * WARNING str is not '\0' terminated by this routine 350 */ 351 pt = str + len - 1; 352 353 /* 354 * do a tailwise conversion (start at right most end of string to place 355 * least significant digit). Keep shifting until conversion value goes 356 * to zero (all digits were converted) 357 */ 358 if (base == HEX) { 359 while (pt >= str) { 360 if ((digit = (val & 0xf)) < 10) 361 *pt-- = '0' + (char)digit; 362 else 363 *pt-- = 'a' + (char)(digit - 10); 364 if ((val = (val >> 4)) == (u_quad_t)0) 365 break; 366 } 367 } else { 368 while (pt >= str) { 369 *pt-- = '0' + (char)(val & 0x7); 370 if ((val = (val >> 3)) == (u_quad_t)0) 371 break; 372 } 373 } 374 375 /* 376 * pad with leading ascii ZEROS. We return -1 if we ran out of space. 377 */ 378 while (pt >= str) 379 *pt-- = '0'; 380 if (val != (u_quad_t)0) 381 return(-1); 382 return(0); 383 } 384