Print this page
4732 /usr/bin/file should provide -b option for compatiblity with GNU/BSD file
Reviewed by: Andy Stormont <andyjstormont@gmail.com>
Reviewed by: Serghei Samsi <sscdvp@gmail.com>
Reviewed by: Alexander Pyhalov <alp@rsu.ru>
Reviewed by: Garrett D'Amore <garrett@damore.org>


 188 
 189 #ifdef XPG4
 190         /* SUSv3 requires a single <space> after the colon */
 191 #define prf(x)  (void) printf("%s: ", x);
 192 #else   /* !XPG4 */
 193 #define prf(x)  (void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
 194 #endif  /* XPG4 */
 195 
 196 /*
 197  * Static program identifier - used to prevent localization of the name "file"
 198  * within individual error messages.
 199  */
 200 const char *File = "file";
 201 
 202 int
 203 main(int argc, char **argv)
 204 {
 205         char    *p;
 206         int     ch;
 207         FILE    *fl;

 208         int     cflg = 0;
 209         int     eflg = 0;
 210         int     fflg = 0;
 211         char    *ap = NULL;
 212         int     pathlen;
 213         char    **filep;
 214 
 215         (void) setlocale(LC_ALL, "");
 216 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 217 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 218 #endif
 219         (void) textdomain(TEXT_DOMAIN);
 220 
 221         while ((ch = getopt(argc, argv, "M:cdf:him:")) != EOF) {
 222                 switch (ch) {
 223 
 224                 case 'M':
 225                         add_to_mlist(optarg, !dflg);
 226                         M_flg++;
 227                         break;
 228 




 229                 case 'c':
 230                         cflg++;
 231                         break;
 232 
 233                 case 'd':
 234                         if (!dflg) {
 235                                 default_magic();
 236                                 add_to_mlist(dfile, 0);
 237                                 dflg++;
 238                         }
 239                         break;
 240 
 241                 case 'f':
 242                         fflg++;
 243                         errno = 0;
 244                         if ((fl = fopen(optarg, "r")) == NULL) {
 245                                 int err = errno;
 246                                 (void) fprintf(stderr, gettext("%s: cannot "
 247                                     "open file %s: %s\n"), File, optarg,
 248                                     err ? strerror(err) : "");


 271 
 272                 case 'i':
 273                         iflg++;
 274                         break;
 275 
 276                 case 'm':
 277                         add_to_mlist(optarg, !dflg);
 278                         mflg++;
 279                         break;
 280 
 281                 case '?':
 282                         eflg++;
 283                         break;
 284                 }
 285         }
 286         if (!cflg && !fflg && (eflg || optind == argc))
 287                 usage();
 288         if (iflg && (dflg || mflg || M_flg)) {
 289                 usage();
 290         }
 291         if (iflg && cflg) {
 292                 usage();
 293         }
 294 
 295         if (!dflg && !mflg && !M_flg && !iflg) {
 296         /* no -d, -m, nor -M option; also -i option doesn't need magic  */
 297                 default_magic();
 298                 if (f_mkmtab(dfile, cflg, 0) == -1) {
 299                         exit(2);
 300                 }
 301         }
 302 
 303         else if (mflg && !M_flg && !dflg) {
 304         /* -m specified without -d nor -M */
 305 
 306 #ifdef XPG4     /* For SUSv3 only */
 307 
 308                 /*
 309                  * The default position-dependent magic file tests
 310                  * in /etc/magic will follow all the -m magic tests.
 311                  */


 392                             "failed: %s\n"), File, strerror(err));
 393                         exit(1);
 394                 }
 395                 exit(0);
 396         }
 397 
 398         for (; fflg || optind < argc; optind += !fflg) {
 399                 register int    l;
 400 
 401                 if (fflg) {
 402                         if ((p = fgets(ap, pathlen, fl)) == NULL) {
 403                                 fflg = 0;
 404                                 optind--;
 405                                 continue;
 406                         }
 407                         l = strlen(p);
 408                         if (l > 0)
 409                                 p[l - 1] = '\0';
 410                 } else
 411                         p = argv[optind];


 412                 prf(p);                         /* print "file_name:<tab>" */
 413 
 414                 if (type(p))
 415                         tret = 1;
 416         }
 417         if (ap != NULL)
 418                 free(ap);
 419         if (tret != 0)
 420                 exit(tret);
 421 
 422         if (ferror(stdout) != 0) {
 423                 (void) fprintf(stderr, gettext("%s: error writing to "
 424                     "stdout\n"), File);
 425                 exit(1);
 426         }
 427         if (fclose(stdout) != 0) {
 428                 int err = errno;
 429                 (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
 430                     File, strerror(err));
 431                 exit(1);


1628                 const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1629                     "compressed " : "";
1630                 const char *l = swap(dh.dump_flags) & DF_LIVE ?
1631                     "live" : "crash";
1632 
1633                 (void) printf(gettext(
1634                     "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
1635                     dh.dump_utsname.sysname, dh.dump_utsname.release,
1636                     dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1637                     c, l, dh.dump_utsname.nodename);
1638         } else {
1639                 (void) printf(gettext("SunOS %u-bit %s crash dump\n"),
1640                     swap(dhp->dump_wordsize), isa);
1641         }
1642 }
1643 
1644 static void
1645 usage(void)
1646 {
1647         (void) fprintf(stderr, gettext(
1648             "usage: file [-dh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1649             "       file [-dh] [-M mfile] [-m mfile] -f ffile\n"
1650             "       file -i [-h] [-f ffile] file ...\n"
1651             "       file -i [-h] -f ffile\n"
1652             "       file -c [-d] [-M mfile] [-m mfile]\n"));
1653         exit(2);
1654 }
1655 
1656 static uint32_t
1657 swap_uint32(uint32_t in)
1658 {
1659         uint32_t out;
1660 
1661         out = (in & 0x000000ff) << 24;
1662         out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
1663         out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
1664         out |= (in & 0xff000000) >> 24;
1665 
1666         return (out);
1667 }
1668 
1669 static uint32_t
1670 return_uint32(uint32_t in)
1671 {




 188 
 189 #ifdef XPG4
 190         /* SUSv3 requires a single <space> after the colon */
 191 #define prf(x)  (void) printf("%s: ", x);
 192 #else   /* !XPG4 */
 193 #define prf(x)  (void) printf("%s:%s", x, (int)strlen(x) > 6 ? "\t" : "\t\t");
 194 #endif  /* XPG4 */
 195 
 196 /*
 197  * Static program identifier - used to prevent localization of the name "file"
 198  * within individual error messages.
 199  */
 200 const char *File = "file";
 201 
 202 int
 203 main(int argc, char **argv)
 204 {
 205         char    *p;
 206         int     ch;
 207         FILE    *fl;
 208         int     bflg = 0;
 209         int     cflg = 0;
 210         int     eflg = 0;
 211         int     fflg = 0;
 212         char    *ap = NULL;
 213         int     pathlen;
 214         char    **filep;
 215 
 216         (void) setlocale(LC_ALL, "");
 217 #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 218 #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 219 #endif
 220         (void) textdomain(TEXT_DOMAIN);
 221 
 222         while ((ch = getopt(argc, argv, "M:bcdf:him:")) != EOF) {
 223                 switch (ch) {
 224 
 225                 case 'M':
 226                         add_to_mlist(optarg, !dflg);
 227                         M_flg++;
 228                         break;
 229 
 230                 case 'b':
 231                         bflg++;
 232                         break;
 233 
 234                 case 'c':
 235                         cflg++;
 236                         break;
 237 
 238                 case 'd':
 239                         if (!dflg) {
 240                                 default_magic();
 241                                 add_to_mlist(dfile, 0);
 242                                 dflg++;
 243                         }
 244                         break;
 245 
 246                 case 'f':
 247                         fflg++;
 248                         errno = 0;
 249                         if ((fl = fopen(optarg, "r")) == NULL) {
 250                                 int err = errno;
 251                                 (void) fprintf(stderr, gettext("%s: cannot "
 252                                     "open file %s: %s\n"), File, optarg,
 253                                     err ? strerror(err) : "");


 276 
 277                 case 'i':
 278                         iflg++;
 279                         break;
 280 
 281                 case 'm':
 282                         add_to_mlist(optarg, !dflg);
 283                         mflg++;
 284                         break;
 285 
 286                 case '?':
 287                         eflg++;
 288                         break;
 289                 }
 290         }
 291         if (!cflg && !fflg && (eflg || optind == argc))
 292                 usage();
 293         if (iflg && (dflg || mflg || M_flg)) {
 294                 usage();
 295         }
 296         if ((iflg && cflg) || (cflg && bflg)) {
 297                 usage();
 298         }
 299 
 300         if (!dflg && !mflg && !M_flg && !iflg) {
 301         /* no -d, -m, nor -M option; also -i option doesn't need magic  */
 302                 default_magic();
 303                 if (f_mkmtab(dfile, cflg, 0) == -1) {
 304                         exit(2);
 305                 }
 306         }
 307 
 308         else if (mflg && !M_flg && !dflg) {
 309         /* -m specified without -d nor -M */
 310 
 311 #ifdef XPG4     /* For SUSv3 only */
 312 
 313                 /*
 314                  * The default position-dependent magic file tests
 315                  * in /etc/magic will follow all the -m magic tests.
 316                  */


 397                             "failed: %s\n"), File, strerror(err));
 398                         exit(1);
 399                 }
 400                 exit(0);
 401         }
 402 
 403         for (; fflg || optind < argc; optind += !fflg) {
 404                 register int    l;
 405 
 406                 if (fflg) {
 407                         if ((p = fgets(ap, pathlen, fl)) == NULL) {
 408                                 fflg = 0;
 409                                 optind--;
 410                                 continue;
 411                         }
 412                         l = strlen(p);
 413                         if (l > 0)
 414                                 p[l - 1] = '\0';
 415                 } else
 416                         p = argv[optind];
 417 
 418                 if (!bflg)
 419                         prf(p);         /* print "file_name:<tab>" */
 420 
 421                 if (type(p))
 422                         tret = 1;
 423         }
 424         if (ap != NULL)
 425                 free(ap);
 426         if (tret != 0)
 427                 exit(tret);
 428 
 429         if (ferror(stdout) != 0) {
 430                 (void) fprintf(stderr, gettext("%s: error writing to "
 431                     "stdout\n"), File);
 432                 exit(1);
 433         }
 434         if (fclose(stdout) != 0) {
 435                 int err = errno;
 436                 (void) fprintf(stderr, gettext("%s: fclose failed: %s\n"),
 437                     File, strerror(err));
 438                 exit(1);


1635                 const char *c = swap(dh.dump_flags) & DF_COMPRESSED ?
1636                     "compressed " : "";
1637                 const char *l = swap(dh.dump_flags) & DF_LIVE ?
1638                     "live" : "crash";
1639 
1640                 (void) printf(gettext(
1641                     "%s %s %s %u-bit %s %s%s dump from '%s'\n"),
1642                     dh.dump_utsname.sysname, dh.dump_utsname.release,
1643                     dh.dump_utsname.version, swap(dh.dump_wordsize), isa,
1644                     c, l, dh.dump_utsname.nodename);
1645         } else {
1646                 (void) printf(gettext("SunOS %u-bit %s crash dump\n"),
1647                     swap(dhp->dump_wordsize), isa);
1648         }
1649 }
1650 
1651 static void
1652 usage(void)
1653 {
1654         (void) fprintf(stderr, gettext(
1655             "usage: file [-bdh] [-M mfile] [-m mfile] [-f ffile] file ...\n"
1656             "       file [-bdh] [-M mfile] [-m mfile] -f ffile\n"
1657             "       file -i [-bh] [-f ffile] file ...\n"
1658             "       file -i [-bh] -f ffile\n"
1659             "       file -c [-d] [-M mfile] [-m mfile]\n"));
1660         exit(2);
1661 }
1662 
1663 static uint32_t
1664 swap_uint32(uint32_t in)
1665 {
1666         uint32_t out;
1667 
1668         out = (in & 0x000000ff) << 24;
1669         out |= (in & 0x0000ff00) << 8; /* >> 8 << 16 */
1670         out |= (in & 0x00ff0000) >> 8; /* >> 16 << 8 */
1671         out |= (in & 0xff000000) >> 24;
1672 
1673         return (out);
1674 }
1675 
1676 static uint32_t
1677 return_uint32(uint32_t in)
1678 {