Compare commits
8 Commits
8fa443879c
...
cb7d5c9d09
Author | SHA1 | Date |
---|---|---|
Bananymous | cb7d5c9d09 | |
Bananymous | 097ab82529 | |
Bananymous | ccb603d77f | |
Bananymous | d7b02db832 | |
Bananymous | 56cc5da9fb | |
Bananymous | 1903079f96 | |
Bananymous | b6d0950ee9 | |
Bananymous | c1a32a4041 |
|
@ -2,6 +2,8 @@ set(AOC2024_PROJECTS
|
|||
day1
|
||||
day2
|
||||
day3
|
||||
day4
|
||||
day5
|
||||
full
|
||||
)
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(aoc2024_day4 ${SOURCES})
|
||||
banan_include_headers(aoc2024_day4 ban)
|
||||
banan_link_library(aoc2024_day4 libc)
|
||||
|
||||
install(TARGETS aoc2024_day4 OPTIONAL)
|
|
@ -0,0 +1,132 @@
|
|||
#include <BAN/StringView.h>
|
||||
#include <BAN/Vector.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
using i8 = int8_t;
|
||||
using i16 = int16_t;
|
||||
using i32 = int32_t;
|
||||
using i64 = int64_t;
|
||||
|
||||
using u8 = uint8_t;
|
||||
using u16 = uint16_t;
|
||||
using u32 = uint32_t;
|
||||
using u64 = uint64_t;
|
||||
|
||||
struct Grid2D
|
||||
{
|
||||
size_t width { 0 };
|
||||
size_t height { 0 };
|
||||
BAN::Vector<char> data;
|
||||
};
|
||||
|
||||
static Grid2D read_grid2d(FILE* fp)
|
||||
{
|
||||
size_t width { 0 };
|
||||
size_t height { 0 };
|
||||
BAN::Vector<char> data;
|
||||
|
||||
char buffer[1024];
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
{
|
||||
const size_t len = strlen(buffer);
|
||||
if (len == 0 || buffer[0] == '\n')
|
||||
break;
|
||||
if (data.empty())
|
||||
width = len - 1;
|
||||
height++;
|
||||
|
||||
ASSERT(buffer[width] == '\n');
|
||||
|
||||
if (data.capacity() < height * width)
|
||||
MUST(data.reserve(2 * height * width));
|
||||
|
||||
MUST(data.resize(height * width));
|
||||
memcpy(&data[(height - 1) * width], buffer, width);
|
||||
}
|
||||
|
||||
(void)data.shrink_to_fit();
|
||||
|
||||
return Grid2D {
|
||||
.width = width,
|
||||
.height = height,
|
||||
.data = BAN::move(data),
|
||||
};
|
||||
}
|
||||
|
||||
static bool check_match(const Grid2D& puzzle, size_t x, size_t y, i32 step_x, i32 step_y, BAN::StringView target)
|
||||
{
|
||||
if (i32 ex = (i32)x + step_x * (i32)(target.size() - 1); ex < 0 || ex >= (i32)puzzle.width)
|
||||
return false;
|
||||
if (i32 ey = (i32)y + step_y * (i32)(target.size() - 1); ey < 0 || ey >= (i32)puzzle.height)
|
||||
return false;
|
||||
for (size_t i = 0; i < target.size(); i++)
|
||||
if (target[i] != puzzle.data[(y + i * step_y) * puzzle.width + (x + i * step_x)])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
i64 part1(FILE* fp)
|
||||
{
|
||||
i64 result = 0;
|
||||
|
||||
auto puzzle = read_grid2d(fp);
|
||||
for (size_t y = 0; y < puzzle.height; y++) {
|
||||
for (size_t x = 0; x < puzzle.width; x++) {
|
||||
result += check_match(puzzle, x, y, 1, 0, "XMAS"_sv);
|
||||
result += check_match(puzzle, x, y, 0, 1, "XMAS"_sv);
|
||||
result += check_match(puzzle, x, y, 1, 1, "XMAS"_sv);
|
||||
result += check_match(puzzle, x, y, 1, -1, "XMAS"_sv);
|
||||
|
||||
result += check_match(puzzle, x, y, 1, 0, "SAMX"_sv);
|
||||
result += check_match(puzzle, x, y, 0, 1, "SAMX"_sv);
|
||||
result += check_match(puzzle, x, y, 1, 1, "SAMX"_sv);
|
||||
result += check_match(puzzle, x, y, 1, -1, "SAMX"_sv);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
i64 part2(FILE* fp)
|
||||
{
|
||||
i64 result = 0;
|
||||
|
||||
auto puzzle = read_grid2d(fp);
|
||||
for (size_t y = 1; y < puzzle.height - 1; y++) {
|
||||
for (size_t x = 1; x < puzzle.width - 1; x++) {
|
||||
if (!check_match(puzzle, x - 1, y - 1, 1, 1, "MAS") && !check_match(puzzle, x - 1, y - 1, 1, 1, "SAM"))
|
||||
continue;
|
||||
if (!check_match(puzzle, x - 1, y + 1, 1, -1, "MAS") && !check_match(puzzle, x - 1, y + 1, 1, -1, "SAM"))
|
||||
continue;
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* file_path = "/usr/share/aoc2024/day4_input.txt";
|
||||
|
||||
if (argc >= 2)
|
||||
file_path = argv[1];
|
||||
|
||||
FILE* fp = fopen(file_path, "r");
|
||||
if (fp == nullptr)
|
||||
{
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("part1: %" PRId64 "\n", part1(fp));
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
printf("part2: %" PRId64 "\n", part2(fp));
|
||||
|
||||
fclose(fp);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(aoc2024_day5 ${SOURCES})
|
||||
banan_include_headers(aoc2024_day5 ban)
|
||||
banan_link_library(aoc2024_day5 libc)
|
||||
|
||||
install(TARGETS aoc2024_day5 OPTIONAL)
|
|
@ -0,0 +1,109 @@
|
|||
#include <BAN/Array.h>
|
||||
#include <BAN/Vector.h>
|
||||
#include <BAN/StringView.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using i8 = int8_t;
|
||||
using i16 = int16_t;
|
||||
using i32 = int32_t;
|
||||
using i64 = int64_t;
|
||||
|
||||
using u8 = uint8_t;
|
||||
using u16 = uint16_t;
|
||||
using u32 = uint32_t;
|
||||
using u64 = uint64_t;
|
||||
|
||||
i64 part1(FILE* fp)
|
||||
{
|
||||
bool requirements[100][100] {};
|
||||
char buffer[128];
|
||||
|
||||
int a, b;
|
||||
while (fgets(buffer, sizeof(buffer), fp) && sscanf(buffer, "%d|%d", &a, &b) == 2)
|
||||
requirements[b][a] = true;
|
||||
|
||||
i64 result = 0;
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
{
|
||||
BAN::Vector<int> update;
|
||||
for (size_t i = 0; isdigit(buffer[i]); i += 3)
|
||||
MUST(update.push_back(atoi(&buffer[i])));
|
||||
|
||||
bool valid = true;
|
||||
for (size_t i = 0; i < update.size() && valid; i++)
|
||||
for (size_t j = i + 1; j < update.size() && valid; j++)
|
||||
if (requirements[update[i]][update[j]])
|
||||
valid = false;
|
||||
|
||||
if (valid)
|
||||
result += update[update.size() / 2];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
i64 part2(FILE* fp)
|
||||
{
|
||||
bool requirements[100][100] {};
|
||||
char buffer[128];
|
||||
|
||||
int a, b;
|
||||
while (fgets(buffer, sizeof(buffer), fp) && sscanf(buffer, "%d|%d", &a, &b) == 2)
|
||||
requirements[b][a] = true;
|
||||
|
||||
i64 result = 0;
|
||||
|
||||
while (fgets(buffer, sizeof(buffer), fp))
|
||||
{
|
||||
BAN::Vector<int> update;
|
||||
for (size_t i = 0; isdigit(buffer[i]); i += 3)
|
||||
MUST(update.push_back(atoi(&buffer[i])));
|
||||
|
||||
bool correct { true };
|
||||
for (size_t i = 0; i < update.size(); i++)
|
||||
{
|
||||
for (size_t j = i + 1; j < update.size(); j++)
|
||||
{
|
||||
if (requirements[update[i]][update[j]])
|
||||
{
|
||||
BAN::swap(update[i], update[j]);
|
||||
correct = false;
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!correct)
|
||||
result += update[update.size() / 2];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* file_path = "/usr/share/aoc2024/day5_input.txt";
|
||||
|
||||
if (argc >= 2)
|
||||
file_path = argv[1];
|
||||
|
||||
FILE* fp = fopen(file_path, "r");
|
||||
if (fp == nullptr)
|
||||
{
|
||||
perror("fopen");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("part1: %" PRId64 "\n", part1(fp));
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
printf("part2: %" PRId64 "\n", part2(fp));
|
||||
|
||||
fclose(fp);
|
||||
}
|
|
@ -529,11 +529,8 @@ int pclose(FILE* file)
|
|||
return -1;
|
||||
}
|
||||
|
||||
pid_t pid = file->pid;
|
||||
(void)fclose(file);
|
||||
|
||||
int stat;
|
||||
while (waitpid(pid, &stat, 0) != -1)
|
||||
while (waitpid(file->pid, &stat, 0) == -1)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
|
@ -541,6 +538,8 @@ int pclose(FILE* file)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
(void)fclose(file);
|
||||
return stat;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include <BAN/Math.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <time.h>
|
||||
|
@ -64,11 +65,8 @@ char* ctime(const time_t* clock)
|
|||
return ctime_r(clock, buf);
|
||||
}
|
||||
|
||||
struct tm* gmtime_r(const time_t* timer, struct tm* __restrict result)
|
||||
static constexpr bool is_leap_year(uint64_t year)
|
||||
{
|
||||
constexpr auto is_leap_year =
|
||||
[](time_t year) -> bool
|
||||
{
|
||||
if (year % 400 == 0)
|
||||
return true;
|
||||
if (year % 100 == 0)
|
||||
|
@ -76,8 +74,63 @@ struct tm* gmtime_r(const time_t* timer, struct tm* __restrict result)
|
|||
if (year % 4 == 0)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
time_t mktime(struct tm* tm)
|
||||
{
|
||||
if (tm->tm_year < 70)
|
||||
{
|
||||
errno = EOVERFLOW;
|
||||
return -1;
|
||||
}
|
||||
|
||||
tm->tm_min += tm->tm_sec / 60;
|
||||
tm->tm_sec %= 60;
|
||||
|
||||
tm->tm_hour += tm->tm_min / 60;
|
||||
tm->tm_min %= 60;
|
||||
|
||||
tm->tm_mday += tm->tm_hour / 24;
|
||||
tm->tm_hour %= 24;
|
||||
|
||||
static constexpr int month_days[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int days_in_month = month_days[tm->tm_mon];
|
||||
if (tm->tm_mon == 1 && is_leap_year(tm->tm_year))
|
||||
days_in_month++;
|
||||
|
||||
if (tm->tm_mday <= days_in_month)
|
||||
break;
|
||||
|
||||
tm->tm_mday -= days_in_month;
|
||||
tm->tm_mon++;
|
||||
}
|
||||
|
||||
tm->tm_year += tm->tm_mon / 12;
|
||||
tm->tm_mon %= 12;
|
||||
|
||||
tm->tm_yday = tm->tm_mday - 1;
|
||||
if (tm->tm_mon > 0)
|
||||
tm->tm_yday += month_days[tm->tm_mon - 1];
|
||||
|
||||
const time_t num_febs = (tm->tm_mon > 1) ? tm->tm_year + 1 : tm->tm_year;
|
||||
const time_t leap_years = (num_febs - 69) / 4 - (num_febs - 1) / 100 + (num_febs + 299) / 400;
|
||||
|
||||
const time_t years = tm->tm_year - 70;
|
||||
const time_t days = years * 365 + leap_years + tm->tm_yday;
|
||||
const time_t hours = days * 24 + tm->tm_hour;
|
||||
const time_t minutes = hours * 60 + tm->tm_min;
|
||||
const time_t seconds = minutes * 60 + tm->tm_sec;
|
||||
|
||||
tm->tm_wday = (days + 4) % 7;
|
||||
|
||||
return seconds;
|
||||
}
|
||||
|
||||
struct tm* gmtime_r(const time_t* timer, struct tm* __restrict result)
|
||||
{
|
||||
constexpr uint64_t month_days[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
|
||||
|
||||
time_t time = *timer;
|
||||
|
@ -134,7 +187,7 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
|
||||
struct conversion_t
|
||||
{
|
||||
char flag = '\0';
|
||||
int flag = '\0';
|
||||
int width = -1;
|
||||
char modifier = '\0';
|
||||
};
|
||||
|
@ -154,9 +207,9 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
};
|
||||
|
||||
const auto append_string =
|
||||
[&](const char* string) -> bool
|
||||
[&s, &len, &maxsize](const char* string) -> bool
|
||||
{
|
||||
size_t string_len = strlen(string);
|
||||
const size_t string_len = strlen(string);
|
||||
if (len + string_len >= maxsize)
|
||||
return false;
|
||||
strcpy(s + len, string);
|
||||
|
@ -165,7 +218,7 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
};
|
||||
|
||||
const auto append_string_from_list =
|
||||
[&]<size_t LIST_SIZE>(int index, const char* const (&list)[LIST_SIZE]) -> bool
|
||||
[&append_string]<size_t LIST_SIZE>(int index, const char* const (&list)[LIST_SIZE]) -> bool
|
||||
{
|
||||
const char* string = "INVALID";
|
||||
if (index >= 0 && index < (int)LIST_SIZE)
|
||||
|
@ -174,15 +227,43 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
};
|
||||
|
||||
const auto append_value =
|
||||
[&](const char* format, int value) -> bool
|
||||
[&s, &len, &maxsize]<typename T>(const char* format, T value) -> bool
|
||||
{
|
||||
int written = snprintf(s + len, maxsize - len, format, value);
|
||||
const int written = snprintf(s + len, maxsize - len, format, value);
|
||||
if (len + written >= maxsize)
|
||||
return false;
|
||||
len += written;
|
||||
return true;
|
||||
};
|
||||
|
||||
const auto append_value_weird =
|
||||
[&append_string, &append_value](long long value, int flag, int width) -> bool
|
||||
{
|
||||
char format[32];
|
||||
char* ptr = format;
|
||||
*ptr++ = '%';
|
||||
if (flag == '+')
|
||||
*ptr++ = '+';
|
||||
*ptr++ = '0';
|
||||
if (width != -1)
|
||||
ptr += sprintf(ptr, "%d", width);
|
||||
*ptr++ = 'l';
|
||||
*ptr++ = 'l';
|
||||
*ptr++ = 'd';
|
||||
*ptr++ = '\0';
|
||||
|
||||
// idk why but musl libc test says that +4Y -> 2016 and +10F -> 2016-01-03
|
||||
// i have no idea why the + is not printed in those cases :)
|
||||
if (width < 11 && flag == '+')
|
||||
{
|
||||
char temp_buffer[12];
|
||||
int nprint = sprintf(temp_buffer, format, value);
|
||||
return append_string(temp_buffer + (nprint == width + 1));
|
||||
}
|
||||
|
||||
return append_value(format, value);
|
||||
};
|
||||
|
||||
while (*format && len < maxsize)
|
||||
{
|
||||
if (*format != '%')
|
||||
|
@ -194,20 +275,14 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
format++;
|
||||
|
||||
conversion_t conversion;
|
||||
switch (*format)
|
||||
{
|
||||
case '+':
|
||||
case '0':
|
||||
conversion.flag = *format;
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
if (*format == '0' || *format == '+')
|
||||
conversion.flag = *format++;
|
||||
if (isdigit(*format))
|
||||
{
|
||||
conversion.width = 0;
|
||||
while (isdigit(*format))
|
||||
{
|
||||
conversion.width = (conversion.width * 10) + (*format + '0');
|
||||
conversion.width = (conversion.width * 10) + (*format - '0');
|
||||
format++;
|
||||
}
|
||||
}
|
||||
|
@ -247,16 +322,15 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
break;
|
||||
case 'C':
|
||||
{
|
||||
if (conversion.flag == '\0')
|
||||
conversion.flag = ' ';
|
||||
if (conversion.flag == '+' && conversion.width <= 2)
|
||||
conversion.flag = '0';
|
||||
if (conversion.width < 2)
|
||||
if (conversion.width == -1)
|
||||
conversion.width = 2;
|
||||
|
||||
char new_format[32];
|
||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
||||
if (!append_value(new_format, timeptr->tm_year % 100))
|
||||
if (conversion.flag == '+')
|
||||
sprintf(new_format, "%%+0%dd", conversion.width);
|
||||
else
|
||||
sprintf(new_format, "%%0%dd", conversion.width);
|
||||
if (!append_value(new_format, (1900 + timeptr->tm_year) / 100))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
|
@ -270,20 +344,24 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
else return 0;
|
||||
break;
|
||||
case 'e':
|
||||
if (!append_value("% 2d", timeptr->tm_mday))
|
||||
if (!append_value("%2d", timeptr->tm_mday))
|
||||
return 0;
|
||||
break;
|
||||
case 'F':
|
||||
{
|
||||
if (conversion.flag == '\0')
|
||||
conversion.flag = '+';
|
||||
if (conversion.width == -1)
|
||||
conversion.width = 10;
|
||||
if (conversion.width < 6)
|
||||
conversion.width = 6;
|
||||
// remove trailing "-mm-dd" from width
|
||||
if (conversion.width >= 6)
|
||||
conversion.width -= 6;
|
||||
|
||||
char new_format[32];
|
||||
sprintf(new_format, "%%%c%dY-%%m-%%d", conversion.flag, conversion.width - 6);
|
||||
char* ptr = new_format;
|
||||
|
||||
*ptr++ = '%';
|
||||
if (conversion.flag)
|
||||
*ptr++ = conversion.flag;
|
||||
if (conversion.width != -1)
|
||||
ptr += sprintf(ptr, "%d", conversion.width);
|
||||
strcpy(ptr, "Y-%m-%d");
|
||||
|
||||
if (size_t ret = strftime(s + len, maxsize - len, new_format, timeptr))
|
||||
len += ret;
|
||||
|
@ -327,6 +405,13 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
len += ret;
|
||||
else return 0;
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
struct tm tm_copy = *timeptr;
|
||||
if (!append_value("%llu", mktime(&tm_copy)))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
case 'S':
|
||||
if (!append_value("%02d", timeptr->tm_sec))
|
||||
return 0;
|
||||
|
@ -353,22 +438,10 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
{
|
||||
// Adapted from GNU libc implementation
|
||||
|
||||
constexpr auto is_leap_year =
|
||||
[](int year) -> bool
|
||||
{
|
||||
if (year % 400 == 0)
|
||||
return true;
|
||||
if (year % 100 == 0)
|
||||
return false;
|
||||
if (year % 4 == 0)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
constexpr auto iso_week_days =
|
||||
[](int yday, int wday) -> int
|
||||
{
|
||||
return yday - (wday + 382) % 7 + 3;
|
||||
return yday - (yday - wday + 382) % 7 + 3;
|
||||
};
|
||||
|
||||
int year = timeptr->tm_year + 1900;
|
||||
|
@ -396,20 +469,13 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
return 0;
|
||||
break;
|
||||
case 'G':
|
||||
{
|
||||
if (conversion.flag == '\0')
|
||||
conversion.flag = ' ';
|
||||
if (conversion.flag == '+' && conversion.width <= 4)
|
||||
conversion.flag = '0';
|
||||
if (conversion.flag == '\0' && 1900 + year > 9999)
|
||||
conversion.flag = '+';
|
||||
if (conversion.width == -1)
|
||||
conversion.width = 0;
|
||||
|
||||
char new_format[32];
|
||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
||||
if (!append_value(new_format, year))
|
||||
conversion.width = 4;
|
||||
if (!append_value_weird(year, conversion.flag, conversion.width))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
case 'V':
|
||||
if (!append_value("%02d", days / 7 + 1))
|
||||
return 0;
|
||||
|
@ -436,24 +502,17 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
|
|||
else return 0;
|
||||
break;
|
||||
case 'y':
|
||||
if (!append_value("%d", timeptr->tm_yday % 100))
|
||||
if (!append_value("%02d", timeptr->tm_year % 100))
|
||||
return 0;
|
||||
break;
|
||||
case 'Y':
|
||||
{
|
||||
if (conversion.flag == '\0')
|
||||
conversion.flag = ' ';
|
||||
if (conversion.flag == '+' && conversion.width <= 4)
|
||||
conversion.flag = '0';
|
||||
if (conversion.flag == '\0' && timeptr->tm_year > 9999 - 1900)
|
||||
conversion.flag = '+';
|
||||
if (conversion.width == -1)
|
||||
conversion.width = 0;
|
||||
|
||||
char new_format[32];
|
||||
sprintf(new_format, "%%%c%dd", conversion.flag, conversion.width);
|
||||
if (!append_value(new_format, 1900 + timeptr->tm_year))
|
||||
conversion.width = 4;
|
||||
if (!append_value_weird(1900ll + timeptr->tm_year, conversion.flag, conversion.width))
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
case 'z':
|
||||
// FIXME: support timezones
|
||||
break;
|
||||
|
@ -483,8 +542,3 @@ void tzset()
|
|||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
time_t mktime(struct tm*)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
set(USERSPACE_PROGRAMS
|
||||
bananfetch
|
||||
basename
|
||||
cat
|
||||
cat-mmap
|
||||
chmod
|
||||
cp
|
||||
dd
|
||||
dhcp-client
|
||||
dirname
|
||||
DynamicLoader
|
||||
echo
|
||||
env
|
||||
|
@ -14,6 +16,7 @@ set(USERSPACE_PROGRAMS
|
|||
id
|
||||
image
|
||||
init
|
||||
ln
|
||||
loadfont
|
||||
loadkeys
|
||||
ls
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(basename ${SOURCES})
|
||||
banan_link_library(basename libc)
|
||||
|
||||
install(TARGETS basename OPTIONAL)
|
|
@ -0,0 +1,30 @@
|
|||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int usage(const char* argv0, int ret)
|
||||
{
|
||||
FILE* fout = ret ? stderr : stdout;
|
||||
fprintf(fout, "usage: %s STRING [SUFFIX]\n", argv0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
if (argc != 2 && argc != 3)
|
||||
return usage(argv[0], 1);
|
||||
|
||||
const char* result = basename(const_cast<char*>(argv[1]));
|
||||
int result_len = strlen(result);
|
||||
|
||||
if (argc == 3)
|
||||
{
|
||||
int suffix_len = strlen(argv[2]);
|
||||
if (result_len >= suffix_len && strcmp(result - suffix_len, argv[2]) == 0)
|
||||
result_len -= suffix_len;
|
||||
}
|
||||
|
||||
printf("%.*s\n", result_len, result);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(dirname ${SOURCES})
|
||||
banan_link_library(dirname libc)
|
||||
|
||||
install(TARGETS dirname OPTIONAL)
|
|
@ -0,0 +1,18 @@
|
|||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int usage(const char* argv0, int ret)
|
||||
{
|
||||
FILE* fout = ret ? stderr : stdout;
|
||||
fprintf(fout, "usage: %s STRING\n", argv0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
return usage(argv[0], 1);
|
||||
const char* result = dirname(const_cast<char*>(argv[1]));
|
||||
printf("%s\n", result);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(ln ${SOURCES})
|
||||
banan_link_library(ln libc)
|
||||
|
||||
install(TARGETS ln OPTIONAL)
|
|
@ -0,0 +1,65 @@
|
|||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int usage(const char* argv0, int ret)
|
||||
{
|
||||
FILE* fout = ret ? stderr : stdout;
|
||||
fprintf(fout, "usage: %s [OPTION]... TARGET [LINK_NAME]\n", argv0);
|
||||
fprintf(fout, " -s, --symbolic create symbolic link instead of hard link\n");
|
||||
fprintf(fout, " -h, --help show this message and exit\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
bool do_symlink = false;
|
||||
|
||||
int i = 1;
|
||||
for (; i < argc; i++)
|
||||
{
|
||||
if (argv[i][0] != '-')
|
||||
break;
|
||||
|
||||
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--symbolic") == 0)
|
||||
do_symlink = true;
|
||||
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
||||
return usage(argv[0], 0);
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "unrecognized option '%s'\n", argv[i]);
|
||||
return usage(argv[0], 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (i == argc)
|
||||
{
|
||||
fprintf(stderr, "missing target\n");
|
||||
return usage(argv[0], 1);
|
||||
}
|
||||
|
||||
const char* target = argv[i++];
|
||||
|
||||
struct stat st;
|
||||
if (stat(target, &st) == -1)
|
||||
{
|
||||
perror("stat");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* link_name = (i == argc)
|
||||
? basename(const_cast<char*>(target))
|
||||
: argv[i];
|
||||
|
||||
int (*link_func)(const char*, const char*) = do_symlink ? &symlink : &link;
|
||||
|
||||
if (link_func(target, link_name) == -1)
|
||||
{
|
||||
perror(do_symlink ? "symlink" : "link");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue