Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 4 additions & 49 deletions .github/workflows/awesome_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ jobs:
uses: TheAlgorithms/scripts/formatter@main
with:
filetypes: .c,.h
- name: Update DIRECTORY.md
run: |
wget https://raw.githubusercontent.com/TheAlgorithms/scripts/main/build_directory_md.py
python3 build_directory_md.py C . .c,.h leetcode/ > DIRECTORY.md
git commit -m "updating DIRECTORY.md" DIRECTORY.md || true
- name: Get file changes
run: |
git branch
Expand All @@ -42,48 +37,8 @@ jobs:
# be able to catch any errors for other platforms.
run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
- name: Lint modified files
shell: python
run: |
import os
import subprocess
import sys

print("Python {}.{}.{}".format(*sys.version_info)) # Python 3.8
with open("git_diff.txt") as in_file:
modified_files = sorted(in_file.read().splitlines())
print("{} files were modified.".format(len(modified_files)))

cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
print(f"{len(cpp_files)} C++ files were modified.")
if not cpp_files:
sys.exit(0)
subprocess.run(["clang-tidy", "-p=build", "--fix", *cpp_files, "--"],
check=True, text=True, stderr=subprocess.STDOUT)
subprocess.run(["clang-format", "-i", *cpp_files],
check=True, text=True, stderr=subprocess.STDOUT)

upper_files = [file for file in cpp_files if file != file.lower()]
if upper_files:
print(f"{len(upper_files)} files contain uppercase characters:")
print("\n".join(upper_files) + "\n")

space_files = [file for file in cpp_files if " " in file or "-" in file]
if space_files:
print(f"{len(space_files)} files contain space or dash characters:")
print("\n".join(space_files) + "\n")

nodir_files = [file for file in cpp_files if file.count(os.sep) != 1 and "project_euler" not in file and "data_structure" not in file]
if len(nodir_files) > 1:
nodir_file_bad_files = len(nodir_files) - 1
print(f"{len(nodir_files)} files are not in one and only one directory:")
print("\n".join(nodir_files) + "\n")
else:
nodir_file_bad_files = 0

bad_files = nodir_file_bad_files + len(upper_files + space_files)
if bad_files:
sys.exit(bad_files)
shell: bash
run: python3 scripts/file_linter.py
- name: Commit and push changes
run: |
git diff DIRECTORY.md
Expand All @@ -97,14 +52,14 @@ jobs:
needs: [MainSequence]
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
os: [windows-latest, ubuntu-latest, macOS-latest]
steps:
- uses: actions/checkout@v3
with:
submodules: true
- run: |
cmake -B ./build -S .
cmake --build build
cmake --build build --config Release
- name: Label on PR fail
uses: actions/github-script@v6
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/directory_writer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Directory writer
on:
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
build:
if: github.repository == 'TheAlgorithms/C' # We only need this to run in our repository.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Build directory
uses: TheAlgorithms/scripts/directory_md@main
with:
language: C
working-directory: .
filetypes: .c,.h
ignored-directories: leetcode/,scripts/
6 changes: 3 additions & 3 deletions .github/workflows/leetcode_directory_writer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ jobs:
shell: bash
run: |
git checkout -b leetcode-directory-${{ github.sha }}
git commit -m "docs: updating `leetcode/DIRECTORY.md`
git commit -m "docs: updating `leetcode/DIRECTORY.md`"
git push origin leetcode-directory-${{ github.sha }}:leetcode-directory-${{ github.sha }}
- name: Creating the pull request
shell: bash
run: |
if [[ `git status --porcelain` ]]; then
gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).'
if [[ $(git log --branches --not --remotes) ]]; then
gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).' || true
fi
env:
GH_TOKEN: ${{ github.token }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.out
.vscode/
build/
git_diff.txt
15 changes: 7 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
cmake_minimum_required(VERSION 3.6)
cmake_minimum_required(VERSION 3.22)
project(Algorithms_in_C
LANGUAGES C
VERSION 1.0.0
DESCRIPTION "Set of algorithms implemented in C."
)
LANGUAGES C
VERSION 1.0.0
DESCRIPTION "Set of algorithms implemented in C."
)

# Set compilation standards
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES)

