refactor: reorganize directory structure
This commit is contained in:
parent
433a4f2c0d
commit
d4913ac8df
5 changed files with 5 additions and 5 deletions
22
assignment1/default.nix
Normal file
22
assignment1/default.nix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
let
|
||||
basic-c = name: { stdenv }:
|
||||
stdenv.mkDerivation {
|
||||
inherit name;
|
||||
src = ./.;
|
||||
buildPhase = ''
|
||||
runHook preBuild
|
||||
gcc part2/${name}.c -o ${name}
|
||||
runHook postBuild
|
||||
'';
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
mkdir -p $out/bin
|
||||
install ${name} $out/bin/
|
||||
runHook postInstall
|
||||
'';
|
||||
};
|
||||
in {
|
||||
a1-hello = basic-c "hello";
|
||||
a1-employee = basic-c "employee";
|
||||
a1-logwriter = basic-c "logwriter";
|
||||
}
|
||||
34
assignment1/part2/employee.c
Normal file
34
assignment1/part2/employee.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
// Character array to store name
|
||||
// In C, strings are arrays of characters ending with '\0'
|
||||
char name[50];
|
||||
int employee_id;
|
||||
float hours_worked;
|
||||
|
||||
printf("OwlTech Employee Registration\n");
|
||||
printf("=============================\n");
|
||||
|
||||
printf("Enter your name: ");
|
||||
// fgets reads a line including spaces
|
||||
// stdin means "standard input" (keyboard)
|
||||
fgets(name, sizeof(name), stdin);
|
||||
|
||||
// Remove newline that fgets includes
|
||||
name[strcspn(name, "\n")] = '\0';
|
||||
|
||||
printf("Enter your employee ID: ");
|
||||
// & means "address of" - scanf needs to know where to store the value
|
||||
scanf("%d", &employee_id);
|
||||
printf("Hours worked this week: ");
|
||||
scanf("%f", &hours_worked);
|
||||
|
||||
printf("\nEmployee Summary:\n");
|
||||
printf("Name: %s\n", name); // %s for string
|
||||
printf("ID: %d\n", employee_id); // %d for integer
|
||||
printf("Hours: %.2f\n", hours_worked); // %.2f for float with 2 decimals
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
assignment1/part2/hello.c
Normal file
8
assignment1/part2/hello.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
printf("Welcome to OwlTech Industries!\n");
|
||||
printf("Systems Programming Division\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
assignment1/part2/logwriter.c
Normal file
39
assignment1/part2/logwriter.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main() {
|
||||
FILE *logfile; // Pointer to file structure
|
||||
char message[100];
|
||||
time_t current_time;
|
||||
|
||||
// Open file for appending ("a" mode)
|
||||
// "w" would overwrite, "r" would read
|
||||
logfile = fopen("owltech.log", "a");
|
||||
|
||||
// Always check if file opened successfully
|
||||
if (logfile == NULL) {
|
||||
printf("Error: Could not open log file\n");
|
||||
return 1; // Return non-zero to indicate error
|
||||
}
|
||||
|
||||
printf("Enter log message: ");
|
||||
fgets(message, sizeof(message), stdin);
|
||||
|
||||
// Get current time
|
||||
time(¤t_time);
|
||||
|
||||
// Write to file with timestamp
|
||||
// fprintf works like printf but writes to a file
|
||||
fprintf(logfile, "[%s] %s", ctime(¤t_time), message);
|
||||
|
||||
// Always close files when done
|
||||
fclose(logfile);
|
||||
printf("Log entry saved successfully!\n");
|
||||
|
||||
// Display the log file contents
|
||||
printf("\n--- Current Log Contents ---\n");
|
||||
system("cat owltech.log");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue