Skip to content

Commit b82fefb

Browse files
fix: resolve compilation errors and add CI/CD / Release workflows
1 parent efd0e4b commit b82fefb

3 files changed

Lines changed: 99 additions & 83 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Advanced Mini Bash Shell Makefile
22

33
CC = gcc
4-
CFLAGS = -Wall -Wextra -std=c99 -g -O2
4+
CFLAGS = -Wall -Wextra -std=gnu99 -g -O2
55
LDFLAGS =
66
TARGET = mini-bash
77
HEADERS_DIR = headers

headers/shell.h

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
#ifndef SHELL_H
22
#define SHELL_H
33

4+
#ifndef _POSIX_C_SOURCE
5+
#define _POSIX_C_SOURCE 200809L
6+
#endif
7+
8+
#ifndef _XOPEN_SOURCE
9+
#define _XOPEN_SOURCE 700
10+
#endif
11+
12+
#include <errno.h>
13+
#include <fcntl.h>
14+
#include <limits.h>
15+
#include <signal.h>
416
#include <stdio.h>
517
#include <stdlib.h>
618
#include <string.h>
7-
#include <unistd.h>
8-
#include <sys/wait.h>
9-
#include <sys/types.h>
1019
#include <sys/stat.h>
11-
#include <fcntl.h>
12-
#include <signal.h>
13-
#include <errno.h>
14-
#include <time.h>
15-
#include <limits.h>
20+
#include <sys/types.h>
21+
#include <sys/wait.h>
1622
#include <termios.h>
23+
#include <time.h>
24+
#include <unistd.h>
1725

1826
// Maximum command length
1927
#define MAX_CMD_LEN 1024
@@ -23,22 +31,22 @@
2331

2432
// Command structure
2533
typedef struct {
26-
char **args;
27-
int argc;
28-
char *input_file;
29-
char *output_file;
30-
char *error_file;
31-
int append_output;
32-
int background;
34+
char **args;
35+
int argc;
36+
char *input_file;
37+
char *output_file;
38+
char *error_file;
39+
int append_output;
40+
int background;
3341
} command_t;
3442

3543
// Job structure
3644
typedef struct {
37-
pid_t pid;
38-
int job_id;
39-
char *command;
40-
int status; // 0: running, 1: stopped, 2: completed
41-
time_t start_time;
45+
pid_t pid;
46+
int job_id;
47+
char *command;
48+
int status; // 0: running, 1: stopped, 2: completed
49+
time_t start_time;
4250
} job_t;
4351

4452
// Global variables

utils.c

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,99 @@
11
#include "headers/utils.h"
2+
#include <limits.h>
3+
#include <stdlib.h>
4+
#include <string.h>
25

36
// Expand tilde in path
4-
char* expand_tilde(char *path) {
5-
if (!path) return NULL;
6-
7-
if (path[0] == '~') {
8-
char *home = getenv("HOME");
9-
if (!home) {
10-
return strdup(path); // Return original if HOME not set
11-
}
12-
13-
int home_len = strlen(home);
14-
int path_len = strlen(path);
15-
char *expanded = malloc(home_len + path_len);
16-
17-
if (!expanded) {
18-
return NULL;
19-
}
20-
21-
strcpy(expanded, home);
22-
strcat(expanded, path + 1); // Skip the '~'
23-
24-
return expanded;
7+
char *expand_tilde(char *path) {
8+
if (!path)
9+
return NULL;
10+
11+
if (path[0] == '~') {
12+
char *home = getenv("HOME");
13+
if (!home) {
14+
return strdup(path); // Return original if HOME not set
2515
}
26-
27-
return strdup(path);
16+
17+
int home_len = strlen(home);
18+
int path_len = strlen(path);
19+
char *expanded = malloc(home_len + path_len);
20+
21+
if (!expanded) {
22+
return NULL;
23+
}
24+
25+
strcpy(expanded, home);
26+
strcat(expanded, path + 1); // Skip the '~'
27+
28+
return expanded;
29+
}
30+
31+
return strdup(path);
2832
}
2933

3034
// Get absolute path
31-
char* get_absolute_path(char *path) {
32-
if (!path) return NULL;
33-
34-
char *abs_path = malloc(PATH_MAX);
35-
if (!abs_path) return NULL;
36-
37-
if (realpath(path, abs_path) == NULL) {
38-
free(abs_path);
39-
return strdup(path); // Return original if realpath fails
40-
}
41-
42-
return abs_path;
35+
char *get_absolute_path(char *path) {
36+
if (!path)
37+
return NULL;
38+
39+
char *abs_path = malloc(PATH_MAX);
40+
if (!abs_path)
41+
return NULL;
42+
43+
if (realpath(path, abs_path) == NULL) {
44+
free(abs_path);
45+
return strdup(path); // Return original if realpath fails
46+
}
47+
48+
return abs_path;
4349
}
4450

4551
// Check if file is executable
4652
int is_executable(char *path) {
47-
if (!path) return 0;
48-
49-
struct stat st;
50-
if (stat(path, &st) == 0) {
51-
return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH);
52-
}
53-
53+
if (!path)
5454
return 0;
55+
56+
struct stat st;
57+
if (stat(path, &st) == 0) {
58+
return (st.st_mode & S_IXUSR) || (st.st_mode & S_IXGRP) ||
59+
(st.st_mode & S_IXOTH);
60+
}
61+
62+
return 0;
5563
}
5664

5765
// Print error message
58-
void print_error(char *msg) {
59-
fprintf(stderr, "mini-bash: %s\n", msg);
60-
}
66+
void print_error(char *msg) { fprintf(stderr, "mini-bash: %s\n", msg); }
6167

6268
// Print error message with errno
6369
void print_error_with_errno(char *msg) {
64-
fprintf(stderr, "mini-bash: %s: %s\n", msg, strerror(errno));
70+
fprintf(stderr, "mini-bash: %s: %s\n", msg, strerror(errno));
6571
}
6672

6773
// String duplicate (for compatibility with older systems)
6874
#ifndef _POSIX_C_SOURCE
69-
char* strdup(const char *s) {
70-
if (!s) return NULL;
71-
72-
size_t len = strlen(s) + 1;
73-
char *dup = malloc(len);
74-
if (dup) {
75-
memcpy(dup, s, len);
76-
}
77-
return dup;
75+
char *strdup(const char *s) {
76+
if (!s)
77+
return NULL;
78+
79+
size_t len = strlen(s) + 1;
80+
char *dup = malloc(len);
81+
if (dup) {
82+
memcpy(dup, s, len);
83+
}
84+
return dup;
7885
}
7986
#endif
8087

8188
// Free string array
8289
void free_string_array(char **arr, int count) {
83-
if (!arr) return;
84-
85-
for (int i = 0; i < count; i++) {
86-
if (arr[i]) {
87-
free(arr[i]);
88-
}
90+
if (!arr)
91+
return;
92+
93+
for (int i = 0; i < count; i++) {
94+
if (arr[i]) {
95+
free(arr[i]);
8996
}
90-
free(arr);
97+
}
98+
free(arr);
9199
}

0 commit comments

Comments
 (0)