• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

C Program for TAIL command of UNIX

April 27, 2015 by Dhaval Dave

/*
* usage: myTail [-n] [<filename>] */

#include <stdio.h>
#include <stdlib.h>

#define MAX_LINE_LEN 1024
#define DEFAULT_N 10

int main(int argc, char *argv[]) {
char **tail;
int count = DEFAULT_N, n, i, headX = 0, tailX = 0, z = 1;

/* Validate command line arguments, if any. */
/* 2 or more args; 1st argument may be -n, the count */ 

 if (argc >= 2) {
   if ((sscanf(argv[1], "%d", &n) == 1) && (n < 0)) {
   count = -n; /* save size of tail */
   z = 2; /* check argv[2], if present, for filename */
   }
/* 1st or 2nd arg may be the filename */
 if ((z < argc) && (freopen(argv[z], "r", stdin) == NULL)) {
   fprintf(stderr, "%s not found.\n", argv[z]);
 }
}

/* Allocate space for tail buffer */
 tail = calloc(count, sizeof(char *));

 for (i = 0; i < count; i++) {
   tail[i] = calloc(MAX_LINE_LEN, sizeof(char));
 }

/* Fill circular tail buffer until EOF */

while (fgets(tail[tailX], MAX_LINE_LEN, stdin) != NULL) {
   tailX = (tailX + 1) % count;
   if (tailX == headX) {
     headX = (headX + 1) % count; 
   }
 }

/* Display tail */
i = tailX;
do {
  printf("%s", tail[i]);
  i = (i + 1) % count;
  } while (i != tailX);

return 0;
}

 

Sample runs (before this ‘#if 0’ text added):

$ ./myTail -7 myTail.c

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, problem Tagged With: c, Unix

Reader Interactions

Comments

  1. Petra says

    August 2, 2015 at 9:50 am

    gohired.in has potential, you can make your page go viral easily
    using one tricky method. Just type in google:
    Kimting’s Method To Go Viral

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Facebook Interview Question : Interleave List

Given a float number convert it into the string WITHOUT using any inbuilt Function

Python Array String

Diagonal Traversal of Binary Tree

BFS (Breath First Search)

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

CodeChef’ RRCOPY

SAP Interview Questions

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Sort Stack in place

HackeEarth Flipkart’s Drone

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

How strtok() Works

Minimum insertions to form a palindrome

Longest Increasing Subsequence

Find min element in Sorted Rotated Array (Without Duplicates)

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

System Design: Designing a LLD for Hotel Booking

Amazon Interview Experience – SDE Chennai

SAP Off Campus Hiring_ March 2015 Verbal Skills

Calculate price of parking from parking start end time prices

Hackerearth : Counting Subarrays

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Find loop in Singly linked list

Circular Linked List

Find if two rectangles overlap

Stickler thief

Copyright © 2026 · Genesis Framework · WordPress · Log in