-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathruler.c
More file actions
61 lines (53 loc) · 1021 Bytes
/
Copy pathruler.c
File metadata and controls
61 lines (53 loc) · 1021 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#ifdef __linux__
#define HAS_IOCTL
#endif
#ifdef HAS_IOCTL
#include <sys/ioctl.h>
#define USAGE_STRING "Usage: %s [columns]\n"
#else
#define USAGE_STRING "Usage: %s columns\n"
#endif
int main(int argc, char *argv[])
{
int cols;
int arg_error = 0;
if (argc == 1) {
#ifdef HAS_IOCTL
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1)
{
fprintf(stderr, "%s: error getting terminal size\n", argv[0]);
return 1;
}
cols = ws.ws_col;
#else
arg_error = 1;
#endif
} else if (argc == 2) {
cols = atoi(argv[1]);
if (cols == 0) {
fprintf(stderr, "Argument must be a number 1 or greater.\n");
arg_error = 1;
}
} else {
arg_error = 1;
}
if (arg_error) {
printf(USAGE_STRING, argv[0]);
return 2;
}
int digits = snprintf(NULL, 0, "%u", cols-1);
int d; for (d=digits-1; d>=0; --d) {
int x; for (x=0; x<cols; ++x) {
int n = x;
int i; for (i=0; i<d; ++i) {
n /= 10;
}
putchar('0'+n%10);
}
putchar('\n');
}
return 0;
}