if(MSVC)
if (MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
# add_compile_options(/Za)
endif(MSVC)
endif (MSVC)

# check for math library
# addresses a bug when linking on OSX
Expand Down
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
* [Rot13](https://github.com/TheAlgorithms/C/blob/HEAD/cipher/rot13.c)

## Client Server
* [Bool](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/bool.h)
* [Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/client.c)
* [Fork](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/fork.h)
* [Remote Command Exec Udp Client](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_client.c)
* [Remote Command Exec Udp Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/remote_command_exec_udp_server.c)
* [Server](https://github.com/TheAlgorithms/C/blob/HEAD/client_server/server.c)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The repository is a collection of open-source implementations of a variety of al
* The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - [C](https://en.wikipedia.org/wiki/C_(programming_language)).
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
* Each source code is atomic using standard C library [`libc`](https://en.wikipedia.org/wiki/C_standard_library) and _no external libraries_ are required for their compilation and execution. Thus the fundamentals of the algorithms can be studied in much depth.
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS and Ubuntu (Linux) using MSVC 16 2019, AppleClang 11.0 and GNU 7.5.0 respectively.
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of two major operating systems viz., MacOS and Ubuntu (Linux) using AppleClang 14.0.0 and GNU 11.3.0 respectively.
* Strict adherence to [C11](https://en.wikipedia.org/wiki/C11_(C_standard_revision)) standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
* Self-checks within programs ensure correct implementations with confidence.
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
Expand Down
30 changes: 19 additions & 11 deletions client_server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
include(CheckIncludeFile)

if(WIN32)
check_include_file(winsock2.h WINSOCK_HEADER)
CHECK_INCLUDE_FILE(winsock2.h WINSOCK_HEADER)
else()
check_include_file(arpa/inet.h ARPA_HEADERS)
CHECK_INCLUDE_FILE(arpa/inet.h ARPA_HEADERS)
endif()

include(CheckSymbolExists)
if(ARPA_HEADERS OR WINSOCK_HEADER)
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
Expand All @@ -13,16 +16,21 @@ if(ARPA_HEADERS OR WINSOCK_HEADER)
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".c" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )

if(OpenMP_C_FOUND)
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
string(REPLACE ".c" "" testname ${testsourcefile})

if(NOT WIN32)
if(${testname} STREQUAL "fork" OR ${testname} STREQUAL "bool")
continue()
endif()
endif()
if(MATH_LIBRARY)
add_executable(${testname} ${testsourcefile})

if (OpenMP_C_FOUND)
target_link_libraries(${testname} PRIVATE OpenMP::OpenMP_C)
endif ()
if (MATH_LIBRARY)
target_link_libraries(${testname} PRIVATE ${MATH_LIBRARY})
endif()

endif ()
# if(HAS_UNISTD)
# target_compile_definitions(${testname} PRIVATE HAS_UNISTD)
# endif()
Expand All @@ -32,7 +40,7 @@ if(ARPA_HEADERS OR WINSOCK_HEADER)
# target_compile_definitions(${testname} PRIVATE WINSOCK_HEADER)
# endif()

if(WINSOCK_HEADER)
if (WINSOCK_HEADER)
target_link_libraries(${testname} PRIVATE ws2_32) # link winsock library on windows
endif()

Expand Down
55 changes: 55 additions & 0 deletions client_server/bool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) 2007 - INRIA
*
* Copyright (C) 2012 - 2016 - Scilab Enterprises
*
* This file is hereby licensed under the terms of the GNU GPL v2.0,
* pursuant to article 5.3.4 of the CeCILL v.2.1.
* This file was originally licensed under the terms of the CeCILL v2.1,
* and continues to be available under such terms.
* For more information, see the COPYING file which you should have received
* along with this program.
*
*/
#ifndef __BOOL_H__
#define __BOOL_H__

/* define boolean type */
#ifdef BOOL
#undef BOOL
#endif

#ifdef TRUE
#undef TRUE
#endif

#ifdef FALSE
#undef FALSE
#endif


#ifndef _MSC_VER
typedef enum
{
FALSE = 0,
TRUE = 1
} BOOL;

#else
/* Please notice that BOOL is defined in <windef.h> */
/* BUT windef.h includes all others windows include */
/* it is better to redefine as */
typedef int BOOL;
#define FALSE 0
#define TRUE 1

#endif
/* converts BOOL to bool */
#define BOOLtobool(w) ((w != FALSE) ? true : false)

/* converts bool to BOOL */
#define booltoBOOL(w) ((w == true) ? TRUE : FALSE)

#endif /* __BOOL_H__ */
/*--------------------------------------------------------------------------*/
Loading