diff --git a/HW/homework/HW/.vs/HW/v16/.suo b/HW/homework/HW/.vs/HW/v16/.suo index b0bd3d11..d2491ffa 100644 Binary files a/HW/homework/HW/.vs/HW/v16/.suo and b/HW/homework/HW/.vs/HW/v16/.suo differ diff --git a/HW/homework/HW/.vs/HW/v16/Browse.VC.db b/HW/homework/HW/.vs/HW/v16/Browse.VC.db index 5c2ed63c..05ece777 100644 Binary files a/HW/homework/HW/.vs/HW/v16/Browse.VC.db and b/HW/homework/HW/.vs/HW/v16/Browse.VC.db differ diff --git a/HW/homework/HW/.vs/HW/v16/Solution.VC.db b/HW/homework/HW/.vs/HW/v16/Solution.VC.db new file mode 100644 index 00000000..f256e07c Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/Solution.VC.db differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d14d211ea35eb64/TASK4.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d14d211ea35eb64/TASK4.ipch new file mode 100644 index 00000000..3ad8f3f2 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d14d211ea35eb64/TASK4.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d8291ed29aceac4/TASK3.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d8291ed29aceac4/TASK3.ipch new file mode 100644 index 00000000..08432aa4 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/1d8291ed29aceac4/TASK3.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/24bd680a828822e4/TASK2.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/24bd680a828822e4/TASK2.ipch new file mode 100644 index 00000000..000bd02f Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/24bd680a828822e4/TASK2.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/8c64df022634fd29/SOURCE.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/8c64df022634fd29/SOURCE.ipch new file mode 100644 index 00000000..d0cf6319 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/8c64df022634fd29/SOURCE.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a0d3911b3639c144/TASK0.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a0d3911b3639c144/TASK0.ipch index 3bf29c47..9862d043 100644 Binary files a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a0d3911b3639c144/TASK0.ipch and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a0d3911b3639c144/TASK0.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a7871eaff74733d4/TASK5.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a7871eaff74733d4/TASK5.ipch new file mode 100644 index 00000000..3d8c5b10 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/a7871eaff74733d4/TASK5.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/ac15bb8eb34e99da/MAIN.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/ac15bb8eb34e99da/MAIN.ipch new file mode 100644 index 00000000..d86fddfd Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/ac15bb8eb34e99da/MAIN.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d32ffa5f172f73df/SOURCE.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d32ffa5f172f73df/SOURCE.ipch new file mode 100644 index 00000000..a8b92c13 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d32ffa5f172f73df/SOURCE.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d5c3242bd4ea326d/MAIN.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d5c3242bd4ea326d/MAIN.ipch new file mode 100644 index 00000000..ec5206c9 Binary files /dev/null and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/d5c3242bd4ea326d/MAIN.ipch differ diff --git a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/e2d3386b1d2cb0b4/TASK1.ipch b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/e2d3386b1d2cb0b4/TASK1.ipch index 548758cf..3568dd34 100644 Binary files a/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/e2d3386b1d2cb0b4/TASK1.ipch and b/HW/homework/HW/.vs/HW/v16/ipch/AutoPCH/e2d3386b1d2cb0b4/TASK1.ipch differ diff --git a/HW/homework/HW/HW.sln b/HW/homework/HW/HW.sln index e89a4909..c07871f3 100644 --- a/HW/homework/HW/HW.sln +++ b/HW/homework/HW/HW.sln @@ -7,6 +7,16 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task0", "task0\task0.vcxpro EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task1", "task1\task1.vcxproj", "{D8D8A82C-5BAA-40D4-96E2-9A293C0C3AEB}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task2", "task2\task2.vcxproj", "{C0223A77-5103-44D5-AD1A-B080A6D74CD5}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task3", "task3\task3.vcxproj", "{03F3E6C9-383E-4A07-A3F9-339C6C704FCA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task4", "task4\task4.vcxproj", "{B2629934-3850-4594-94A6-D5B2C65426EC}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task1(2s)", "task1(2s)\task1(2s).vcxproj", "{D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task2(2s)", "task2(2s)\task2(2s).vcxproj", "{132B728A-02D0-424E-9A3C-4EA427FB9405}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -31,6 +41,46 @@ Global {D8D8A82C-5BAA-40D4-96E2-9A293C0C3AEB}.Release|x64.Build.0 = Release|x64 {D8D8A82C-5BAA-40D4-96E2-9A293C0C3AEB}.Release|x86.ActiveCfg = Release|Win32 {D8D8A82C-5BAA-40D4-96E2-9A293C0C3AEB}.Release|x86.Build.0 = Release|Win32 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Debug|x64.ActiveCfg = Debug|x64 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Debug|x64.Build.0 = Debug|x64 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Debug|x86.ActiveCfg = Debug|Win32 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Debug|x86.Build.0 = Debug|Win32 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Release|x64.ActiveCfg = Release|x64 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Release|x64.Build.0 = Release|x64 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Release|x86.ActiveCfg = Release|Win32 + {C0223A77-5103-44D5-AD1A-B080A6D74CD5}.Release|x86.Build.0 = Release|Win32 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Debug|x64.ActiveCfg = Debug|x64 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Debug|x64.Build.0 = Debug|x64 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Debug|x86.ActiveCfg = Debug|Win32 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Debug|x86.Build.0 = Debug|Win32 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Release|x64.ActiveCfg = Release|x64 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Release|x64.Build.0 = Release|x64 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Release|x86.ActiveCfg = Release|Win32 + {03F3E6C9-383E-4A07-A3F9-339C6C704FCA}.Release|x86.Build.0 = Release|Win32 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Debug|x64.ActiveCfg = Debug|x64 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Debug|x64.Build.0 = Debug|x64 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Debug|x86.ActiveCfg = Debug|Win32 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Debug|x86.Build.0 = Debug|Win32 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Release|x64.ActiveCfg = Release|x64 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Release|x64.Build.0 = Release|x64 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Release|x86.ActiveCfg = Release|Win32 + {B2629934-3850-4594-94A6-D5B2C65426EC}.Release|x86.Build.0 = Release|Win32 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Debug|x64.ActiveCfg = Debug|x64 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Debug|x64.Build.0 = Debug|x64 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Debug|x86.ActiveCfg = Debug|Win32 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Debug|x86.Build.0 = Debug|Win32 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Release|x64.ActiveCfg = Release|x64 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Release|x64.Build.0 = Release|x64 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Release|x86.ActiveCfg = Release|Win32 + {D4D982E9-5188-4A1E-AB9E-27CAAEEE3D69}.Release|x86.Build.0 = Release|Win32 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Debug|x64.ActiveCfg = Debug|x64 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Debug|x64.Build.0 = Debug|x64 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Debug|x86.ActiveCfg = Debug|Win32 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Debug|x86.Build.0 = Debug|Win32 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Release|x64.ActiveCfg = Release|x64 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Release|x64.Build.0 = Release|x64 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Release|x86.ActiveCfg = Release|Win32 + {132B728A-02D0-424E-9A3C-4EA427FB9405}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HW/homework/HW/dirent-master/.gitignore b/HW/homework/HW/dirent-master/.gitignore new file mode 100644 index 00000000..1e9efe31 --- /dev/null +++ b/HW/homework/HW/dirent-master/.gitignore @@ -0,0 +1,24 @@ +/CMakeCache.txt +/CMakeFiles +/CTestTestfile.cmake +/DartConfiguration.tcl +/Makefile +/Testing +/Win32 +/cmake_install.cmake +/find +/locate +/ls +/scandir +/cat +/t-compile +/t-dirent +/t-scandir +/t-cplusplus +/t-unicode +/t-strverscmp +/t-utf8 +/updatedb +/*.filters +/*.vcxproj +/*.dir diff --git a/HW/homework/HW/dirent-master/CMakeLists.txt b/HW/homework/HW/dirent-master/CMakeLists.txt new file mode 100644 index 00000000..a607adf3 --- /dev/null +++ b/HW/homework/HW/dirent-master/CMakeLists.txt @@ -0,0 +1,62 @@ +cmake_minimum_required (VERSION 2.8.12) +project (dirent LANGUAGES C CXX) + +# User options +option(DIRENT_BUILD_TESTS "Build bundled tests" ON) +message(STATUS "Build test and example programs: ${DIRENT_BUILD_TESTS}") + +# Initialize C and C++ compilers +enable_language (C CXX) + +# Compile in debug mode by default +if (NOT CMAKE_BUILD_TYPE) + set (CMAKE_BUILD_TYPE Debug + CACHE STRING + "Type of build: None Debug Release RelWithDebInfo MinSizeRel." + FORCE + ) +endif (NOT CMAKE_BUILD_TYPE) + +# Use the include directory only on Windows +if (WIN32) + include_directories (${CMAKE_SOURCE_DIR}/include) +endif (WIN32) + +# Install dirent.h +if (WIN32) + install (FILES include/dirent.h DESTINATION include) +endif (WIN32) + +# Add distclean target +add_custom_target (distclean + COMMAND ${CMAKE_BUILD_TOOL} clean + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/distclean.cmake +) + +# Build example programs +if(DIRENT_BUILD_TESTS) + add_executable (find examples/find.c) + add_executable (ls examples/ls.c) + add_executable (locate examples/locate.c) + add_executable (updatedb examples/updatedb.c) + add_executable (scandir examples/scandir.c) + add_executable (cat examples/cat.c) + add_executable (dirls examples/dirls.c) + + # Build test programs + include (CTest) + add_custom_target (check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C ${CMAKE_CFG_INTDIR}) + function (add_test_executable TEST_NAME) + add_executable (${TEST_NAME} EXCLUDE_FROM_ALL ${ARGN}) + add_test (NAME ${TEST_NAME} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} COMMAND $) + add_dependencies (check ${TEST_NAME}) + endfunction (add_test_executable) + + add_test_executable (t-compile tests/t-compile.c) + add_test_executable (t-dirent tests/t-dirent.c) + add_test_executable (t-scandir tests/t-scandir.c) + add_test_executable (t-unicode tests/t-unicode.c) + add_test_executable (t-cplusplus tests/t-cplusplus.cpp) + add_test_executable (t-strverscmp tests/t-strverscmp.c) + add_test_executable (t-utf8 tests/t-utf8.c) +endif(DIRENT_BUILD_TESTS) diff --git a/HW/homework/HW/dirent-master/CONTRIBUTING.md b/HW/homework/HW/dirent-master/CONTRIBUTING.md new file mode 100644 index 00000000..bff76c3e --- /dev/null +++ b/HW/homework/HW/dirent-master/CONTRIBUTING.md @@ -0,0 +1,41 @@ +# Contributing to Dirent + +Dirent is an open source project and we love to receive contributions from our +community -- you! There are many ways to contribute, from writing tutorials +or blog posts, improving the documentation, submitting bug reports and +feature requests, or writing code which can be incorporated into Dirent. + +For example, we are looking for contributions which + +1. Improve portability of code developed for Unix/Linux to Microsoft Windows. +2. Make Dirent easier to use and/or more useful for programmers working on + Microsoft Windows platform. +3. Remove compiler warnings or fix bugs. + +## How to Suggest a Feature + +We use Github to host code, to track issues and to discuss about Dirent. If +you would like to suggest a feature, then open an issue at Github. + +## How to Report a Bug + +If you have trouble using Dirent, then try to repeat the problem with the +latest version found at Github. If the problem remains, then open an issue +at Github. + +## How to Submit a Fix or an Enhancement + +If you wish to contribute code, then fork the repo and test the change in your +private repo first. If the change works for you and you would like to donate +the change back to the community, then make a pull request at Github. + +Before making a pull request, please consider that: + +1. We can only accept contributions which are compatible with [MIT Software + License](LICENSE). +2. By making a pull request, you agree to transfer all copyrights to us. +3. Our code will be reviewed and edited by us. In particular, we may + reformat your code according to the [Linux Kernel Coding + Style](https://www.kernel.org/doc/html/v4.10/process/coding-style.html) + + diff --git a/HW/homework/HW/dirent-master/ChangeLog b/HW/homework/HW/dirent-master/ChangeLog new file mode 100644 index 00000000..d79cea25 --- /dev/null +++ b/HW/homework/HW/dirent-master/ChangeLog @@ -0,0 +1,129 @@ +2018-05-08 Toni Rönkkö + + * Version 1.23.2: fixes bad scandir prototype. + +2017-08-27 Toni Rönkkö + + * Version 1.23: support readdir_r and scandir functions. + +2017-07-18 Toni Rönkkö + + * Created release branches v1.22 and v1.21 to Git. Published version + 1.22 at softagalleria.net. + +2016-09-11 Toni Rönkkö + + * Version 1.22: added support for CMake. Thanks to Paul Fultz II. + +2014-09-25 Toni Rönkkö + + * Version 1.21: compiles correctly under Open Watcom. Thanks to + Virgil Banowetz for a patch! + +2014-04-07 Toni Rönkkö + + * Version 1.20.1: the zip file from the previous version did not open + correctly with Microsoft's compressed folders. Thanks to Alexandre + for info! + +2014-03-17 Toni Ronkko + + * Version 1.20: dirent.h compiles correctly in 64-bit architecture. + Thanks to Aaron Simmons! + +2014-03-03 Toni Ronkko + + * Version 1.13.2: define DT_LNK for compatibility with Unix + programs. Thanks to Joel Bruick for suggestion! + +2013-01-27 Toni Ronkko + + * Version 1.13.1: patch from Edward Berner fixes set_errno() on + Windows NT 4.0. + + * Revised wcstombs() and mbstowcs() wrappers to make sure that they do + not write past their target string. + + * PATH_MAX from windows.h includes zero terminator so there is no + need to add one extra byte to variables and structures. + +2012-12-12 Toni Ronkko + + * Version 1.13: use the traditional 8+3 file naming scheme if a file + name cannot be represented in the default ANSI code page. Now + compiles again with MSVC 6.0. Thanks to Konstantin Khomoutov for + testing. + +2012-10-01 Toni Ronkko + + * Version 1.12.1: renamed wide-character DIR structure _wDIR to + _WDIR (with capital W) in order to maintain compatibility with MingW. + +2012-09-30 Toni Ronkko + + * Version 1.12: define PATH_MAX and NAME_MAX. Added wide-character + variants _wDIR, _wdirent, _wopendir(), _wreaddir(), _wclosedir() and + _wrewinddir(). Thanks to Edgar Buerkle and Jan Nijtmans for ideas + and code. + + * Now avoiding windows.h. This allows dirent.h to be integrated + more easily into programs using winsock. Thanks to Fernando + Azaldegui. + +2011-03-15 Toni Ronkko + + * Version 1.11: defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0. + +2010-08-11 Toni Ronkko + + * Version 1.10: added d_type and d_namlen fields to dirent structure. + The former is especially useful for determining whether directory + entry represents a file or a directory. For more information, see + http://www.delorie.com/gnu/docs/glibc/libc_270.html + + * Improved conformance to the standards. For example, errno is now + set properly on failure and assert() is never used. Thanks to Peter + Brockam for suggestions. + + * Fixed a bug in rewinddir(): when using relative directory names, + change of working directory no longer causes rewinddir() to fail. + +2009-12-15 John Cunningham + + * Version 1.9: added rewinddir member function + +2008-01-18 Toni Ronkko + + * Version 1.8: Using FindFirstFileA and WIN32_FIND_DATAA to avoid + converting string between multi-byte and unicode representations. + This makes the code simpler and also allows the code to be compiled + under MingW. Thanks to Azriel Fasten for the suggestion. + +2007-03-04 Toni Ronkko + + * Bug fix: due to the strncpy_s() function this file only compiled in + Visual Studio 2005. Using the new string functions only when the + compiler version allows. + +2006-11-02 Toni Ronkko + + * Major update: removed support for Watcom C, MS-DOS and Turbo C to + simplify the file, updated the code to compile cleanly on Visual + Studio 2005 with both unicode and multi-byte character strings, + removed rewinddir() as it had a bug. + +2006-08-20 Toni Ronkko + + * Removed all remarks about MSVC 1.0, which is antiqued now. + Simplified comments by removing SGML tags. + +2002-05-14 Toni Ronkko + + * Embedded the function definitions directly to the header so that no + source modules need to be included in the Visual Studio project. + Removed all the dependencies to other projects so that this header + file can be used independently. + +1998-05-28 Toni Ronkko + + * First version. diff --git a/HW/homework/HW/dirent-master/LICENSE b/HW/homework/HW/dirent-master/LICENSE new file mode 100644 index 00000000..af043606 --- /dev/null +++ b/HW/homework/HW/dirent-master/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 1998-2019 Toni Ronkko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/HW/homework/HW/dirent-master/README.md b/HW/homework/HW/dirent-master/README.md new file mode 100644 index 00000000..bd31abca --- /dev/null +++ b/HW/homework/HW/dirent-master/README.md @@ -0,0 +1,135 @@ +# Dirent + +Dirent is a C/C++ programming interface that allows programmers to retrieve +information about files and directories under Linux/UNIX. This project +provides Linux compatible Dirent interface for Microsoft Windows. + + +# How to Enable UTF-8 Support + +By default, Dirent functions expect the directory names to be represented in +the currently selected windows codepage. Moverover, Dirent functions return +file names in the presently selected codepage. If you wish to use UTF-8 file +names instead, then set the program's locale to ".utf8" or similar. For +example, your C main program might look like- + +``` +#include + +int main(int argc, char *argv[]) +{ + setlocale(LC_ALL, "LC_CTYPE=.utf8"); + + /*...*/ +} +``` + +For more information on UTF-8 support, please see setlocale in Visual Studio +[C runtime library reference](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160#utf-8-support). + + +# Installation + +Download the latest Dirent installation package from +[GitHub](https://github.com/tronkko/dirent/releases) and +unpack the installation file with 7-zip, for example. The installation +package contains ``include/dirent.h`` file as well as a few example and test +programs. + + +## Installing Dirent for All Programs + +To make dirent.h available for all C/C++ programs, simply copy the +``include/dirent.h`` file to the system include directory. System include +directory contains header files such as ``assert.h`` and ``windows.h``. In +Visual Studio 2008, for example, the system include may be found at +``C:\Program Files\Microsoft Visual Studio 9.0\VC\include``. + +Everything you need is included in the single ``dirent.h`` file, and you can +start using Dirent immediately -- there is no need to add files to your +Visual Studio project. + + +## Embedding Dirent into Your Own Project + +If you wish to distribute ``dirent.h`` alongside with your own source code, +then copy ``include/dirent.h`` file to a new sub-directory within your project +and add that directory to include path on Windows while omitting the directory +under Linux/UNIX. This allows your project to be compiled against native +``dirent.h`` on Linux/UNIX while substituting the functionality on Microsoft +Windows. + + +## Examples + +The installation package contains six example programs: + +Program | Purpose +-------- | ----------------------------------------------------------------- +ls | List files in a directory, e.g. ls "c:\Program Files" +find | Find files in subdirectories, e.g. find "c:\Program Files\CMake" +updatedb | Build database of files in a drive, e.g. updatedb c:\ +locate | Locate a file from database, e.g. locate notepad +scandir | Demonstrate scandir() function +cat | Print a text file to screen + +Please install [CMake](https://cmake.org/) to build example and test programs. +Then, open command prompt and create a temporary directory ``c:\temp\dirent`` +for the build files as + +``` +c:\ +mkdir temp +mkdir temp\dirent +cd temp\dirent +``` + +Generate build files as + +``` +cmake d:\dirent +``` + +where ``d:\dirent`` is the root directory of the Dirent package (containing +this README.md file). If wish to omit example programs from the +build, then append the option ``-DDIRENT_BUILD_TESTS=OFF`` to the CMake +command line. + +Once CMake is finished, open Visual Studio, load the generated ``dirent.sln`` +file from the build directory and build the whole solution. Once the build +completes, run the example programs ls, find, updatedb and locate from the +command prompt as + +``` +cd Debug +ls . +find . +updatedb c:\ +locate cmd.exe +``` + +Visual Studio project also contains a solution named ``check`` which can be +used to verify that Dirent works as expected. Just build the solution from +Visual Studio to run the test programs. + + +# Contributing + +We love to receive contributions from you. See the +[CONTRIBUTING](CONTRIBUTING.md) file for details. + + +# Copying + +Dirent may be freely distributed under the MIT license. See the +[LICENSE](LICENSE) file for details. + + +# Alternatives to Dirent + +I ported Dirent to Microsoft Windows in 1998 when only a few alternatives +were available. However, the situation has changed since then and nowadays +both [Cygwin](http://www.cygwin.com) and [MingW](http://www.mingw.org) +allow you to compile a great number of UNIX programs in Microsoft Windows. +They both provide a full dirent API as well as many other UNIX APIs. MingW +can even be used for commercial applications! diff --git a/HW/homework/HW/dirent-master/distclean.cmake b/HW/homework/HW/dirent-master/distclean.cmake new file mode 100644 index 00000000..60e69adc --- /dev/null +++ b/HW/homework/HW/dirent-master/distclean.cmake @@ -0,0 +1,62 @@ +# Remove CMake generated temporary files +set (cmake_generated + ${CMAKE_BINARY_DIR}/ALL_BUILD.vcxproj + ${CMAKE_BINARY_DIR}/ALL_BUILD.vcxproj.filters + ${CMAKE_BINARY_DIR}/CMakeCache.txt + ${CMAKE_BINARY_DIR}/CMakeFiles + ${CMAKE_BINARY_DIR}/CTestTestfile.cmake + ${CMAKE_BINARY_DIR}/Continuous.vcxproj + ${CMAKE_BINARY_DIR}/Continuous.vcxproj.filters + ${CMAKE_BINARY_DIR}/DartConfiguration.tcl + ${CMAKE_BINARY_DIR}/Debug + ${CMAKE_BINARY_DIR}/Experimental.vcxproj + ${CMAKE_BINARY_DIR}/Experimental.vcxproj.filters + ${CMAKE_BINARY_DIR}/INSTALL.vcxproj + ${CMAKE_BINARY_DIR}/INSTALL.vcxproj.filters + ${CMAKE_BINARY_DIR}/Makefile + ${CMAKE_BINARY_DIR}/Nightly.vcxproj + ${CMAKE_BINARY_DIR}/Nightly.vcxproj.filters + ${CMAKE_BINARY_DIR}/NightlyMemoryCheck.vcxproj + ${CMAKE_BINARY_DIR}/NightlyMemoryCheck.vcxproj.filters + ${CMAKE_BINARY_DIR}/RUN_TESTS.vcxproj + ${CMAKE_BINARY_DIR}/RUN_TESTS.vcxproj.filters + ${CMAKE_BINARY_DIR}/Testing + ${CMAKE_BINARY_DIR}/Win32 + ${CMAKE_BINARY_DIR}/ZERO_CHECK.vcxproj + ${CMAKE_BINARY_DIR}/ZERO_CHECK.vcxproj.filters + ${CMAKE_BINARY_DIR}/check.vcxproj + ${CMAKE_BINARY_DIR}/check.vcxproj.filters + ${CMAKE_BINARY_DIR}/cmake_install.cmake + ${CMAKE_BINARY_DIR}/dirent.sln + ${CMAKE_BINARY_DIR}/distclean.vcxproj + ${CMAKE_BINARY_DIR}/distclean.vcxproj.filters + ${CMAKE_BINARY_DIR}/find + ${CMAKE_BINARY_DIR}/find.dir + ${CMAKE_BINARY_DIR}/find.vcxproj + ${CMAKE_BINARY_DIR}/find.vcxproj.filters + ${CMAKE_BINARY_DIR}/locate + ${CMAKE_BINARY_DIR}/locate.dir + ${CMAKE_BINARY_DIR}/locate.vcxproj + ${CMAKE_BINARY_DIR}/locate.vcxproj.filters + ${CMAKE_BINARY_DIR}/ls + ${CMAKE_BINARY_DIR}/ls.dir + ${CMAKE_BINARY_DIR}/ls.vcxproj + ${CMAKE_BINARY_DIR}/ls.vcxproj.filters + ${CMAKE_BINARY_DIR}/t-compile + ${CMAKE_BINARY_DIR}/t-compile.dir + ${CMAKE_BINARY_DIR}/t-compile.vcxproj + ${CMAKE_BINARY_DIR}/t-compile.vcxproj.filters + ${CMAKE_BINARY_DIR}/t-dirent + ${CMAKE_BINARY_DIR}/t-dirent.dir + ${CMAKE_BINARY_DIR}/t-dirent.vcxproj + ${CMAKE_BINARY_DIR}/t-dirent.vcxproj.filters + ${CMAKE_BINARY_DIR}/updatedb + ${CMAKE_BINARY_DIR}/updatedb.dir + ${CMAKE_BINARY_DIR}/updatedb.vcxproj + ${CMAKE_BINARY_DIR}/updatedb.vcxproj.filters +) +foreach (file ${cmake_generated}) + if (EXISTS ${file}) + file (REMOVE_RECURSE ${file}) + endif() +endforeach (file) diff --git a/HW/homework/HW/dirent-master/examples/cat.c b/HW/homework/HW/dirent-master/examples/cat.c new file mode 100644 index 00000000..07f37312 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/cat.c @@ -0,0 +1,73 @@ +/* + * Output contents of a file. + * + * Compile this file with Visual Studio and run the produced command in + * console with a file name argument. For example, command + * + * cat include\dirent.h + * + * will output the dirent.h to screen. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +static void output_file(const char *fn); + + +int main(int argc, char *argv[]) +{ + /* Select default locale */ + setlocale(LC_ALL, ""); + + /* Require at least one file */ + if (argc == 1) { + fprintf(stderr, "Usage: cat filename\n"); + return EXIT_FAILURE; + } + + /* For each file name argument in command line */ + int i = 1; + while (i < argc) { + output_file(argv[i]); + i++; + } + return EXIT_SUCCESS; +} + +/* + * Output file to screen + */ +static void output_file(const char *fn) +{ + /* Open file */ + FILE *fp = fopen(fn, "r"); + if (!fp) { + /* Could not open directory */ + fprintf(stderr, "Cannot open %s (%s)\n", fn, strerror(errno)); + exit(EXIT_FAILURE); + } + + /* Output file to screen */ + size_t n; + do { + /* Read some bytes from file */ + char buffer[4096]; + n = fread(buffer, 1, 4096, fp); + + /* Output bytes to screen */ + fwrite(buffer, 1, n, stdout); + } while (n != 0); + + /* Close file */ + fclose(fp); +} diff --git a/HW/homework/HW/dirent-master/examples/dirls.c b/HW/homework/HW/dirent-master/examples/dirls.c new file mode 100644 index 00000000..20302f48 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/dirls.c @@ -0,0 +1,192 @@ +/* + * An example demonstrating basic file information listing. + * + * Compile this file with Visual Studio and run the produced command in + * console with a directory name argument. For example, command + * + * dirls "C:\Users\User 1\Documents" + * + * might output something like + * + * ./ + * ../ + * budget.xlsx 19180 + * directory_1/ + * directory_2/ + * notes.txt 320 + * to_do_list.txt 245 + * + * The dirls command provided by this file is only an example: the command + * does not have any fancy options. + * + * Copyright (C) 2006-2012 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#include +#include +#include + +#define ERR_MSG_LEN 256 + +static void list_directory(const char* dirname); +static void list_size(const char* full_path); +static void print_error_msg(const char* dirname, char* msg_buffer); +static void get_full_path(const char* dirname, const char* filename, char* buffer); + + +int main(int argc, char* argv[]) +{ + /* Select default locale */ + setlocale(LC_ALL, ""); + + /* For each directory in command line. */ + int i = 1; + while (i < argc) { + list_directory(argv[i]); + i++; + printf("\n"); + } + + /* List current working directory if no arguments on command line */ + if (argc == 1) + list_directory("."); + + return EXIT_SUCCESS; +} + +/* + * List files and file sizes in directory. + */ +static void list_directory(const char* dirname) +{ + /* Open directory stream */ + DIR* dir = opendir(dirname); + if (!dir) { + /* Could not open directory; print error message */ + char msg_buff[ERR_MSG_LEN]; + + /* Function replaces strerror with strerror_s */ + print_error_msg(dirname, msg_buff); + + exit(EXIT_FAILURE); + } + + struct dirent* ent; + /* Print all files, file sizes, and directories within the directory */ + while ((ent = readdir(dir)) != NULL) { + char buffer[PATH_MAX + 2]; + + switch (ent->d_type) { + case DT_REG: + printf("%s ", ent->d_name); + /* Get full file path */ + get_full_path(dirname, ent->d_name, buffer); + /* Print file size next to file name */ + list_size(buffer); + break; + + case DT_DIR: + printf("%s/\n", ent->d_name); + break; + + case DT_LNK: + printf("%s@ ", ent->d_name); + /* Get full file path */ + get_full_path(dirname, ent->d_name, buffer); + /* Print file size next to file name */ + list_size(buffer); + break; + + default: + printf("%s* ", ent->d_name); + /* Get full file path */ + get_full_path(dirname, ent->d_name, buffer); + /* Print file size next to file name */ + list_size(buffer); + break; + } + } + + closedir(dir); +} + +/* + * Print file size next to file name. + */ +static void list_size(const char* full_path) +{ + struct stat stbuf; + + if (stat(full_path, &stbuf) == -1) + printf("%s %s\n", "Can't access", full_path); + else + printf("%d\n", stbuf.st_size); +} + +/* + * Enforce error message size limit and ensure valid error number. + * Print error message. + */ +static void print_error_msg(const char* dirname, char* msg_buff) +{ + int error_num; + error_num = strerror_s(msg_buff, ERR_MSG_LEN, errno); + + switch (error_num) { + case 0: + printf("Cannot open %s (%s)\n", dirname, msg_buff); + break; + + case EINVAL: + printf("strerror_s() failed: invalid error code, %d\n", + error_num); + break; + + case ERANGE: + printf("strerror_s() failed: buffer too small: %d\n", + ERR_MSG_LEN); + break; + } +} + +/* + * Combine directory and file name. + * Write full path to memory. + */ +static void get_full_path(const char* dirname, const char* filename, char* buffer) +{ + char* dest = buffer; + char* end = &buffer[PATH_MAX]; + + /* Copy directory name to buffer */ + const char* src = dirname; + while (dest < end && *src != '\0') { + *dest++ = *src++; + } + *dest = '\0'; + + char* new_dest = dest; + char c; + + /* Get final character of directory name */ + if (buffer < new_dest) + c = new_dest[-1]; + else + c = ':'; + + /* Append directory separator if not already there */ + if (c != ':' && c != '/' && c != '\\') + *new_dest++ = '/'; + + /* Append file name */ + src = filename; + while (new_dest < end && *src != '\0') { + *new_dest++ = *src++; + } + *new_dest = '\0'; +} diff --git a/HW/homework/HW/dirent-master/examples/find.c b/HW/homework/HW/dirent-master/examples/find.c new file mode 100644 index 00000000..a5a6b267 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/find.c @@ -0,0 +1,130 @@ +/* + * An example demonstrating recursive directory traversal. + * + * Compile this file with Visual Studio and run the produced command in + * console with a directory name argument. For example, command + * + * find "C:\Program Files" + * + * will output thousands of file names such as + * + * c:\Program Files/7-Zip/7-zip.chm + * c:\Program Files/7-Zip/7-zip.dll + * c:\Program Files/7-Zip/7z.dll + * c:\Program Files/Adobe/Reader 10.0/Reader/logsession.dll + * c:\Program Files/Adobe/Reader 10.0/Reader/LogTransport2.exe + * c:\Program Files/Windows NT/Accessories/wordpad.exe + * c:\Program Files/Windows NT/Accessories/write.wpc + * + * The find command provided by this file is only an example: the command does + * not provide options to restrict the output to certain files as the Linux + * version does. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +static int find_directory(const char *dirname); + + +int main(int argc, char *argv[]) +{ + /* Select default locale */ + setlocale(LC_ALL, ""); + + /* For each directory in command line */ + int i = 1; + while (i < argc) { + if (!find_directory(argv[i])) + exit(EXIT_FAILURE); + i++; + } + + /* List current working directory if no arguments on command line */ + if (argc == 1) + find_directory("."); + + return EXIT_SUCCESS; +} + +/* Find files and subdirectories recursively */ +static int find_directory(const char *dirname) +{ + char buffer[PATH_MAX + 2]; + char *p = buffer; + char *end = &buffer[PATH_MAX]; + + /* Copy directory name to buffer */ + const char *src = dirname; + while (p < end && *src != '\0') { + *p++ = *src++; + } + *p = '\0'; + + /* Open directory stream */ + DIR *dir = opendir(dirname); + if (!dir) { + /* Could not open directory */ + fprintf(stderr, + "Cannot open %s (%s)\n", dirname, strerror(errno)); + return /*failure*/ 0; + } + + /* Print all files and directories within the directory */ + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + char *q = p; + char c; + + /* Get final character of directory name */ + if (buffer < q) + c = q[-1]; + else + c = ':'; + + /* Append directory separator if not already there */ + if (c != ':' && c != '/' && c != '\\') + *q++ = '/'; + + /* Append file name */ + src = ent->d_name; + while (q < end && *src != '\0') { + *q++ = *src++; + } + *q = '\0'; + + /* Decide what to do with the directory entry */ + switch (ent->d_type) { + case DT_LNK: + case DT_REG: + /* Output file name with directory */ + printf("%s\n", buffer); + break; + + case DT_DIR: + /* Scan sub-directory recursively */ + if (strcmp(ent->d_name, ".") != 0 + && strcmp(ent->d_name, "..") != 0) { + find_directory(buffer); + } + break; + + default: + /* Ignore device entries */ + /*NOP*/; + } + + } + + closedir(dir); + return /*success*/ 1; +} diff --git a/HW/homework/HW/dirent-master/examples/locate.c b/HW/homework/HW/dirent-master/examples/locate.c new file mode 100644 index 00000000..556871a2 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/locate.c @@ -0,0 +1,259 @@ +/* + * A file look-up utility to complement updatedb + * + * Compile and run updatedb command first to create locate.db file. Then, + * compile this program with Visual Studio and run the program in console with + * a file name argument. For example, the command + * + * locate autoexec + * + * might output something like + * + * c:/AUTOEXEC.BAT + * c:/WINDOWS/repair/autoexec.nt + * c:/WINDOWS/system32/AUTOEXEC.NT + * + * Be ware that this file uses wide-character API which is not compatible + * with Linux or other major Unixes. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#include +#ifdef WIN32 +# include +# include +#endif +#include + +/* File name and location of database file */ +#define DB_LOCATION L"locate.db" + + +/* Forward-decl */ +static int db_locate(const wchar_t *pattern); +static int db_match(const wchar_t *fn, const wchar_t *pattern); +static void db_open(void); +static void db_close(void); +static int db_read(wchar_t *buffer, size_t max); + + +/* Module local variables */ +static FILE *db = NULL; + +int main(int argc, char *argv[]) +{ +#ifdef WIN32 + /* Prepare for unicode output */ + _setmode(_fileno(stdout), _O_U16TEXT); + + /* For each pattern in command line */ + int i = 1; + while (i < argc) { + wchar_t buffer[PATH_MAX + 1]; + size_t n; + int count = 0; + + /* Convert ith argument to wide-character string */ + errno_t error = mbstowcs_s(&n, buffer, PATH_MAX, argv[i], _TRUNCATE); + if (!error) { + /* Find files matching pattern */ + count = db_locate(buffer); + + /* Output warning if string is not found */ + if (count == 0) { + wprintf(L"%s not found\n", buffer); + } + } + + i++; + } + + if (argc < 2) { + wprintf(L"Usage: locate pattern\n"); + exit(EXIT_FAILURE); + } +#else + printf("locate only works on Microsoft Windows\n"); +#endif + return EXIT_SUCCESS; +} + +/* Match pattern against files in locate.db file */ +static int db_locate(const wchar_t *pattern) +{ + int count = 0; + +#ifdef WIN32 + wchar_t buffer[PATH_MAX + 1]; + + /* Open locate.db for read */ + db_open(); + + /* Read one directory and file name at a time from database file */ + while (db_read(buffer, PATH_MAX + 1)) { + /* See if file name in buffer matches the search pattern */ + if (db_match(buffer, pattern)) { + /* Match found => output file name and path */ + wprintf(L"%s\n", buffer); + count++; + } + } + + db_close(); +#endif + + return count; +} + +/* Match pattern against file name */ +static int db_match(const wchar_t *fn, const wchar_t *pattern) +{ + int found = 0; + +#ifdef WIN32 + /* Locate zero-terminator from fn */ + wchar_t *p = wcschr(fn, '\0'); + + /* Find base name from buffer */ + int done = 0; + while (fn < p && !done) { + switch (p[-1]) { + case ':': + case '/': + case '\\': + /* Final path separator found */ + done = 1; + break; + + default: + /* No path separator yet */ + p--; + } + } + + /* Convert base name to lower case */ + int i = 0; + wchar_t base[PATH_MAX + 1]; + while (i < PATH_MAX && p[i] != '\0') { + base[i] = towlower(p[i]); + i++; + } + base[i] = '\0'; + + /* Convert search pattern to lower case */ + i = 0; + wchar_t patt[PATH_MAX + 1]; + while (i < PATH_MAX && pattern[i] != '\0') { + patt[i] = towlower(pattern[i]); + i++; + } + patt[i] = '\0'; + + /* See if file name matches pattern */ + if (wcsstr(base, patt) != NULL) { + found = 1; + } else { + found = 0; + } +#endif + + return found; +} + +/* + * Read line from locate.db. This function is same as fgetws() except + * that new-line at the end of line is not included. + */ +static int db_read(wchar_t *buffer, size_t max) +{ + int ok = 0; + +#ifdef WIN32 + size_t i = 0; + wchar_t c; + int done = 0; + + if (!db) { + wprintf(L"Database not open\n"); + exit(EXIT_SUCCESS); + } + + do { + /* Read wide-character from stream */ + c = fgetwc(db); + + /* Determine how to process character */ + switch (c) { + case '\r': + /* Ignore, should be handled by run-time libraries */ + /*NOP*/; + break; + + case '\n': + /* End of string => return file name */ + done = 1; + ok = 1; + break; + + case /*EOF*/WEOF: + /* End of file */ + done = 1; + if (i == 0) { + /* No data in buffer => return false */ + ok = 0; + } else { + /* Data in buffer => return file name */ + ok = 1; + } + break; + + default: + /* Store character */ + if (i < max - 1) { + buffer[i++] = c; + } else { + buffer[max - 1] = '\0'; + wprintf(L"Buffer too small: %s", buffer); + exit(EXIT_FAILURE); + } + } + } while (!done); + + /* Zero-terminate buffer */ + buffer[i] = '\0'; +#endif + + return ok; +} + +/* Open database file locate.db */ +static void db_open(void) +{ +#ifdef WIN32 + if (db) + return; + + /* Open file for writing */ + errno_t error = _wfopen_s(&db, DB_LOCATION, L"rt, ccs=UNICODE"); + if (error) { + wprintf(L"Cannot open %s\n", DB_LOCATION); + exit(EXIT_FAILURE); + } +#endif +} + +/* Close database file */ +static void db_close(void) +{ + if (!db) + return; + + fclose(db); + db = NULL; +} diff --git a/HW/homework/HW/dirent-master/examples/ls.c b/HW/homework/HW/dirent-master/examples/ls.c new file mode 100644 index 00000000..d1fccf32 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/ls.c @@ -0,0 +1,94 @@ +/* + * An example demonstrating basic directory listing. + * + * Compile this file with Visual Studio and run the produced command in + * console with a directory name argument. For example, command + * + * ls "c:\Program Files" + * + * might output something like + * + * ./ + * ../ + * 7-Zip/ + * Internet Explorer/ + * Microsoft Visual Studio 9.0/ + * Microsoft.NET/ + * Mozilla Firefox/ + * + * The ls command provided by this file is only an example: the command does + * not have any fancy options like "ls -al" in Linux and the command does not + * support file name matching like "ls *.c". + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +static void list_directory(const char *dirname); + + +int main(int argc, char *argv[]) +{ + /* Select default locale */ + setlocale(LC_ALL, ""); + + /* For each directory in command line */ + int i = 1; + while (i < argc) { + list_directory(argv[i]); + i++; + } + + /* List current working directory if no arguments on command line */ + if (argc == 1) + list_directory("."); + + return EXIT_SUCCESS; +} + +/* + * List files and directories within a directory. + */ +static void list_directory(const char *dirname) +{ + /* Open directory stream */ + DIR *dir = opendir(dirname); + if (!dir) { + /* Could not open directory */ + fprintf(stderr, + "Cannot open %s (%s)\n", dirname, strerror(errno)); + exit(EXIT_FAILURE); + } + + /* Print all files and directories within the directory */ + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + switch (ent->d_type) { + case DT_REG: + printf("%s\n", ent->d_name); + break; + + case DT_DIR: + printf("%s/\n", ent->d_name); + break; + + case DT_LNK: + printf("%s@\n", ent->d_name); + break; + + default: + printf("%s*\n", ent->d_name); + } + } + + closedir(dir); +} diff --git a/HW/homework/HW/dirent-master/examples/scandir.c b/HW/homework/HW/dirent-master/examples/scandir.c new file mode 100644 index 00000000..0f776203 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/scandir.c @@ -0,0 +1,100 @@ +/* + * Example program demonstrating the use of scandir function. + * + * Compile this file with Visual Studio and run the produced command in + * console with a directory name argument. For example, command + * + * scandir "c:\Program Files" + * + * might output something like + * + * ./ + * ../ + * 7-Zip/ + * Internet Explorer/ + * Microsoft Visual Studio 9.0/ + * Microsoft.NET/ + * Mozilla Firefox/ + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +static void list_directory(const char *dirname); + + +int main(int argc, char *argv[]) +{ + /* Select default locale */ + setlocale(LC_ALL, ""); + + /* For each directory in command line */ + int i = 1; + while (i < argc) { + list_directory(argv[i]); + i++; + } + + /* List current working directory if no arguments on command line */ + if (argc == 1) + list_directory("."); + + return EXIT_SUCCESS; +} + +/* + * List files and directories within a directory. + */ +static void +list_directory( + const char *dirname) +{ + /* Scan files in directory */ + struct dirent **files; + int n = scandir(dirname, &files, NULL, alphasort); + if (n < 0) { + fprintf(stderr, + "Cannot open %s (%s)\n", dirname, strerror(errno)); + exit(EXIT_FAILURE); + } + + /* Loop through file names */ + for (int i = 0; i < n; i++) { + /* Get pointer to file entry */ + struct dirent *ent = files[i]; + + /* Output file name */ + switch (ent->d_type) { + case DT_REG: + printf("%s\n", ent->d_name); + break; + + case DT_DIR: + printf("%s/\n", ent->d_name); + break; + + case DT_LNK: + printf("%s@\n", ent->d_name); + break; + + default: + printf("%s*\n", ent->d_name); + } + + } + + /* Release file names */ + for (int i = 0; i < n; i++) { + free(files[i]); + } + free(files); +} diff --git a/HW/homework/HW/dirent-master/examples/updatedb.c b/HW/homework/HW/dirent-master/examples/updatedb.c new file mode 100644 index 00000000..74a35335 --- /dev/null +++ b/HW/homework/HW/dirent-master/examples/updatedb.c @@ -0,0 +1,211 @@ +/* + * An example demonstrating wide-character functions + * + * Compile this file with Visual Studio and run the produced command in + * console with a directory name argument. For example, command + * + * updatedb C:\ + * + * will produce the file locate.db with one file name per line such as + * + * c:\Program Files/7-Zip/7-zip.chm + * c:\Program Files/7-Zip/7-zip.dll + * c:\Program Files/7-Zip/7z.dll + * c:\Program Files/Adobe/Reader 10.0/Reader/logsession.dll + * c:\Program Files/Adobe/Reader 10.0/Reader/LogTransport2.exe + * c:\Program Files/Windows NT/Accessories/wordpad.exe + * c:\Program Files/Windows NT/Accessories/write.wpc + * + * Be ware that this file uses wide-character API which is not compatible + * with Linux or other major Unixes. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#include +#ifdef WIN32 +# include +# include +#endif +#include + +/* File name and location of database file */ +#define DB_LOCATION L"locate.db" + + +/* Forward-decl */ +static int update_directory(const wchar_t *dirname); +static void db_open(void); +static void db_close(void); +static void db_store(const wchar_t *dirname); + + +/* Module local variables */ +static FILE *db = NULL; + + +int main(int argc, char *argv[]) +{ +#ifdef WIN32 + /* Prepare for unicode output */ + _setmode(_fileno(stdout), _O_U16TEXT); + + /* Open locate.db */ + db_open(); + + /* For each directory in command line */ + int i = 1; + while (i < argc) { + wchar_t buffer[PATH_MAX + 1]; + errno_t error; + size_t n; + + /* Convert ith argument to wide-character string */ + error = mbstowcs_s(&n, buffer, PATH_MAX, argv[i], _TRUNCATE); + if (!error) { + + /* Scan directory for files */ + int ok = update_directory(buffer); + if (!ok) { + wprintf(L"Cannot open directory %s\n", buffer); + exit(EXIT_FAILURE); + } + + } + + i++; + } + + /* Use current working directory if no arguments on command line */ + if (argc == 1) + update_directory(L"."); + + db_close(); +#else + printf("updatedb only works on Microsoft Windows\n"); +#endif + return EXIT_SUCCESS; +} + +/* Find files recursively */ +static int update_directory(const wchar_t *dirname) +{ +#ifdef WIN32 + wchar_t buffer[PATH_MAX + 2]; + wchar_t *p = buffer; + wchar_t *end = &buffer[PATH_MAX]; + + /* Copy directory name to buffer */ + const wchar_t *src = dirname; + while (p < end && *src != '\0') { + *p++ = *src++; + } + *p = '\0'; + + /* Open directory stream */ + _WDIR *dir = _wopendir(dirname); + if (!dir) { + /* Cannot open directory */ + return /*failure*/ 0; + } + + /* Print all files and directories within the directory */ + struct _wdirent *ent; + while ((ent = _wreaddir (dir)) != NULL) { + wchar_t *q = p; + wchar_t c; + + /* Get final character of directory name */ + if (buffer < q) + c = q[-1]; + else + c = ':'; + + /* Append directory separator if not already there */ + if (c != ':' && c != '/' && c != '\\') + *q++ = '/'; + + /* Append file name */ + src = ent->d_name; + while (q < end && *src != '\0') { + *q++ = *src++; + } + *q = '\0'; + + /* Decide what to do with the directory entry */ + switch (ent->d_type) { + case DT_REG: + /* Store file name */ + db_store(buffer); + break; + + case DT_DIR: + /* Scan sub-directory recursively */ + if (wcscmp(ent->d_name, L".") != 0 + && wcscmp(ent->d_name, L"..") != 0) { + update_directory(buffer); + } + break; + + default: + /* Do not device entries */ + /*NOP*/; + } + + } + + wclosedir(dir); + return /*success*/ 1; +#else + return /*failure*/ 0; +#endif +} + +/* Store file name to locate.db */ +static void db_store(const wchar_t *dirname) +{ +#ifdef WIN32 + if (!db) { + wprintf(L"Database not open\n"); + exit(EXIT_FAILURE); + } + + /* Output line to file */ + fwprintf(db, L"%s\n", dirname); +#endif +} + +/* Open database file locate.db */ +static void db_open(void) +{ +#ifdef WIN32 + if (db) + return; + + /* Open file for writing */ + errno_t error = _wfopen_s(&db, DB_LOCATION, L"wt, ccs=UNICODE"); + if (error) { + wprintf(L"Cannot open %s\n", DB_LOCATION); + exit(EXIT_FAILURE); + } +#endif +} + +/* Close database file */ +static void +db_close( + void) +{ + if (!db) { + return; + } + + /* Close file */ + fclose(db); + db = NULL; +} diff --git a/HW/homework/HW/dirent-master/include/dirent.h b/HW/homework/HW/dirent-master/include/dirent.h new file mode 100644 index 00000000..a2e847a0 --- /dev/null +++ b/HW/homework/HW/dirent-master/include/dirent.h @@ -0,0 +1,1027 @@ +/* + * Dirent interface for Microsoft Visual Studio + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#ifndef DIRENT_H +#define DIRENT_H + +/* Hide warnings about unreferenced local functions */ +#if defined(__clang__) +# pragma clang diagnostic ignored "-Wunused-function" +#elif defined(_MSC_VER) +# pragma warning(disable:4505) +#elif defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wunused-function" +#endif + +/* + * Include windows.h without Windows Sockets 1.1 to prevent conflicts with + * Windows Sockets 2.0. + */ +#ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Indicates that d_type field is available in dirent structure */ +#define _DIRENT_HAVE_D_TYPE + +/* Indicates that d_namlen field is available in dirent structure */ +#define _DIRENT_HAVE_D_NAMLEN + +/* Entries missing from MSVC 6.0 */ +#if !defined(FILE_ATTRIBUTE_DEVICE) +# define FILE_ATTRIBUTE_DEVICE 0x40 +#endif + +/* File type and permission flags for stat(), general mask */ +#if !defined(S_IFMT) +# define S_IFMT _S_IFMT +#endif + +/* Directory bit */ +#if !defined(S_IFDIR) +# define S_IFDIR _S_IFDIR +#endif + +/* Character device bit */ +#if !defined(S_IFCHR) +# define S_IFCHR _S_IFCHR +#endif + +/* Pipe bit */ +#if !defined(S_IFFIFO) +# define S_IFFIFO _S_IFFIFO +#endif + +/* Regular file bit */ +#if !defined(S_IFREG) +# define S_IFREG _S_IFREG +#endif + +/* Read permission */ +#if !defined(S_IREAD) +# define S_IREAD _S_IREAD +#endif + +/* Write permission */ +#if !defined(S_IWRITE) +# define S_IWRITE _S_IWRITE +#endif + +/* Execute permission */ +#if !defined(S_IEXEC) +# define S_IEXEC _S_IEXEC +#endif + +/* Pipe */ +#if !defined(S_IFIFO) +# define S_IFIFO _S_IFIFO +#endif + +/* Block device */ +#if !defined(S_IFBLK) +# define S_IFBLK 0 +#endif + +/* Link */ +#if !defined(S_IFLNK) +# define S_IFLNK 0 +#endif + +/* Socket */ +#if !defined(S_IFSOCK) +# define S_IFSOCK 0 +#endif + +/* Read user permission */ +#if !defined(S_IRUSR) +# define S_IRUSR S_IREAD +#endif + +/* Write user permission */ +#if !defined(S_IWUSR) +# define S_IWUSR S_IWRITE +#endif + +/* Execute user permission */ +#if !defined(S_IXUSR) +# define S_IXUSR 0 +#endif + +/* Read group permission */ +#if !defined(S_IRGRP) +# define S_IRGRP 0 +#endif + +/* Write group permission */ +#if !defined(S_IWGRP) +# define S_IWGRP 0 +#endif + +/* Execute group permission */ +#if !defined(S_IXGRP) +# define S_IXGRP 0 +#endif + +/* Read others permission */ +#if !defined(S_IROTH) +# define S_IROTH 0 +#endif + +/* Write others permission */ +#if !defined(S_IWOTH) +# define S_IWOTH 0 +#endif + +/* Execute others permission */ +#if !defined(S_IXOTH) +# define S_IXOTH 0 +#endif + +/* Maximum length of file name */ +#if !defined(PATH_MAX) +# define PATH_MAX MAX_PATH +#endif +#if !defined(FILENAME_MAX) +# define FILENAME_MAX MAX_PATH +#endif +#if !defined(NAME_MAX) +# define NAME_MAX FILENAME_MAX +#endif + +/* File type flags for d_type */ +#define DT_UNKNOWN 0 +#define DT_REG S_IFREG +#define DT_DIR S_IFDIR +#define DT_FIFO S_IFIFO +#define DT_SOCK S_IFSOCK +#define DT_CHR S_IFCHR +#define DT_BLK S_IFBLK +#define DT_LNK S_IFLNK + +/* Macros for converting between st_mode and d_type */ +#define IFTODT(mode) ((mode) & S_IFMT) +#define DTTOIF(type) (type) + +/* + * File type macros. Note that block devices, sockets and links cannot be + * distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are + * only defined for compatibility. These macros should always return false + * on Windows. + */ +#if !defined(S_ISFIFO) +# define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) +#endif +#if !defined(S_ISDIR) +# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#endif +#if !defined(S_ISREG) +# define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) +#endif +#if !defined(S_ISLNK) +# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) +#endif +#if !defined(S_ISSOCK) +# define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) +#endif +#if !defined(S_ISCHR) +# define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) +#endif +#if !defined(S_ISBLK) +# define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) +#endif + +/* Return the exact length of the file name without zero terminator */ +#define _D_EXACT_NAMLEN(p) ((p)->d_namlen) + +/* Return the maximum size of a file name */ +#define _D_ALLOC_NAMLEN(p) ((PATH_MAX)+1) + + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Wide-character version */ +struct _wdirent { + /* Always zero */ + long d_ino; + + /* File position within stream */ + long d_off; + + /* Structure size */ + unsigned short d_reclen; + + /* Length of name without \0 */ + size_t d_namlen; + + /* File type */ + int d_type; + + /* File name */ + wchar_t d_name[PATH_MAX+1]; +}; +typedef struct _wdirent _wdirent; + +struct _WDIR { + /* Current directory entry */ + struct _wdirent ent; + + /* Private file data */ + WIN32_FIND_DATAW data; + + /* True if data is valid */ + int cached; + + /* Win32 search handle */ + HANDLE handle; + + /* Initial directory name */ + wchar_t *patt; +}; +typedef struct _WDIR _WDIR; + +/* Multi-byte character version */ +struct dirent { + /* Always zero */ + long d_ino; + + /* File position within stream */ + long d_off; + + /* Structure size */ + unsigned short d_reclen; + + /* Length of name without \0 */ + size_t d_namlen; + + /* File type */ + int d_type; + + /* File name */ + char d_name[PATH_MAX+1]; +}; +typedef struct dirent dirent; + +struct DIR { + struct dirent ent; + struct _WDIR *wdirp; +}; +typedef struct DIR DIR; + + +/* Dirent functions */ +static DIR *opendir(const char *dirname); +static _WDIR *_wopendir(const wchar_t *dirname); + +static struct dirent *readdir(DIR *dirp); +static struct _wdirent *_wreaddir(_WDIR *dirp); + +static int readdir_r( + DIR *dirp, struct dirent *entry, struct dirent **result); +static int _wreaddir_r( + _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result); + +static int closedir(DIR *dirp); +static int _wclosedir(_WDIR *dirp); + +static void rewinddir(DIR* dirp); +static void _wrewinddir(_WDIR* dirp); + +static int scandir(const char *dirname, struct dirent ***namelist, + int (*filter)(const struct dirent*), + int (*compare)(const struct dirent**, const struct dirent**)); + +static int alphasort(const struct dirent **a, const struct dirent **b); + +static int versionsort(const struct dirent **a, const struct dirent **b); + +static int strverscmp(const char *a, const char *b); + +/* For compatibility with Symbian */ +#define wdirent _wdirent +#define WDIR _WDIR +#define wopendir _wopendir +#define wreaddir _wreaddir +#define wclosedir _wclosedir +#define wrewinddir _wrewinddir + +/* Compatibility with older Microsoft compilers and non-Microsoft compilers */ +#if !defined(_MSC_VER) || _MSC_VER < 1400 +# define wcstombs_s dirent_wcstombs_s +# define mbstowcs_s dirent_mbstowcs_s +#endif + +/* Optimize dirent_set_errno() away on modern Microsoft compilers */ +#if defined(_MSC_VER) && _MSC_VER >= 1400 +# define dirent_set_errno _set_errno +#endif + + +/* Internal utility functions */ +static WIN32_FIND_DATAW *dirent_first(_WDIR *dirp); +static WIN32_FIND_DATAW *dirent_next(_WDIR *dirp); + +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static int dirent_mbstowcs_s( + size_t *pReturnValue, wchar_t *wcstr, size_t sizeInWords, + const char *mbstr, size_t count); +#endif + +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static int dirent_wcstombs_s( + size_t *pReturnValue, char *mbstr, size_t sizeInBytes, + const wchar_t *wcstr, size_t count); +#endif + +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static void dirent_set_errno(int error); +#endif + + +/* + * Open directory stream DIRNAME for read and return a pointer to the + * internal working area that is used to retrieve individual directory + * entries. + */ +static _WDIR *_wopendir(const wchar_t *dirname) +{ + wchar_t *p; + + /* Must have directory name */ + if (dirname == NULL || dirname[0] == '\0') { + dirent_set_errno(ENOENT); + return NULL; + } + + /* Allocate new _WDIR structure */ + _WDIR *dirp = (_WDIR*) malloc(sizeof(struct _WDIR)); + if (!dirp) + return NULL; + + /* Reset _WDIR structure */ + dirp->handle = INVALID_HANDLE_VALUE; + dirp->patt = NULL; + dirp->cached = 0; + + /* + * Compute the length of full path plus zero terminator + * + * Note that on WinRT there's no way to convert relative paths + * into absolute paths, so just assume it is an absolute path. + */ +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) + /* Desktop */ + DWORD n = GetFullPathNameW(dirname, 0, NULL, NULL); +#else + /* WinRT */ + size_t n = wcslen(dirname); +#endif + + /* Allocate room for absolute directory name and search pattern */ + dirp->patt = (wchar_t*) malloc(sizeof(wchar_t) * n + 16); + if (dirp->patt == NULL) + goto exit_closedir; + + /* + * Convert relative directory name to an absolute one. This + * allows rewinddir() to function correctly even when current + * working directory is changed between opendir() and rewinddir(). + * + * Note that on WinRT there's no way to convert relative paths + * into absolute paths, so just assume it is an absolute path. + */ +#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) + /* Desktop */ + n = GetFullPathNameW(dirname, n, dirp->patt, NULL); + if (n <= 0) + goto exit_closedir; +#else + /* WinRT */ + wcsncpy_s(dirp->patt, n+1, dirname, n); +#endif + + /* Append search pattern \* to the directory name */ + p = dirp->patt + n; + switch (p[-1]) { + case '\\': + case '/': + case ':': + /* Directory ends in path separator, e.g. c:\temp\ */ + /*NOP*/; + break; + + default: + /* Directory name doesn't end in path separator */ + *p++ = '\\'; + } + *p++ = '*'; + *p = '\0'; + + /* Open directory stream and retrieve the first entry */ + if (!dirent_first(dirp)) + goto exit_closedir; + + /* Success */ + return dirp; + + /* Failure */ +exit_closedir: + _wclosedir(dirp); + return NULL; +} + +/* + * Read next directory entry. + * + * Returns pointer to static directory entry which may be overwritten by + * subsequent calls to _wreaddir(). + */ +static struct _wdirent *_wreaddir(_WDIR *dirp) +{ + /* + * Read directory entry to buffer. We can safely ignore the return + * value as entry will be set to NULL in case of error. + */ + struct _wdirent *entry; + (void) _wreaddir_r(dirp, &dirp->ent, &entry); + + /* Return pointer to statically allocated directory entry */ + return entry; +} + +/* + * Read next directory entry. + * + * Returns zero on success. If end of directory stream is reached, then sets + * result to NULL and returns zero. + */ +static int _wreaddir_r( + _WDIR *dirp, struct _wdirent *entry, struct _wdirent **result) +{ + /* Read next directory entry */ + WIN32_FIND_DATAW *datap = dirent_next(dirp); + if (!datap) { + /* Return NULL to indicate end of directory */ + *result = NULL; + return /*OK*/0; + } + + /* + * Copy file name as wide-character string. If the file name is too + * long to fit in to the destination buffer, then truncate file name + * to PATH_MAX characters and zero-terminate the buffer. + */ + size_t n = 0; + while (n < PATH_MAX && datap->cFileName[n] != 0) { + entry->d_name[n] = datap->cFileName[n]; + n++; + } + entry->d_name[n] = 0; + + /* Length of file name excluding zero terminator */ + entry->d_namlen = n; + + /* File type */ + DWORD attr = datap->dwFileAttributes; + if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) + entry->d_type = DT_CHR; + else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) + entry->d_type = DT_DIR; + else + entry->d_type = DT_REG; + + /* Reset dummy fields */ + entry->d_ino = 0; + entry->d_off = 0; + entry->d_reclen = sizeof(struct _wdirent); + + /* Set result address */ + *result = entry; + return /*OK*/0; +} + +/* + * Close directory stream opened by opendir() function. This invalidates the + * DIR structure as well as any directory entry read previously by + * _wreaddir(). + */ +static int _wclosedir(_WDIR *dirp) +{ + if (!dirp) { + dirent_set_errno(EBADF); + return /*failure*/-1; + } + + /* Release search handle */ + if (dirp->handle != INVALID_HANDLE_VALUE) + FindClose(dirp->handle); + + /* Release search pattern */ + free(dirp->patt); + + /* Release directory structure */ + free(dirp); + return /*success*/0; +} + +/* + * Rewind directory stream such that _wreaddir() returns the very first + * file name again. + */ +static void _wrewinddir(_WDIR* dirp) +{ + if (!dirp) + return; + + /* Release existing search handle */ + if (dirp->handle != INVALID_HANDLE_VALUE) + FindClose(dirp->handle); + + /* Open new search handle */ + dirent_first(dirp); +} + +/* Get first directory entry */ +static WIN32_FIND_DATAW *dirent_first(_WDIR *dirp) +{ + if (!dirp) + return NULL; + + /* Open directory and retrieve the first entry */ + dirp->handle = FindFirstFileExW( + dirp->patt, FindExInfoStandard, &dirp->data, + FindExSearchNameMatch, NULL, 0); + if (dirp->handle == INVALID_HANDLE_VALUE) + goto error; + + /* A directory entry is now waiting in memory */ + dirp->cached = 1; + return &dirp->data; + +error: + /* Failed to open directory: no directory entry in memory */ + dirp->cached = 0; + + /* Set error code */ + DWORD errorcode = GetLastError(); + switch (errorcode) { + case ERROR_ACCESS_DENIED: + /* No read access to directory */ + dirent_set_errno(EACCES); + break; + + case ERROR_DIRECTORY: + /* Directory name is invalid */ + dirent_set_errno(ENOTDIR); + break; + + case ERROR_PATH_NOT_FOUND: + default: + /* Cannot find the file */ + dirent_set_errno(ENOENT); + } + return NULL; +} + +/* Get next directory entry */ +static WIN32_FIND_DATAW *dirent_next(_WDIR *dirp) +{ + /* Is the next directory entry already in cache? */ + if (dirp->cached) { + /* Yes, a valid directory entry found in memory */ + dirp->cached = 0; + return &dirp->data; + } + + /* No directory entry in cache */ + if (dirp->handle == INVALID_HANDLE_VALUE) + return NULL; + + /* Read the next directory entry from stream */ + if (FindNextFileW(dirp->handle, &dirp->data) == FALSE) + goto exit_close; + + /* Success */ + return &dirp->data; + + /* Failure */ +exit_close: + FindClose(dirp->handle); + dirp->handle = INVALID_HANDLE_VALUE; + return NULL; +} + +/* Open directory stream using plain old C-string */ +static DIR *opendir(const char *dirname) +{ + /* Must have directory name */ + if (dirname == NULL || dirname[0] == '\0') { + dirent_set_errno(ENOENT); + return NULL; + } + + /* Allocate memory for DIR structure */ + struct DIR *dirp = (DIR*) malloc(sizeof(struct DIR)); + if (!dirp) + return NULL; + + /* Convert directory name to wide-character string */ + wchar_t wname[PATH_MAX + 1]; + size_t n; + int error = mbstowcs_s(&n, wname, PATH_MAX + 1, dirname, PATH_MAX+1); + if (error) + goto exit_failure; + + /* Open directory stream using wide-character name */ + dirp->wdirp = _wopendir(wname); + if (!dirp->wdirp) + goto exit_failure; + + /* Success */ + return dirp; + + /* Failure */ +exit_failure: + free(dirp); + return NULL; +} + +/* Read next directory entry */ +static struct dirent *readdir(DIR *dirp) +{ + /* + * Read directory entry to buffer. We can safely ignore the return + * value as entry will be set to NULL in case of error. + */ + struct dirent *entry; + (void) readdir_r(dirp, &dirp->ent, &entry); + + /* Return pointer to statically allocated directory entry */ + return entry; +} + +/* + * Read next directory entry into called-allocated buffer. + * + * Returns zero on success. If the end of directory stream is reached, then + * sets result to NULL and returns zero. + */ +static int readdir_r( + DIR *dirp, struct dirent *entry, struct dirent **result) +{ + /* Read next directory entry */ + WIN32_FIND_DATAW *datap = dirent_next(dirp->wdirp); + if (!datap) { + /* No more directory entries */ + *result = NULL; + return /*OK*/0; + } + + /* Attempt to convert file name to multi-byte string */ + size_t n; + int error = wcstombs_s( + &n, entry->d_name, PATH_MAX + 1, + datap->cFileName, PATH_MAX + 1); + + /* + * If the file name cannot be represented by a multi-byte string, then + * attempt to use old 8+3 file name. This allows the program to + * access files although file names may seem unfamiliar to the user. + * + * Be ware that the code below cannot come up with a short file name + * unless the file system provides one. At least VirtualBox shared + * folders fail to do this. + */ + if (error && datap->cAlternateFileName[0] != '\0') { + error = wcstombs_s( + &n, entry->d_name, PATH_MAX + 1, + datap->cAlternateFileName, PATH_MAX + 1); + } + + if (!error) { + /* Length of file name excluding zero terminator */ + entry->d_namlen = n - 1; + + /* File attributes */ + DWORD attr = datap->dwFileAttributes; + if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) + entry->d_type = DT_CHR; + else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) + entry->d_type = DT_DIR; + else + entry->d_type = DT_REG; + + /* Reset dummy fields */ + entry->d_ino = 0; + entry->d_off = 0; + entry->d_reclen = sizeof(struct dirent); + } else { + /* + * Cannot convert file name to multi-byte string so construct + * an erroneous directory entry and return that. Note that + * we cannot return NULL as that would stop the processing + * of directory entries completely. + */ + entry->d_name[0] = '?'; + entry->d_name[1] = '\0'; + entry->d_namlen = 1; + entry->d_type = DT_UNKNOWN; + entry->d_ino = 0; + entry->d_off = -1; + entry->d_reclen = 0; + } + + /* Return pointer to directory entry */ + *result = entry; + return /*OK*/0; +} + +/* Close directory stream */ +static int closedir(DIR *dirp) +{ + int ok; + + if (!dirp) + goto exit_failure; + + /* Close wide-character directory stream */ + ok = _wclosedir(dirp->wdirp); + dirp->wdirp = NULL; + + /* Release multi-byte character version */ + free(dirp); + return ok; + +exit_failure: + /* Invalid directory stream */ + dirent_set_errno(EBADF); + return /*failure*/-1; +} + +/* Rewind directory stream to beginning */ +static void rewinddir(DIR* dirp) +{ + if (!dirp) + return; + + /* Rewind wide-character string directory stream */ + _wrewinddir(dirp->wdirp); +} + +/* Scan directory for entries */ +static int scandir( + const char *dirname, struct dirent ***namelist, + int (*filter)(const struct dirent*), + int (*compare)(const struct dirent**, const struct dirent**)) +{ + int result; + + /* Open directory stream */ + DIR *dir = opendir(dirname); + if (!dir) { + /* Cannot open directory */ + return /*Error*/ -1; + } + + /* Read directory entries to memory */ + struct dirent *tmp = NULL; + struct dirent **files = NULL; + size_t size = 0; + size_t allocated = 0; + while (1) { + /* Allocate room for a temporary directory entry */ + if (!tmp) { + tmp = (struct dirent*) malloc(sizeof(struct dirent)); + if (!tmp) + goto exit_failure; + } + + /* Read directory entry to temporary area */ + struct dirent *entry; + if (readdir_r(dir, tmp, &entry) != /*OK*/0) + goto exit_failure; + + /* Stop if we already read the last directory entry */ + if (entry == NULL) + goto exit_success; + + /* Determine whether to include the entry in results */ + if (filter && !filter(tmp)) + continue; + + /* Enlarge pointer table to make room for another pointer */ + if (size >= allocated) { + /* Compute number of entries in the new table */ + size_t num_entries = size * 2 + 16; + + /* Allocate new pointer table or enlarge existing */ + void *p = realloc(files, sizeof(void*) * num_entries); + if (!p) + goto exit_failure; + + /* Got the memory */ + files = (dirent**) p; + allocated = num_entries; + } + + /* Store the temporary entry to ptr table */ + files[size++] = tmp; + tmp = NULL; + } + +exit_failure: + /* Release allocated file entries */ + for (size_t i = 0; i < size; i++) { + free(files[i]); + } + + /* Release the pointer table */ + free(files); + files = NULL; + + /* Exit with error code */ + result = /*error*/ -1; + goto exit_status; + +exit_success: + /* Sort directory entries */ + qsort(files, size, sizeof(void*), + (int (*) (const void*, const void*)) compare); + + /* Pass pointer table to caller */ + if (namelist) + *namelist = files; + + /* Return the number of directory entries read */ + result = (int) size; + +exit_status: + /* Release temporary directory entry, if we had one */ + free(tmp); + + /* Close directory stream */ + closedir(dir); + return result; +} + +/* Alphabetical sorting */ +static int alphasort(const struct dirent **a, const struct dirent **b) +{ + return strcoll((*a)->d_name, (*b)->d_name); +} + +/* Sort versions */ +static int versionsort(const struct dirent **a, const struct dirent **b) +{ + return strverscmp((*a)->d_name, (*b)->d_name); +} + +/* Compare strings */ +static int strverscmp(const char *a, const char *b) +{ + size_t i = 0; + size_t j; + + /* Find first difference */ + while (a[i] == b[i]) { + if (a[i] == '\0') { + /* No difference */ + return 0; + } + ++i; + } + + /* Count backwards and find the leftmost digit */ + j = i; + while (j > 0 && isdigit(a[j-1])) { + --j; + } + + /* Determine mode of comparison */ + if (a[j] == '0' || b[j] == '0') { + /* Find the next non-zero digit */ + while (a[j] == '0' && a[j] == b[j]) { + j++; + } + + /* String with more digits is smaller, e.g 002 < 01 */ + if (isdigit(a[j])) { + if (!isdigit(b[j])) { + return -1; + } + } else if (isdigit(b[j])) { + return 1; + } + } else if (isdigit(a[j]) && isdigit(b[j])) { + /* Numeric comparison */ + size_t k1 = j; + size_t k2 = j; + + /* Compute number of digits in each string */ + while (isdigit(a[k1])) { + k1++; + } + while (isdigit(b[k2])) { + k2++; + } + + /* Number with more digits is bigger, e.g 999 < 1000 */ + if (k1 < k2) + return -1; + else if (k1 > k2) + return 1; + } + + /* Alphabetical comparison */ + return (int) ((unsigned char) a[i]) - ((unsigned char) b[i]); +} + +/* Convert multi-byte string to wide character string */ +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static int dirent_mbstowcs_s( + size_t *pReturnValue, wchar_t *wcstr, + size_t sizeInWords, const char *mbstr, size_t count) +{ + /* Older Visual Studio or non-Microsoft compiler */ + size_t n = mbstowcs(wcstr, mbstr, sizeInWords); + if (wcstr && n >= count) + return /*error*/ 1; + + /* Zero-terminate output buffer */ + if (wcstr && sizeInWords) { + if (n >= sizeInWords) + n = sizeInWords - 1; + wcstr[n] = 0; + } + + /* Length of multi-byte string with zero terminator */ + if (pReturnValue) { + *pReturnValue = n + 1; + } + + /* Success */ + return 0; +} +#endif + +/* Convert wide-character string to multi-byte string */ +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static int dirent_wcstombs_s( + size_t *pReturnValue, char *mbstr, + size_t sizeInBytes, const wchar_t *wcstr, size_t count) +{ + /* Older Visual Studio or non-Microsoft compiler */ + size_t n = wcstombs(mbstr, wcstr, sizeInBytes); + if (mbstr && n >= count) + return /*error*/1; + + /* Zero-terminate output buffer */ + if (mbstr && sizeInBytes) { + if (n >= sizeInBytes) { + n = sizeInBytes - 1; + } + mbstr[n] = '\0'; + } + + /* Length of resulting multi-bytes string WITH zero-terminator */ + if (pReturnValue) { + *pReturnValue = n + 1; + } + + /* Success */ + return 0; +} +#endif + +/* Set errno variable */ +#if !defined(_MSC_VER) || _MSC_VER < 1400 +static void dirent_set_errno(int error) +{ + /* Non-Microsoft compiler or older Microsoft compiler */ + errno = error; +} +#endif + +#ifdef __cplusplus +} +#endif +#endif /*DIRENT_H*/ diff --git a/HW/homework/HW/dirent-master/tests/1/dir/readme.txt b/HW/homework/HW/dirent-master/tests/1/dir/readme.txt new file mode 100644 index 00000000..e59af2f4 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/1/dir/readme.txt @@ -0,0 +1,3 @@ +This file ensures that the directory dir will be created accordingly when +you unzip dirent to your computer. The directory itself is needed by the +test program t-dirent. diff --git a/HW/homework/HW/dirent-master/tests/1/file b/HW/homework/HW/dirent-master/tests/1/file new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/2/Testfile-1.2.3.dat b/HW/homework/HW/dirent-master/tests/2/Testfile-1.2.3.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/2/file.txt b/HW/homework/HW/dirent-master/tests/2/file.txt new file mode 100644 index 00000000..d32e0045 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/2/file.txt @@ -0,0 +1 @@ +This dummy file is needed by the test program t-dirent. diff --git a/HW/homework/HW/dirent-master/tests/3/3zero.dat b/HW/homework/HW/dirent-master/tests/3/3zero.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/666.dat b/HW/homework/HW/dirent-master/tests/3/666.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/Qwerty-my-aunt.dat b/HW/homework/HW/dirent-master/tests/3/Qwerty-my-aunt.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/README.txt b/HW/homework/HW/dirent-master/tests/3/README.txt new file mode 100644 index 00000000..a07591cc --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/3/README.txt @@ -0,0 +1,2 @@ +This directory contains some random files for the t-scandir test program. The +files are empty and only the file names matter. diff --git a/HW/homework/HW/dirent-master/tests/3/aaa.dat b/HW/homework/HW/dirent-master/tests/3/aaa.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/dirent.dat b/HW/homework/HW/dirent-master/tests/3/dirent.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/empty.dat b/HW/homework/HW/dirent-master/tests/3/empty.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/sane-1.12.0.dat b/HW/homework/HW/dirent-master/tests/3/sane-1.12.0.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/sane-1.2.30.dat b/HW/homework/HW/dirent-master/tests/3/sane-1.2.30.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/sane-1.2.4.dat b/HW/homework/HW/dirent-master/tests/3/sane-1.2.4.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/3/zebra.dat b/HW/homework/HW/dirent-master/tests/3/zebra.dat new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/dirent-master/tests/t-compile.c b/HW/homework/HW/dirent-master/tests/t-compile.c new file mode 100644 index 00000000..8eddb8e9 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-compile.c @@ -0,0 +1,45 @@ +/* + * Test program to make sure that dirent compiles cleanly with winsock. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#ifdef WIN32 +# include +# include +#endif +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct dirent *dirp = NULL; + + (void) argc; + (void) argv; + +#ifdef _DIRENT_HAVE_D_TYPE + printf("Has d_type\n"); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + printf("Has d_namlen\n"); +#endif +#ifdef _D_EXACT_NAMLEN + printf("Has _D_EXACT_NAMLEN\n"); +#endif +#ifdef _D_ALLOC_NAMLEN + printf("Has _D_ALLOC_NAMLEN\n"); +#endif +#ifdef _D_ALLOC_NAMLEN + printf("Has _D_ALLOC_NAMLEN\n"); +#endif + printf("Length of d_name with terminator: %d\n", + (int) sizeof(dirp->d_name)); + + printf("OK\n"); + return EXIT_SUCCESS; +} diff --git a/HW/homework/HW/dirent-master/tests/t-cplusplus.cpp b/HW/homework/HW/dirent-master/tests/t-cplusplus.cpp new file mode 100644 index 00000000..0f80a8c6 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-cplusplus.cpp @@ -0,0 +1,151 @@ +/* + * Test program to make sure that dirent compiles cleanly with C++ + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#include +using namespace std; + +/* Filter and sort functions */ +static int only_readme(const struct dirent *entry); +static void test_retrieval(void); +static void test_scan(void); + +int main(int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + test_retrieval(); + test_scan(); + + cout << "OK" << endl; + return EXIT_SUCCESS; +} + +/* Test basic directory retrieval */ +static void test_retrieval(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/1"); + if (dir == NULL) { + cerr << "Directory tests/1 not found" << endl; + abort(); + } + + /* Read directory entries */ + struct dirent *ent; + int found = 0; + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 1); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 1); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 1); +#endif + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 2); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 2); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 2); +#endif + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 4); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 4); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 4); +#endif + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 3); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 3); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 3); +#endif + found += 8; + } else { + /* Other file */ + cerr << "Unexpected file " << ent->d_name << endl; + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + closedir(dir); +} + +/* Text basic scan with simple filter function */ +static void test_scan(void) +{ + struct dirent **files; + + /* Read directory entries */ + int n = scandir("tests/3", &files, only_readme, alphasort); + assert(n == 1); + + /* Make sure that the filter works */ + assert(strcmp(files[0]->d_name, "README.txt") == 0); + + /* Release file names */ + for (int i = 0; i < n; i++) { + free(files[i]); + } + free(files); +} + +/* Only pass README.txt file */ +static int only_readme(const struct dirent *entry) +{ + int pass; + + if (strcmp (entry->d_name, "README.txt") == 0) { + pass = 1; + } else { + pass = 0; + } + + return pass; +} diff --git a/HW/homework/HW/dirent-master/tests/t-dirent.c b/HW/homework/HW/dirent-master/tests/t-dirent.c new file mode 100644 index 00000000..9b2118d6 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-dirent.c @@ -0,0 +1,597 @@ +/* + * A test program to make sure that dirent works correctly. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ +#include +#include +#include +#ifdef _MSC_VER +# include +# define chdir(x) _chdir(x) +#else +# include +#endif +#include +#include +#include + +#undef NDEBUG +#include + +static void test_macros(void); +static void test_retrieval(void); +static void test_nonexistent(void); +static void test_isfile(void); +static void test_zero(void); +static void test_rewind(void); +static void test_chdir(void); +static void test_filename(void); +static void test_readdir(void); +static void test_wreaddir(void); + +int main(int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + /* Execute tests */ + test_macros(); + test_retrieval(); + test_nonexistent(); + test_isfile(); + test_zero(); + test_rewind(); + test_chdir(); + test_filename(); + test_readdir(); + test_wreaddir(); + + printf("OK\n"); + return EXIT_SUCCESS; +} + +/* Test file type macros */ +static void test_macros(void) +{ + assert(DTTOIF(DT_REG) == S_IFREG); + assert(DTTOIF(DT_DIR) == S_IFDIR); + assert(DTTOIF(DT_FIFO) == S_IFIFO); + assert(DTTOIF(DT_SOCK) == S_IFSOCK); + assert(DTTOIF(DT_CHR) == S_IFCHR); + assert(DTTOIF(DT_BLK) == S_IFBLK); + + assert(IFTODT(S_IFREG) == DT_REG); + assert(IFTODT(S_IFDIR) == DT_DIR); + assert(IFTODT(S_IFIFO) == DT_FIFO); + assert(IFTODT(S_IFSOCK) == DT_SOCK); + assert(IFTODT(S_IFCHR) == DT_CHR); + assert(IFTODT(S_IFBLK) == DT_BLK); +} + +/* Test basic directory retrieval */ +static void test_retrieval(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/1"); + if (dir == NULL) { + fprintf(stderr, "Directory tests/1 not found\n"); + abort(); + } + + /* Read entries */ + struct dirent *ent; + int found = 0; + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 1); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 1); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 1); +#endif + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 2); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 2); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 2); +#endif + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 4); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 4); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 4); +#endif + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 3); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 3); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 3); +#endif + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + closedir(dir); +} + +/* Function opendir() fails if directory doesn't exist */ +static void test_nonexistent(void) +{ + DIR *dir = opendir("tests/invalid"); + assert(dir == NULL); + assert(errno == ENOENT); +} + +/* Function opendir() fails if pathname is really a file */ +static void test_isfile(void) +{ + DIR *dir = opendir("tests/1/file"); + assert(dir == NULL); + assert(errno == ENOTDIR); +} + +/* Function opendir() fails if pathname is a zero-length string */ +static void test_zero(void) +{ + DIR *dir = opendir(""); + assert(dir == NULL); + assert(errno == ENOENT); +} + +/* Test rewind of directory stream */ +static void test_rewind(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/1"); + assert(dir != NULL); + + /* Read entries */ + int found = 0; + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + /* Rewind stream and read entries again */ + rewinddir(dir); + found = 0; + + /* Read entries */ + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + closedir(dir); +} + +/* Test rewind with intervening change of working directory */ +static void test_chdir(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/1"); + assert(dir != NULL); + + /* Read entries */ + struct dirent *ent; + int found = 0; + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + /* Change working directory */ + int errorcode = chdir("tests"); + assert(errorcode == 0); + + /* Rewind stream and read entries again */ + rewinddir(dir); + found = 0; + + /* Read entries */ + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ + found += 2; + } else if (strcmp(ent->d_name, "file") == 0) { + /* Regular file */ + found += 4; + } else if (strcmp(ent->d_name, "dir") == 0) { + /* Just a directory */ + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + /* Restore working directory */ + errorcode = chdir(".."); + assert(errorcode == 0); + + closedir(dir); +} + +/* Test long file name */ +static void test_filename(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/2"); + if (dir == NULL) { + fprintf(stderr, "Directory tests/2 not found\n"); + abort(); + } + + /* Read entries */ + struct dirent *ent; + int found = 0; + while ((ent = readdir(dir)) != NULL) { + /* Check each file */ + if (strcmp(ent->d_name, ".") == 0) { + /* Directory itself */ + found += 1; + } else if (strcmp(ent->d_name, "..") == 0) { + /* Parent directory */ + found += 2; + } else if (strcmp(ent->d_name, "file.txt") == 0) { + /* Regular 8+3 filename */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 8); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 8); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 8); +#endif + found += 4; + } else if (strcmp(ent->d_name, "Testfile-1.2.3.dat") == 0) { + /* Long file name with multiple dots */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(ent->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(ent->d_namlen == 18); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(ent) == 18); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(ent) > 18); +#endif + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", ent->d_name); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + closedir(dir); +} + +/* Test basic directory retrieval with readdir_r */ +static void test_readdir(void) +{ + /* Open directory */ + DIR *dir = opendir("tests/1"); + if (dir == NULL) { + fprintf(stderr, "Directory tests/1 not found\n"); + abort(); + } + + /* Read entries to table */ + struct dirent ent[10]; + struct dirent *entry; + size_t i = 0; + size_t n = 0; + while (readdir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != 0) { + n++; + assert(n <= 4); + } + + /* Make sure that we got all the files from directory */ + assert(n == 4); + + /* Check entries in memory */ + int found = 0; + for (i = 0; i < 4; i++) { + entry = &ent[i]; + + /* Check each file */ + if (strcmp(entry->d_name, ".") == 0) { + /* Directory itself */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 1); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 1); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 1); +#endif + found += 1; + } else if (strcmp(entry->d_name, "..") == 0) { + /* Parent directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 2); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 2); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 2); +#endif + found += 2; + } else if (strcmp(entry->d_name, "file") == 0) { + /* Regular file */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 4); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 4); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 4); +#endif + found += 4; + } else if (strcmp(entry->d_name, "dir") == 0) { + /* Just a directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 3); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 3); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 3); +#endif + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file %s\n", entry->d_name); + abort(); + } + + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + closedir(dir); +} + +/* Basic directory retrieval with _wreaddir_r */ +static void test_wreaddir(void) +{ +#ifdef WIN32 + /* Open directory */ + _WDIR *dir = _wopendir(L"tests/1"); + if (dir == NULL) { + fprintf(stderr, "Directory tests/1 not found\n"); + abort(); + } + + /* Read entries to table */ + struct _wdirent ent[10]; + struct _wdirent *entry; + size_t i = 0; + size_t n = 0; + while (_wreaddir_r(dir, &ent[n], &entry) == /*OK*/0 && entry != 0) { + n++; + assert(n <= 4); + } + + /* Make sure that we got all the files from directory */ + assert(n == 4); + + /* Check entries in memory */ + int found = 0; + for (i = 0; i < 4; i++) { + entry = &ent[i]; + + /* Check each file */ + if (wcscmp(entry->d_name, L".") == 0) { + /* Directory itself */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 1); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 1); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 1); +#endif + found += 1; + } else if (wcscmp(entry->d_name, L"..") == 0) { + /* Parent directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 2); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 2); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 2); +#endif + found += 2; + } else if (wcscmp(entry->d_name, L"file") == 0) { + /* Regular file */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_REG); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 4); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 4); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 4); +#endif + found += 4; + } else if (wcscmp(entry->d_name, L"dir") == 0) { + /* Just a directory */ +#ifdef _DIRENT_HAVE_D_TYPE + assert(entry->d_type == DT_DIR); +#endif +#ifdef _DIRENT_HAVE_D_NAMLEN + assert(entry->d_namlen == 3); +#endif +#ifdef _D_EXACT_NAMLEN + assert(_D_EXACT_NAMLEN(entry) == 3); +#endif +#ifdef _D_ALLOC_NAMLEN + assert(_D_ALLOC_NAMLEN(entry) > 3); +#endif + found += 8; + } else { + /* Other file */ + fprintf(stderr, "Unexpected file\n"); + abort(); + } + } + + /* Make sure that all files were found */ + assert(found == 0xf); + + _wclosedir(dir); +#endif +} diff --git a/HW/homework/HW/dirent-master/tests/t-scandir.c b/HW/homework/HW/dirent-master/tests/t-scandir.c new file mode 100644 index 00000000..5b52b8d7 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-scandir.c @@ -0,0 +1,290 @@ +/* + * Make sure that scandir function works OK. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ + +/* Silence warning about fopen being insecure (MS Visual Studio) */ +#define _CRT_SECURE_NO_WARNINGS + +/* Include prototype for versionsort (Linux) */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include + +#undef NDEBUG +#include + + +/* Filter and sort functions */ +static int only_readme(const struct dirent *entry); +static int no_directories(const struct dirent *entry); +static int reverse_alpha(const struct dirent **a, const struct dirent **b); + + +int main(int argc, char *argv[]) +{ + struct dirent **files; + int i; + int n; + + (void) argc; + (void) argv; + + /* Initialize random number generator */ + srand((unsigned) time(NULL)); + + /* Basic scan with simple filter function */ + { + /* Read directory entries */ + n = scandir("tests/3", &files, only_readme, alphasort); + assert(n == 1); + + /* Make sure that the filter works */ + assert(strcmp(files[0]->d_name, "README.txt") == 0); + + /* Release file names */ + for (i = 0; i < n; i++) { + free(files[i]); + } + free(files); + } + + /* Basic scan with default sorting function */ + { + /* Read directory entries in alphabetic order */ + n = scandir("tests/3", &files, NULL, alphasort); + assert(n == 13); + + /* Make sure that we got all the names in the proper order */ + assert(strcmp(files[0]->d_name, ".") == 0); + assert(strcmp(files[1]->d_name, "..") == 0); + assert(strcmp(files[2]->d_name, "3zero.dat") == 0); + assert(strcmp(files[3]->d_name, "666.dat") == 0); + assert(strcmp(files[4]->d_name, "Qwerty-my-aunt.dat") == 0); + assert(strcmp(files[5]->d_name, "README.txt") == 0); + assert(strcmp(files[6]->d_name, "aaa.dat") == 0); + assert(strcmp(files[7]->d_name, "dirent.dat") == 0); + assert(strcmp(files[8]->d_name, "empty.dat") == 0); + assert(strcmp(files[9]->d_name, "sane-1.12.0.dat") == 0); + assert(strcmp(files[10]->d_name, "sane-1.2.30.dat") == 0); + assert(strcmp(files[11]->d_name, "sane-1.2.4.dat") == 0); + assert(strcmp(files[12]->d_name, "zebra.dat") == 0); + + /* Release file names */ + for (i = 0; i < n; i++) { + free(files[i]); + } + free(files); + } + + /* Custom filter AND sort function */ + { + /* Read directory entries in alphabetic order */ + n = scandir("tests/3", &files, no_directories, reverse_alpha); + assert(n == 11); + + /* Make sure that we got file names in the reverse order */ + assert(strcmp(files[0]->d_name, "zebra.dat") == 0); + assert(strcmp(files[1]->d_name, "sane-1.2.4.dat") == 0); + assert(strcmp(files[2]->d_name, "sane-1.2.30.dat") == 0); + assert(strcmp(files[3]->d_name, "sane-1.12.0.dat") == 0); + assert(strcmp(files[4]->d_name, "empty.dat") == 0); + assert(strcmp(files[5]->d_name, "dirent.dat") == 0); + assert(strcmp(files[6]->d_name, "aaa.dat") == 0); + assert(strcmp(files[7]->d_name, "README.txt") == 0); + assert(strcmp(files[8]->d_name, "Qwerty-my-aunt.dat") == 0); + assert(strcmp(files[9]->d_name, "666.dat") == 0); + assert(strcmp(files[10]->d_name, "3zero.dat") == 0); + + /* Release file names */ + for (i = 0; i < n; i++) { + free(files[i]); + } + free(files); + } + + /* Trying to read from non-existent directory leads to an error */ + { + files = NULL; + n = scandir("tests/invalid", &files, NULL, alphasort); + assert(n == -1); + assert(files == NULL); + assert(errno == ENOENT); + } + + /* Trying to open file as a directory produces ENOTDIR error */ + { + files = NULL; + n = scandir("tests/3/666.dat", &files, NULL, alphasort); + assert(n == -1); + assert(files == NULL); + assert(errno == ENOTDIR); + } + + /* Sort files using versionsort() */ + { + files = NULL; + n = scandir("tests/3", &files, no_directories, versionsort); + assert(n == 11); + + /* + * Make sure that we got all the file names in the proper order: + * 1.2.4 < 1.2.30 < 1.12.0 + */ + assert(strcmp(files[0]->d_name, "3zero.dat") == 0); + assert(strcmp(files[1]->d_name, "666.dat") == 0); + assert(strcmp(files[2]->d_name, "Qwerty-my-aunt.dat") == 0); + assert(strcmp(files[3]->d_name, "README.txt") == 0); + assert(strcmp(files[4]->d_name, "aaa.dat") == 0); + assert(strcmp(files[5]->d_name, "dirent.dat") == 0); + assert(strcmp(files[6]->d_name, "empty.dat") == 0); + assert(strcmp(files[7]->d_name, "sane-1.2.4.dat") == 0); + assert(strcmp(files[8]->d_name, "sane-1.2.30.dat") == 0); + assert(strcmp(files[9]->d_name, "sane-1.12.0.dat") == 0); + assert(strcmp(files[10]->d_name, "zebra.dat") == 0); + + /* Release file names */ + for (i = 0; i < n; i++) { + free(files[i]); + } + free(files); + } + + /* Scan large directory */ + { + char dirname[PATH_MAX+1]; + int i; + int ok; + + /* Copy name of temporary directory to variable dirname */ +#ifdef WIN32 + i = GetTempPathA(PATH_MAX, dirname); + assert(i > 0); +#else + strcpy(dirname, "/tmp/"); + i = strlen(dirname); +#endif + + /* Append random characters to dirname */ + for (int j = 0; j < 10; j++) { + char c; + + /* Generate random character */ + c = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; + + /* Append character to dirname */ + assert(i < PATH_MAX); + dirname[i++] = c; + } + + /* Terminate directory name */ + assert(i < PATH_MAX); + dirname[i] = '\0'; + + /* Create directory */ +#ifdef WIN32 + ok = CreateDirectoryA(dirname, NULL); + assert(ok); +#else + ok = mkdir(dirname, 0700); + assert(ok == /*success*/0); +#endif + + /* Create one thousand files */ + assert(i + 5 < PATH_MAX); + for (int j = 0; j < 1000; j++) { + FILE *fp; + + /* Construct file name */ + dirname[i] = '/'; + dirname[i+1] = 'z'; + dirname[i+2] = '0' + ((j / 100) % 10); + dirname[i+3] = '0' + ((j / 10) % 10); + dirname[i+4] = '0' + (j % 10); + dirname[i+5] = '\0'; + + /* Create file */ + fp = fopen(dirname, "w"); + assert(fp != NULL); + fclose(fp); + + } + + /* Cut out the file name part */ + dirname[i] = '\0'; + + /* Scan directory */ + n = scandir(dirname, &files, no_directories, alphasort); + assert(n == 1000); + + /* Make sure that all 1000 files are read back */ + for (int j = 0; j < n; j++) { + char match[100]; + + /* Construct file name */ + match[0] = 'z'; + match[1] = '0' + ((j / 100) % 10); + match[2] = '0' + ((j / 10) % 10); + match[3] = '0' + (j % 10); + match[4] = '\0'; + + /* Make sure that file name matches that on the disk */ + assert(strcmp(files[j]->d_name, match) == 0); + + } + + /* Release file names */ + for (int j = 0; j < n; j++) { + free(files[j]); + } + free(files); + } + + printf("OK\n"); + return EXIT_SUCCESS; +} + +/* Only pass README.txt file */ +static int only_readme(const struct dirent *entry) +{ + int pass; + + if (strcmp(entry->d_name, "README.txt") == 0) { + pass = 1; + } else { + pass = 0; + } + + return pass; +} + +/* Filter out directories */ +static int no_directories(const struct dirent *entry) +{ + int pass; + + if (entry->d_type != DT_DIR) { + pass = 1; + } else { + pass = 0; + } + + return pass; +} + +/* Sort in reverse direction */ +static int reverse_alpha(const struct dirent **a, const struct dirent **b) +{ + return strcoll((*b)->d_name, (*a)->d_name); +} diff --git a/HW/homework/HW/dirent-master/tests/t-strverscmp.c b/HW/homework/HW/dirent-master/tests/t-strverscmp.c new file mode 100644 index 00000000..26e905bf --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-strverscmp.c @@ -0,0 +1,206 @@ +/* + * Test program to make sure that strverscmp works correctly + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ + +/* Include prototype for strverscmp */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +int +main( + int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + /* Strings without digits are compared as in strcmp() */ + assert(strverscmp("", "") == 0); + assert(strverscmp("abc", "abc") == 0); + assert(strverscmp("a", "b") < 0); + assert(strverscmp("b", "a") > 0); + + /* Shorter string is smaller, other things being equal */ + assert(strverscmp("a", "aa") < 0); + assert(strverscmp("aa", "a") > 0); + assert(strverscmp("abcdef", "abcdefg") < 0); + assert(strverscmp("abcdefg", "abcdef") > 0); + + /* Integers with equal length are compared as in strcmp() */ + assert(strverscmp("0", "0") == 0); + assert(strverscmp("000", "000") == 0); + assert(strverscmp("1", "2") < 0); + assert(strverscmp("2", "1") > 0); + assert(strverscmp("001", "100") < 0); + assert(strverscmp("100", "001") > 0); + assert(strverscmp("2020-07-01", "2020-07-02") < 0); + assert(strverscmp("2020-07-02", "2020-07-01") > 0); + assert(strverscmp("jan999", "jan999") == 0); + + /* Integers of different length are compared as numbers */ + assert(strverscmp("jan9", "jan10") < 0); + assert(strverscmp("jan10", "jan9") > 0); + assert(strverscmp("999", "1000") < 0); + assert(strverscmp("1000", "999") > 0); + assert(strverscmp("t12-1000", "t12-9999") < 0); + assert(strverscmp("t12-9999", "t12-1000") > 0); + assert(strverscmp("1000", "10001") < 0); + assert(strverscmp("10001", "1000") > 0); + assert(strverscmp("1000!", "10001") < 0); + assert(strverscmp("10001", "1000!") > 0); + assert(strverscmp("1000Z", "10001") < 0); + assert(strverscmp("10001", "1000Z") > 0); + + /* If numbers starts with zero, then longer number is smaller */ + assert(strverscmp("00", "0") < 0); + assert(strverscmp("0", "00") > 0); + assert(strverscmp("a000", "a00") < 0); + assert(strverscmp("a00", "a000") > 0); + assert(strverscmp("0000", "000") < 0); + assert(strverscmp("000", "0000") > 0); + assert(strverscmp("0000", "000!") < 0); + assert(strverscmp("000!", "0000") > 0); + assert(strverscmp("0000", "000Z") < 0); + assert(strverscmp("000Z", "0000") > 0); + assert(strverscmp("0000", "000Z") < 0); + assert(strverscmp("000Z", "0000") > 0); + assert(strverscmp("1.01", "1.0") < 0); + assert(strverscmp("1.0", "1.01") > 0); + assert(strverscmp("1.01", "1.0!") < 0); + assert(strverscmp("1.0!", "1.01") > 0); + assert(strverscmp("1.01", "1.0~") < 0); + assert(strverscmp("1.0~", "1.01") > 0); + + /* Number having more leading zeros is considered smaller */ + assert(strverscmp("item-0001", "item-001") < 0); + assert(strverscmp("item-001", "item-0001") > 0); + assert(strverscmp("item-001", "item-01") < 0); + assert(strverscmp("item-01", "item-001") > 0); + assert(strverscmp(".0001000", ".001") < 0); + assert(strverscmp(".001", ".0001000") > 0); + assert(strverscmp(".0001000", ".01") < 0); + assert(strverscmp(".01", ".0001000") > 0); + assert(strverscmp(".0001000", ".1") < 0); + assert(strverscmp(".1", ".0001000") > 0); + assert(strverscmp("1.0002", "1.0010000") < 0); + assert(strverscmp("1.0010000", "1.0002") > 0); + + /* Number starting with zero is smaller than any number */ + assert(strverscmp("item-009", "item-1") < 0); + assert(strverscmp("item-1", "item-009") > 0); + assert(strverscmp("item-099", "item-2") < 0); + assert(strverscmp("item-2", "item-099") > 0); + + /* Number vs alphabetical comparison */ + assert(strverscmp("1.001", "1.00!") < 0); + assert(strverscmp("1.00!", "1.001") > 0); + assert(strverscmp("1.001", "1.00x") < 0); + assert(strverscmp("1.00x", "1.001") > 0); + assert(strverscmp("1", "x") < 0); + assert(strverscmp("x", "1") > 0); + assert(strverscmp("1", "!") > 0); + assert(strverscmp("!", "1") < 0); + + /* Handling the end of string */ + assert(strverscmp("01", "011") < 0); + assert(strverscmp("011", "01") > 0); + assert(strverscmp("0100", "01000") < 0); + assert(strverscmp("01000", "0100") > 0); + assert(strverscmp("1", "1!") < 0); + assert(strverscmp("1!", "1") > 0); + assert(strverscmp("1", "1z") < 0); + assert(strverscmp("1z", "1") > 0); + + /* Ordering 000 < 00 < 01 < 010 < 09 < 0 < 1 < 9 < 10 */ + assert(strverscmp("000", "00") < 0); + assert(strverscmp("000", "01") < 0); + assert(strverscmp("000", "010") < 0); + assert(strverscmp("000", "09") < 0); + assert(strverscmp("000", "0") < 0); + assert(strverscmp("000", "1") < 0); + assert(strverscmp("000", "9") < 0); + assert(strverscmp("000", "10") < 0); + + assert(strverscmp("00", "01") < 0); + assert(strverscmp("00", "010") < 0); + assert(strverscmp("00", "09") < 0); + assert(strverscmp("00", "0") < 0); + assert(strverscmp("00", "1") < 0); + assert(strverscmp("00", "9") < 0); + assert(strverscmp("00", "10") < 0); + + assert(strverscmp("01", "010") < 0); + assert(strverscmp("01", "09") < 0); + assert(strverscmp("01", "0") < 0); + assert(strverscmp("01", "1") < 0); + assert(strverscmp("01", "9") < 0); + assert(strverscmp("01", "10") < 0); + + assert(strverscmp("010", "09") < 0); + assert(strverscmp("010", "0") < 0); + assert(strverscmp("010", "1") < 0); + assert(strverscmp("010", "9") < 0); + assert(strverscmp("010", "10") < 0); + + assert(strverscmp("09", "0") < 0); + assert(strverscmp("09", "1") < 0); + assert(strverscmp("09", "9") < 0); + assert(strverscmp("09", "10") < 0); + + assert(strverscmp("0", "1") < 0); + assert(strverscmp("0", "9") < 0); + assert(strverscmp("0", "10") < 0); + + assert(strverscmp("1", "9") < 0); + assert(strverscmp("1", "10") < 0); + + assert(strverscmp("9", "10") < 0); + + /* Compare speed */ + { +#define LENGTH 100 +#define REPEAT 1000000 + char a[LENGTH+1]; + char b[LENGTH+1]; + size_t i; + size_t j; + char letters[] = "01234567890123456789abdefghjkpqrtwxyz-/."; + size_t n = strlen(letters); + + /* Repeat test */ + for(i = 0; i < REPEAT; i++) { + int diff1; + int diff2; + + /* Generate two random strings of LENGTH characters */ + for(j = 0; j < LENGTH; j++) { + a[j] = letters[rand() % n]; + b[j] = letters[rand() % n]; + } + a[j] = '\0'; + b[j] = '\0'; + + /* Compare strings in both directions */ + diff1 = strverscmp(a, b); + diff2 = strverscmp(b, a); + + /* Must give identical result in both directions */ + assert((diff1 < 0 && diff2 > 0) + || (diff1 == 0 && diff2 == 0) + || (diff1 > 0 && diff2 < 0)); + } + } + + printf("OK\n"); + return EXIT_SUCCESS; +} diff --git a/HW/homework/HW/dirent-master/tests/t-unicode.c b/HW/homework/HW/dirent-master/tests/t-unicode.c new file mode 100644 index 00000000..76cdca50 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-unicode.c @@ -0,0 +1,385 @@ +/* + * Test program to try unicode file names. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ + +/* Silence warning about fopen being insecure */ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include +#include +#include + +#undef NDEBUG +#include + +int main(int argc, char *argv[]) +{ +#ifdef WIN32 + wchar_t wpath[MAX_PATH+1]; + char path[MAX_PATH+1]; + DWORD i, j, k, x; + BOOL ok; + HANDLE fh; + _WDIR *wdir; + struct _wdirent *wentry; + DIR *dir; + struct dirent *entry; + char buffer[100]; + FILE *fp; + int counter = 0; + + + /* Initialize random number generator */ + srand(((int) time(NULL)) * 257 + ((int) GetCurrentProcessId())); + + /* Set current locale */ + if (argc > 1) { + printf("Locale %s\n", argv[1]); + setlocale(LC_ALL, argv[1]); + } else { + setlocale(LC_ALL, ""); + } + + + /****** CREATE FILE WITH UNICODE FILE NAME ******/ + + /* Get path to temporary directory (wide-character and ascii) */ + i = GetTempPathW(MAX_PATH, wpath); + assert(i > 0); + j = GetTempPathA(MAX_PATH, path); + assert(j > 0); + + /* Append random directory name */ + for (k = 0; k < 10; k++) { + /* Generate random character */ + char c = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; + + /* Append character to paths */ + assert(i < MAX_PATH && j < MAX_PATH); + wpath[i++] = c; + path[j++] = c; + } + + /* Terminate paths */ + assert(i < MAX_PATH && j < MAX_PATH); + wpath[i] = '\0'; + path[j] = '\0'; + + /* Remember the end of directory name */ + k = i; + + /* Create directory using unicode */ + ok = CreateDirectoryW(wpath, NULL); + if (!ok) { + DWORD e = GetLastError(); + wprintf(L"Cannot create directory %ls (code %u)\n", wpath, e); + abort(); + } + + /* Overwrite zero terminator with path separator */ + assert(i < MAX_PATH && j < MAX_PATH); + wpath[i++] = '\\'; + + /* Append a few unicode characters */ + assert(i < MAX_PATH); + wpath[i++] = 0x6d4b; + assert(i < MAX_PATH); + wpath[i++] = 0x8bd5; + + /* Terminate string */ + assert(i < MAX_PATH); + wpath[i] = '\0'; + + /* Create file with unicode */ + fh = CreateFileW( + wpath, + /* Access */ GENERIC_READ | GENERIC_WRITE, + /* Share mode */ 0, + /* Security attributes */ NULL, + /* Creation disposition */ CREATE_NEW, + /* Attributes */ FILE_ATTRIBUTE_NORMAL, + /* Template files */ NULL + ); + assert(fh != INVALID_HANDLE_VALUE); + + /* Write some data to file */ + ok = WriteFile( + /* File handle */ fh, + /* Pointer to data */ "hep\n", + /* Number of bytes to write */ 4, + /* Number of bytes written */ NULL, + /* Overlapped */ NULL + ); + assert(ok); + + /* Close file */ + ok = CloseHandle(fh); + assert(ok); + + + /****** MAKE SURE THAT UNICODE FILE CAN BE READ BY _WREADDIR ******/ + + /* Zero terminate wide-character path and open directory stream */ + wpath[k] = '\0'; + wdir = _wopendir(wpath); + if (wdir == NULL) { + wprintf(L"Cannot open directory %ls\n", wpath); + abort(); + } + + /* Read through entries */ + counter = 0; + while ((wentry = _wreaddir(wdir)) != NULL) { + /* Skip pseudo directories */ + if (wcscmp(wentry->d_name, L".") == 0) { + continue; + } + if (wcscmp(wentry->d_name, L"..") == 0) { + continue; + } + + /* Found a file */ + counter++; + assert(wentry->d_type == DT_REG); + + /* Append file name to path */ + i = k; + assert(i < MAX_PATH); + wpath[i++] = '\\'; + x = 0; + while (wentry->d_name[x] != '\0') { + assert(i < MAX_PATH); + wpath[i++] = wentry->d_name[x++]; + } + assert(i < MAX_PATH); + wpath[i] = '\0'; + + /* Open file for read */ + fh = CreateFileW( + wpath, + /* Access */ GENERIC_READ, + /* Share mode */ 0, + /* Security attributes */ NULL, + /* Creation disposition */ OPEN_EXISTING, + /* Attributes */ FILE_ATTRIBUTE_NORMAL, + /* Template files */ NULL + ); + assert(fh != INVALID_HANDLE_VALUE); + + /* Read data from file */ + ok = ReadFile( + /* File handle */ fh, + /* Output buffer */ buffer, + /* Max number of bytes to read */ sizeof(buffer) - 1, + /* Number of bytes actually read */ &x, + /* Overlapped */ NULL + ); + assert(ok); + + /* Make sure that we got the file contents right */ + assert(x == 4); + assert(buffer[0] == 'h'); + assert(buffer[1] == 'e'); + assert(buffer[2] == 'p'); + assert(buffer[3] == '\n'); + + /* Close file */ + ok = CloseHandle(fh); + assert(ok); + } + assert(counter == 1); + + /* Close directory */ + _wclosedir(wdir); + + + /****** MAKE SURE THAT UNICODE FILE NAME CAN BE READ BY READDIR *****/ + + /* Zero terminate ascii path and open directory stream */ + k = j; + path[k] = '\0'; + dir = opendir(path); + if (dir == NULL) { + fprintf(stderr, "Cannot open directory %s\n", path); + abort(); + } + + /* Read through entries */ + counter = 0; + while ((entry = readdir(dir)) != NULL) { + /* Skip pseudo directories */ + if (strcmp(entry->d_name, ".") == 0) { + continue; + } + if (strcmp(entry->d_name, "..") == 0) { + continue; + } + + /* Found a file */ + counter++; + assert(entry->d_type == DT_REG); + + /* Append file name to path */ + j = k; + assert(j < MAX_PATH); + path[j++] = '\\'; + x = 0; + while (entry->d_name[x] != '\0') { + assert(j < MAX_PATH); + path[j++] = entry->d_name[x++]; + } + assert(j < MAX_PATH); + path[j] = '\0'; + + /* Open file for read */ + fp = fopen(path, "r"); + if (!fp) { + fprintf(stderr, "Cannot open file %s\n", path); + abort(); + } + + /* Read data from file */ + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + fprintf(stderr, "Cannot read file %s\n", path); + abort(); + } + + /* Make sure that we got the file contents right */ + assert(buffer[0] == 'h'); + assert(buffer[1] == 'e'); + assert(buffer[2] == 'p'); + assert(buffer[3] == '\n'); + assert(buffer[4] == '\0'); + + /* Close file */ + fclose(fp); + } + assert(counter == 1); + + /* Close directory */ + closedir(dir); + + + /****** CREATE FILE WITH UTF-8 ******/ + + /* Append UTF-8 file name (åäö.txt) to path */ + j = k; + path[j++] = '\\'; + path[j++] = 0xc3; + path[j++] = 0xa5; + path[j++] = 0xc3; + path[j++] = 0xa4; + path[j++] = 0xc3; + path[j++] = 0xb6; + path[j++] = 0x2e; + path[j++] = 0x74; + path[j++] = 0x78; + path[j++] = 0x74; + assert(j < MAX_PATH); + path[j] = '\0'; + + /* + * Create file. + * + * Be ware that the code below creates a different file depending on + * the current locale! For example, if the current locale is + * english_us.65001, then the file name will be "åäö.txt" (7 + * characters). However, if the current locale is english_us.1252, + * then the file name will be "ÃċÃĊö.txt" (10 characters). + */ + printf("Creating %s\n", path); + fp = fopen(path, "w"); + if (!fp) { + fprintf(stderr, "Cannot open file %s\n", path); + abort(); + } + fputs("hep\n", fp); + fclose(fp); + + /* Open directory again */ + path[k] = '\0'; + dir = opendir(path); + if (dir == NULL) { + fprintf(stderr, "Cannot open directory %s\n", path); + abort(); + } + + /* Read through entries */ + counter = 0; + while ((entry = readdir(dir)) != NULL) { + /* Skip pseudo directories */ + if (strcmp(entry->d_name, ".") == 0) { + continue; + } + if (strcmp(entry->d_name, "..") == 0) { + continue; + } + + /* Found a file */ + counter++; + assert(entry->d_type == DT_REG); + + /* Append file name to path */ + j = k; + assert(j < MAX_PATH); + path[j++] = '\\'; + x = 0; + while (entry->d_name[x] != '\0') { + assert(j < MAX_PATH); + path[j++] = entry->d_name[x++]; + } + assert(j < MAX_PATH); + path[j] = '\0'; + + /* Print file name for debugging */ + printf("Opening \"%s\" hex ", path + k + 1); + x = 0; + while (entry->d_name[x] != '\0') { + printf("0x%02x ", + (unsigned) (entry->d_name[x++] & 0xff)); + } + printf("\n"); + + /* Open file for read */ + fp = fopen(path, "r"); + if (!fp) { + fprintf(stderr, "Cannot open file %s\n", path); + abort(); + } + + /* Read data from file */ + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + fprintf(stderr, "Cannot read file %s\n", path); + abort(); + } + + /* Make sure that we got the file contents right */ + assert(buffer[0] == 'h'); + assert(buffer[1] == 'e'); + assert(buffer[2] == 'p'); + assert(buffer[3] == '\n'); + assert(buffer[4] == '\0'); + + /* Close file */ + fclose(fp); + } + assert(counter == 2); + + /* Close directory */ + closedir(dir); +#else + /* Linux */ + (void) argc; + (void) argv; +#endif + return EXIT_SUCCESS; +} diff --git a/HW/homework/HW/dirent-master/tests/t-utf8.c b/HW/homework/HW/dirent-master/tests/t-utf8.c new file mode 100644 index 00000000..90abbbe0 --- /dev/null +++ b/HW/homework/HW/dirent-master/tests/t-utf8.c @@ -0,0 +1,237 @@ +/* + * Test program to try UTF-8 file names. + * + * Copyright (C) 1998-2019 Toni Ronkko + * This file is part of dirent. Dirent may be freely distributed + * under the MIT license. For all details and documentation, see + * https://github.com/tronkko/dirent + */ + +/* Silence warning about fopen being insecure */ +#define _CRT_SECURE_NO_WARNINGS + +#include +#include +#include +#include +#include +#include +#include + +#undef NDEBUG +#include + +int main(int argc, char *argv[]) +{ +#ifdef WIN32 + /* + * Select UTF-8 locale. This will change the way how C runtime + * functions such as fopen() and mkdir() handle character strings. + * For more information, please see: + * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160#utf-8-support + */ + setlocale(LC_ALL, "LC_CTYPE=.utf8"); + + /* Initialize random number generator */ + srand(((int) time(NULL)) * 257 + ((int) GetCurrentProcessId())); + + /* Get path to temporary directory */ + wchar_t wpath[MAX_PATH+1]; + DWORD i = GetTempPathW(MAX_PATH, wpath); + assert(i > 0); + + /* Ensure that path name ends in directory separator */ + assert(wpath[i - 1] == '\\'); + + /* Append random prefix */ + DWORD k; + for (k = 0; k < 8; k++) { + /* Generate random character */ + char c = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; + + /* Append character to path */ + assert(i < MAX_PATH); + wpath[i++] = c; + } + + /* Append a wide character to the path name */ + wpath[i++] = 0x00c4; + + /* Terminate the path name */ + assert(i < MAX_PATH); + wpath[i] = '\0'; + + /* Create directory with unicode name */ + BOOL ok = CreateDirectoryW(wpath, NULL); + if (!ok) { + DWORD e = GetLastError(); + wprintf(L"Cannot create directory %ls (code %u)\n", wpath, e); + abort(); + } + + /* Overwrite zero terminator with path separator */ + assert(i < MAX_PATH); + wpath[i++] = '\\'; + + /* Append a few unicode characters */ + assert(i < MAX_PATH); + wpath[i++] = 0x00f6; + assert(i < MAX_PATH); + wpath[i++] = 0x00e4; + + /* Terminate string */ + assert(i < MAX_PATH); + wpath[i] = '\0'; + + /* Create file with unicode name */ + HANDLE fh = CreateFileW( + wpath, + /* Access */ GENERIC_READ | GENERIC_WRITE, + /* Share mode */ 0, + /* Security attributes */ NULL, + /* Creation disposition */ CREATE_NEW, + /* Attributes */ FILE_ATTRIBUTE_NORMAL, + /* Template files */ NULL + ); + assert(fh != INVALID_HANDLE_VALUE); + + /* Write some data to file */ + ok = WriteFile( + /* File handle */ fh, + /* Pointer to data */ "hep\n", + /* Number of bytes to write */ 4, + /* Number of bytes written */ NULL, + /* Overlapped */ NULL + ); + assert(ok); + + /* Close file */ + ok = CloseHandle(fh); + assert(ok); + + /* Convert file name to UTF-8 */ + char path[MAX_PATH+1]; + int n = WideCharToMultiByte( + /* Code page to use in conversion */ CP_UTF8, + /* Flags */ 0, + /* Pointer to unicode string */ wpath, + /* Length of unicode string in characters */ i, + /* Pointer to output buffer */ path, + /* Size of output buffer in bytes */ MAX_PATH, + /* Pointer to default character */ NULL, + /* Pointer to boolean variable */ NULL + ); + assert(n > 0); + + /* Zero-terminate path */ + path[(size_t) n] = '\0'; + + /* Make sure that fopen() can open the file with UTF-8 file name */ + FILE *fp = fopen(path, "r"); + if (!fp) { + fprintf(stderr, "Cannot open file %s\n", path); + abort(); + } + + /* Read data from file */ + char buffer[100]; + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + fprintf(stderr, "Cannot read file %s\n", path); + abort(); + } + + /* Make sure that we got the file contents right */ + assert(buffer[0] == 'h'); + assert(buffer[1] == 'e'); + assert(buffer[2] == 'p'); + assert(buffer[3] == '\n'); + assert(buffer[4] == '\0'); + + /* Close file */ + fclose(fp); + + /* Truncate path name to the last directory separator */ + i = 0; + k = 0; + while (path[k] != '\0') { + if (path[k] == '\\' || path[k] == '/') { + i = k; + } + k++; + } + path[i] = '\0'; + + /* Ensure that opendir() can open the directory with UTF-8 name */ + DIR *dir = opendir(path); + if (dir == NULL) { + fprintf(stderr, "Cannot open directory %s\n", path); + abort(); + } + + /* Read entries */ + int counter = 0; + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + /* Skip pseudo directories */ + if (strcmp(entry->d_name, ".") == 0) { + continue; + } + if (strcmp(entry->d_name, "..") == 0) { + continue; + } + + /* Found a file */ + counter++; + assert(entry->d_type == DT_REG); + + /* Append file name to path */ + k = i; + assert(i < MAX_PATH); + path[i++] = '\\'; + DWORD x = 0; + while (entry->d_name[x] != '\0') { + assert(i < MAX_PATH); + path[i++] = entry->d_name[x++]; + } + assert(i < MAX_PATH); + path[i] = '\0'; + + /* Reset buffer */ + for (x = 0; x < sizeof(buffer); x++) { + buffer[x] = '\0'; + } + + /* Open file for read */ + fp = fopen(path, "r"); + if (!fp) { + fprintf(stderr, "Cannot open file %s\n", path); + abort(); + } + + /* Read data from file */ + if (fgets(buffer, sizeof(buffer), fp) == NULL) { + fprintf(stderr, "Cannot read file %s\n", path); + abort(); + } + + /* Make sure that we got the file contents right */ + assert(buffer[0] == 'h'); + assert(buffer[1] == 'e'); + assert(buffer[2] == 'p'); + assert(buffer[3] == '\n'); + assert(buffer[4] == '\0'); + + /* Close file */ + fclose(fp); + } + assert(counter == 1); + + /* Close directory */ + closedir(dir); +#else + /* Linux */ + (void) argc; + (void) argv; +#endif + return EXIT_SUCCESS; +} diff --git a/HW/homework/HW/task0/task0.c b/HW/homework/HW/task0/task0.c index 01e17393..1225bab8 100644 --- a/HW/homework/HW/task0/task0.c +++ b/HW/homework/HW/task0/task0.c @@ -1,35 +1,34 @@ -#include -#include -#include +#include +#define _CRT_SECURE_NO_WARNINGS +#define SIZE 10 +int main() { + int array[SIZE]; + int count = 0; + int newArray[SIZE]; -int main() -{ - setlocale(LC_ALL, "russian"); + for (int i = 0; i < SIZE; i++) { + array[i] = -5 + rand() % (15 + 1); + } - printf(" :\n"); - int x, y, r; - scanf_s("%d %d %d", &x, &y, &r); + for (int i = 0; i < SIZE; i++) { + printf("nachal_massiv[%d] = %d\n", i, array[i]); + } - printf(" :\n"); - int x1, y1, r1; - scanf_s("%d %d %d", &x1, &y1, &r1); + for (int i = 1; i < SIZE - 1; i++) { + if ((array[i] > array[i - 1]) && (array[i] > array[i + 1])) { + newArray[count] = array[i]; + count++; + } + } - int d, diff; - diff = pow((x1-x), 2) + pow((y1-y), 2); - d = sqrt(diff); + printf("Local Mamximum = \n"); + for (int i = 0; i < count; i++) { + printf("massiv_max[%d] = %d\n", i + 1, newArray[i]); + } + return 0; +} - if ((r + r1 > d) && (r + d > r1) && (r1 + d > r)) - printf(" "); - else - if ((r + r1 == d) || (abs(r - r1) == d)) - printf(" "); - else - printf(" "); - return 0; - - -} \ No newline at end of file diff --git a/HW/homework/HW/task1(2s)/Source.h b/HW/homework/HW/task1(2s)/Source.h new file mode 100644 index 00000000..06d82353 --- /dev/null +++ b/HW/homework/HW/task1(2s)/Source.h @@ -0,0 +1,93 @@ +using namespace std; + +#include +#include + +#define RUB 100 + +class Money +{ +private: + int rub, coin; + +public: + Money() { rub == coin == 0; } + + Money(const char* str) + { + string a; + int n = 0, ob[2]; + for (int i = 0; i < (strlen(str) + 1); i++) + if (str[i] != '.' && i != strlen(str)) + a += str[i]; + else + { + ob[n] = atoi(a.c_str()); + + n++; + a = ""; + } + rub = ob[0]; + coin = ob[1]; + } + + void ShowMoney() + { + cout << rub << "." << coin << endl; + } + + void EditMoney() + { + this->rub = (this->rub + this->coin / RUB); + this->coin %= RUB; + } + + Money& operator += (const Money& money) + { + rub += money.rub; + coin += money.coin; + + return *this; + } + + void ConvMoney(const Money& money) + { + int randPr = 0, flag; + randPr = 85; + + + flag = rub; + rub = this->rub / randPr; + coin = (flag % randPr * RUB + money.coin) * RUB / (randPr * RUB); + + } + + Money& operator -= (const Money& money) + { + if ((rub - money.rub) < 0) + rub = 0; + else + rub = rub - money.rub; + if ((coin - money.coin) < 0) + { + rub = rub - (money.coin / RUB); + coin = money.coin % RUB; + } + + return *this; + } + + void MoneyProcent(const Money& money) + { + float randPr = 0; + randPr = rand() % 90 + 70; + + float AllCoin = 0; + + AllCoin = (money.rub * RUB + money.coin) * (randPr / RUB); + + this->rub = (int)AllCoin / RUB; + this->coin = (int)AllCoin % RUB; + } + +}; \ No newline at end of file diff --git a/HW/homework/HW/task1(2s)/main.cpp b/HW/homework/HW/task1(2s)/main.cpp new file mode 100644 index 00000000..b29ccac1 --- /dev/null +++ b/HW/homework/HW/task1(2s)/main.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +//#include +//#include +//#include + +using namespace std; + +int main() +{ + char str[80]; + int a; + + cout << "Enter Money if format: (Rub.Coin)"; + cin >> str; + + while (true) + { + cout << "Enter operation: " << endl << "1) 1 - minus" << endl << "2) 2 - plus" << endl << "3) 3 - multiplication by procent" << endl << "4) 4 - Convertation to $" << endl << "5) 5 - exit" << endl; + cin >> a; + if (a == 5) + break; + if (a == 2) + { + Money ob(str); + cout << "Enter Money in format: (Rub.Coin)"; + cin >> str; + Money ob2(str); + ob += ob2; + ob.EditMoney(); + ob.ShowMoney(); + } + if (a == 1) + { + Money ob(str); + cout << "Enter Money in format: (Rub.Coin)"; + cin >> str; + Money ob1(str); + ob -= ob1; + ob.EditMoney(); + ob.ShowMoney(); + } + if (a == 3) + { + cout << "Enter Money in format: (Rub.Coin)"; + cin >> str; + Money ob3(str); + ob3.EditMoney(); + ob3.MoneyProcent(ob3); + ob3.ShowMoney(); + } + if (a == 4) + { + Money ob(str); + cout << "Enter Money in format: (Rub.Coin)"; + cin >> str; + Money ob4(str); + ob.EditMoney(); + ob.ConvMoney(ob4); + ob.ShowMoney(); + } + } + + system("Pause"); +} \ No newline at end of file diff --git a/HW/homework/HW/task1(2s)/task1(2s).vcxproj b/HW/homework/HW/task1(2s)/task1(2s).vcxproj new file mode 100644 index 00000000..4e0ee460 --- /dev/null +++ b/HW/homework/HW/task1(2s)/task1(2s).vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {d4d982e9-5188-4a1e-ab9e-27caaeee3d69} + task12s + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/HW/homework/HW/task1(2s)/task1(2s).vcxproj.filters b/HW/homework/HW/task1(2s)/task1(2s).vcxproj.filters new file mode 100644 index 00000000..172c7d15 --- /dev/null +++ b/HW/homework/HW/task1(2s)/task1(2s).vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/HW/homework/HW/task1(2s)/task1(2s).vcxproj.user b/HW/homework/HW/task1(2s)/task1(2s).vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/task1(2s)/task1(2s).vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW/homework/HW/task1/task1.c b/HW/homework/HW/task1/task1.c index e789da3b..1abf4045 100644 --- a/HW/homework/HW/task1/task1.c +++ b/HW/homework/HW/task1/task1.c @@ -1,51 +1,26 @@ -#include -#include -#include -#define scale 1000000 -#define rast 40 +#include int main() { - setlocale(LC_ALL, "russian"); - int dverWood, polkaDSP, h, d, w, tolshina3 = 1; - float zadDSP, bokDSP, nizDSP, tolshina1 = 0.5, tolshina2 = 1.5; - int flag = 0; - do { - printf(" (:180-220; :80-120)( ):"); - scanf_s(" %d %d", &h, &w); - if ((h > 179 && h < 221) && (w > 79) & (w > 121)) - flag = 1; - else - printf(" \n"); - } while (flag != 1); - - do { - printf(" : "); - scanf_s("%d", &d); - if (d > 49 && d < 90) - flag = 1; - else - printf(" \n"); - } while (flag != 1); - - int dsp, dvp, wood; - printf(" "); - scanf_s("%d %d %d", &dsp, &dvp, &wood); - - zadDSP = (h * w * tolshina1) / scale; - bokDSP = (h * w * tolshina2) / scale; - nizDSP = (w * d * tolshina2) / scale; - dverWood = (h * w / 2 * tolshina3) / scale; - - float tolshina4; - - printf(" :"); - scanf_s(" %f", &tolshina4); - polkaDSP = (d * w * tolshina4)/scale; - float kolvopolok = h / rast; - float m; - m = (zadDSP * dvp) + ((bokDSP * 2) * dsp) + ((nizDSP * 2) * dsp) + ((dverWood * 2) * wood) + (floor(kolvopolok) * polkaDSP * wood); - printf(" -%.1f", m); - return 0; - + int i, n, * mas; + int k = 0; + setlocale(0, "rus"); + printf(" : "); + scanf_s("%d", &n); + srand(time(0)); + mas = malloc(n * sizeof(int)); + printf(" :\n"); + for (i = 0; i < n; i++) { + mas[i] = rand() % 100; // 0 100 + printf("%d ", mas[i]); + } + for (i = 1; i < n; i++) { + if (mas[i] > mas[i - 1] && ) + k++; + } + printf("%d ", &k); + putchar('\n'); + free(mas); + system("pause"); + return 0; } \ No newline at end of file diff --git a/HW/homework/HW/task2(2s)/Source.h b/HW/homework/HW/task2(2s)/Source.h new file mode 100644 index 00000000..61ac237f --- /dev/null +++ b/HW/homework/HW/task2(2s)/Source.h @@ -0,0 +1,138 @@ +#include + +class Vector +{ +private: + std::vector main_vector; + bool isEnd; + int x, y; + int size; + +public: + Vector() { + main_vector.reserve(0); + isEnd = true; + x = 0; + y = 0; + size = 0; + }; + + Vector(std::vector _main_vector, bool _isEnd, int _x, int _y, int _size) { + isEnd = _isEnd; + x = _x; + y = _y; + main_vector = _main_vector; + size = _size; + }; + + Vector(const Vector& vect) { + isEnd = vect.isEnd; + x = vect.x; + y = vect.y; + main_vector = vect.main_vector; + size = vect.size; + }; +public: + Vector(int _begin, int _end) { + + int size; + + x = _begin; + y = _end; + + size = abs(x - y); + + this->main_vector.resize(size); + + if (x > y) { + + for (int i = 0; i < main_vector.size(); i++) { + this->main_vector[i] = y + i; + } + } + if (y > x) { + for (int i = 0; i < main_vector.size(); i++) { + this->main_vector[i] = x + i; + } + } + }; + +public: + void ShowVector() { + + std::cout << " : "; + + for (int i = 0; i < main_vector.size(); i++) { + std::cout << main_vector[i] << " "; + } + + std::cout << std::endl; + + }; + + void ShowSize() { + size = main_vector.size(); + std::cout << size; + } + + Vector operator=(const Vector& vect) { + + if (this == &vect) + return *this; + + main_vector = vect.main_vector; + return *this; + }; + + Vector operator+(const Vector& right) { + + if (main_vector.size() <= right.main_vector.size()) { + + for (int i = 0; i < main_vector.size(); i++) { + main_vector[i] = main_vector[i] + right.main_vector[i]; + } + + return *this; + } + + if (main_vector.size() >= right.main_vector.size()) { + + for (int i = 0; i < right.main_vector.size(); i++) { + main_vector[i] = main_vector[i] + right.main_vector[i]; + } + + return *this; + } + + } + + Vector operator*(const Vector& right) { + + if (main_vector.size() <= right.main_vector.size()) { + + for (int i = 0; i < main_vector.size(); i++) { + main_vector[i] = main_vector[i] * right.main_vector[i]; + } + return *this; + } + + + if (main_vector.size() >= right.main_vector.size()) { + + for (int i = 0; i < right.main_vector.size(); i++) { + main_vector[i] = main_vector[i] * right.main_vector[i]; + } + return *this; + } + + } + + Vector operator*(const int val) { + for (int i = 0; i < main_vector.size(); i++) { + main_vector[i] = main_vector[i] * val; + } + + return *this; + } + +}; diff --git a/HW/homework/HW/task2(2s)/main.cpp b/HW/homework/HW/task2(2s)/main.cpp new file mode 100644 index 00000000..6b3b63d6 --- /dev/null +++ b/HW/homework/HW/task2(2s)/main.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#include + + +int main() { + + setlocale(LC_ALL, "Russian"); + + int begin, end; + int num; + int scal; + + std::cout << " : "; + std::cin >> begin; + + std::cout << " : "; + std::cin >> end; + + Vector vector_1(begin, end); + + vector_1.ShowVector(); + + while (true) { + + std::cout << std::endl << " : " << std::endl << "1 - " << std::endl << "2 - " << std::endl << "3 - " << std::endl << "4 - " << std::endl << "5 - " << std::endl; + std::cout << " = "; + + std::cin >> num; + + if (num == 5) + break; + + switch (num) + { + case 1: + { + std::cout << " : "; + std::cin >> begin; + + std::cout << " : "; + std::cin >> end; + + Vector vector_2(begin, end); + + vector_2.ShowVector(); + + vector_1 = vector_1 + vector_2; + + vector_1.ShowVector(); + break; + } + case 2: + { + std::cout << " : "; + std::cin >> begin; + + std::cout << " : "; + std::cin >> end; + + Vector vector_2(begin, end); + + vector_1 = vector_1 * vector_2; + + vector_1.ShowVector(); + break; + } + case 3: + { + + std::cout << " : "; + std::cin >> scal; + + vector_1 = vector_1 * scal; + + vector_1.ShowVector(); + break; + } + case 4: + { + + + std::cout << " = "; + vector_1.ShowSize(); + break; + } + + } + } + + + return 0; +} \ No newline at end of file diff --git a/HW/homework/HW/task2(2s)/task2(2s).vcxproj b/HW/homework/HW/task2(2s)/task2(2s).vcxproj new file mode 100644 index 00000000..7bcfe3ba --- /dev/null +++ b/HW/homework/HW/task2(2s)/task2(2s).vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {132b728a-02d0-424e-9a3c-4ea427fb9405} + task22s + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/HW/homework/HW/task2(2s)/task2(2s).vcxproj.filters b/HW/homework/HW/task2(2s)/task2(2s).vcxproj.filters new file mode 100644 index 00000000..172c7d15 --- /dev/null +++ b/HW/homework/HW/task2(2s)/task2(2s).vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/HW/homework/HW/task2(2s)/task2(2s).vcxproj.user b/HW/homework/HW/task2(2s)/task2(2s).vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/task2(2s)/task2(2s).vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW/homework/HW/task2/task2.c b/HW/homework/HW/task2/task2.c new file mode 100644 index 00000000..57df1483 --- /dev/null +++ b/HW/homework/HW/task2/task2.c @@ -0,0 +1,80 @@ +#include +#include +#include +#ifndef __cplusplus +#define bool int +#define true 1 +#define false 0 +#endif + +void guess_number() { + setlocale(LC_ALL, "russian"); + + int nomer, kolvo = 0, random_nomer; + srand(time(0)); + random_nomer = rand() % 1000 + 1; + + while (true) { + + printf(" "); + scanf_s("%d", &nomer); + + kolvo++; + + if (nomer < random_nomer) + printf(" \n"); + + if (nomer > random_nomer) + printf(" \n"); + if (nomer == random_nomer) { + printf("Vi ugadali chislo!\n"); + break; + } + } + printf("skritoe chislo: %d\n", nomer); + printf("kolvo popitok: %d\n", kolvo); +} + void input_number() { + + int nomer, kolvo = 0, x1 = 0, x2 = 1000, flag = 0, ug_n; + char vv; + while ((x1 < x2) && (flag == 0)) { + + ug_n = x1 + ((x2 - x1) / 2); + + printf("skritoe chislo: %d?\n", ug_n); + scanf_s("%c", &vv, 1); + vv = getchar(); + kolvo++; + switch (vv) + { + case '>': x1 = ug_n; + break; + case '<': x2 = ug_n; + break; + case '=': printf("PC prav!\n"); + flag = 1; + break; + } + } + printf("skritoe chislo: %d\n", ug_n); + printf("kolvo popitok: %d", kolvo); + } + int main() { + int programa; + while (true) { + + printf("viberi rejim(1 ili 2): "); + scanf_s("%d", &programa); + + if ((programa == 1) || (programa == 2)) + break; + } + + if (programa == 1) + guess_number(); + if (programa == 2) + input_number(); + + return 0; + } \ No newline at end of file diff --git a/HW/homework/HW/task2/task2.vcxproj b/HW/homework/HW/task2/task2.vcxproj new file mode 100644 index 00000000..e07f19a9 --- /dev/null +++ b/HW/homework/HW/task2/task2.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {c0223a77-5103-44d5-ad1a-b080a6d74cd5} + task2 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/HW/homework/HW/task2/task2.vcxproj.filters b/HW/homework/HW/task2/task2.vcxproj.filters new file mode 100644 index 00000000..9da70b42 --- /dev/null +++ b/HW/homework/HW/task2/task2.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/HW/homework/HW/task2/task2.vcxproj.user b/HW/homework/HW/task2/task2.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/task2/task2.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW/homework/HW/task3/task3.c b/HW/homework/HW/task3/task3.c new file mode 100644 index 00000000..eb0873de --- /dev/null +++ b/HW/homework/HW/task3/task3.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#ifndef __cplusplus +#define bool int +#define true 1 +#define false 0 +#endif + +int *user_n, *pc_n, num; + +int num_generate(int n) { + + setlocale(LC_ALL, "russian"); + int num = 0, i = 0; + int numbers[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + srand(time(0)); + + while (n > 0) { + + i = rand() % 10 + 1; + while ((numbers[i] < 0) && (i < 10)) + i++; + while ((numbers[i] < 0) && (i > -1)) + i--; + num = num * 10 + numbers[i]; + numbers[i] = -1; + n--; + } + + return num; +} + +void c_and_b(int *pc_num, int *user_num, int n, int *c_count, int *b_count) { + + *c_count = *b_count = 0; + + for (int i = 0; i < n; i++) { + + if ((pc_n[i] == user_n[i]) && (user_n[i] > -1)) { + *b_count += 1; + user_n[i] = -1; + } + + } + + for (int i = 0; i < n; i++) { + + for (int j = 0; j < n; j++) { + + if ((pc_n[i] == user_n[j]) && (user_n[j] > -1)) { + + *c_count += 1; + user_n[j] = -1; + } + } + } +} + + + +int main() { + + setlocale(LC_ALL, "russian"); + int n, user_num, c = 0, b = 0; + + while (true) { + + printf(" ( 2 5): \n"); + scanf_s("%d", &n); + + if ((n >= 2) && (n <= 5)) + break; + else + printf(" \n"); + } + + user_n = (int)malloc(n * sizeof(int)); + pc_n = (int)malloc(n * sizeof(int)); + num = num_generate(n); + + int i = 0; + + while (num != 0) { + + pc_n[i] = num % 10; + num /= 10; + i++; + } + + + while (true) { + + printf(" : "); + scanf_s("%d", &user_num); + + i = 0; + + while (user_num != 0) { + + user_n[i] = user_num % 10; + user_num /= 10; + i++; + } + + c_and_b(pc_n, user_n, n, &c, &b); + + if (b == n) { + + printf(" !"); + break; + } + else + printf("- = %d\n - = %d\n", c, b); + } + + return 0; +} \ No newline at end of file diff --git a/HW/homework/HW/task3/task3.vcxproj b/HW/homework/HW/task3/task3.vcxproj new file mode 100644 index 00000000..68102761 --- /dev/null +++ b/HW/homework/HW/task3/task3.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {03f3e6c9-383e-4a07-a3f9-339c6c704fca} + task3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/HW/homework/HW/task3/task3.vcxproj.filters b/HW/homework/HW/task3/task3.vcxproj.filters new file mode 100644 index 00000000..4d6d9647 --- /dev/null +++ b/HW/homework/HW/task3/task3.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/HW/homework/HW/task3/task3.vcxproj.user b/HW/homework/HW/task3/task3.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/task3/task3.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW/homework/HW/task4/task4.c b/HW/homework/HW/task4/task4.c new file mode 100644 index 00000000..f04553ce --- /dev/null +++ b/HW/homework/HW/task4/task4.c @@ -0,0 +1,195 @@ +#include +#include +#include +#ifndef __cplusplus +#define bool int +#define true 1 +#define false 0 +#endif +#define SIZE 10 +#define BAR_LEN 5 +#define PROD_LEN 20 +#define MAX_CHECK_COUNT 100 + + +#define SCAN_CURRENT_PRODUCT 1 +#define SHOW_PRODUCT 2 +#define ADD_TO_CHECK 3 +#define CREATE_CHECK 4 +#define CALCULATE_COST -1 +#define EXIT_PROGRAMM 5 +#define SHOW_ALL_PRODUCTS 0 +#define SHOW_MENU 6 + + +char producti[SIZE][PROD_LEN]; +char cod[SIZE][BAR_LEN]; +float cena[SIZE]; +float skidka[SIZE]; +int lp = -1, cc = 0; +int pch[MAX_CHECK_COUNT][2]; +float cost; + +int user_choice() { + int s = 0; + while (true) { + printf("vvedi nomer "); + scanf_s("%d", &s); + if ((s >= SHOW_ALL_PRODUCTS) && (s <= SHOW_MENU)) + break; + } + return s; +} + +void define_product() { + + strcpy_s(producti[0], 20, "cigaretes"); + strcpy_s(producti[1], 20, "coal"); + strcpy_s(producti[2], 20, "rope"); + strcpy_s(producti[3], 20, "meat"); + strcpy_s(producti[4], 20, "gps"); + strcpy_s(producti[5], 20, "oil"); + strcpy_s(producti[6], 20, "phone"); + strcpy_s(producti[7], 20, "coca-cola"); + strcpy_s(producti[8], 20, "coockie"); + + strcpy_s(cod[0], 5, "0001"); + strcpy_s(cod[1], 5, "0002"); + strcpy_s(cod[2], 5, "0003"); + strcpy_s(cod[3], 5, "0004"); + strcpy_s(cod[4], 5, "0005"); + strcpy_s(cod[5], 5, "0006"); + strcpy_s(cod[6], 5, "0007"); + strcpy_s(cod[7], 5, "0008"); + strcpy_s(cod[8], 5, "0009"); + + cena[0] = 100.0; + cena[1] = 150.0; + cena[2] = 109.0; + cena[3] = 200.0; + cena[4] = 179.0; + cena[5] = 180.0; + cena[6] = 500.0; + cena[7] = 350.0; + cena[8] = 300.0; + + for (int i = 0; i < 8; i++) { + skidka[i] = rand() % 50; + } + +} + +int show_product() { + printf("-------------------------------------------------------------------\n"); + printf("Product \t\t cena \t\t skidka \t\t code\n\n"); + for (int i = 0; i < SIZE; i++) { + printf("%-20s \t\t %.1f \t\t %.1f \t\t %s\n\n", producti[i], cena[i], skidka[i], cod[i]); + } + printf("-------------------------------------------------------------------\n"); +} + +bool find_product(char* code, int* pi) { + for (int i = 0; i < SIZE; i++) { + if (strcmp(code, cod[i]) == 0) { + *pi = i; + return true; + } + } + return false; +} + +int scan_product() { + printf("vvedite cod producta\n"); + char bcode[BAR_LEN]; + int i; + + scanf_s("%s", &bcode, BAR_LEN); + + if (find_product(bcode, &i)) + lp = i; + else + printf("ne nashel cod"); + +} + +int add_to_check() { + if ((cc >= MAX_CHECK_COUNT) || (lp < 0)) + return; + + if ((cc > 0) && (pch[cc - 1][0] == lp)) { + pch[cc - 1][1]++; + printf("obnovit chek %d\n", pch[cc - 1][1]); + } + else + { + cc++; + pch[cc - 1][1] = 1; + pch[cc - 1][0] = lp; + printf("dobavit chek %d, check kolvo: %d\n", pch[cc - 1][1], cc); + } +} + +int create_check() { + float sum = 0; + printf("Product \t\t cena \t skidka \tcod\n\n"); + for (int i = 0; i < cc; i++) { + printf("x* %d %-20s %.1f %.1f %s\n\n", pch[i][1], producti[pch[i][0]], cena[pch[i][0]], skidka[pch[i][0]], cod[pch[i][0]]); + } + sum = calculate_cost(); + printf("vsya cena is: %.1f\n", sum); +} + +int calculate_cost() { + if (cc == 0) + return "net producta v cheke"; + for (int i = 0; i < cc; i++) { + if (skidka[pch[i][0]] > 0) { + cost = pch[i][1] * (cena[pch[i][0]] / 100 * (100 - skidka[pch[i][0]])) + cost; + } + else + cost = pch[i][1] * cena[pch[i][0]] + cost; + } + return cost; +} + +int exit_programm() { + printf("vihod iz programmi\n"); +} +int show_menu() { + printf("\ \n 0 - pokazat producti\n 1 - scan product\n 2 - pokazatproduct\n 3 - dobavit product v chek\n 4 - sdelat chek\n 5 - vihod iz programmi\n 6 - pokazat menu\n"); +} + +int main() { + define_product(); + printf("\n 0 - show all producti\n 1 - scan current product\n 2 - show current product\n 3 - add product to check\n 4 - create check\n 5 - Exit programm\n 6 - Show menu\n"); + while + (true) { + switch (user_choice()) + { + case SCAN_CURRENT_PRODUCT: + scan_product(); + break; + case SHOW_PRODUCT: + add_to_check(); + break; + case ADD_TO_CHECK: + add_to_check(); + break; + case CREATE_CHECK: + create_check(); + break; + case CALCULATE_COST: + calculate_cost(); + break; + case EXIT_PROGRAMM: + exit_programm(); + break; + case SHOW_ALL_PRODUCTS: + show_product(); + break; + case SHOW_MENU: + show_menu(); + break; + } + } +} \ No newline at end of file diff --git a/HW/homework/HW/task4/task4.vcxproj b/HW/homework/HW/task4/task4.vcxproj new file mode 100644 index 00000000..174845ec --- /dev/null +++ b/HW/homework/HW/task4/task4.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 16.0 + Win32Proj + {b2629934-3850-4594-94a6-d5b2c65426ec} + task4 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/HW/homework/HW/task4/task4.vcxproj.filters b/HW/homework/HW/task4/task4.vcxproj.filters new file mode 100644 index 00000000..362b46e9 --- /dev/null +++ b/HW/homework/HW/task4/task4.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/HW/homework/HW/task4/task4.vcxproj.user b/HW/homework/HW/task4/task4.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/task4/task4.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HW/homework/HW/tusk6/task6.c b/HW/homework/HW/tusk6/task6.c new file mode 100644 index 00000000..e69de29b diff --git a/HW/homework/HW/tusk6/tusk6.vcxproj b/HW/homework/HW/tusk6/tusk6.vcxproj new file mode 100644 index 00000000..4cab4d1b --- /dev/null +++ b/HW/homework/HW/tusk6/tusk6.vcxproj @@ -0,0 +1,150 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + 16.0 + Win32Proj + {95f33513-0f86-4e2e-9efe-34cb3ab4f901} + tusk6 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + diff --git a/HW/homework/HW/tusk6/tusk6.vcxproj.filters b/HW/homework/HW/tusk6/tusk6.vcxproj.filters new file mode 100644 index 00000000..153170c7 --- /dev/null +++ b/HW/homework/HW/tusk6/tusk6.vcxproj.filters @@ -0,0 +1,17 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + \ No newline at end of file diff --git a/HW/homework/HW/tusk6/tusk6.vcxproj.user b/HW/homework/HW/tusk6/tusk6.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/HW/homework/HW/tusk6/tusk6.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/TasksInTerminalClass/TK/.vs/TK/v16/.suo b/TasksInTerminalClass/TK/.vs/TK/v16/.suo index 6f37bcb1..c7c8ab41 100644 Binary files a/TasksInTerminalClass/TK/.vs/TK/v16/.suo and b/TasksInTerminalClass/TK/.vs/TK/v16/.suo differ diff --git a/TasksInTerminalClass/TK/.vs/TK/v16/Browse.VC.db b/TasksInTerminalClass/TK/.vs/TK/v16/Browse.VC.db index f0e69071..6fc61768 100644 Binary files a/TasksInTerminalClass/TK/.vs/TK/v16/Browse.VC.db and b/TasksInTerminalClass/TK/.vs/TK/v16/Browse.VC.db differ diff --git a/TasksInTerminalClass/TK/.vs/TK/v16/Solution.VC.db b/TasksInTerminalClass/TK/.vs/TK/v16/Solution.VC.db new file mode 100644 index 00000000..c6c13cae Binary files /dev/null and b/TasksInTerminalClass/TK/.vs/TK/v16/Solution.VC.db differ diff --git a/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/2f500908907e1074/TASK3.ipch b/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/2f500908907e1074/TASK3.ipch new file mode 100644 index 00000000..c058b49d Binary files /dev/null and b/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/2f500908907e1074/TASK3.ipch differ diff --git a/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/b5b485f78819ff14/TASK0.ipch b/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/b5b485f78819ff14/TASK0.ipch new file mode 100644 index 00000000..b733f1be Binary files /dev/null and b/TasksInTerminalClass/TK/.vs/TK/v16/ipch/AutoPCH/b5b485f78819ff14/TASK0.ipch differ diff --git a/TasksInTerminalClass/TK/TK.sln b/TasksInTerminalClass/TK/TK.sln index 65f92562..fb812e84 100644 --- a/TasksInTerminalClass/TK/TK.sln +++ b/TasksInTerminalClass/TK/TK.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31702.278 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task0", "task0\task0.vcxproj", "{61402561-46A3-440F-8223-493D14343FDB}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "task3", "task3\task3.vcxproj", "{2A862D59-4C3D-4BE3-8599-3AACBBE68B01}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -21,6 +23,14 @@ Global {61402561-46A3-440F-8223-493D14343FDB}.Release|x64.Build.0 = Release|x64 {61402561-46A3-440F-8223-493D14343FDB}.Release|x86.ActiveCfg = Release|Win32 {61402561-46A3-440F-8223-493D14343FDB}.Release|x86.Build.0 = Release|Win32 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Debug|x64.ActiveCfg = Debug|x64 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Debug|x64.Build.0 = Debug|x64 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Debug|x86.ActiveCfg = Debug|Win32 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Debug|x86.Build.0 = Debug|Win32 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Release|x64.ActiveCfg = Release|x64 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Release|x64.Build.0 = Release|x64 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Release|x86.ActiveCfg = Release|Win32 + {2A862D59-4C3D-4BE3-8599-3AACBBE68B01}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TasksInTerminalClass/TK/task3/task3.c b/TasksInTerminalClass/TK/task3/task3.c new file mode 100644 index 00000000..25bb3505 --- /dev/null +++ b/TasksInTerminalClass/TK/task3/task3.c @@ -0,0 +1,30 @@ +#define _CRT_SECURE_NO_WARNINGS +#include + +int main() +{ + int n, iw = 1; + while (iw) { + printf("vvedite nomer\n"); + printf("dlya vihoda vvedi 0"); + int ie = 0; + do { + if (ie) printf("oshibka, poprobui eshe"); + printf("vvedite polojitelnii nomer"); + scanf("%d", &n); + if (n == 0) iw = 0; break; + ie = (n <= 0); + + } while (ie); + int inv = 0, d = 10; + while (n) { + inv *= d; + inv += n % d; + n = n / d; + } + if (iw) printf("rezultat %d\n\n", inv); + if (ie) printf("vihod\n\n"); + } + + return 0; +} diff --git a/TasksInTerminalClass/TK/task3/task3.vcxproj b/TasksInTerminalClass/TK/task3/task3.vcxproj new file mode 100644 index 00000000..7c8c46fe --- /dev/null +++ b/TasksInTerminalClass/TK/task3/task3.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2a862d59-4c3d-4be3-8599-3aacbbe68b01} + task3 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/TasksInTerminalClass/TK/task3/task3.vcxproj.filters b/TasksInTerminalClass/TK/task3/task3.vcxproj.filters new file mode 100644 index 00000000..4d6d9647 --- /dev/null +++ b/TasksInTerminalClass/TK/task3/task3.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/TasksInTerminalClass/TK/task3/task3.vcxproj.user b/TasksInTerminalClass/TK/task3/task3.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/TasksInTerminalClass/TK/task3/task3.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file