File: limo.c

package info (click to toggle)
limo 0.2.1-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 132 kB
  • ctags: 143
  • sloc: ansic: 1,406; makefile: 80
file content (378 lines) | stat: -rw-r--r-- 11,763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* $Id: limo.c,v 1.6 1999/06/15 23:46:34 fraserm Exp $
   $Log: limo.c,v $
   Revision 1.6  1999/06/15 23:46:34  fraserm
   added ioctl and COLUMNS output_width checks

   Revision 1.5  1999/05/30 11:35:15  fraserm
   added support for multiple sort chunks

   Revision 1.4  1999/05/30 01:07:16  fraserm
   -v verbose now shows chunk meanings
   -h help lists chunk meanings

   Revision 1.3  1999/05/26 23:51:43  fraserm
   added custom command stuff

   Revision 1.2  1999/05/16 19:49:15  fraserm
   *** empty log message ***

   Revision 1.1  1999/04/28 00:34:44  fraserm
   Initial revision

*/

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include "limo.h"

/* main module for custom ls - lc */

extern char *optarg;
extern int optind;

/* regrettably this must be global, so that the compare routine passed to
   qsort() can see it */
extern char *sort_chunks;
/* these don't have to be global, but I'm going to make them global anyway */
struct custcmd cmds[MAXCUSTCMDS]; /* custom commands */
int numcustcmds = 0; /* number of custom commands defined */

/* helpful hints and stuff */
void showhelp(char querychunk)
{
  int i, j;
  char chunks[] = {
    NAME, NAMEID, DESCSHORT, DESCLONG, SIZEBLOCKS, BYTESSHORT, BYTESLONG,
    MODESHORT, MODELONG, OWNERNUMBER, OWNERNAME, GROUPNUMBER, GROUPNAME,
    INODE, LINKS, ACCESSSHORT, ACCESSLONG, MODIFYSHORT, MODIFYLONG,
    CHANGESHORT, CHANGELONG, RELATIVE, RAWTIME, LINK, '\0'
  };
  char *chunkhelp[] = {
    D_NAME, D_NAMEID, D_DESCSHORT, D_DESCLONG, D_SIZEBLOCKS, D_BYTESSHORT,
    D_BYTESLONG, D_MODESHORT, D_MODELONG, D_OWNERNUMBER, D_OWNERNAME,
    D_GROUPNUMBER, D_GROUPNAME, D_INODE, D_LINKS, D_ACCESSSHORT, D_ACCESSLONG,
    D_MODIFYSHORT, D_MODIFYLONG, D_CHANGESHORT, D_CHANGELONG, D_RELATIVE,
    D_RAWTIME, D_LINK
  };

  if (querychunk == HELP) {
    printf("\nChunks are:\n");
  }
  for (i = 0, j = 0; chunks[i] != '\0'; ++i) {
    if (querychunk == HELP) {
      printf("%c - %-24s%s", chunks[i], chunkhelp[i],
	     (j++ % 2) ? "\n" : "");
    }
    else if (querychunk == chunks[i]) {
      printf(":%c=%s", chunks[i], chunkhelp[i]);
    }
  }
  if (j % 2) {
    printf("\n");
  }
}

/* show usage */
void usage(int argc, char *argv[])
{
  printf("This is Limo (LIst files with Many Options), version %s.\nThis is free software distributed under the terms of the GNU General Public\nLicence. You are welcome to distribute copies of it under certain conditions\noutlined in the file LICENCE, supplied with the distribution.\nThere is no warranty for this software, not even the implied warranty of\nmerchantability or fitness for a particular purpose.\n\n", VERSION);
  fprintf(stderr, "%s [-o<sortopt>] [-p<style>] [-f<output-format>] [opts] [file ...]\n\tsortopt = + or - followed by sort code, default '%s'\n\tstyle = d (down) / a (across) / , (comma separated), default '%c'\n\toutput-format = list of chunks to show for each file, default '%s'\n",
	  argv[0], SORT_CHUNKS_DEFAULT,
	  STYLE_DEFAULT, DEFAULTCHUNKS);
  showhelp(HELP);
}

int set_sort(int argc, char *argv[], char *s)
{
  char *i;

  for (i = s; *i != '\0'; ++i) {
    if (*i != SORT_DIR_ASC && *i != SORT_DIR_DESC
	&& index(SORT_ALLOWED_CHUNKS, *i) == NULL) {
      fprintf(stderr, "%s: '%c' in '%s' is not a valid sort chunk character\n",
	      argv[0], *i, s);
      return ERROR_ARGS;
    }
  }
  sort_chunks = s;
  return 0;
}

