home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / find12as.zip / LISTFILE.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  7KB  |  302 lines

  1. /* listfile.c -- display a long listing of a file
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.
  26.  
  27.    $Header: e:/gnu/find/RCS/listfile.c 1.2.0.3 90/09/23 16:09:41 tho Exp $
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <pwd.h>
  34. #ifndef MSDOS
  35. struct passwd *getpwuid ();
  36. #include <grp.h>
  37. struct group *getgrgid ();
  38. #include <time.h>
  39. #endif /* not MSDOS */
  40. #ifdef USG
  41. #ifdef MSDOS
  42. #define minor(n) (n)
  43. #define major(n) (n)
  44. #else /* not MSDOS */
  45. #include <sys/sysmacros.h>
  46. #endif /* not MSDOS */
  47. #include <string.h>
  48. #else
  49. #include <strings.h>
  50. #endif
  51.  
  52. #ifdef MSDOS
  53. #include <time.h>
  54.  
  55. extern void list_file (char *name, struct stat *statp);
  56. extern char *xmalloc (unsigned n);
  57. extern void mode_string (unsigned short mode, char *str);
  58. extern char *copystring (char *string);
  59. extern void print_name_with_quoting (char *p);
  60. extern char *getuser (int uid);
  61. extern char *getgroup (int gid);
  62. #endif /* MSDOS */
  63.  
  64. extern int errno;
  65.  
  66. long time ();
  67. void free ();
  68.  
  69. char *xmalloc ();
  70. void error ();
  71. void mode_string ();
  72.  
  73. char *copystring ();
  74. char *get_link_name ();
  75. char *getgroup ();
  76. char *getuser ();
  77. void print_name_with_quoting ();
  78.  
  79. void
  80. list_file (name, statp)
  81.      char *name;
  82.      struct stat *statp;
  83. {
  84.   char modebuf[20];
  85.   char timebuf[40];
  86.   long current_time = time ((time_t *) 0);
  87.  
  88.   mode_string (statp->st_mode, modebuf);
  89.   modebuf[10] = '\0';
  90.  
  91.   strcpy (timebuf, ctime (&statp->st_mtime));
  92.   if (current_time - statp->st_mtime > 6L * 30L * 24L * 60L * 60L
  93.       || current_time - statp->st_mtime < 0L)
  94.     {
  95.       /* The file is fairly old or in the future.
  96.      POSIX says the cutoff is 6 months old;
  97.      approximate this by 6*30 days.
  98.      Show the year instead of the time of day.  */
  99.       strcpy (timebuf + 11, timebuf + 19);
  100.     }
  101.   timebuf[16] = 0;
  102.  
  103. #ifdef MSDOS            /* poor MS-DOS has no inodes, links, etc. . */
  104.  
  105.   printf ("%s ", modebuf);
  106.  
  107.   if ((statp->st_mode & S_IFMT) == S_IFCHR)
  108.     printf ("%3u, %3u ", major (statp->st_rdev), minor (statp->st_rdev));
  109.   else
  110.     printf ("%8lu ", statp->st_size);
  111.  
  112. #else /* not MSDOS */
  113.  
  114.   printf ("%6u ", statp->st_ino);
  115.  
  116.   /* The space between the mode and the number of links is the POSIX
  117.      "optional alternate access method flag". */
  118.   printf ("%s %3u ", modebuf, statp->st_nlink);
  119.  
  120.   printf ("%-8.8s ", getuser (statp->st_uid));
  121.  
  122.   printf ("%-8.8s ", getgroup (statp->st_gid));
  123.  
  124.   if ((statp->st_mode & S_IFMT) == S_IFCHR
  125.       || (statp->st_mode & S_IFMT) == S_IFBLK)
  126.     printf ("%3u, %3u ", major (statp->st_rdev), minor (statp->st_rdev));
  127.   else
  128.     printf ("%8lu ", statp->st_size);
  129.  
  130. #endif /* MSDOS */
  131.  
  132.   printf ("%s ", timebuf + 4);
  133.  
  134.   print_name_with_quoting (name);
  135.  
  136. #ifdef S_IFLNK
  137.   if ((statp->st_mode & S_IFMT) == S_IFLNK)
  138.     {
  139.       char *linkname = get_link_name (name, statp);
  140.  
  141.       if (linkname)
  142.     {
  143.       fputs (" -> ", stdout);
  144.       print_name_with_quoting (linkname);
  145.       free (linkname);
  146.     }
  147.     }
  148. #endif
  149.   putchar ('\n');
  150. }
  151.  
  152. void
  153. print_name_with_quoting (p)
  154.      register char *p;
  155. {
  156.   register unsigned char c;
  157.  
  158.   while (c = *p++)
  159.     {
  160.       switch (c)
  161.     {
  162.     case '\\':
  163.       printf ("\\\\");
  164.       break;
  165.  
  166.     case '\n':
  167.       printf ("\\n");
  168.       break;
  169.  
  170.     case '\b':
  171.       printf ("\\b");
  172.       break;
  173.  
  174.     case '\r':
  175.       printf ("\\r");
  176.       break;
  177.  
  178.     case '\t':
  179.       printf ("\\t");
  180.       break;
  181.  
  182.     case '\f':
  183.       printf ("\\f");
  184.       break;
  185.  
  186.     case ' ':
  187.       printf ("\\ ");
  188.       break;
  189.  
  190.     case '"':
  191.       printf ("\\\"");
  192.       break;
  193.  
  194.     default:
  195.       if (c > 040 && c < 0177)
  196.         putchar (c);
  197.       else
  198.         printf ("\\%03o", (unsigned int) c);
  199.     }
  200.     }
  201. }
  202.  
  203. #ifdef S_IFLNK
  204. char *
  205. get_link_name (filename, statp)
  206.      char *filename;
  207.      struct stat *statp;
  208. {
  209.   register char *linkbuf;
  210.   register int bufsiz = statp->st_size;
  211.  
  212.   linkbuf = (char *) xmalloc (bufsiz + 1);
  213.   linkbuf[bufsiz] = 0;
  214.   if (readlink (filename, linkbuf, bufsiz) < 0)
  215.     {
  216.       error (0, errno, "%s", filename);
  217.       free (linkbuf);
  218.       return 0;
  219.     }
  220.   return linkbuf;
  221. }
  222. #endif
  223.  
  224. struct userid
  225. {
  226.   int uid;
  227.   char *name;
  228.   struct userid *next;
  229. };
  230.  
  231. struct userid *user_alist;
  232.  
  233. /* Translate `uid' to a login name, with cache.  */
  234.  
  235. char *
  236. getuser (uid)
  237.      int uid;
  238. {
  239.   register struct userid *tail;
  240.   struct passwd *pwent;
  241.   char usernum_string[20];
  242.  
  243.   for (tail = user_alist; tail; tail = tail->next)
  244.     if (tail->uid == uid)
  245.       return tail->name;
  246.  
  247.   pwent = getpwuid (uid);
  248.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  249.   tail->uid = uid;
  250.   tail->next = user_alist;
  251.   if (pwent == 0)
  252.     {
  253.       sprintf (usernum_string, "%u", uid);
  254.       tail->name = copystring (usernum_string);
  255.     }
  256.   else
  257.     tail->name = copystring (pwent->pw_name);
  258.   user_alist = tail;
  259.   return tail->name;
  260. }
  261.  
  262. /* We use the same struct as for userids.  */
  263. struct userid *group_alist;
  264.  
  265. /* Translate `gid' to a group name, with cache.  */
  266.  
  267. char *
  268. getgroup (gid)
  269.      int gid;
  270. {
  271.   register struct userid *tail;
  272.   struct group *grent;
  273.   char groupnum_string[20];
  274.  
  275.   for (tail = group_alist; tail; tail = tail->next)
  276.     if (tail->uid == gid)
  277.       return tail->name;
  278.  
  279.   grent = getgrgid (gid);
  280.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  281.   tail->uid = gid;
  282.   tail->next = group_alist;
  283.   if (grent == 0)
  284.     {
  285.       sprintf (groupnum_string, "%u", gid);
  286.       tail->name = copystring (groupnum_string);
  287.     }
  288.   else
  289.     tail->name = copystring (grent->gr_name);
  290.   group_alist = tail;
  291.   return tail->name;
  292. }
  293.  
  294. /* Return a newly allocated copy of `string'. */
  295.  
  296. char *
  297. copystring (string)
  298.      char *string;
  299. {
  300.   return strcpy ((char *) xmalloc (strlen (string) + 1), string);
  301. }
  302.