/* sort input lines */
int main(int argc, char *argv[])
{
  int nlines;  /* number of input lines read */
  int tail = 0;  /* tail lines to print */
  int c;
  
  if(argc > 2) {  /* too many arguments */
    printf("usage: tail -n\n");
    return 0;
  }
  
  if(argc == 2)  /* the -n argument may be used */
    if((*++argv)[0] == '-') {
      while(c = *++argv[0] ) {
        if(isdigit(c))
          tail = 10*tail + ( c - '0' );
        else
          break;
      }
    }
  
  if( tail == 0 )
    tail = 10;  /* default to 10 lines */
    
  if( tail > MAXLINES) {
    printf("warning: requested tail of %d truncated to maximum value of %d\n",tail,MAXLINES);
    tail = MAXLINES;
  }
  
  if((nlines = readlines(lineptr,MAXLINES)) >= 0) {
    writetail(lineptr,nlines,tail);
    return 0;
  } else {
    printf("error: input too big to handle\n");
    return 1;
  }
  
  
}
示例#2
0
文件: mkmbrc.c 项目: TidOS/winopt
int main(int argc, char **argv) {
	FILE *input, *output;
	int size, offset;
	char *loader, *addcode;
	
	if(argc < 4) {
		fprintf(stderr, "USAGE: mkmbrc.exe <BINARY> <OUTPUT> <ADDRESSOFFSET> [ADDITIONAL CODE]\n");
		return -1;
	}

	printf("Opening %s...\n", argv[1]);
	if((input = fopen(argv[1], "rb")) == NULL) {
		fprintf(stderr, "ERROR: Could not open '%s' for reading.\n", argv[1]);
		return -1;
	}

	printf("Opening %s...\n", argv[2]);
	if((output = fopen(argv[2], "w")) == NULL) {
		fprintf(stderr, "ERROR: Could not open '%s' for writing.\n", argv[2]);
		fclose(input);
		return -1;
	}

	offset = atoi(argv[3]);

	if((loader = (char*)malloc(LDRSIZE)) == NULL) {
		fprintf(stderr, "ERROR: Could not allocate %d bytes of memory.\n", LDRSIZE);
		fclose(input);
		fclose(output);
		return -1;
	}

	fseek(input, 0, SEEK_END);
	size = ftell(input);
	if(size > LDRSIZE) {
		fprintf(stderr, "WARNING: Input file is bigger than %d bytes. (%d bytes)\n", LDRSIZE, size);
	} else if(size < LDRSIZE) {
		fprintf(stderr, "WARNING: Input file is smaller than %d bytes. (%d bytes)\n", LDRSIZE, size);
	}
	printf("Reading input file...\n");
	fflush(stdout);

	fseek(input, 0, SEEK_SET);
	fread(loader, LDRSIZE, 1, input);

	printf("Writing head...\n");
	writehead(output);
	printf("Writing table (Offset = %s)...\n", argv[3]);
	writetable(output, loader, LDRSIZE, argv[3]);
	printf("Writing tail...\n");
	writetail(output);
	printf("Done.\n");
	fclose(output);

	if(size > LDRSIZE) {
		if(argc < 5) {
			if((addcode = (char*)malloc(512)) == NULL)
				return -1;
			strcpy(addcode, "a.out");
		} else
			addcode = argv[4];

		printf("Writing additional code to %s...\n", addcode);

		output = fopen(addcode, "wb");

		do {
			size = fread(loader, LDRSIZE, 1, input);
			fwrite(loader, size * LDRSIZE, 1, output);
		} while (!feof(input));

		printf("Done.\n");
	}
	
	fclose(input);
	free(loader);
	return 0;
}