int main(int argc, char *argv[])
{
  int o;
  char print_style = STYLE_DEFAULT;
  int output_width = WIDTH_DEFAULT;
  int tmp_output_width;
  char *txtwidth; /* just used to get the width from the environment */
  int options = OPTIONS_DEFAULT;
  char *chunks; /* which chunks are output for each file */
  struct flist deffilelst; /* list of files given on the command
			      line that must be examined */
  struct flist defdirlst; /* ditto for directories, listed after the
			     files - unless -d in effect, in which
			     case, empty */
  unsigned long i;
  struct fnode *tmp;
  time_t now;
  struct stat cfile;
  char *j;

  /* get defaults from environment variables, if any */
  if ((chunks = getenv(LIMO_FORMAT)) == NULL || *chunks == '\0') {
    chunks = DEFAULTCHUNKS;
  }
  if ((sort_chunks = getenv(LIMO_ORDER)) == NULL || *sort_chunks == '\0') {
    sort_chunks = SORT_CHUNKS_DEFAULT;
  }
  /* override the screen width with the terminal parameters, if possible */
#ifdef TIOCGWINSZ
  {
    struct winsize ws;

    if (ioctl (1, TIOCGWINSZ, &ws) != -1 && ws.ws_col != 0)
      output_width = ws.ws_col;
  }
#endif
  /* override the screen width with the environment, if it exists */
  if ((txtwidth = getenv(LIMO_WIDTH)) != NULL && *txtwidth != '\0') {
    if (sscanf(txtwidth, "%d", &tmp_output_width) != 1
	|| tmp_output_width < 1) {
      fprintf(stderr, "%s: warning: env variable %s contains invalid output width '%s'\n", argv[0], LIMO_WIDTH, txtwidth);
    }
    else {
      output_width = tmp_output_width;
    }
  }
  /* get the current time; this time is overridden by the -c (compare file)
     option */
  if ((now = time(NULL)) == (time_t)-1) {
    fprintf(stderr, "%s: time: %s\n", argv[0], strerror(errno));
    exit(ERROR_PROC);
  }
  while ((o = getopt(argc, argv, "c:o:p:w:f:e:adhlqRsv")) != EOF) {
    /* parse command line options */
    switch (o) {
    case 'c': /* compare file - this file is used as the value for "now"
		 for the file age options */
      /* lstat() the file in question */
      if (lstat(optarg, &cfile) != 0) { /* stat failed, just bomb */
	fprintf(stderr, "%s: can't compare with file %s: %s\n",
		argv[0], optarg, strerror(errno));
	exit(ERROR_ARGS);
      }
      options |= USE_COMPARE_FILE;
      break;
    case 'o': /* set the sort chunks */
      if (set_sort(argc, argv, optarg) == ERROR_ARGS) {
	exit(ERROR_ARGS);
      }
      break;
    case 'p':
      switch (*optarg) {
      case COMMA_JUST:
      case COLUMN_DOWN:
      case COLUMN_ACROSS:
	print_style = *optarg;
	break;
      default:
	fprintf(stderr, "%s: '%c' is not a valid print style character\n",
		argv[0], *optarg);
	exit(ERROR_ARGS);
	break;
      }
      break;
    case 'w':
      if (sscanf(optarg, "%d", &output_width) != 1
	  || output_width < 1) {
	fprintf(stderr, "%s: invalid output width '%s'\n", argv[0], optarg);
	exit(ERROR_ARGS);
      }
      break;
    case 'f': /* output format - set chunks */
      chunks = optarg;
      if (*chunks == '\0') {
	  fprintf(stderr, "%s: empty list of chunks\n", argv[0]);
	  exit(ERROR_ARGS);
      }
      break;
    case 'e': /* define a custom command */
      if (numcustcmds >= MAXCUSTCMDS) {
	fprintf(stderr, "%s: too many custom commands, '%s' ignored\n",
		argv[0], optarg);
      }
      else {
	int cmdindex;
	char *cmd;

	if (sscanf(optarg, "%d:%d:%n",
		   &cmds[numcustcmds].linenum,
		   &cmds[numcustcmds].wordnum,
		   &cmdindex)
	    < 2
	    || cmds[numcustcmds].linenum < 1
	    || cmds[numcustcmds].wordnum < 1) {
	  fprintf(stderr, "%s: custom command must be: line-num:word-num:command\n\t('%s')\n",
		  argv[0], optarg);
	  exit(ERROR_ARGS);
	}
	else { /* custom command is in proper format - now parse it */
	  cmd = optarg + cmdindex;
	  if (strstr(cmd, "'%s'") != NULL) { /* user has kindly quoted it
						themselves, no reformatting
						necessary */
	    if ((cmds[numcustcmds].cmd = (char *) malloc(strlen(cmd)+1))
		== NULL) {
	      fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	      exit(ERROR_ARGS);
	    }
	    strcpy(cmds[numcustcmds].cmd, cmd);
	  }
	  else if (strstr(cmd, "%s") != NULL) { /* user just put a %s, we must
						  quote it to be safe */
	    if ((cmds[numcustcmds].cmd = (char *) malloc(strlen(cmd)+3))
		== NULL) { /* +3 = 1 for the terminating \0 and 2 for the
			      extra 'quotes' */
	      fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	      exit(ERROR_ARGS);
	    }
	    sprintf(cmds[numcustcmds].cmd, cmd, "'%s'");
	  }
	  else { /* user didn't put a %s at all, we better add one */
	    if ((cmds[numcustcmds].cmd = (char *) malloc(strlen(cmd)+6))
		== NULL) { /* +6 = 1 for the terminating \0 and 5 for the
			      extra " '%s'" */
	      fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	      exit(ERROR_ARGS);
	    }
	    strcpy(cmds[numcustcmds].cmd, cmd);
	    strcat(cmds[numcustcmds].cmd, " '%s'");
	  }
	  ++numcustcmds;
	}
      }
      break;
    case 'a':
      options |= SHOW_ALL;
      break;
    case 'd':
      options |= DIRS_AS_FILES;
      break;
    case 'h': /* help - print usage information */
      usage(argc, argv);
      exit(ERROR_SUCCESS);
      break;
    case 'l': /* simulate ls -l */
      chunks = LONGCHUNKS;
      break;
    case 'q':
      options |= QUOTE_UNPRINTABLE;
      break;
    case 'R': /* recurse into directories */
      options |= (RECURSE | SHOW_DIR_HEAD);
      break;
    case 's': /* simulate ls -s */
      chunks = SHORTCHUNKS;
      break;
    case 'v': /* verbose */
      options |= VERBOSE;
      break;
    case '?':
      exit(ERROR_ARGS);
      break;
    default:
      fprintf(stderr, "%s: internal error; fell out of option switch with option '%c'\n",
	      argv[0], o);
      exit(ERROR_ARGS);
      break;
    }
  }
  /* right, got all the options, down to the nitty-gritty */
  /* display directory header lines? */
  if (argc - optind > 1) { /* more than one argument */
    options |= SHOW_DIR_HEAD;
  }
  /* for each file, get the user and group names? */
  if (index(sort_chunks, OWNERNAME) != NULL) {
    options |= GET_OWNER;
  }
  else if (index(sort_chunks, GROUPNAME) != NULL) {
    options |= GET_GROUP;
  }
  for (j = chunks; *j != '\0'; ++j) {
    switch (*j) {
    case OWNERNAME:
      options |= GET_OWNER;
      break;
    case GROUPNAME:
      options |= GET_GROUP;
      break;
    case LINK:
      options |= GET_LINK_TARGET;
      break;
    default:
      /* nothing */
    }
  }
  /* clear the lists */
  flist_clear(argc, argv, &defdirlst);
  flist_clear(argc, argv, &deffilelst);
  if (optind == argc) { /* no actual files given on the command line, so just
			   do the currect directory */
    if ((tmp = newfnode(argc, argv, ".", options))
	== NULL) { /* this creates the record and does all the stat()ing
		      and stuff */
      exit(ERROR_PROC);
    }
    if (flist_append(argc, argv, &defdirlst, tmp) == 0) {
      exit(ERROR_PROC);
    }
  }
  else {
    for (; optind < argc; ++optind) {
      if ((tmp = newfnode(argc, argv, argv[optind], options))
	  == NULL) { /* this creates the record and does all the stat()ing
			and stuff */
	exit(ERROR_PROC);
      }
      if (tmp->s != NULL) { /* check that the stat succeeded, if not, ignore
			       this file */
	if (S_ISDIR(tmp->s->st_mode) && !(options & DIRS_AS_FILES)) {
	  if (flist_append(argc, argv, &defdirlst, tmp) == 0) {
	    exit(ERROR_PROC);
	  }
	}
	else {
	  if (flist_append(argc, argv, &deffilelst, tmp) == 0) {
	    exit(ERROR_PROC);
	  }
	}
      }
    }
  }
  /* okay, now we have everything that we want to show in one list or
     another */
  if (options & VERBOSE) {
    printf("%s: output format: \"%s\" order: \"%s\" style: \"%c\" line width: %d\n",
	   argv[0], chunks, sort_chunks, print_style, output_width);
    printf("Format chunks are, in order:\n");
    for (j = chunks; *j != '\0'; ++j) {
      showhelp(*j);
    }
    printf(":\n");
  }
  display_file_list(argc, argv, &deffilelst, print_style, output_width,
		    chunks, &cfile, options, now);
  flist_sort(argc, argv, &defdirlst);
  for (i = 0; i < defdirlst.size; ++i) {
    display_directory(argc, argv, defdirlst.fa[i], print_style,
		      output_width, chunks, &cfile, options, now);
  }
  return ERROR_SUCCESS;
}