Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <limits.h>
- enum
- {
- RIGHTS = 0666,
- };
- void
- write_le(int fd, int val)
- {
- char tmp[sizeof(val)];
- unsigned mask = (1 << CHAR_BIT) - 1;
- for (int i = 0; i < sizeof(val); ++i) {
- tmp[i] = val & mask;
- val >>= CHAR_BIT;
- }
- write(fd, tmp, sizeof(tmp));
- }
- void
- write_be(int fd, int val)
- {
- char tmp[sizeof(val)];
- unsigned mask = (1 << CHAR_BIT) - 1;
- for (int i = sizeof(val) - 1; i >= 0; --i) {
- tmp[i] = val & mask;
- val >>= CHAR_BIT;
- }
- write(fd, tmp, sizeof(tmp));
- }
- int
- main(int argc, char **argv)
- {
- int fd = open(argv[1], O_CREAT | O_RDWR | O_TRUNC, RIGHTS);
- switch (argv[2][0]) {
- case 'L':
- for (int i = 1; i < 11; ++i) {
- write_le(fd, i);
- }
- break;
- case 'B':
- for (int i = 1; i < 11; ++i) {
- write_be(fd, i);
- }
- break;
- default:
- for (int i = 1; i < 11; ++i) {
- write(fd, &i, sizeof(i));
- }
- }
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement